Skip to content

Instantly share code, notes, and snippets.

@ohlol
Created April 12, 2020 18:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
def subnet_generator(subnets, vpc):
vpc_network = vpc.cidr_block.apply(ip_network)
nxt = str(vpc_network.network_address)
print(nxt) # prints `<pulumi.output.Output object at 0x108b93730>`
def valid_cidr(s1, s2):
if s1.network_address == s2.network_address:
return True
elif s1 > s2:
return True
else:
return False
for subnet in subnets:
count = subnet.get('az_count')
availability_zones = getn_availability_zones(count)
for i in range(count):
availability_zone = availability_zones[i]
mask = subnet.get('cidr_mask')
subnet_cidr = next(sn for sn in vpc_network.subnets(new_prefix=mask) if valid_cidr(sn, ip_network(nxt)))
nxt = str(subnet_cidr.broadcast_address + 1) # raises `TypeError: 'Output' object is not callable`
yield {
'name': vpc.name.apply(lambda x: f'{x}-{subnet.get("name")}_{availability_zone}'),
'cidr_block': str(subnet_cidr),
'has_natgw': subnet.get('has_natgw', False),
'availability_zone': availability_zone,
'kind': subnet.get('kind'),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment