Skip to content

Commit bc33054

Browse files
committed
fix(network): add id field to get security group (#85)
1 parent 5bbca59 commit bc33054

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/openstack_mcp_server/tools/network_tools.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,12 +1175,14 @@ def get_security_groups(
11751175
self,
11761176
project_id: str | None = None,
11771177
name: str | None = None,
1178+
id: str | None = None,
11781179
) -> list[SecurityGroup]:
11791180
"""
11801181
Get the list of Security Groups with optional filtering.
11811182
11821183
:param project_id: Filter by project ID
11831184
:param name: Filter by security group name
1185+
:param id: Filter by security group ID
11841186
:return: List of SecurityGroup objects
11851187
"""
11861188
conn = get_openstack_conn()
@@ -1189,6 +1191,8 @@ def get_security_groups(
11891191
filters["project_id"] = project_id
11901192
if name:
11911193
filters["name"] = name
1194+
if id:
1195+
filters["id"] = id
11921196
security_groups = conn.network.security_groups(**filters)
11931197
return [
11941198
self._convert_to_security_group_model(sg) for sg in security_groups
@@ -1282,7 +1286,8 @@ def _convert_to_security_group_model(self, openstack_sg) -> SecurityGroup:
12821286
rules = getattr(openstack_sg, "security_group_rules", None)
12831287
if rules is not None:
12841288
dto_rules = [
1285-
self._convert_to_security_group_rule_model(r) for r in rules
1289+
SecurityGroupRule.model_validate(r, from_attributes=True)
1290+
for r in rules
12861291
]
12871292
rule_ids = [str(r.id) for r in dto_rules if getattr(r, "id", None)]
12881293

@@ -1294,12 +1299,3 @@ def _convert_to_security_group_model(self, openstack_sg) -> SecurityGroup:
12941299
project_id=getattr(openstack_sg, "project_id", None),
12951300
security_group_rule_ids=rule_ids,
12961301
)
1297-
1298-
def _convert_to_security_group_rule_model(self, rule) -> SecurityGroupRule:
1299-
"""
1300-
Convert an OpenStack Security Group Rule to a SecurityGroupRule DTO.
1301-
1302-
:param rule: OpenStack security group rule object or dict
1303-
:return: Pydantic SecurityGroupRule model
1304-
"""
1305-
return SecurityGroupRule.model_validate(rule, from_attributes=True)

tests/tools/test_network_tools.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,37 @@ def test_get_security_groups_filters(self, mock_openstack_connect_network):
14001400
project_id="proj-1", name="default"
14011401
)
14021402

1403+
def test_get_security_groups_filter_by_id(
1404+
self, mock_openstack_connect_network
1405+
):
1406+
mock_conn = mock_openstack_connect_network
1407+
1408+
sg = Mock()
1409+
sg.id = "sg-1"
1410+
sg.name = "default"
1411+
sg.status = None
1412+
sg.description = "desc"
1413+
sg.project_id = "proj-1"
1414+
sg.security_group_rules = [
1415+
{"id": "r-1"},
1416+
{"id": "r-2"},
1417+
]
1418+
mock_conn.network.security_groups.return_value = [sg]
1419+
1420+
tools = self.get_network_tools()
1421+
res = tools.get_security_groups(id="sg-1")
1422+
assert res == [
1423+
SecurityGroup(
1424+
id="sg-1",
1425+
name="default",
1426+
status=None,
1427+
description="desc",
1428+
project_id="proj-1",
1429+
security_group_rule_ids=["r-1", "r-2"],
1430+
)
1431+
]
1432+
mock_conn.network.security_groups.assert_called_once_with(id="sg-1")
1433+
14031434
def test_create_security_group(self, mock_openstack_connect_network):
14041435
mock_conn = mock_openstack_connect_network
14051436
sg = Mock()
@@ -1463,7 +1494,10 @@ def test_update_security_group(self, mock_openstack_connect_network):
14631494
"sg-4", name="new-name", description="new-desc"
14641495
)
14651496

1466-
# No fields -> returns current
1497+
def test_update_security_group_no_fields_returns_current(
1498+
self, mock_openstack_connect_network
1499+
):
1500+
mock_conn = mock_openstack_connect_network
14671501
current = Mock()
14681502
current.id = "sg-5"
14691503
current.name = "cur"
@@ -1472,8 +1506,11 @@ def test_update_security_group(self, mock_openstack_connect_network):
14721506
current.project_id = None
14731507
current.security_group_rules = None
14741508
mock_conn.network.get_security_group.return_value = current
1475-
res2 = tools.update_security_group("sg-5")
1476-
assert res2.id == "sg-5"
1509+
1510+
tools = self.get_network_tools()
1511+
res = tools.update_security_group("sg-5")
1512+
assert res.id == "sg-5"
1513+
mock_conn.network.get_security_group.assert_called_once_with("sg-5")
14771514

14781515
def test_delete_security_group(self, mock_openstack_connect_network):
14791516
mock_conn = mock_openstack_connect_network

0 commit comments

Comments
 (0)