Example #1
0
def action_delete(module):
    """
    Delete the workflow instance specified by workflow_key.
    If workflow_key is not supplied, delete the workflow instance specified by workflow_name.
    Return the message to indicate whether the workflow instance does not exist or is deleted.
    :param AnsibleModule module: the ansible module
    """
    workflow_key = ''
    delete_by_key = False
    delete_result = dict(changed=False, deleted=False, message='')
    # create session
    session = get_connect_session(module)
    # decide if delete by name or key
    if module.params['workflow_key'] is not None and module.params[
            'workflow_key'].strip() != '':
        workflow_key = module.params['workflow_key']
        delete_by_key = True
    # step1 - find workflow instance by name if needed
    if workflow_key == '':
        if module.params['workflow_name'] is None or module.params[
                'workflow_name'].strip() == '':
            module.fail_json(
                msg=
                'A valid argument of either workflow_name or workflow_key is required.'
            )
        response_list = call_workflow_api(module, session, 'list',
                                          workflow_key)
        if isinstance(response_list, dict):
            if 'workflows' in response_list and len(
                    response_list['workflows']) > 0:
                workflow_key = response_list['workflows'][0]['workflowKey']
            else:
                delete_result[
                    'message'] = 'Workflow instance named: ' + module.params[
                        'workflow_name'] + ' does not exist.'
                module.exit_json(**delete_result)
        else:
            module.fail_json(msg='Failed to find workflow instance named: ' +
                             module.params['workflow_name'] + ' ---- ' +
                             response_list)
    # step2 - delete workflow instance
    response_delete = call_workflow_api(module, session, 'delete',
                                        workflow_key)
    if isinstance(response_delete, dict):
        delete_result['changed'] = True
        delete_result['deleted'] = True
        if delete_by_key is True:
            delete_result[
                'message'] = 'Workflow instance with key: ' + workflow_key + ' is deleted.'
        else:
            delete_result[
                'message'] = 'Workflow instance named: ' + module.params[
                    'workflow_name'] + ' is deleted.'
        module.exit_json(**delete_result)
    else:
        if delete_by_key is True:
            module.fail_json(
                msg='Failed to delete workflow instance with key: ' +
                workflow_key + ' ---- ' + response_delete)
        else:
            module.fail_json(msg='Failed to delete workflow instance named: ' +
                             module.params['workflow_name'] + ' ---- ' +
                             response_delete)
Example #2
0
def action_start(module):
    """
    Start the workflow instance specified by workflow_key.
    If workflow_key is not supplied, create the workflow instance specified by workflow_name if not exist and then start it.
    Return the message to indicate the workflow instance is started.
    Return the workflow_key of the started workflow instance.
    :param AnsibleModule module: the ansible module
    """
    workflow_key = ''
    start_by_key = False
    start_result = dict(changed=False, workflow_key='', message='')
    # create session
    session = get_connect_session(module)
    # decide if start by name or key
    if module.params['workflow_key'] is not None and module.params[
            'workflow_key'].strip() != '':
        workflow_key = module.params['workflow_key']
        start_by_key = True
    # step1 - find workflow instance by name
    if workflow_key == '':
        if module.params['workflow_name'] is None or module.params[
                'workflow_name'].strip() == '':
            module.fail_json(
                msg=
                'A valid argument of either workflow_name or workflow_key is required.'
            )
        response_list = call_workflow_api(module, session, 'list',
                                          workflow_key)
        if isinstance(response_list, dict):
            if 'workflows' in response_list and len(
                    response_list['workflows']) > 0:
                workflow_key = response_list['workflows'][0]['workflowKey']
        else:
            module.fail_json(msg='Failed to find workflow instance named: ' +
                             module.params['workflow_name'] + ' ---- ' +
                             response_list)
    # step2 - create workflow instance if needed
    if workflow_key == '':
        response_create = call_workflow_api(module, session, 'create',
                                            workflow_key)
        if isinstance(response_create, dict):
            if 'workflowKey' in response_create and response_create[
                    'workflowKey'] != '':
                workflow_key = response_create['workflowKey']
            else:
                module.fail_json(
                    msg='Failed to create workflow instance named: ' +
                    module.params['workflow_name'] + '.')
        else:
            module.fail_json(msg='Failed to create workflow instance named: ' +
                             module.params['workflow_name'] + ' ---- ' +
                             response_create)
    # step3 - start workflow instance
    response_start = call_workflow_api(module, session, 'start', workflow_key)
    if isinstance(response_start, dict):
        start_result['changed'] = True
        start_result['workflow_key'] = workflow_key
        if start_by_key is True:
            start_result[
                'message'] = 'Workflow instance with key: ' + workflow_key + ' is started, you can use state=check to check its final status.'
        else:
            start_result['message'] = 'Workflow instance named: ' + module.params['workflow_name'] \
                + ' is started, you can use state=check to check its final status.'
        module.exit_json(**start_result)
    else:
        # handle start issue caused by non-automated step
        next_step_message = ''
        if response_start.find('IZUWF5007E') > 0:
            next_step_message = ' You can manually complete this step in z/OSMF Workflows task,' \
                + ' and start this workflow instance again with next step name specified in argument: workflow_step_name.'
        if start_by_key is True:
            module.fail_json(
                msg='Failed to start workflow instance with key: ' +
                workflow_key + ' ---- ' + response_start + next_step_message)
        else:
            module.fail_json(msg='Failed to start workflow instance named: ' +
                             module.params['workflow_name'] + ' ---- ' +
                             response_start + next_step_message)
