Skip to content
This repository was archived by the owner on Oct 23, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions senza/manaus/ec2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import OrderedDict
from typing import Dict, List, Iterator
from typing import Dict, List, Iterator, Optional

import boto3

Expand All @@ -16,9 +16,10 @@ class EC2VPC:
def __init__(self,
vpc_id: str,
is_default: bool,
tags: List[Dict[str, str]]):
tags: Optional[List[Dict[str, str]]]):
self.vpc_id = vpc_id
self.is_default = is_default
tags = tags or [] # type: List[Dict[str, str]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can make this much shorter by using tags or []

self.tags = OrderedDict([(t['Key'], t['Value']) for t in tags]) # type: Dict[str, str]

self.name = self.tags.get('Name', self.vpc_id)
Expand Down
12 changes: 11 additions & 1 deletion tests/test_manaus/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def test_get_default_vpc(monkeypatch):
mock_vpc3.is_default = False
mock_vpc3.tags = []

mock_vpc4 = MagicMock()
mock_vpc4.vpc_id = 'vpc-id4'
mock_vpc4.is_default = True
mock_vpc4.tags = None

m_resource = MagicMock()
m_resource.return_value = m_resource
monkeypatch.setattr('boto3.resource', m_resource)
Expand All @@ -59,11 +64,16 @@ def test_get_default_vpc(monkeypatch):
ec2.get_default_vpc()
assert str(exc_info.value) == "Can't find any VPC!"

# no vpcs
# multiple vpcs
m_resource.vpcs.all.return_value = [mock_vpc2, mock_vpc3]
with pytest.raises(VPCError) as exc_info:
ec2.get_default_vpc()

# no tags in vpc return default vpc
m_resource.vpcs.all.return_value = [mock_vpc4, mock_vpc2]
vpc3 = ec2.get_default_vpc()
assert vpc3.vpc_id == 'vpc-id4'

assert str(exc_info.value) == ("Multiple VPCs are only supported if one "
"VPC is the default VPC (IsDefault=true)!")

Expand Down