|
| 1 | +from fastmcp import FastMCP |
| 2 | + |
| 3 | +from .base import get_openstack_conn |
| 4 | +from .response.block_storage import ( |
| 5 | + Volume, |
| 6 | + VolumeAttachment, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +class BlockStorageTools: |
| 11 | + """ |
| 12 | + A class to encapsulate Block Storage-related tools and utilities. |
| 13 | + """ |
| 14 | + |
| 15 | + def register_tools(self, mcp: FastMCP): |
| 16 | + """ |
| 17 | + Register Block Storage-related tools with the FastMCP instance. |
| 18 | + """ |
| 19 | + mcp.tool()(self.get_volumes) |
| 20 | + mcp.tool()(self.get_volume_details) |
| 21 | + mcp.tool()(self.create_volume) |
| 22 | + mcp.tool()(self.delete_volume) |
| 23 | + mcp.tool()(self.extend_volume) |
| 24 | + |
| 25 | + def get_volumes(self) -> list[Volume]: |
| 26 | + """ |
| 27 | + Get the list of Block Storage volumes. |
| 28 | +
|
| 29 | + :return: A list of Volume objects representing the volumes. |
| 30 | + """ |
| 31 | + conn = get_openstack_conn() |
| 32 | + |
| 33 | + # List the volumes |
| 34 | + volume_list = [] |
| 35 | + for volume in conn.block_storage.volumes(): |
| 36 | + attachments = [] |
| 37 | + for attachment in volume.attachments or []: |
| 38 | + attachments.append( |
| 39 | + VolumeAttachment( |
| 40 | + server_id=attachment.get("server_id"), |
| 41 | + device=attachment.get("device"), |
| 42 | + attachment_id=attachment.get("id"), |
| 43 | + ), |
| 44 | + ) |
| 45 | + |
| 46 | + volume_list.append( |
| 47 | + Volume( |
| 48 | + id=volume.id, |
| 49 | + name=volume.name, |
| 50 | + status=volume.status, |
| 51 | + size=volume.size, |
| 52 | + volume_type=volume.volume_type, |
| 53 | + availability_zone=volume.availability_zone, |
| 54 | + created_at=str(volume.created_at) |
| 55 | + if volume.created_at |
| 56 | + else None, |
| 57 | + is_bootable=volume.is_bootable, |
| 58 | + is_encrypted=volume.is_encrypted, |
| 59 | + description=volume.description, |
| 60 | + attachments=attachments, |
| 61 | + ), |
| 62 | + ) |
| 63 | + |
| 64 | + return volume_list |
| 65 | + |
| 66 | + def get_volume_details(self, volume_id: str) -> Volume: |
| 67 | + """ |
| 68 | + Get detailed information about a specific volume. |
| 69 | +
|
| 70 | + :param volume_id: The ID of the volume to get details for |
| 71 | + :return: A Volume object with detailed information |
| 72 | + """ |
| 73 | + conn = get_openstack_conn() |
| 74 | + |
| 75 | + volume = conn.block_storage.get_volume(volume_id) |
| 76 | + |
| 77 | + attachments = [] |
| 78 | + for attachment in volume.attachments or []: |
| 79 | + attachments.append( |
| 80 | + VolumeAttachment( |
| 81 | + server_id=attachment.get("server_id"), |
| 82 | + device=attachment.get("device"), |
| 83 | + attachment_id=attachment.get("id"), |
| 84 | + ), |
| 85 | + ) |
| 86 | + |
| 87 | + return Volume( |
| 88 | + id=volume.id, |
| 89 | + name=volume.name, |
| 90 | + status=volume.status, |
| 91 | + size=volume.size, |
| 92 | + volume_type=volume.volume_type, |
| 93 | + availability_zone=volume.availability_zone, |
| 94 | + created_at=str(volume.created_at), |
| 95 | + is_bootable=volume.is_bootable, |
| 96 | + is_encrypted=volume.is_encrypted, |
| 97 | + description=volume.description, |
| 98 | + attachments=attachments, |
| 99 | + ) |
| 100 | + |
| 101 | + def create_volume( |
| 102 | + self, |
| 103 | + name: str, |
| 104 | + size: int, |
| 105 | + description: str | None = None, |
| 106 | + volume_type: str | None = None, |
| 107 | + availability_zone: str | None = None, |
| 108 | + bootable: bool | None = None, |
| 109 | + image: str | None = None, |
| 110 | + ) -> Volume: |
| 111 | + """ |
| 112 | + Create a new volume. |
| 113 | +
|
| 114 | + :param name: Name for the new volume |
| 115 | + :param size: Size of the volume in GB |
| 116 | + :param description: Optional description for the volume |
| 117 | + :param volume_type: Optional volume type |
| 118 | + :param availability_zone: Optional availability zone |
| 119 | + :param bootable: Optional flag to make the volume bootable |
| 120 | + :param image: Optional Image name, ID or object from which to create |
| 121 | + :return: The created Volume object |
| 122 | + """ |
| 123 | + conn = get_openstack_conn() |
| 124 | + |
| 125 | + volume_kwargs = { |
| 126 | + "name": name, |
| 127 | + } |
| 128 | + |
| 129 | + if description is not None: |
| 130 | + volume_kwargs["description"] = description |
| 131 | + if volume_type is not None: |
| 132 | + volume_kwargs["volume_type"] = volume_type |
| 133 | + if availability_zone is not None: |
| 134 | + volume_kwargs["availability_zone"] = availability_zone |
| 135 | + |
| 136 | + volume = conn.block_storage.create_volume( |
| 137 | + size=size, |
| 138 | + image=image, |
| 139 | + bootable=bootable, |
| 140 | + **volume_kwargs, |
| 141 | + ) |
| 142 | + |
| 143 | + volume_obj = Volume( |
| 144 | + id=volume.id, |
| 145 | + name=volume.name, |
| 146 | + status=volume.status, |
| 147 | + size=volume.size, |
| 148 | + volume_type=volume.volume_type, |
| 149 | + availability_zone=volume.availability_zone, |
| 150 | + created_at=str(volume.created_at), |
| 151 | + is_bootable=volume.is_bootable, |
| 152 | + is_encrypted=volume.is_encrypted, |
| 153 | + description=volume.description, |
| 154 | + attachments=[], |
| 155 | + ) |
| 156 | + |
| 157 | + return volume_obj |
| 158 | + |
| 159 | + def delete_volume(self, volume_id: str, force: bool = False) -> None: |
| 160 | + """ |
| 161 | + Delete a volume. |
| 162 | +
|
| 163 | + :param volume_id: The ID of the volume to delete |
| 164 | + :param force: Whether to force delete the volume |
| 165 | + :return: None |
| 166 | + """ |
| 167 | + conn = get_openstack_conn() |
| 168 | + |
| 169 | + conn.block_storage.delete_volume( |
| 170 | + volume_id, |
| 171 | + force=force, |
| 172 | + ignore_missing=False, |
| 173 | + ) |
| 174 | + |
| 175 | + def extend_volume(self, volume_id: str, new_size: int) -> None: |
| 176 | + """ |
| 177 | + Extend a volume to a new size. |
| 178 | +
|
| 179 | + :param volume_id: The ID of the volume to extend |
| 180 | + :param new_size: The new size in GB (must be larger than current size) |
| 181 | + :return: None |
| 182 | + """ |
| 183 | + conn = get_openstack_conn() |
| 184 | + |
| 185 | + conn.block_storage.extend_volume(volume_id, new_size) |
0 commit comments