Example #3
0
def action_check(module):
    """
    Check status of the workflow instance specified by workflow_key.
    If workflow_key is not supplied, check status of the workflow instance specified by workflow_name.
    Return the message to indicate whether the workflow instance is completed, is not completed, or is still in progress.
    Return the waiting flag to indicate whether it needs to wait and check again since the workflow instance is still in progress.
    :param AnsibleModule module: the ansible module
    """
    workflow_key = ''
    check_by_key = False
    next_step_name = ''
    check_result = dict(changed=False,
                        waiting=True,
                        completed=False,
                        message='')
    # create session
    session = get_connect_session(module)
    # decide if check by name or key
    if module.params['workflow_key'] is not None and module.params[
            'workflow_key'].strip() != '':
        workflow_key = module.params['workflow_key']
        check_by_key = True
    # step1 - find workflow instance by name if needed
    if workflow_key == '':
        if module.params['workflow_name'] is None or module.params[
                'workflow_name'].strip() == '':
            module.fail_json(
                msg=
                'A valid argument of either workflow_name or workflow_key is required.'
            )
        response_list = call_workflow_api(module, session, 'list',
                                          workflow_key)
        if isinstance(response_list, dict):
            if 'workflows' in response_list and len(
                    response_list['workflows']) > 0:
                workflow_key = response_list['workflows'][0]['workflowKey']
            else:
                module.fail_json(msg='No workflow instance named: ' +
                                 module.params['workflow_name'] + ' is found.')
        else:
            module.fail_json(msg='Failed to find workflow instance named: ' +
                             module.params['workflow_name'] + ' ---- ' +
                             response_list)
    # step2 - get workflow properties
    response_retrieveP = call_workflow_api(module, session,
                                           'retrieveProperties', workflow_key)
    if isinstance(response_retrieveP, dict):
        if 'statusName' in response_retrieveP:
            status = response_retrieveP['statusName']
            if status == 'automation-in-progress':
                if check_by_key is True:
                    check_result[
                        'message'] = 'Workflow instance with key: ' + workflow_key + ' is still in progress.'
                else:
                    check_result[
                        'message'] = 'Workflow instance named: ' + module.params[
                            'workflow_name'] + ' is still in progress.'
                module.exit_json(**check_result)
            elif status == 'complete':
                check_result['waiting'] = False
                check_result['completed'] = True
                if check_by_key is True:
                    check_result[
                        'message'] = 'Workflow instance with key: ' + workflow_key + ' is completed.'
                else:
                    check_result[
                        'message'] = 'Workflow instance named: ' + module.params[
                            'workflow_name'] + ' is completed.'
                module.exit_json(**check_result)
            else:
                step_status = response_retrieveP['automationStatus']
                check_result['waiting'] = False
                if step_status is None:
                    if check_by_key is True:
                        check_result[
                            'message'] = 'Workflow instance with key: ' + workflow_key + ' is not completed: No step is started.'
                    else:
                        check_result[
                            'message'] = 'Workflow instance named: ' + module.params[
                                'workflow_name'] + ' is not completed: No step is started.'
                else:
                    current_step_message = ''
                    next_step_message = ''
                    if step_status['currentStepNumber'] is not None:
                        current_step_message = 'In step ' + step_status[
                            'currentStepNumber'] + ' ' + step_status[
                                'currentStepTitle'] + ': '
                    # handle specific start issues
                    if step_status['messageID'] == 'IZUWF0145E' and step_status[
                            'currentStepNumber'] is not None:
                        next_step_name = get_next_step_name(
                            module, step_status['currentStepNumber'],
                            response_retrieveP)
                        if next_step_name != '':
                            next_step_message = ' You can manually complete this step in z/OSMF Workflows task,' \
                                + ' and start this workflow instance again with next step name: ' \
                                + next_step_name + ' specified in argument: workflow_step_name.'
                    if step_status['messageID'] == 'IZUWF0162I':
                        next_step_message = ' While one or more steps may be skipped.'
                    if check_by_key is True:
                        check_result['message'] = 'Workflow instance with key: ' + workflow_key + ' is not completed: ' \
                            + current_step_message + step_status['messageText'] + next_step_message
                    else:
                        check_result['message'] = 'Workflow instance named: ' + module.params['workflow_name'] + ' is not completed: ' \
                            + current_step_message + step_status['messageText'] + next_step_message
                module.exit_json(**check_result)
        else:
            if check_by_key is True:
                module.fail_json(
                    msg=
                    'Failed to get properties of workflow instance with key: '
                    + workflow_key + '.')
            else:
                module.fail_json(
                    msg='Failed to get properties of workflow instance named: '
                    + module.params['workflow_name'] + '.')
    else:
        if check_by_key is True:
            module.fail_json(
                msg='Failed to get properties of workflow instance with key: '
                + workflow_key + ' ---- ' + response_retrieveP)
        else:
            module.fail_json(
                msg='Failed to get properties of workflow instance named: ' +
                module.params['workflow_name'] + ' ---- ' + response_retrieveP)
