def fetch_stack_output(self, value, param, key=None): try: [_, account_id, region, stack_name, export] = str(value).split(':') except ValueError: raise ValueError( "{0} is not a valid import string." "syntax should be import:account_id:region:stack_name:export_key" .format(str(value))) LOGGER.info( "Assuming the role %s", 'arn:aws:iam::{0}:role/{1}'.format( account_id, 'adf-cloudformation-deployment-role')) role = self.sts.assume_cross_account_role( 'arn:aws:iam::{0}:role/{1}'.format( account_id, 'adf-cloudformation-deployment-role'), 'importer') cloudformation = CloudFormation( region=region, deployment_account_region=os.environ["AWS_REGION"], role=role, stack_name=stack_name, account_id=account_id) LOGGER.info("Retrieving value of key %s from %s on %s in %s", export, stack_name, account_id, region) stack_output = cloudformation.get_stack_output(export) if not stack_output: raise Exception("No Key was found on {0} with the name {1}".format( stack_name, export)) LOGGER.info("Stack output value is %s", stack_output) if key: self.stage_parameters[key][param] = stack_output return self.stage_parameters[key] = stack_output
def fetch_stack_output(self, value, key, optional=False): # pylint: disable=too-many-statements try: [_, account_id, region, stack_name, output_key] = str(value).split(':') except ValueError: raise ValueError( "{0} is not a valid import string." "syntax should be import:account_id:region:stack_name:output_key" .format(str(value))) if Resolver._is_optional(output_key): LOGGER.info("Parameter %s is considered optional", output_key) optional = True output_key = output_key[:-1] if optional else output_key try: role = self.sts.assume_cross_account_role( 'arn:aws:iam::{0}:role/{1}'.format( account_id, 'adf-readonly-automation-role'), 'importer') cloudformation = CloudFormation( region=region, deployment_account_region=os.environ["AWS_REGION"], role=role, stack_name=stack_name, account_id=account_id) stack_output = self.cache.check( value) or cloudformation.get_stack_output(output_key) if stack_output: LOGGER.info("Stack output value is %s", stack_output) self.cache.add(value, stack_output) except ClientError: if not optional: raise stack_output = "" pass try: parent_key = list( Resolver.determine_parent_key(self.comparison_parameters, key))[0] if optional: self.stage_parameters[parent_key][key] = stack_output else: if not stack_output: raise Exception( "No Stack Output found on {account_id} in {region} " "with stack name {stack} and output key " "{output_key}".format( account_id=account_id, region=region, stack=stack_name, output_key=output_key, )) self.stage_parameters[parent_key][key] = stack_output except IndexError: if stack_output: if self.stage_parameters.get(key): self.stage_parameters[key] = stack_output else: raise Exception( "Could not determine the structure of the file in order to import from CloudFormation" ) return True
def fetch_stack_output(self, value, key, param=None, optional=False): #pylint: disable=R0912, R0915 try: [_, account_id, region, stack_name, export] = str(value).split(':') if export.endswith('?'): export = export[:-1] LOGGER.info("Import %s is considered optional", export) optional = True except ValueError: raise ValueError( "{0} is not a valid import string." "syntax should be import:account_id:region:stack_name:export_key" .format(str(value))) LOGGER.info( "Assuming the role %s", 'arn:aws:iam::{0}:role/{1}'.format( account_id, 'adf-cloudformation-deployment-role')) try: role = self.sts.assume_cross_account_role( 'arn:aws:iam::{0}:role/{1}'.format( account_id, 'adf-cloudformation-deployment-role'), 'importer') cloudformation = CloudFormation( region=region, deployment_account_region=os.environ["AWS_REGION"], role=role, stack_name=stack_name, account_id=account_id) LOGGER.info("Retrieving value of key %s from %s on %s in %s", export, stack_name, account_id, region) stack_output = cloudformation.get_stack_output(export) LOGGER.info("Stack output value is %s", stack_output) except ClientError: if not optional: raise stack_output = "" pass if optional: if param: self.stage_parameters[param][key] = stack_output else: self.stage_parameters[key] = stack_output return else: if not stack_output: raise Exception( "No Stack Output found on %s in %s with stack name %s and output key %s", account_id, region, stack_name, export) #pylint: disable=W0715 if param: self.stage_parameters[param][key] = stack_output else: self.stage_parameters[key] = stack_output return