""" Simple Workflow Utils ===================== Utils for Amazon Simple Workflow operations. """ import json import boto3 from ows_accounting import config from ows_accounting.constants import header from ows_accounting.constants import utils as constants def _get_client(): """Get aws swf client Returns: Boto3 SWF client """ return boto3.client('swf') def generate_workflow_id(prefix, **params): """Generate workflow id from params Args: prefix (str): prefix of workflow id. params (dict): user params for the workflow. Returns: str: workflow_id """ template = ( '{prefix}-{user_id}-{user_type}-{period_ids}-' '{transaction_types}-{locale}-{file_format}-' '{report_version}-{client_email}') workflow_id = template.format( prefix=prefix, **params ) return workflow_id def execute_workflow(workflow_id, correlation_id, **params): """Execute simple workflow. Example call: swf.execute_workflow( 'custom_export_processing-accounting-18805-label-205-all-es_ES-txt', '868ae-83aa-8273-2874-1982', user_id='18805', user_type='label', period_ids='205', transaction_types='DT,DA', locale='es_ES', file_format='txt') Args: workflow_id (str): workflow id which uniquely identify each workflow. correlation_id (str): correlation_id in the header. params (dict): report specific params. """ params[header.CORRELATION_ID] = '{}.1'.format(correlation_id) exe_start_to_close = constants.SWF_EXECUTION_START_TO_CLOSE_TIMEOUT task_start_to_close = constants.SWF_TASK_START_TO_CLOSE_TIMEOUT client = _get_client() client.start_workflow_execution( domain=config.SWF_DOMAIN, workflowId=workflow_id, workflowType={ 'name': config.CUSTOM_EXPORT_WORKFLOW_TYPE, 'version': config.CUSTOM_EXPORT_WORKFLOW_TYPE_VERSION }, taskList={ 'name': config.CUSTOM_EXPORT_TASKLIST }, input=json.dumps(params, sort_keys=True), executionStartToCloseTimeout=str(exe_start_to_close), taskStartToCloseTimeout=str(task_start_to_close), childPolicy=constants.SWF_CHILD_POLICY)