From 654d8c0228e875f59a672e3f448bdff803af06d0 Mon Sep 17 00:00:00 2001 From: Cooper Walbrun <69944858+cooperwalbrun@users.noreply.github.com> Date: Tue, 13 Feb 2024 18:18:06 -0600 Subject: [PATCH] Handle tagless VPCs gracefully --- CHANGELOG.md | 7 +++++++ src/aws_cidr_finder/boto_wrapper.py | 9 ++++----- tests/test_boto_wrapper.py | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14f2d9f..768b8e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Nothing currently! +## v0.6.2 - 2024-02-13 + +### Fixed + +* VPCs with no tags will no longer induce the `KeyError: 'Tags'` error from Boto (by + [@cooperwalbrun](https://github.com/cooperwalbrun)) + ## v0.6.1 - 2024-01-18 ### Added diff --git a/src/aws_cidr_finder/boto_wrapper.py b/src/aws_cidr_finder/boto_wrapper.py index fb0cea6..9c4dac0 100644 --- a/src/aws_cidr_finder/boto_wrapper.py +++ b/src/aws_cidr_finder/boto_wrapper.py @@ -3,15 +3,14 @@ import boto3 from mypy_boto3_ec2 import EC2Client -from mypy_boto3_ec2.type_defs import TagTypeDef, VpcTypeDef, DescribeSubnetsResultTypeDef, \ - SubnetTypeDef +from mypy_boto3_ec2.type_defs import VpcTypeDef, DescribeSubnetsResultTypeDef, SubnetTypeDef from aws_cidr_finder import core from aws_cidr_finder.custom_types import VPC, SingleCIDRVPC -def _get_vpc_name(tags: list[TagTypeDef]) -> Optional[str]: - for key_value_pair in tags: +def _get_vpc_name(vpc: VpcTypeDef) -> Optional[str]: + for key_value_pair in vpc.get("Tags", []): if key_value_pair["Key"] == "Name": return key_value_pair["Value"] return None @@ -66,7 +65,7 @@ def _get_vpc_data(self, *, ipv6: bool) -> list[VPC]: # pragma: no cover return [ VPC( id=vpc["VpcId"], - name=_get_vpc_name(vpc["Tags"]), + name=_get_vpc_name(vpc), cidrs=_parse_vpc_cidrs(vpc, ipv6=ipv6), subnets=_parse_subnet_cidrs( self._get_subnet_cidrs(vpc["VpcId"])["Subnets"], ipv6=ipv6 diff --git a/tests/test_boto_wrapper.py b/tests/test_boto_wrapper.py index 5feccfc..a4d3504 100644 --- a/tests/test_boto_wrapper.py +++ b/tests/test_boto_wrapper.py @@ -9,8 +9,8 @@ def _assert_lists_equal(actual: list[Any], expected: list[Any]) -> None: def test_get_vpc_name() -> None: - assert boto_wrapper._get_vpc_name([]) is None - assert boto_wrapper._get_vpc_name([{"Key": "Name", "Value": "test"}]) == "test" + assert boto_wrapper._get_vpc_name({}) is None + assert boto_wrapper._get_vpc_name({"Tags": [{"Key": "Name", "Value": "test"}]}) == "test" def test_parse_vpc_cidrs_ipv4() -> None: