Skip to content

Commit 444cdd0

Browse files
S0okJuhalucinor
authored andcommitted
feat: Add get attachments tool (#84)
1 parent 810d2f5 commit 444cdd0

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/openstack_mcp_server/tools/block_storage_tools.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def register_tools(self, mcp: FastMCP):
2525
mcp.tool()(self.extend_volume)
2626

2727
mcp.tool()(self.get_attachment_details)
28+
mcp.tool()(self.get_attachments)
2829

2930
def get_volumes(self) -> list[Volume]:
3031
"""
@@ -225,3 +226,41 @@ def get_attachment_details(self, attachment_id: str) -> Attachment:
225226
}
226227

227228
return Attachment(**params)
229+
230+
def get_attachments(
231+
self,
232+
volume_id: str | None = None,
233+
instance: str | None = None,
234+
) -> list[Attachment]:
235+
"""
236+
Get the list of attachments.
237+
238+
:param volume_id: The ID of the volume.
239+
:param instance: The ID of the instance.
240+
:return: A list of Attachment objects.
241+
"""
242+
conn = get_openstack_conn()
243+
244+
filter = {}
245+
if volume_id:
246+
filter["volume_id"] = volume_id
247+
if instance:
248+
filter["instance"] = instance
249+
250+
attachments = []
251+
for attachment in conn.block_storage.attachments(**filter):
252+
attachments.append(
253+
Attachment(
254+
id=attachment.id,
255+
instance=attachment.instance,
256+
volume_id=attachment.volume_id,
257+
status=attachment.status,
258+
connection_info=attachment.connection_info,
259+
attach_mode=attachment.attach_mode,
260+
connector=attachment.connector,
261+
attached_at=attachment.attached_at,
262+
detached_at=attachment.detached_at,
263+
)
264+
)
265+
266+
return attachments

tests/tools/test_block_storage_tools.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,3 +741,45 @@ def test_get_attachment_details(
741741
mock_conn.block_storage.get_attachment.assert_called_once_with(
742742
"attach-123"
743743
)
744+
745+
def test_get_attachments(self, mock_get_openstack_conn_block_storage):
746+
"""Test getting attachments."""
747+
mock_conn = mock_get_openstack_conn_block_storage
748+
749+
# Create mock attachment object
750+
mock_attachment = Mock()
751+
mock_attachment.id = "attach-123"
752+
mock_attachment.instance = "server-123"
753+
mock_attachment.volume_id = "vol-123"
754+
mock_attachment.status = "attached"
755+
mock_attachment.connection_info = None
756+
mock_attachment.connector = None
757+
mock_attachment.attach_mode = None
758+
mock_attachment.attached_at = None
759+
mock_attachment.detached_at = None
760+
761+
mock_conn.block_storage.attachments.return_value = [mock_attachment]
762+
763+
# Test attachments
764+
block_storage_tools = BlockStorageTools()
765+
766+
filter = {
767+
"volume_id": "vol-123",
768+
"instance": "server-123",
769+
}
770+
result = block_storage_tools.get_attachments(**filter)
771+
772+
# Verify the result
773+
assert isinstance(result, list)
774+
assert len(result) == 1
775+
assert result[0].id == "attach-123"
776+
assert result[0].instance == "server-123"
777+
assert result[0].volume_id == "vol-123"
778+
assert result[0].attached_at is None
779+
assert result[0].detached_at is None
780+
assert result[0].attach_mode is None
781+
assert result[0].connection_info is None
782+
assert result[0].connector is None
783+
784+
# Verify the mock calls
785+
mock_conn.block_storage.attachments.assert_called_once_with(**filter)

0 commit comments

Comments
 (0)