def delete_stack(self, name, region): """Destroy a created cfn stack.""" try: set_credentials(region, self.__credentials) id = self.__get_stack_internal_id(name, region) if id in self.__created_stacks: logging.info("Destroying stack {0} in region {1}".format( name, region)) try: stack = self.__created_stacks[id] cfn_client = boto3.client("cloudformation", region_name=stack.region) cfn_client.delete_stack(StackName=stack.name) final_status = self.__wait_for_stack_deletion( stack.cfn_stack_id, cfn_client) self.__assert_stack_status(final_status, "DELETE_COMPLETE") except Exception as e: logging.error( "Deletion of stack {0} in region {1} failed with exception: {2}" .format(name, region, e)) raise del self.__created_stacks[id] logging.info( "Stack {0} deleted successfully in region {1}".format( name, region)) else: logging.warning( "Couldn't find stack with name {0} in region {1}. Skipping deletion." .format(name, region)) finally: unset_credentials()
def get_az_id_to_az_name_map(region, credential): """Return a dict mapping AZ IDs (e.g, 'use1-az2') to AZ names (e.g., 'us-east-1c').""" # credentials are managed manually rather than via setup_sts_credentials because this function # is called by a session-scoped fixture, which cannot make use of a class-scoped fixture. set_credentials(region, credential) try: ec2_client = boto3.client("ec2", region_name=region) return { entry.get("ZoneId"): entry.get("ZoneName") for entry in ec2_client.describe_availability_zones().get("AvailabilityZones") } finally: unset_credentials()
def _create_vpc_stack(request, template, region, cfn_stacks_factory): try: set_credentials(region, request.config.getoption("credential")) if request.config.getoption("vpc_stack"): logging.info("Using stack {0} in region {1}".format(request.config.getoption("vpc_stack"), region)) stack = CfnStack(name=request.config.getoption("vpc_stack"), region=region, template=template.to_json()) else: stack = CfnStack( name=generate_stack_name("integ-tests-vpc", request.config.getoption("stackname_suffix")), region=region, template=template.to_json(), ) cfn_stacks_factory.create_stack(stack) finally: unset_credentials() return stack
def create_stack(self, stack): """ Create a cfn stack with a given template. :param stack: stack to create. """ name = stack.name region = stack.region try: set_credentials(region, self.__credentials) id = self.__get_stack_internal_id(name, region) if id in self.__created_stacks: raise ValueError( "Stack {0} already exists in region {1}".format( name, region)) logging.info("Creating stack {0} in region {1}".format( name, region)) self.__created_stacks[id] = stack try: cfn_client = boto3.client("cloudformation", region_name=region) result = cfn_client.create_stack(StackName=name, TemplateBody=stack.template, Parameters=stack.parameters) stack.cfn_stack_id = result["StackId"] final_status = self.__wait_for_stack_creation( stack.cfn_stack_id, cfn_client) self.__assert_stack_status(final_status, "CREATE_COMPLETE") except Exception as e: logging.error( "Creation of stack {0} in region {1} failed with exception: {2}" .format(name, region, e)) raise logging.info("Stack {0} created successfully in region {1}".format( name, region)) finally: unset_credentials()
def get_availability_zones(region, credential): """ Return a list of availability zones for the given region. Note that this function is called by the vpc_stacks fixture. Because vcp_stacks is session-scoped, it cannot utilize setup_sts_credentials, which is required in opt-in regions in order to call describe_availability_zones. """ set_credentials(region, credential) az_list = [] try: client = boto3.client("ec2", region_name=region) response_az = client.describe_availability_zones( Filters=[ {"Name": "region-name", "Values": [str(region)]}, {"Name": "zone-type", "Values": ["availability-zone"]}, ] ) for az in response_az.get("AvailabilityZones"): az_list.append(az.get("ZoneName")) finally: unset_credentials() return az_list
def setup_sts_credentials(region, request): """Setup environment for the integ tests""" set_credentials(region, request.config.getoption("credential")) yield unset_credentials()