|
| 1 | +from .base import get_openstack_conn |
| 2 | +from fastmcp import FastMCP |
| 3 | +from openstack_mcp_server.tools.response.neutron import Network |
| 4 | + |
| 5 | + |
| 6 | +class NeutronTools: |
| 7 | + """ |
| 8 | + A class to encapsulate Neutron-related tools and utilities. |
| 9 | + """ |
| 10 | + |
| 11 | + def register_tools(self, mcp: FastMCP): |
| 12 | + """ |
| 13 | + Register Neutron-related tools with the FastMCP instance. |
| 14 | + """ |
| 15 | + |
| 16 | + mcp.tool()(self.get_neutron_networks) |
| 17 | + mcp.tool()(self.create_network) |
| 18 | + mcp.tool()(self.get_network_detail) |
| 19 | + mcp.tool()(self.update_network) |
| 20 | + mcp.tool()(self.delete_network) |
| 21 | + |
| 22 | + def get_neutron_networks( |
| 23 | + self, status_filter: str | None = None, shared_only: bool = False |
| 24 | + ) -> list[Network]: |
| 25 | + """ |
| 26 | + Get the list of Neutron networks with optional filtering. |
| 27 | +
|
| 28 | + Args: |
| 29 | + status_filter: Filter networks by status (e.g., 'ACTIVE', 'DOWN') |
| 30 | + shared_only: If True, only show shared networks |
| 31 | +
|
| 32 | + Returns: |
| 33 | + List of Network objects |
| 34 | + """ |
| 35 | + conn = get_openstack_conn() |
| 36 | + |
| 37 | + filters = {} |
| 38 | + |
| 39 | + if status_filter: |
| 40 | + filters["status"] = status_filter.upper() |
| 41 | + |
| 42 | + if shared_only: |
| 43 | + filters["shared"] = True |
| 44 | + |
| 45 | + networks = conn.list_networks(filters=filters) |
| 46 | + |
| 47 | + return [ |
| 48 | + self._convert_to_network_model(network) for network in networks |
| 49 | + ] |
| 50 | + |
| 51 | + def create_network( |
| 52 | + self, |
| 53 | + name: str, |
| 54 | + description: str | None = None, |
| 55 | + is_admin_state_up: bool = True, |
| 56 | + is_shared: bool = False, |
| 57 | + provider_network_type: str | None = None, |
| 58 | + provider_physical_network: str | None = None, |
| 59 | + provider_segmentation_id: int | None = None, |
| 60 | + ) -> Network: |
| 61 | + """ |
| 62 | + Create a new Neutron network. |
| 63 | +
|
| 64 | + Args: |
| 65 | + name: Network name |
| 66 | + description: Network description |
| 67 | + is_admin_state_up: Administrative state |
| 68 | + is_shared: Whether the network is shared |
| 69 | + provider_network_type: Provider network type (e.g., 'vlan', 'flat', 'vxlan') |
| 70 | + provider_physical_network: Physical network name |
| 71 | + provider_segmentation_id: Segmentation ID for VLAN/VXLAN |
| 72 | +
|
| 73 | + Returns: |
| 74 | + Created Network object |
| 75 | + """ |
| 76 | + conn = get_openstack_conn() |
| 77 | + |
| 78 | + network_args = { |
| 79 | + "name": name, |
| 80 | + "admin_state_up": is_admin_state_up, |
| 81 | + "shared": is_shared, |
| 82 | + } |
| 83 | + |
| 84 | + if description: |
| 85 | + network_args["description"] = description |
| 86 | + |
| 87 | + if provider_network_type: |
| 88 | + network_args["provider_network_type"] = provider_network_type |
| 89 | + |
| 90 | + if provider_physical_network: |
| 91 | + network_args["provider_physical_network"] = ( |
| 92 | + provider_physical_network |
| 93 | + ) |
| 94 | + |
| 95 | + if provider_segmentation_id is not None: |
| 96 | + network_args["provider_segmentation_id"] = provider_segmentation_id |
| 97 | + |
| 98 | + network = conn.network.create_network(**network_args) |
| 99 | + |
| 100 | + return self._convert_to_network_model(network) |
| 101 | + |
| 102 | + def get_network_detail(self, network_id: str) -> Network: |
| 103 | + """ |
| 104 | + Get detailed information about a specific Neutron network. |
| 105 | +
|
| 106 | + Args: |
| 107 | + network_id: ID of the network to retrieve |
| 108 | +
|
| 109 | + Returns: |
| 110 | + Network object with detailed information |
| 111 | + """ |
| 112 | + conn = get_openstack_conn() |
| 113 | + |
| 114 | + network = conn.network.get_network(network_id) |
| 115 | + if not network: |
| 116 | + raise Exception(f"Network with ID {network_id} not found") |
| 117 | + |
| 118 | + return self._convert_to_network_model(network) |
| 119 | + |
| 120 | + def update_network( |
| 121 | + self, |
| 122 | + network_id: str, |
| 123 | + name: str | None = None, |
| 124 | + description: str | None = None, |
| 125 | + is_admin_state_up: bool | None = None, |
| 126 | + is_shared: bool | None = None, |
| 127 | + ) -> Network: |
| 128 | + """ |
| 129 | + Update an existing Neutron network. |
| 130 | +
|
| 131 | + Args: |
| 132 | + network_id: ID of the network to update |
| 133 | + name: New network name |
| 134 | + description: New network description |
| 135 | + is_admin_state_up: New administrative state |
| 136 | + is_shared: New shared state |
| 137 | +
|
| 138 | + Returns: |
| 139 | + Updated Network object |
| 140 | + """ |
| 141 | + conn = get_openstack_conn() |
| 142 | + |
| 143 | + update_args = {} |
| 144 | + |
| 145 | + if name is not None: |
| 146 | + update_args["name"] = name |
| 147 | + if description is not None: |
| 148 | + update_args["description"] = description |
| 149 | + if is_admin_state_up is not None: |
| 150 | + update_args["admin_state_up"] = is_admin_state_up |
| 151 | + if is_shared is not None: |
| 152 | + update_args["shared"] = is_shared |
| 153 | + |
| 154 | + if not update_args: |
| 155 | + raise Exception("No update parameters provided") |
| 156 | + |
| 157 | + network = conn.network.update_network(network_id, **update_args) |
| 158 | + |
| 159 | + return self._convert_to_network_model(network) |
| 160 | + |
| 161 | + def delete_network(self, network_id: str) -> None: |
| 162 | + """ |
| 163 | + Delete a Neutron network. |
| 164 | +
|
| 165 | + Args: |
| 166 | + network_id: ID of the network to delete |
| 167 | +
|
| 168 | + Returns: |
| 169 | + None |
| 170 | + """ |
| 171 | + conn = get_openstack_conn() |
| 172 | + |
| 173 | + network = conn.network.get_network(network_id) |
| 174 | + if not network: |
| 175 | + raise Exception(f"Network with ID {network_id} not found") |
| 176 | + |
| 177 | + conn.network.delete_network(network_id, ignore_missing=False) |
| 178 | + |
| 179 | + return None |
| 180 | + |
| 181 | + def _convert_to_network_model(self, openstack_network) -> Network: |
| 182 | + """ |
| 183 | + Convert an OpenStack network object to a Network pydantic model. |
| 184 | +
|
| 185 | + Args: |
| 186 | + openstack_network: OpenStack network object |
| 187 | +
|
| 188 | + Returns: |
| 189 | + Network pydantic model |
| 190 | + """ |
| 191 | + return Network( |
| 192 | + id=openstack_network.id, |
| 193 | + name=openstack_network.name or "", |
| 194 | + status=openstack_network.status or "", |
| 195 | + description=openstack_network.description or None, |
| 196 | + is_admin_state_up=openstack_network.admin_state_up or False, |
| 197 | + is_shared=openstack_network.shared or False, |
| 198 | + mtu=openstack_network.mtu or None, |
| 199 | + provider_network_type=openstack_network.provider_network_type |
| 200 | + or None, |
| 201 | + provider_physical_network=openstack_network.provider_physical_network |
| 202 | + or None, |
| 203 | + provider_segmentation_id=openstack_network.provider_segmentation_id |
| 204 | + or None, |
| 205 | + project_id=openstack_network.project_id or None, |
| 206 | + ) |
0 commit comments