Skip to content

Commit

Permalink
Merge pull request #31 from cooperwalbrun/tagless-fix
Browse files Browse the repository at this point in the history
Handle Tagless VPCs Gracefully
  • Loading branch information
cooperwalbrun authored Feb 14, 2024
2 parents 8f7f352 + 654d8c0 commit c0f8ef3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/aws_cidr_finder/boto_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_boto_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c0f8ef3

Please sign in to comment.