Skip to content

Commit

Permalink
AwsSubnet: create in specific availability zone
Browse files Browse the repository at this point in the history
* AwsSubnet.do_create: support aws_availability_zone

* AwsVpc: support creation with name and vpc_cidr in the class.

* AwsVpc.delete: make async.  Could be better about what is handled in
executor context.

* Add tests for vpc and subnet creation
  • Loading branch information
hartmans committed Nov 13, 2023
1 parent 3275e26 commit 213701c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
23 changes: 16 additions & 7 deletions carthage_aws/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ class AwsVirtualPrivateCloud(AwsManaged):
stamp_type = "vpc"
resource_type = 'vpc'

vpc_cidr:str = None #: String representation of the v4 CIDR block for the VPC


def __init__(self, **kwargs):
def __init__(self, vpc_cidr=None, **kwargs):
super().__init__( **kwargs)
config = self.config_layout
if self.name is None:
Expand All @@ -45,6 +46,9 @@ def __init__(self, **kwargs):
if config.aws.vpc_id == None:
self.id = ''
else: self.id = config.aws.vpc_id
if vpc_cidr: self.vpc_cidr = vpc_cidr
if self.vpc_cidr is None:
self.vpc_cidr = str(config.aws.vpc_cidr)
self.vms = []


Expand All @@ -64,7 +68,7 @@ def do_create(self):
try:
r = self.connection.client.create_vpc(
InstanceTenancy='default',
CidrBlock=str(self.config_layout.aws.vpc_cidr),
CidrBlock=self.vpc_cidr,
TagSpecifications=[self.resource_tags])
self.id = r['Vpc']['VpcId']

Expand Down Expand Up @@ -105,19 +109,19 @@ def groups(self):
self.groups = list( groups['SecurityGroups'])
return self.groups

def delete(self):
async def delete(self):
for sn in self.mob.subnets.all():
sn.delete()
await run_in_executor(sn.delete)
for g in self.mob.security_groups.all():
try: g.delete()
try: await run_in_executor(g.delete)
except: pass
for gw in self.mob.internet_gateways.all():
gw.detach_from_vpc(VpcId=self.id)
gw.delete()
for rt in self.mob.route_tables.all():
try: rt.delete()
except: pass
self.mob.delete()
await run_in_executor(self.mob.delete)


@dataclasses.dataclass(frozen=True)
Expand Down Expand Up @@ -340,10 +344,15 @@ async def find(self):


def do_create(self):
availability_zone = self._gfi("aws_availability_zone", default=None)
extra_args = {}
if availability_zone:
extra_args['AvailabilityZone'] = availability_zone
try:
r = self.connection.client.create_subnet(VpcId=self.vpc.id,
CidrBlock=str(self.network.v4_config.network),
TagSpecifications=[self.resource_tags]
TagSpecifications=[self.resource_tags],
**extra_args
)
self.id = r['Subnet']['SubnetId']
# No need to associate subnet with main route table
Expand Down
26 changes: 26 additions & 0 deletions tests/test_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,29 @@ async def test_elastic_ip(carthage_layout):
await layout.ip_test.machine.delete()
await layout.ip_1.delete()

@async_test
async def test_aws_subnet_create(ainjector):
'''
Test creation of a VPC and a subnet
'''
class creation_vpc(AwsVirtualPrivateCloud, InjectableModel):
name = 'creation_vpc'
vpc_cidr = '10.1.0.0/16'

class created_subnet(NetworkModel):
v4_config = V4Config(network='10.1.0.0/24')
aws_availability_zone = 'us-east-1c'
try:
ainjector.add_provider(creation_vpc)
vpc = None
vpc = await ainjector.get_instance_async(creation_vpc)
with instantiation_not_ready():
subnet = await vpc.created_subnet.access_by(AwsSubnet)
await subnet.find()
assert not subnet.mob
await subnet.async_become_ready()
assert subnet.mob
assert subnet.mob.availability_zone == subnet._gfi("aws_availability_zone")
finally:
if vpc: await vpc.delete()

0 comments on commit 213701c

Please sign in to comment.