Example #4
0
def action_compare(module, argument_spec_mapping):
    """
    Indicate whether the workflow instance specified by workflow_name already exists.
    If the workflow instance already exists, indicate whether they have same definition files, variables and properties.
    Return the message to indicate whether the workflow instance does not exist, or exists with same or different definition file, variables and properties.
    Return the workflow_key of the existing workflow instance.
    Return the same_workflow_instance flag to indicate whether the existing workflow instance has same or different definition file, variables and properties.
    Return the completed flag to indicate whether the existing workflow instance with same definition file, variables and properties has been completed.
    :param AnsibleModule module: the ansible module
    :param dict[str, dict] argument_spec_mapping: the mapping between arguments of ansible module and params of all workflow APIs
    """
    workflow_key = ''
    compare_result = dict(changed=False,
                          workflow_key='',
                          same_workflow_instance=False,
                          completed=False,
                          message='')
    # create session
    session = get_connect_session(module)
    # step1 - find workflow instance by name
    response_list = call_workflow_api(module, session, 'list', workflow_key)
    if isinstance(response_list, dict):
        if 'workflows' in response_list and len(
                response_list['workflows']) > 0:
            workflow_key = response_list['workflows'][0]['workflowKey']
        else:
            compare_result[
                'message'] = 'No workflow instance named: ' + module.params[
                    'workflow_name'] + ' is found.'
            module.exit_json(**compare_result)
    else:
        module.fail_json(msg='Failed to find workflow instance named: ' +
                         module.params['workflow_name'] + ' ---- ' +
                         response_list)
    # step2 - compare the properties and definition files
    response_retrieveP = call_workflow_api(module, session,
                                           'retrieveProperties', workflow_key)
    if isinstance(response_retrieveP, str):
        module.fail_json(
            msg='Failed to get properties of workflow instance named: ' +
            module.params['workflow_name'] + ' ---- ' + response_retrieveP)
    response_retrieveD = dict()
    if module.params['workflow_file'] is not None and module.params[
            'workflow_file'].strip() != '':
        response_retrieveD = call_workflow_api(module, session,
                                               'retrieveDefinition',
                                               workflow_key)
        if isinstance(response_retrieveD, str):
            module.fail_json(
                msg='Failed to get definition file of workflow instance named: '
                + module.params['workflow_name'] + ' ---- ' +
                response_retrieveD)
    (sameD, sameV, sameP, diff_name,
     diff_value) = is_same_workflow_instance(module, argument_spec_mapping,
                                             response_retrieveP,
                                             response_retrieveD)
    if sameD is False:
        compare_result[
            'message'] = 'Workflow instance named: ' + module.params[
                'workflow_name'] + ' with different definition file is found.'
    elif sameV is False:
        compare_result['message'] = 'Workflow instance named: ' + module.params['workflow_name'] + ' with different variable: ' \
            + diff_name + ' = ' + str(diff_value) + ' is found.'
    elif sameP is False:
        compare_result['message'] = 'Workflow instance named: ' + module.params['workflow_name'] + ' with different property: ' \
            + diff_name + ' = ' + str(diff_value) + ' is found.'
    elif sameD is None or sameV is None:
        compare_result['same_workflow_instance'] = True
        compare_result['message'] = 'Workflow instance named: ' + module.params['workflow_name'] \
            + ' is found. While it could not be compared since the argument: workflow_file is required,' \
            + ' and please supply variables by the argument: workflow_vars rather than the argument: workflow_vars_file.'
    else:
        compare_result['same_workflow_instance'] = True
        compare_result['message'] = 'Workflow instance named: ' + module.params['workflow_name'] \
            + ' with same definition file, variables and properties is found.'
    if compare_result[
            'same_workflow_instance'] is not False and response_retrieveP[
                'statusName'] == 'complete':
        compare_result['completed'] = True
    compare_result['workflow_key'] = workflow_key
    module.exit_json(**compare_result)