def apply_tags(self, aws_obj, role=None): delegate = self._delegate['delegate'] apply_tag(aws_obj, tag='Name', val=stanza('nametag')) apply_tag(aws_obj, tag='Role', val=role) apply_tag(aws_obj, tag='Delegate', val=delegate)
def subnet_obj(self, create=False, dry_run=False): """ Subnet object is returned from cache if cached. Otherwise, the method validates the subnet, creates it if necessary, populates tree, and returns subnet object. """ if self._subnet['s_obj']: return self._subnet['s_obj'] s_stanza = stanza('subnets') log.debug("Subnet stanza {!r}".format(s_stanza)) az = self.availability_zone() vpc = self.vpc() vpc_obj = self.vpc_obj(create=False, quiet=True) delegate = self._subnet['delegate'] cidr_block = '10.0.{}.0/24'.format(delegate) if delegate not in s_stanza or s_stanza[delegate] == {}: # # create new subnet if create: s_stanza[delegate] = {} log.debug("About to create subnet {}".format(cidr_block)) if dry_run: log.info("Dry run: doing nothing") s_obj = None else: s_obj = vpc.create_subnet(vpc_obj.id, cidr_block, dry_run=dry_run, availability_zone=az) log.info("Created subnet {} ({})".format( s_obj.id, s_obj.cidr_block)) s_stanza[delegate]['cidr_block'] = s_obj.cidr_block s_stanza[delegate]['id'] = s_obj.id stanza('subnets', s_stanza) apply_tag(s_obj, tag='Name', val=stanza('nametag')) apply_tag(s_obj, tag='Delegate', val=delegate) else: log.info( "Delegate {} subnet ID missing in yaml".format(delegate)) s_obj = None return s_obj # # check id exists and cidr_block matches s_id = s_stanza[delegate]['id'] log.debug("Getting subnet id {}".format(s_id)) s_list = vpc.get_all_subnets(subnet_ids=[s_id]) assert len(s_list) == 1, "Subnet ID {} does not exist".format(s_id) s_obj = s_list[0] log.info("Found subnet {} ({})".format(s_obj.id, s_obj.cidr_block)) if ('cidr_block' not in s_stanza[delegate] or s_stanza[delegate]['cidr_block'] is None): # pragma: no cover # # set cidr_block s_stanza[delegate]['cidr_block'] = s_obj.cidr_block stanza('subnets', s_stanza) else: # # validate cidr_block assert s_stanza[delegate]['cidr_block'] == s_obj.cidr_block, (( "Delegate {} is supposed to have subnet {}, but that " "subnet exists with non-matching CIDR block {}").format( delegate, s_stanza[delegate]['cidr_block'], s_obj.cidr_block)) self._subnet['s_obj'] = s_obj # if self.args.retag: # apply_tag(s_obj, tag='Name', val=stanza('nametag')) # apply_tag(s_obj, tag='Delegate', val=delegate) return s_obj
def vpc_obj(self, create=False, dry_run=False, quiet=False): """ fetch VPC object, create if necessary """ # # cached VPC object if self._vpc['vpc_obj'] is not None: return self._vpc['vpc_obj'] # # non-cached vpc_stanza = stanza('vpc') vpc_conn = self.vpc() if len(vpc_stanza) == 0: # pragma: no cover # # create VPC if create: if dry_run: log.info("Dry run: do nothing") vpc_obj = None else: log.info("VPC ID not specified in yaml: creating VPC") vpc_obj = vpc_conn.create_vpc('10.0.0.0/16') vpc_stanza['id'] = vpc_obj.id vpc_stanza['cidr_block'] = vpc_obj.cidr_block log.info("New VPC ID {} created with CIDR block {}".format( vpc_obj.id, vpc_obj.cidr_block )) apply_tag(vpc_obj, tag='Name', val=stanza('nametag')) self._vpc['vpc_obj'] = vpc_obj stanza('vpc', { 'cidr_block': vpc_obj.cidr_block, 'id': vpc_obj.id }) vpc_conn.modify_vpc_attribute( vpc_obj.id, enable_dns_support=True, ) vpc_conn.modify_vpc_attribute( vpc_obj.id, enable_dns_hostnames=True, ) else: log.info("VPC ID not specified in yaml: nothing to do") vpc_obj = None return vpc_obj # # existing VPC log.debug("VPD ID specified in yaml: fetching it") vpc_id = vpc_stanza['id'] if not quiet: log.info("VPC ID according to yaml is {}".format(vpc_id)) vpc_list = vpc_conn.get_all_vpcs(vpc_ids=vpc_id) assert len(vpc_list) == 1, ( "VPC ID {} does not exist".format(vpc_id)) vpc_obj = vpc_list[0] cidr_block = vpc_obj.cidr_block assert cidr_block == '10.0.0.0/16', ( ("VPC ID {} exists, but has wrong CIDR block {} " "(should be 10.0.0.0/16)").format(vpc_id, cidr_block)) if not quiet: log.info("VPC ID is {}, CIDR block is {}".format( vpc_stanza['id'], vpc_stanza['cidr_block'], )) self._vpc['vpc_obj'] = vpc_obj vpc_conn.modify_vpc_attribute( vpc_obj.id, enable_dns_support=True, ) vpc_conn.modify_vpc_attribute( vpc_obj.id, enable_dns_hostnames=True, ) return vpc_obj
def subnet_obj(self, create=False, dry_run=False): """ Subnet object is returned from cache if cached. Otherwise, the method validates the subnet, creates it if necessary, populates tree, and returns subnet object. """ if self._subnet['s_obj']: return self._subnet['s_obj'] s_stanza = stanza('subnets') log.debug("Subnet stanza {!r}".format(s_stanza)) az = self.availability_zone() vpc = self.vpc() vpc_obj = self.vpc_obj(create=False, quiet=True) delegate = self._subnet['delegate'] cidr_block = '10.0.{}.0/24'.format(delegate) if delegate not in s_stanza or s_stanza[delegate] == {}: # # create new subnet if create: s_stanza[delegate] = {} log.debug("About to create subnet {}".format(cidr_block)) if dry_run: log.info("Dry run: doing nothing") s_obj = None else: s_obj = vpc.create_subnet( vpc_obj.id, cidr_block, dry_run=dry_run, availability_zone=az ) log.info( "Created subnet {} ({})".format( s_obj.id, s_obj.cidr_block ) ) s_stanza[delegate]['cidr_block'] = s_obj.cidr_block s_stanza[delegate]['id'] = s_obj.id stanza('subnets', s_stanza) apply_tag(s_obj, tag='Name', val=stanza('nametag')) apply_tag(s_obj, tag='Delegate', val=delegate) else: log.info("Delegate {} subnet ID missing in yaml" .format(delegate)) s_obj = None return s_obj # # check id exists and cidr_block matches s_id = s_stanza[delegate]['id'] log.debug("Getting subnet id {}".format(s_id)) s_list = vpc.get_all_subnets(subnet_ids=[s_id]) assert len(s_list) == 1, "Subnet ID {} does not exist".format(s_id) s_obj = s_list[0] log.info("Found subnet {} ({})".format(s_obj.id, s_obj.cidr_block)) if ( 'cidr_block' not in s_stanza[delegate] or s_stanza[delegate]['cidr_block'] is None ): # pragma: no cover # # set cidr_block s_stanza[delegate]['cidr_block'] = s_obj.cidr_block stanza('subnets', s_stanza) else: # # validate cidr_block assert s_stanza[delegate]['cidr_block'] == s_obj.cidr_block, ( ("Delegate {} is supposed to have subnet {}, but that " "subnet exists with non-matching CIDR block {}") .format( delegate, s_stanza[delegate]['cidr_block'], s_obj.cidr_block )) self._subnet['s_obj'] = s_obj # if self.args.retag: # apply_tag(s_obj, tag='Name', val=stanza('nametag')) # apply_tag(s_obj, tag='Delegate', val=delegate) return s_obj
def vpc_obj(self, create=False, dry_run=False, quiet=False): """ fetch VPC object, create if necessary """ # # cached VPC object if self._vpc['vpc_obj'] is not None: return self._vpc['vpc_obj'] # # non-cached vpc_stanza = stanza('vpc') vpc_conn = self.vpc() if len(vpc_stanza) == 0: # pragma: no cover # # create VPC if create: if dry_run: log.info("Dry run: do nothing") vpc_obj = None else: log.info("VPC ID not specified in yaml: creating VPC") vpc_obj = vpc_conn.create_vpc('10.0.0.0/16') vpc_stanza['id'] = vpc_obj.id vpc_stanza['cidr_block'] = vpc_obj.cidr_block log.info("New VPC ID {} created with CIDR block {}".format( vpc_obj.id, vpc_obj.cidr_block)) apply_tag(vpc_obj, tag='Name', val=stanza('nametag')) self._vpc['vpc_obj'] = vpc_obj stanza('vpc', { 'cidr_block': vpc_obj.cidr_block, 'id': vpc_obj.id }) vpc_conn.modify_vpc_attribute( vpc_obj.id, enable_dns_support=True, ) vpc_conn.modify_vpc_attribute( vpc_obj.id, enable_dns_hostnames=True, ) else: log.info("VPC ID not specified in yaml: nothing to do") vpc_obj = None return vpc_obj # # existing VPC log.debug("VPD ID specified in yaml: fetching it") vpc_id = vpc_stanza['id'] if not quiet: log.info("VPC ID according to yaml is {}".format(vpc_id)) vpc_list = vpc_conn.get_all_vpcs(vpc_ids=vpc_id) assert len(vpc_list) == 1, ("VPC ID {} does not exist".format(vpc_id)) vpc_obj = vpc_list[0] cidr_block = vpc_obj.cidr_block assert cidr_block == '10.0.0.0/16', (( "VPC ID {} exists, but has wrong CIDR block {} " "(should be 10.0.0.0/16)").format(vpc_id, cidr_block)) if not quiet: log.info("VPC ID is {}, CIDR block is {}".format( vpc_stanza['id'], vpc_stanza['cidr_block'], )) self._vpc['vpc_obj'] = vpc_obj vpc_conn.modify_vpc_attribute( vpc_obj.id, enable_dns_support=True, ) vpc_conn.modify_vpc_attribute( vpc_obj.id, enable_dns_hostnames=True, ) return vpc_obj
def apply_tags(self, aws_obj, role=None): delegate = self._delegate["delegate"] apply_tag(aws_obj, tag="Name", val=stanza("nametag")) apply_tag(aws_obj, tag="Role", val=role) apply_tag(aws_obj, tag="Delegate", val=delegate)