Skip to content

Commit 548cbd5

Browse files
committed
improve(network): integrating port update feature (#30)
1 parent ceebb24 commit 548cbd5

File tree

2 files changed

+41
-52
lines changed

2 files changed

+41
-52
lines changed

src/openstack_mcp_server/tools/network_tools.py

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,12 @@ def register_tools(self, mcp: FastMCP):
3737
mcp.tool()(self.get_port_detail)
3838
mcp.tool()(self.update_port)
3939
mcp.tool()(self.delete_port)
40-
4140
mcp.tool()(self.get_port_allowed_address_pairs)
4241
mcp.tool()(self.set_port_binding)
43-
4442
mcp.tool()(self.get_floating_ips)
4543
mcp.tool()(self.create_floating_ip)
46-
mcp.tool()(self.attach_floating_ip_to_port)
47-
mcp.tool()(self.detach_floating_ip_from_port)
4844
mcp.tool()(self.delete_floating_ip)
49-
mcp.tool()(self.update_floating_ip_description)
50-
mcp.tool()(self.reassign_floating_ip_to_port)
45+
mcp.tool()(self.update_floating_ip)
5146
mcp.tool()(self.create_floating_ips_bulk)
5247
mcp.tool()(self.assign_first_available_floating_ip)
5348

@@ -701,15 +696,46 @@ def attach_floating_ip_to_port(
701696
ip = conn.network.update_ip(floating_ip_id, **update_args)
702697
return self._convert_to_floating_ip_model(ip)
703698

704-
def detach_floating_ip_from_port(self, floating_ip_id: str) -> FloatingIP:
699+
def update_floating_ip(
700+
self,
701+
floating_ip_id: str,
702+
description: str | None | object = _UNSET,
703+
port_id: str | None | object = _UNSET,
704+
fixed_ip_address: str | None | object = _UNSET,
705+
) -> FloatingIP:
705706
"""
706-
Detach a Floating IP from its Port.
707+
Update Floating IP attributes. Only provided parameters are changed; omitted
708+
parameters remain untouched.
707709
708-
:param floating_ip_id: Floating IP ID
710+
Typical use-cases:
711+
- Attach to a port: port_id="port-1" (optionally fixed_ip_address="10.0.0.10").
712+
- Detach from its port: port_id=None.
713+
- Update description: description="new desc" or clear with description=None.
714+
- Reassign to another port: port_id="new-port" (optionally with fixed_ip_address).
715+
716+
Notes:
717+
- Passing None for description clears it.
718+
- Passing None for port_id detaches the address from any port.
719+
- fixed_ip_address is optional and can be provided alongside port_id.
720+
721+
:param floating_ip_id: Floating IP ID to update
722+
:param description: New description or None to clear
723+
:param port_id: Port ID to attach; None to detach; omit to keep unchanged
724+
:param fixed_ip_address: Specific fixed IP to map; omit to keep unchanged
709725
:return: Updated FloatingIP object
710726
"""
711727
conn = get_openstack_conn()
712-
ip = conn.network.update_ip(floating_ip_id, port_id=None)
728+
update_args: dict = {}
729+
if description is not _UNSET:
730+
update_args["description"] = description
731+
if port_id is not _UNSET:
732+
update_args["port_id"] = port_id
733+
if fixed_ip_address is not _UNSET:
734+
update_args["fixed_ip_address"] = fixed_ip_address
735+
if not update_args:
736+
current = conn.network.get_ip(floating_ip_id)
737+
return self._convert_to_floating_ip_model(current)
738+
ip = conn.network.update_ip(floating_ip_id, **update_args)
713739
return self._convert_to_floating_ip_model(ip)
714740

715741
def delete_floating_ip(self, floating_ip_id: str) -> None:
@@ -723,43 +749,6 @@ def delete_floating_ip(self, floating_ip_id: str) -> None:
723749
conn.network.delete_ip(floating_ip_id, ignore_missing=False)
724750
return None
725751

726-
def update_floating_ip_description(
727-
self,
728-
floating_ip_id: str,
729-
description: str | None,
730-
) -> FloatingIP:
731-
"""
732-
Update a Floating IP's description.
733-
734-
:param floating_ip_id: Floating IP ID
735-
:param description: New description (`None` to clear)
736-
:return: Updated FloatingIP object
737-
"""
738-
conn = get_openstack_conn()
739-
ip = conn.network.update_ip(floating_ip_id, description=description)
740-
return self._convert_to_floating_ip_model(ip)
741-
742-
def reassign_floating_ip_to_port(
743-
self,
744-
floating_ip_id: str,
745-
port_id: str,
746-
fixed_ip_address: str | None = None,
747-
) -> FloatingIP:
748-
"""
749-
Reassign a Floating IP to a different Port.
750-
751-
:param floating_ip_id: Floating IP ID
752-
:param port_id: New port ID to attach
753-
:param fixed_ip_address: Specific fixed IP on the port (optional)
754-
:return: Updated Floating IP object
755-
"""
756-
conn = get_openstack_conn()
757-
update_args: dict = {"port_id": port_id}
758-
if fixed_ip_address is not None:
759-
update_args["fixed_ip_address"] = fixed_ip_address
760-
ip = conn.network.update_ip(floating_ip_id, **update_args)
761-
return self._convert_to_floating_ip_model(ip)
762-
763752
def create_floating_ips_bulk(
764753
self,
765754
floating_network_id: str,

tests/tools/test_network_tools.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,15 +1213,15 @@ def test_create_attach_detach_delete_floating_ip(
12131213
updated.router_id = None
12141214
mock_conn.network.update_ip.return_value = updated
12151215

1216-
attached = tools.attach_floating_ip_to_port(
1216+
attached = tools.update_floating_ip(
12171217
"fip-1",
1218-
"port-1",
1218+
port_id="port-1",
12191219
fixed_ip_address="10.0.0.10",
12201220
)
12211221
assert attached.port_id == "port-1"
12221222

12231223
updated.port_id = None
1224-
detached = tools.detach_floating_ip_from_port("fip-1")
1224+
detached = tools.update_floating_ip("fip-1", port_id=None)
12251225
assert detached.port_id is None
12261226

12271227
mock_conn.network.get_ip.return_value = updated
@@ -1251,11 +1251,11 @@ def test_update_reassign_bulk_and_auto_assign_floating_ip(
12511251
mock_conn.network.update_ip.return_value = updated
12521252

12531253
tools = self.get_network_tools()
1254-
res_desc = tools.update_floating_ip_description("fip-1", "new desc")
1254+
res_desc = tools.update_floating_ip("fip-1", description="new desc")
12551255
assert res_desc.description == "new desc"
12561256

12571257
updated.port_id = "port-2"
1258-
res_reassign = tools.reassign_floating_ip_to_port("fip-1", "port-2")
1258+
res_reassign = tools.update_floating_ip("fip-1", port_id="port-2")
12591259
assert res_reassign.port_id == "port-2"
12601260

12611261
f1 = Mock()

0 commit comments

Comments
 (0)