Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 359355d

Browse files
committed
Normalize tag format in GetSecurityGroupsForVpc response (#12602)
Signed-off-by: Ramtin Mesgari <[email protected]>
1 parent 05ecace commit 359355d

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

‎localstack-core/localstack/services/ec2/provider.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,14 @@ def get_security_groups_for_vpc(
560560
GroupName=sg.name,
561561
OwnerId=context.account_id,
562562
PrimaryVpcId=sg.vpc_id,
563-
Tags=[{"Key": k, "Value": v} for k, v in (sg.get_tags() or {}).items()],
563+
Tags=[
564+
{
565+
"Key": tag.get("key") or tag.get("Key"),
566+
"Value": tag.get("value") or tag.get("Value"),
567+
}
568+
for tag in (sg.get_tags() or [])
569+
if ("key" in tag or "Key" in tag) and ("value" in tag or "Value" in tag)
570+
],
564571
)
565572
for sg in filtered_sgs
566573
]

‎tests/aws/services/ec2/test_ec2.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,11 @@ def _create_security_group() -> dict:
706706
def test_get_security_groups_for_vpc(
707707
self, snapshot, aws_client, create_vpc, ec2_create_security_group
708708
):
709+
vpc_name = f"test-vpc-{short_uid()}"
709710
group_name = f"test-security-group-{short_uid()}"
710711
group_description = f"Description for {group_name}"
712+
tag_key = f"test-key-{short_uid()}"
713+
tag_value = f"test-value-{short_uid()}"
711714

712715
# Returned security groups appear to be sorted by the randomly generated GroupId field,
713716
# so we should sort snapshots by this value to mitigate flakiness for runs against AWS.
@@ -719,10 +722,21 @@ def test_get_security_groups_for_vpc(
719722
snapshot.add_transformer(snapshot.transform.key_value("VpcId"))
720723
snapshot.add_transformer(snapshot.transform.key_value("AssociationId"))
721724
snapshot.add_transformer(snapshot.transform.key_value("DhcpOptionsId"))
725+
snapshot.add_transformer(snapshot.transform.key_value("Key"))
726+
snapshot.add_transformer(snapshot.transform.key_value("Value"))
722727

723728
# Create VPC for testing
724729
vpc: dict = create_vpc(
725730
cidr_block="10.0.0.0/16",
731+
tag_specifications=[
732+
{
733+
"ResourceType": "vpc",
734+
"Tags": [
735+
{"Key": "Name", "Value": vpc_name},
736+
{"Key": tag_key, "Value": tag_value},
737+
],
738+
}
739+
],
726740
)
727741
vpc_id: str = vpc["Vpc"]["VpcId"]
728742
snapshot.match("create_vpc_response", vpc)

‎tests/aws/services/ec2/test_ec2.snapshot.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@
337337
}
338338
},
339339
"tests/aws/services/ec2/test_ec2.py::TestEc2Integrations::test_get_security_groups_for_vpc": {
340-
"recorded-date": "18-05-2025, 18:08:00",
340+
"recorded-date": "19-05-2025, 13:08:48",
341341
"recorded-content": {
342342
"create_vpc_response": {
343343
"Vpc": {
@@ -357,6 +357,16 @@
357357
"IsDefault": false,
358358
"OwnerId": "111111111111",
359359
"State": "pending",
360+
"Tags": [
361+
{
362+
"Key": "<key:1>",
363+
"Value": "<value:1>"
364+
},
365+
{
366+
"Key": "<key:2>",
367+
"Value": "<value:2>"
368+
}
369+
],
360370
"VpcId": "<vpc-id:1>"
361371
},
362372
"ResponseMetadata": {
@@ -369,7 +379,7 @@
369379
{
370380
"Description": "<group-name:1> VPC security group",
371381
"GroupId": "<group-id:1>",
372-
"GroupName": "<group-name:1>",
382+
"Group<key:1>": "<group-name:1>",
373383
"OwnerId": "111111111111",
374384
"PrimaryVpcId": "<vpc-id:1>",
375385
"Tags": []
@@ -393,15 +403,15 @@
393403
{
394404
"Description": "<group-name:1> VPC security group",
395405
"GroupId": "<group-id:1>",
396-
"GroupName": "<group-name:1>",
406+
"Group<key:1>": "<group-name:1>",
397407
"OwnerId": "111111111111",
398408
"PrimaryVpcId": "<vpc-id:1>",
399409
"Tags": []
400410
},
401411
{
402412
"Description": "Description for <group-name:2>",
403413
"GroupId": "<group-id:2>",
404-
"GroupName": "<group-name:2>",
414+
"Group<key:1>": "<group-name:2>",
405415
"OwnerId": "111111111111",
406416
"PrimaryVpcId": "<vpc-id:1>",
407417
"Tags": []

‎tests/aws/services/ec2/test_ec2.validation.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"last_validated_date": "2024-06-07T01:11:12+00:00"
1313
},
1414
"tests/aws/services/ec2/test_ec2.py::TestEc2Integrations::test_get_security_groups_for_vpc": {
15-
"last_validated_date": "2025-05-18T18:07:59+00:00"
15+
"last_validated_date": "2025-05-19T13:09:05+00:00"
1616
},
1717
"tests/aws/services/ec2/test_ec2.py::TestEc2Integrations::test_vcp_peering_difference_regions": {
1818
"last_validated_date": "2024-06-07T21:28:25+00:00"

0 commit comments

Comments
 (0)