Best Python code snippet using localstack_python
discovery_utils.py
Source:discovery_utils.py
...41def get_configuration_bucket(): 42 configuration = StackS3Configuration()43 cloud_formation_client = boto3.client('cloudformation', region_name=current_region)44 45 stack_definitions = try_with_backoff(lambda : cloud_formation_client.describe_stack_resources(PhysicalResourceId = lambda_name))46 print 'describe_stack_resources on PhysicalResourceId {} results {}'.format(lambda_name, stack_definitions)47 for stack_definition in stack_definitions['StackResources']:48 if stack_definition.get('LogicalResourceId',None) == 'Configuration':49 configuration.configuration_bucket = stack_definition['PhysicalResourceId']50 configuration.stack_name = stack_definition['StackName']51 break52 return configuration 53def _find_resource_by_physical_name(resources, physical_name):54 for resource in resources:55 if resource.get('PhysicalResourceId', None) == physical_name:56 return resource57 return None58def _find_resource_by_logical_name(resources, logical_name):59 for resource in resources:60 if resource.get('LogicalResourceId', None) == logical_name:61 return resource62 return None63# Stack ARN format: arn:aws:cloudformation:{region}:{account}:stack/{name}/{uuid}64def get_stack_name_from_stack_arn(arn):65 return arn.split('/')[1]66def get_region_from_stack_arn(arn):67 return arn.split(':')[3]68def get_account_id_from_stack_arn(arn):69 return arn.split(':')[4]70class StackInfo(object):71 def __init__(self, stack_arn, resources=None, client=None):72 self.stack_arn = stack_arn73 self.stack_name = get_stack_name_from_stack_arn(stack_arn)74 self.region = get_region_from_stack_arn(stack_arn)75 self.account_id = get_account_id_from_stack_arn(stack_arn)76 self._resources = resources77 self._client = client78 self._owning_stack_resources = None79 def get_client(self):80 if self._client is None:81 self._client = boto3.client('cloudformation', region_name=self.region)82 return self._client83 def get_resources(self):84 if self._resources is None:85 try:86 res = try_with_backoff(lambda : self.get_client().describe_stack_resources(StackName=self.stack_arn))87 print 'describe_stack_resources(StackName="{}") response: {}'.format(self.stack_arn, res)88 except Exception as e:89 print 'describe_stack_resources(StackName="{}") error: {}'.format(self.stack_arn, getattr(e, 'response', e))90 raise e91 self._resources = res['StackResources']92 return self._resources93 def get_resource(self, logical_name, expected_type=None, optional=False):94 resources = self.get_resources()95 resource = _find_resource_by_logical_name(resources, logical_name)96 if expected_type is not None:97 if resource is None:98 if optional:99 return None100 else:101 raise ValidationError('There is no {} resource in stack {}.'.format(logical_name, self.stack_arn))102 if resource['ResourceType'] != expected_type:103 raise ValidationError('The {} resource in stack {} has type {} but type {} was expected.'.format(104 logical_name, 105 self.stack_arn, 106 resource['ResourceType'], 107 expected_type))108 return resource109 def get_owning_stack_resources(self):110 if self._owning_stack_resources is None:111 client = self.get_client()112 try:113 res = try_with_backoff(lambda : client.describe_stack_resources(PhysicalResourceId=self.stack_arn))114 print 'describe_stack_resources(PhysicalResourceId="{}") response: {}.'.format(self.stack_arn, res)115 except Exception as e:116 print 'describe_stack_resources(PhysicalResourceId="{}") error: {}.'.format(self.stack_arn, getattr(e, 'response', e))117 raise e118 self._owning_stack_resources = res['StackResources']119 return self._owning_stack_resources120class FeatureInfo(StackInfo):121 def __init__(self, feature_stack_arn, feature_stack_resource=None, deployment_info=None, resources=None, client=None):122 123 super(FeatureInfo, self).__init__(feature_stack_arn, resources=resources, client=client)124 125 if feature_stack_resource is None:126 feature_stack_resource = _find_resource_by_physical_name(self.get_owning_stack_resources(), self.stack_arn)127 self.feature_name = feature_stack_resource['LogicalResourceId']128 if deployment_info is None:129 deployment_info = DeploymentInfo(feature_stack_resource['StackId'], resources=self._owning_stack_resources, client=self._client)130 self.deployment = deployment_info...
cloudstack.py
Source:cloudstack.py
...46 :param name: The name of the stack, whose VPC and subnet IDs will be revealed47 :return: The VPC and subnet IDs of the provided stack48 '''49 try:50 vpc_id = CLOUD_FORMATION.describe_stack_resources(51 StackName = name,52 LogicalResourceId = "VPC")["StackResources"][0]["PhysicalResourceId"]53 public_subnet1_id = CLOUD_FORMATION.describe_stack_resources(54 StackName = name,55 LogicalResourceId = "PublicSubnet01")["StackResources"][0]["PhysicalResourceId"]56 public_subnet2_id = CLOUD_FORMATION.describe_stack_resources(57 StackName = name,58 LogicalResourceId = "PublicSubnet02")["StackResources"][0]["PhysicalResourceId"]59 private_subnet1_id = CLOUD_FORMATION.describe_stack_resources(60 StackName = name,61 LogicalResourceId = "PrivateSubnet01")["StackResources"][0]["PhysicalResourceId"]62 private_subnet2_id = CLOUD_FORMATION.describe_stack_resources(63 StackName = name,64 LogicalResourceId = "PrivateSubnet02")["StackResources"][0]["PhysicalResourceId"]65 return (vpc_id, public_subnet1_id, public_subnet2_id, private_subnet1_id, private_subnet2_id)66 except ClientError as client_error:67 logging.error(client_error)68def get_control_plane_sg_id(name: str):69 '''70 Retrieves the security group ID -- specifically created for an EKS control plane --71 of the specified CloudFormation stack72 :param name: The name of the stack, whose control plane security group ID will be revealed73 :return: The control plane security group ID of the provided stack74 '''75 try:76 return CLOUD_FORMATION.describe_stack_resources(77 StackName = name,78 LogicalResourceId = "ControlPlaneSecurityGroup")["StackResources"][0]["PhysicalResourceId"]79 except ClientError as client_error:80 logging.error(client_error)81def main():82 '''83 Creates a CloudFormation stack, given the following command line arguments:84 cloudformation.py <stack_name> <stack_tag>85 :CLI arg stack_name: The name of the stack to be created86 :CLI arg stack_tag: The tag for the stack87 :return: None88 '''89 stack_name = sys.argv[1]90 stack_tag = sys.argv[2]...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!