Skip to content

Commit 5ffc0c7

Browse files
committed
feat(block): Add attachments list(#70)
- Add get_attchments tools - Tests are updated and passing
1 parent 0f071d4 commit 5ffc0c7

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
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: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ def test_register_tools(self):
616616
block_storage_tools.register_tools(mock_mcp)
617617

618618
# Verify mcp.tool() was called for each method
619-
assert mock_mcp.tool.call_count == 6
619+
assert mock_mcp.tool.call_count == 7
620620

621621
# Verify all methods were registered
622622
registered_methods = [
@@ -744,3 +744,36 @@ def test_get_attachment_details(
744744
mock_conn.block_storage.get_attachment.assert_called_once_with(
745745
"attach-123"
746746
)
747+
748+
def test_get_attachments(self, mock_get_openstack_conn_block_storage):
749+
"""Test getting attachments."""
750+
mock_conn = mock_get_openstack_conn_block_storage
751+
752+
# Create mock attachment object
753+
mock_attachment = Mock()
754+
mock_attachment.id = "attach-123"
755+
mock_attachment.instance = "server-123"
756+
mock_attachment.volume_id = "vol-123"
757+
mock_attachment.status = "attached"
758+
759+
mock_conn.block_storage.attachments.return_value = [mock_attachment]
760+
761+
# Test attachments
762+
block_storage_tools = BlockStorageTools()
763+
764+
filter = {
765+
"volume_id": "vol-123",
766+
"instance": "server-123",
767+
}
768+
result = block_storage_tools.get_attachments(**filter)
769+
770+
# Verify the result
771+
assert isinstance(result, list)
772+
assert len(result) == 1
773+
assert result[0].id == "attach-123"
774+
assert result[0].instance == "server-123"
775+
assert result[0].volume_id == "vol-123"
776+
assert result[0].status == "attached"
777+
778+
# Verify the mock calls
779+
mock_conn.block_storage.attachments.assert_called_once_with(**filter)

0 commit comments

Comments
 (0)