def create_test_subnets(self, vpc, count_per_zone=1): """ This method is intended to provided the convenience of returning a number of subnets per zone equal to the provided 'count_per_zone'. The intention is this method will take care of first attempting to re-use existing subnets, and creating new ones if needed to meet the count requested. :param vpc: boto VPC object :param count_per_zone: int, number of subnets needed per zone :return: list of subnets """ test_subnets = [] for x in xrange(0, count_per_zone): for zone in self.zones: # Use a /24 of the vpc's larger /16 subnets = self.user.ec2.get_all_subnets(filters={'vpc-id': vpc.id}) subnet_cidr = None attempts = 0 while subnet_cidr is None and attempts < 253: attempts += 1 subnet_cidr = re.sub("(\d+.\d+).\d+.\d+.\d\d", r"\1.{0}.0/24".format(attempts), vpc.cidr_block) if not subnet_cidr: raise ValueError('Failed to parse subnet_cidr from vpc cidr block:{0}' .format(vpc.cidr_block)) for sub in subnets: if sub.cidr_block == subnet_cidr or \ is_address_in_network(subnet_cidr.strip('/24'), sub.cidr_block): self.log.info('Subnet: {0} conflicts with existing:{1}' .format(subnet_cidr, sub.cidr_block)) subnet_cidr = None break try: subnet = self.user.ec2.connection.create_subnet(vpc_id=vpc.id, cidr_block=subnet_cidr, availability_zone=zone) except: try: self.log.error('Existing subnets during create request:') self.user.ec2.show_subnets(subnets, printmethod=self.log.error) except: pass self.log.error('Failed to create subnet for vpc:{0}, cidr:{1} zone:{2}' .format(vpc.id, subnet_cidr, zone)) raise self.user.ec2.create_tags(subnet.id, {self.test_tag_name: x}) test_subnets.append(subnet) self.user.log.info('Created the following SUBNET: {0}'.format(subnet.id)) self.user.ec2.show_subnet(subnet) return test_subnets
def create_test_subnets(self, vpc, count_per_zone=1): test_subnets = [] for x in xrange(0, count_per_zone): for zone in self.zones: # Use a /24 of the vpc's larger /16 subnets = self.user.ec2.get_all_subnets(filters={"vpc-id": vpc.id}) subnet_cidr = None attempts = 0 while subnet_cidr is None and attempts < 253: attempts += 1 subnet_cidr = re.sub("(\d+.\d+).\d+.\d+.\d\d", r"\1.{0}.0/24".format(attempts), vpc.cidr_block) if not subnet_cidr: raise ValueError("Failed to parse subnet_cidr from vpc cidr block:{0}".format(vpc.cidr_block)) for sub in subnets: if sub.cidr_block == subnet_cidr or is_address_in_network( subnet_cidr.strip("/24"), sub.cidr_block ): self.log.info("Subnet: {0} conflicts with existing:{1}".format(subnet_cidr, sub.cidr_block)) subnet_cidr = None break try: subnet = self.user.ec2.create_subnet(vpc_id=vpc.id, cidr_block=subnet_cidr, availability_zone=zone) except: try: self.log.error("Existing subnets during create request:") self.user.ec2.show_subnets(subnets, printmethod=self.log.error) except: pass self.log.error( "Failed to create subnet for vpc:{0}, cidr:{1} zone:{2}".format(vpc.id, subnet_cidr, zone) ) raise self.user.ec2.create_tags(subnet.id, {self.test_tag_name: x}) test_subnets.append(subnet) self.user.log.info("Created the following SUBNET: {0}".format(subnet.id)) self.user.ec2.show_subnet(subnet) return test_subnets