"""Connects to AWS SFN.""" import json import uuid import boto3 from botocore.config import Config config = Config( retries={ 'max_attempts': 6, 'mode': 'standard' } ) def get_full_execution_count_by_arn(sfn_arn, status='RUNNING'): """Get the number of executions with a certain status by sfn name. Args: sfn_arn (str): The AWS ARN of the target SFN to count executions status (str): A status by which to filter results Returns: int: The count executions for the given SFN with given status """ sfn_client = boto3.client('stepfunctions', config=config) current_executions = sfn_client.list_executions( stateMachineArn=sfn_arn, statusFilter=status, maxResults=50 ) next_token = current_executions.get('nextToken') current_sfn_count = len(current_executions['executions']) while next_token: current_executions = sfn_client.list_executions( stateMachineArn=sfn_arn, statusFilter=status, maxResults=50, nextToken=next_token ) current_sfn_count = current_sfn_count + len( current_executions['executions']) next_token = current_executions.get('nextToken') return current_sfn_count def get_full_execution_name_list_by_arn(sfn_arn, status='RUNNING'): """Get the names of executions with a certain status by sfn name. Args: sfn_arn (str): The AWS ARN of the target SFN you wish to exec status (str): A status by which to filter results Returns: list: The list of executions for the given SFN with given status """ sfn_client = boto3.client('stepfunctions', config=config) current_executions = sfn_client.list_executions( stateMachineArn=sfn_arn, statusFilter=status, maxResults=50 ) next_token = current_executions.get('nextToken') name_list = [x['name'] for x in current_executions['executions']] while next_token: current_executions = sfn_client.list_executions( stateMachineArn=sfn_arn, statusFilter=status, maxResults=50, nextToken=next_token ) name_list = name_list + [x['name'] for x in current_executions['executions']] next_token = current_executions.get('nextToken') return name_list def start_execution_by_arn(sfn_arn, bucket, key, ddex_provider, ingest_id, artwork_ingestion_only): """Start a state machine execution to ingest DDEX. Args: sfn_arn (str): The AWS ARN of the target SFN you wish to exec bucket (str): A bucket to pass to the execution via payload key (str): A key to pass to the execution via payload ddex_provider (str): DDEX provider name ingest_id (int): DDEX catalog ingest ID artwork_ingestion_only (bool): Returns: dict: Response from AWS """ sfn_client = boto3.client('stepfunctions', config=config) input_data = { 'detail': { 'requestParameters': { 'bucketName': bucket, 'key': key, 'ddex_provider': ddex_provider, 'ingest_id': ingest_id, 'artwork_ingestion_only': artwork_ingestion_only } } } package_name = key.split('/')[-2] response = sfn_client.start_execution( stateMachineArn=sfn_arn, name=f'{package_name}_{str(uuid.uuid4())}', input=json.dumps(input_data) ) return response