Skip to content

Commit e649122

Browse files
committed
feat(cinder): Add get attachments tool(#70)
- Add get attachments tool - Tests are updated and passing
1 parent 0b84ea2 commit e649122

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/openstack_mcp_server/tools/block_storage_tools.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .base import get_openstack_conn
44
from .response.block_storage import (
55
Attachment,
6+
AttachmentSummary,
67
ConnectionInfo,
78
Volume,
89
VolumeAttachment,
@@ -25,6 +26,7 @@ def register_tools(self, mcp: FastMCP):
2526
mcp.tool()(self.extend_volume)
2627

2728
mcp.tool()(self.get_attachment_details)
29+
mcp.tool()(self.get_attachments)
2830

2931
def get_volumes(self) -> list[Volume]:
3032
"""
@@ -225,3 +227,34 @@ def get_attachment_details(self, attachment_id: str) -> Attachment:
225227
}
226228

227229
return Attachment(**params)
230+
231+
def get_attachments(
232+
self,
233+
volume_id: str | None = None,
234+
instance: str | None = None,
235+
) -> list[Attachment]:
236+
"""
237+
Get the list of attachments.
238+
239+
:return: A list of Attachment objects.
240+
"""
241+
conn = get_openstack_conn()
242+
243+
filter = {}
244+
if volume_id:
245+
filter["volume_id"] = volume_id
246+
if instance:
247+
filter["instance"] = instance
248+
249+
attachments = []
250+
for attachment in conn.block_storage.attachments(**filter):
251+
attachments.append(
252+
AttachmentSummary(
253+
id=attachment.id,
254+
instance=attachment.instance,
255+
volume_id=attachment.volume_id,
256+
status=attachment.status,
257+
)
258+
)
259+
260+
return attachments

src/openstack_mcp_server/tools/response/block_storage.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ class Attachment(BaseModel):
4242
attach_mode: str | None = None
4343
connection_info: ConnectionInfo | None = None
4444
connector: str | None = None
45+
46+
47+
class AttachmentSummary(BaseModel):
48+
id: str
49+
instance: str
50+
volume_id: str
51+
status: str

tests/tools/test_block_storage_tools.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,3 +741,36 @@ 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+
756+
mock_conn.block_storage.attachments.return_value = [mock_attachment]
757+
758+
# Test attachments
759+
block_storage_tools = BlockStorageTools()
760+
761+
filter = {
762+
"volume_id": "vol-123",
763+
"instance": "server-123",
764+
}
765+
result = block_storage_tools.get_attachments(**filter)
766+
767+
# Verify the result
768+
assert isinstance(result, list)
769+
assert len(result) == 1
770+
assert result[0].id == "attach-123"
771+
assert result[0].instance == "server-123"
772+
assert result[0].volume_id == "vol-123"
773+
assert result[0].status == "attached"
774+
775+
# Verify the mock calls
776+
mock_conn.block_storage.attachments.assert_called_once_with(**filter)

0 commit comments

Comments
 (0)