Exemple #1
0
def main():
    state_map = ['present', 'absent', 'read', 'list']
    module_args = dict(
        artifactory_url=dict(type='str', required=True),
        name=dict(type='str', default=''),
        group_config=dict(type='str', default=None),
        username=dict(type='str', default=None),
        auth_password=dict(type='str', no_log=True, default=None),
        auth_token=dict(type='str', no_log=True, default=None),
        validate_certs=dict(type='bool', default=False),
        client_cert=dict(type='path', default=None),
        client_key=dict(type='path', default=None),
        force_basic_auth=dict(type='bool', default=False),
        state=dict(type='str', default='read', choices=state_map),
        group_config_dict=dict(type='dict', default=dict()),
    )

    result = dict(
        changed=False,
        original_message='',
        message='',
        config=dict()
    )

    module = AnsibleModule(
        argument_spec=module_args,
        required_together=[['username', 'auth_password']],
        required_one_of=[['auth_password', 'auth_token']],
        mutually_exclusive=[['auth_password', 'auth_token']],
        required_if=[['state', 'present', ['artifactory_url', 'name']],
                     ['state', 'absent', ['artifactory_url', 'name']],
                     ['state', 'read', ['artifactory_url', 'name']]],
        supports_check_mode=True,
    )

    artifactory_url = module.params['artifactory_url']
    name = module.params['name']
    group_config = module.params['group_config']
    username = module.params['username']
    auth_password = module.params['auth_password']
    auth_token = module.params['auth_token']
    validate_certs = module.params['validate_certs']
    client_cert = module.params['client_cert']
    client_key = module.params['client_key']
    force_basic_auth = module.params['force_basic_auth']
    state = module.params['state']
    group_config_dict = module.params['group_config_dict']

    if group_config:
        # temporarily convert to dict for validation
        group_config = ast.literal_eval(group_config)

    fail_messages = []

    fails = art_base.validate_config_params(group_config, group_config_dict,
                                            'group_config',
                                            'group_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('name', module, group_config,
                                               group_config_dict,
                                               'group_config',
                                               'group_config_dict')
    fail_messages.extend(fails)

    # Populate failure messages
    failure_message = "".join(fail_messages)

    # Conflicting config values should not be resolved
    if failure_message:
        module.fail_json(msg=failure_message, **result)

    sec_dict = dict()
    if module.params['name']:
        sec_dict['name'] = module.params['name']
    if group_config:
        sec_dict.update(group_config)
    if group_config_dict:
        sec_dict.update(group_config_dict)
    group_config = str(sec_dict)

    result['original_message'] = ("Perform state '%s' against target '%s' "
                                  "within artifactory '%s'"
                                  % (state, name, artifactory_url))

    art_grp = art_base.ArtifactoryBase(
        artifactory_url=artifactory_url,
        name=name,
        art_config=group_config,
        username=username,
        password=auth_password,
        auth_token=auth_token,
        validate_certs=validate_certs,
        client_cert=client_cert,
        client_key=client_key,
        force_basic_auth=force_basic_auth,
        config_map=URI_CONFIG_MAP)
    art_base.run_module(module, art_grp, "groups", result,
                        fail_messages, group_config)
def run_module():
    state_map = ['present', 'absent', 'read', 'list']
    rclass_state_map = VALID_RCLASSES
    packageType_state_map = VALID_PACKAGETYPES
    module_args = dict(
        artifactory_url=dict(type='str', required=True),
        name=dict(type='str', required=True),
        repo_position=dict(type='int', default=None),
        repo_config=dict(type='str', default=None),
        username=dict(type='str', default=None),
        password=dict(type='str', no_log=True, default=None),
        auth_token=dict(type='str', no_log=True, default=None),
        validate_certs=dict(type='bool', default=False),
        client_cert=dict(type='path', default=None),
        client_key=dict(type='path', default=None),
        force_basic_auth=dict(type='bool', default=False),
        state=dict(type='str', default='read', choices=state_map),
        rclass=dict(type='str', default=None, choices=rclass_state_map),
        packageType=dict(type='str', default=None,
                         choices=packageType_state_map),
        url=dict(type='str', default=None),
        repoLayoutRef=dict(type='str', default=None),
        repo_config_dict=dict(type='dict', default=dict()),
    )

    result = dict(
        changed=False,
        original_message='',
        message='',
        config=dict()
    )

    module = AnsibleModule(
        argument_spec=module_args,
        required_together=[['username', 'password']],
        required_one_of=[['password', 'auth_token']],
        mutually_exclusive=[['password', 'auth_token']],
        required_if=[['state', 'present', ['artifactory_url', 'name']],
                     ['state', 'absent', ['artifactory_url', 'name']],
                     ['state', 'list', ['artifactory_url', 'name']],
                     ['state', 'read', ['artifactory_url', 'name']]],
        supports_check_mode=True,
    )

    artifactory_url = module.params['artifactory_url']
    repository = module.params['name']
    repo_position = module.params['repo_position']
    repo_config = module.params['repo_config']
    username = module.params['username']
    password = module.params['password']
    auth_token = module.params['auth_token']
    validate_certs = module.params['validate_certs']
    client_cert = module.params['client_cert']
    client_key = module.params['client_key']
    force_basic_auth = module.params['force_basic_auth']
    state = module.params['state']
    repo_config_dict = module.params['repo_config_dict']

    if repo_config:
        # temporarily convert to dict for validation
        repo_config = ast.literal_eval(repo_config)

    fail_messages = []

    fails = art_base.validate_config_params(repo_config, repo_config_dict,
                                            'repo_config', 'repo_config_dict')
    fail_messages.extend(fails)

    fails = art_base.validate_top_level_params('rclass', module, repo_config,
                                               repo_config_dict, 'repo_config',
                                               'repo_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('packageType', module,
                                               repo_config, repo_config_dict,
                                               'repo_config',
                                               'repo_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('url', module, repo_config,
                                               repo_config_dict, 'repo_config',
                                               'repo_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('repoLayoutRef',
                                               module, repo_config,
                                               repo_config_dict, 'repo_config',
                                               'repo_config_dict')
    fail_messages.extend(fails)

    # Populate failure messages
    failure_message = "".join(fail_messages)

    # Conflicting config values should not be resolved
    if failure_message:
        module.fail_json(msg=failure_message, **result)

    repo_dict = dict()
    if module.params['name']:
        repo_dict['key'] = module.params['name']
    if module.params['rclass']:
        repo_dict['rclass'] = module.params['rclass']
    if module.params['packageType']:
        repo_dict['packageType'] = module.params['packageType']
    if module.params['url']:
        repo_dict['url'] = module.params['url']
    if module.params['repoLayoutRef']:
        repo_dict['repoLayoutRef'] = module.params['repoLayoutRef']
    if repo_config:
        repo_dict.update(repo_config)
    if repo_config_dict:
        repo_dict.update(repo_config_dict)
    repo_config = json.dumps(repo_dict)

    result['original_message'] = ("Perform state '%s' against repo '%s' "
                                  "within artifactory '%s'"
                                  % (state, repository, artifactory_url))

    artifactory_repo = ArtifactoryRepoManagement(
        artifactory_url=artifactory_url,
        repo=repository,
        repo_position=repo_position,
        repo_config=repo_config,
        username=username,
        password=password,
        auth_token=auth_token,
        validate_certs=validate_certs,
        client_cert=client_cert,
        client_key=client_key,
        force_basic_auth=force_basic_auth,
        config_map=URI_CONFIG_MAP)

    repository_exists = False
    try:
        artifactory_repo.get_repository_config()
        repository_exists = True
    except urllib_error.HTTPError as http_e:
        if http_e.getcode() == 400:
            # Instead of throwing a 404, a 400 is thrown if a repo doesn't
            # exist. Have to fall through and assume the repo doesn't exist
            # and that another error did not occur. If there is another problem
            # it will have to be caught by try/catch blocks further below.
            pass
        else:
            message = ("HTTP response code was '%s'. Response message was"
                       " '%s'. " % (http_e.getcode(), http_e.read()))
            fail_messages.append(message)
    except urllib_error.URLError as url_e:
        message = ("A generic URLError was thrown. URLError: %s" % str(url_e))
        fail_messages.append(message)

    try:
        # Now that configs are lined up, verify required values in configs
        if state == 'present':
            artifactory_repo.validate_config_values(artifactory_url,
                                                    repo_dict)
            if not repository_exists:
                artifactory_repo.validate_config_required_keys(artifactory_url,
                                                               repo_dict)
    except art_base.ConfigValueTypeMismatch as cvtm:
        fail_messages.append(cvtm.message + ". ")
    except art_base.InvalidConfigurationData as icd:
        fail_messages.append(icd.message + ". ")
    except art_base.InvalidArtifactoryURL as iau:
        fail_messages.append(iau.message + ". ")

    # Populate failure messages
    failure_message = "".join(fail_messages)

    if failure_message:
        module.fail_json(msg=failure_message, **result)

    if module.check_mode:
        result['message'] = 'check_mode success'
        module.exit_json(**result)

    repo_not_exists_msg = ("Repository '%s' does not exist." % repository)
    resp_is_invalid_failure = ("An unknown error occurred while attempting to "
                               "'%s' repo '%s'. Response should "
                               "not be None.")
    try:
        if state == 'list':
            result['message'] = ("List of all repos against "
                                 "artifactory_url: %s" % artifactory_url)
            resp = artifactory_repo.get_repositories()
            result['config'] = json.loads(resp.read())
        elif state == 'read':
            if not repository_exists:
                result['message'] = repo_not_exists_msg
            else:
                resp = artifactory_repo.get_repository_config()
                if resp:
                    result['message'] = ("Successfully read config "
                                         "on repo '%s'." % repository)
                    result['config'] = json.loads(resp.read())
                    result['changed'] = True
                else:
                    failure_message = (resp_is_invalid_failure
                                       % (state, repository))
        elif state == 'present':
            # If the repo doesn't exist, create it.
            # If the repo does exist, perform an update on it ONLY if
            # configuration supplied has values that don't match the remote
            # config.
            if not repository_exists:
                result['message'] = ('Attempting to create repo: %s'
                                     % repository)
                resp = artifactory_repo.create_repository()
                if resp:
                    result['message'] = resp.read()
                    result['changed'] = True
                else:
                    failure_message = (resp_is_invalid_failure
                                       % (state, repository))
            else:
                result['message'] = ('Attempting to update repo: %s'
                                     % repository)
                current_config = artifactory_repo.get_repository_config()
                current_config = json.loads(current_config.read())
                desired_config = ast.literal_eval(repo_config)
                # Compare desired config with current config against repo.
                # If config values are identical, don't update.
                resp = None
                for key in current_config:
                    if key in desired_config:
                        if desired_config[key] != current_config[key]:
                            resp = artifactory_repo.update_repository_config()
                # To guarantee idempotence. If underlying libraries don't
                # throw an exception, it could incorrectly report a success
                # when there was actually a failure.
                if resp:
                    result['message'] = ("Successfully updated config "
                                         "on repo '%s'." % repository)
                    result['changed'] = True
                else:
                    # Config values were identical.
                    result['message'] = ("Repo '%s' was not updated because "
                                         "config was identical." % repository)
            # Attach the repository config to result
            current_config = artifactory_repo.get_repository_config()
            result['config'] = json.loads(current_config.read())
        elif state == 'absent':
            if not repository_exists:
                result['message'] = repo_not_exists_msg
            else:
                # save config for output on successful delete so it can be
                # used later in play if recreating repositories
                current_config = artifactory_repo.get_repository_config()
                resp = artifactory_repo.delete_repository()
                if resp:
                    result['message'] = ("Successfully deleted repo '%s'."
                                         % repository)
                    result['changed'] = True
                    result['config'] = json.loads(current_config.read())
                else:
                    failure_message = (resp_is_invalid_failure
                                       % (state, repository))
    except urllib_error.HTTPError as http_e:
        message = ("HTTP response code was '%s'. Response message was"
                   " '%s'. " % (http_e.getcode(), http_e.read()))
        failure_message = message
    except urllib_error.URLError as url_e:
        message = ("A generic URLError was thrown. URLError: %s"
                   % str(url_e))
        failure_message = message
    except SyntaxError as s_e:
        message = ("%s. Response from artifactory was malformed: '%s' . "
                   % (str(s_e), resp))
        failure_message = message
    except ValueError as v_e:
        message = ("%s. Response from artifactory was malformed: '%s' . "
                   % (str(v_e), resp))
        failure_message = message
    except art_base.ConfigValueTypeMismatch as cvtm:
        failure_message = cvtm.message
    except art_base.InvalidConfigurationData as icd:
        failure_message = icd.message

    if failure_message:
        module.fail_json(msg=failure_message, **result)

    module.exit_json(**result)
Exemple #3
0
def main():
    state_map = ['present', 'absent', 'read', 'list']
    module_args = dict(
        artifactory_url=dict(type='str', required=True),
        name=dict(type='str', default=''),
        perm_config=dict(type='str', default=None),
        username=dict(type='str', default=None),
        auth_password=dict(type='str', no_log=True, default=None),
        auth_token=dict(type='str', no_log=True, default=None),
        validate_certs=dict(type='bool', default=False),
        client_cert=dict(type='path', default=None),
        client_key=dict(type='path', default=None),
        force_basic_auth=dict(type='bool', default=False),
        state=dict(type='str', default='read', choices=state_map),
        perm_config_dict=dict(type='dict', default=dict()),
        repositories=dict(type='list', default=None),
    )

    result = dict(changed=False,
                  original_message='',
                  message='',
                  config=dict())

    module = AnsibleModule(
        argument_spec=module_args,
        required_together=[['username', 'auth_password']],
        required_one_of=[['auth_password', 'auth_token']],
        mutually_exclusive=[['auth_password', 'auth_token']],
        required_if=[['state', 'present', ['artifactory_url', 'name']],
                     ['state', 'absent', ['artifactory_url', 'name']],
                     ['state', 'read', ['artifactory_url', 'name']]],
        supports_check_mode=True,
    )

    artifactory_url = module.params['artifactory_url']
    name = module.params['name']
    perm_config = module.params['perm_config']
    username = module.params['username']
    auth_password = module.params['auth_password']
    auth_token = module.params['auth_token']
    validate_certs = module.params['validate_certs']
    client_cert = module.params['client_cert']
    client_key = module.params['client_key']
    force_basic_auth = module.params['force_basic_auth']
    state = module.params['state']
    perm_config_dict = module.params['perm_config_dict']

    if perm_config:
        # temporarily convert to dict for validation
        perm_config = ast.literal_eval(perm_config)

    fail_messages = []

    fails = art_base.validate_config_params(perm_config, perm_config_dict,
                                            'perm_config', 'perm_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('name', module, perm_config,
                                               perm_config_dict, 'perm_config',
                                               'perm_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('repositories', module,
                                               perm_config, perm_config_dict,
                                               'perm_config',
                                               'perm_config_dict')
    fail_messages.extend(fails)

    # Populate failure messages
    failure_message = "".join(fail_messages)

    # Conflicting config values should not be resolved
    if failure_message:
        module.fail_json(msg=failure_message, **result)

    sec_dict = dict()
    if module.params['name']:
        sec_dict['name'] = module.params['name']
    if module.params['repositories']:
        sec_dict['repositories'] = module.params['repositories']
    if perm_config:
        sec_dict.update(perm_config)
    if perm_config_dict:
        sec_dict.update(perm_config_dict)
    # Artifactory stores the name as lowercase (even if it was passed as
    # multi-case). Calls against that name after it is created will fail
    # since artifactory only recognizes the lower case name.
    sec_dict['name'] = sec_dict['name'].lower()
    name = name.lower()
    perm_config = str(sec_dict)

    result['original_message'] = ("Perform state '%s' against target '%s' "
                                  "within artifactory '%s'" %
                                  (state, name, artifactory_url))

    art_perm = ArtifactoryPermissions(artifactory_url=artifactory_url,
                                      name=name,
                                      perm_config=perm_config,
                                      username=username,
                                      password=auth_password,
                                      auth_token=auth_token,
                                      validate_certs=validate_certs,
                                      client_cert=client_cert,
                                      client_key=client_key,
                                      force_basic_auth=force_basic_auth,
                                      config_map=URI_CONFIG_MAP)
    art_base.run_module(module, art_perm, "permission target", result,
                        fail_messages, perm_config)
Exemple #4
0
def main():
    state_map = ['present', 'absent', 'read', 'list']
    rclass_state_map = VALID_RCLASSES
    packageType_state_map = VALID_PACKAGETYPES
    module_args = dict(
        artifactory_url=dict(type='str', required=True),
        name=dict(type='str', required=True),
        repo_position=dict(type='int', default=None),
        repo_config=dict(type='str', default=None),
        username=dict(type='str', default=None),
        password=dict(type='str', no_log=True, default=None),
        auth_token=dict(type='str', no_log=True, default=None),
        validate_certs=dict(type='bool', default=False),
        client_cert=dict(type='path', default=None),
        client_key=dict(type='path', default=None),
        force_basic_auth=dict(type='bool', default=False),
        state=dict(type='str', default='read', choices=state_map),
        rclass=dict(type='str', default=None, choices=rclass_state_map),
        packageType=dict(type='str',
                         default=None,
                         choices=packageType_state_map),
        url=dict(type='str', default=None),
        repoLayoutRef=dict(type='str', default=None),
        repo_config_dict=dict(type='dict', default=dict()),
    )

    result = dict(changed=False,
                  original_message='',
                  message='',
                  config=dict())

    module = AnsibleModule(
        argument_spec=module_args,
        required_together=[['username', 'password']],
        required_one_of=[['password', 'auth_token']],
        mutually_exclusive=[['password', 'auth_token']],
        required_if=[['state', 'present', ['artifactory_url', 'name']],
                     ['state', 'absent', ['artifactory_url', 'name']],
                     ['state', 'read', ['artifactory_url', 'name']]],
        supports_check_mode=True,
    )

    artifactory_url = module.params['artifactory_url']
    repository = module.params['name']
    repo_position = module.params['repo_position']
    repo_config = module.params['repo_config']
    username = module.params['username']
    password = module.params['password']
    auth_token = module.params['auth_token']
    validate_certs = module.params['validate_certs']
    client_cert = module.params['client_cert']
    client_key = module.params['client_key']
    force_basic_auth = module.params['force_basic_auth']
    state = module.params['state']
    repo_config_dict = module.params['repo_config_dict']

    if repo_config:
        # temporarily convert to dict for validation
        repo_config = ast.literal_eval(repo_config)

    fail_messages = []

    fails = art_base.validate_config_params(repo_config, repo_config_dict,
                                            'repo_config', 'repo_config_dict')
    fail_messages.extend(fails)

    fails = art_base.validate_top_level_params('rclass', module, repo_config,
                                               repo_config_dict, 'repo_config',
                                               'repo_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('packageType', module,
                                               repo_config, repo_config_dict,
                                               'repo_config',
                                               'repo_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('url', module, repo_config,
                                               repo_config_dict, 'repo_config',
                                               'repo_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('repoLayoutRef', module,
                                               repo_config, repo_config_dict,
                                               'repo_config',
                                               'repo_config_dict')
    fail_messages.extend(fails)

    # Populate failure messages
    failure_message = "".join(fail_messages)

    # Conflicting config values should not be resolved
    if failure_message:
        module.fail_json(msg=failure_message, **result)

    repo_dict = dict()
    if module.params['name']:
        repo_dict['key'] = module.params['name']
    if module.params['rclass']:
        repo_dict['rclass'] = module.params['rclass']
    if module.params['packageType']:
        repo_dict['packageType'] = module.params['packageType']
    if module.params['url']:
        repo_dict['url'] = module.params['url']
    if module.params['repoLayoutRef']:
        repo_dict['repoLayoutRef'] = module.params['repoLayoutRef']
    if repo_config:
        repo_dict.update(repo_config)
    if repo_config_dict:
        repo_dict.update(repo_config_dict)
    repo_config = str(repo_dict)

    result['original_message'] = ("Perform state '%s' against repo '%s' "
                                  "within artifactory '%s'" %
                                  (state, repository, artifactory_url))

    art_repo = ArtifactoryRepoManagement(artifactory_url=artifactory_url,
                                         repo=repository,
                                         repo_position=repo_position,
                                         repo_config=repo_config,
                                         username=username,
                                         password=password,
                                         auth_token=auth_token,
                                         validate_certs=validate_certs,
                                         client_cert=client_cert,
                                         client_key=client_key,
                                         force_basic_auth=force_basic_auth,
                                         config_map=URI_CONFIG_MAP)

    art_base.run_module(module, art_repo, "repos", result, fail_messages,
                        repo_config)
def run_module():
    state_map = ['present', 'absent', 'read', 'list']
    module_args = dict(
        artifactory_url=dict(type='str', required=True),
        name=dict(type='str', default=''),
        sec_config=dict(type='str', default=None),
        username=dict(type='str', default=None),
        auth_password=dict(type='str', no_log=True, default=None),
        auth_token=dict(type='str', no_log=True, default=None),
        validate_certs=dict(type='bool', default=False),
        client_cert=dict(type='path', default=None),
        client_key=dict(type='path', default=None),
        force_basic_auth=dict(type='bool', default=False),
        state=dict(type='str', default='read', choices=state_map),
        sec_config_dict=dict(type='dict', default=dict()),
        password=dict(type='str', no_log=True, default=None),
        email=dict(type='str', default=None),
        repositories=dict(type='list', default=None),
    )

    result = dict(
        changed=False,
        original_message='',
        message='',
        config=dict()
    )

    module = AnsibleModule(
        argument_spec=module_args,
        required_together=[['username', 'auth_password']],
        required_one_of=[['auth_password', 'auth_token']],
        mutually_exclusive=[['auth_password', 'auth_token']],
        required_if=[['state', 'present', ['artifactory_url', 'name']],
                     ['state', 'absent', ['artifactory_url', 'name']],
                     ['state', 'read', ['artifactory_url', 'name']]],
        supports_check_mode=True,
    )

    artifactory_url = module.params['artifactory_url']
    name = module.params['name']
    sec_config = module.params['sec_config']
    username = module.params['username']
    auth_password = module.params['auth_password']
    auth_token = module.params['auth_token']
    validate_certs = module.params['validate_certs']
    client_cert = module.params['client_cert']
    client_key = module.params['client_key']
    force_basic_auth = module.params['force_basic_auth']
    state = module.params['state']
    sec_config_dict = module.params['sec_config_dict']

    if sec_config:
        # temporarily convert to dict for validation
        sec_config = ast.literal_eval(sec_config)

    fail_messages = []

    fails = art_base.validate_config_params(sec_config, sec_config_dict,
                                            'sec_config',
                                            'sec_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('name', module, sec_config,
                                               sec_config_dict,
                                               'sec_config',
                                               'sec_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('password', module, sec_config,
                                               sec_config_dict,
                                               'sec_config',
                                               'sec_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('email', module, sec_config,
                                               sec_config_dict,
                                               'sec_config',
                                               'sec_config_dict')
    fail_messages.extend(fails)
    fails = art_base.validate_top_level_params('repositories', module,
                                               sec_config,
                                               sec_config_dict,
                                               'sec_config',
                                               'sec_config_dict')
    fail_messages.extend(fails)

    # Populate failure messages
    failure_message = "".join(fail_messages)

    # Conflicting config values should not be resolved
    if failure_message:
        module.fail_json(msg=failure_message, **result)

    sec_dict = dict()
    if module.params['name']:
        sec_dict['name'] = module.params['name']
    if module.params['password']:
        sec_dict['password'] = module.params['password']
    if module.params['email']:
        sec_dict['email'] = module.params['email']
    if module.params['repositories']:
        sec_dict['repositories'] = module.params['repositories']
    if sec_config:
        sec_dict.update(sec_config)
    if sec_config_dict:
        sec_dict.update(sec_config_dict)
    sec_config = json.dumps(sec_dict)

    result['original_message'] = ("Perform state '%s' against target '%s' "
                                  "within artifactory '%s'"
                                  % (state, name, artifactory_url))

    art_sec = ArtifactorySecurity(
        artifactory_url=artifactory_url,
        name=name,
        sec_config=sec_config,
        username=username,
        password=auth_password,
        auth_token=auth_token,
        validate_certs=validate_certs,
        client_cert=client_cert,
        client_key=client_key,
        force_basic_auth=force_basic_auth,
        config_map=URI_CONFIG_MAP)

    target_exists = False
    try:
        art_sec.get_target_config()
        target_exists = True
    except urllib_error.HTTPError as http_e:
        if http_e.getcode() == 404:
            # If 404, the target is just not found. Continue on.
            pass
        else:
            message = ("HTTP response code was '%s'. Response message was"
                       " '%s'. " % (http_e.getcode(), http_e.read()))
            fail_messages.append(message)
    except urllib_error.URLError as url_e:
        message = ("A generic URLError was thrown. URLError: %s" % str(url_e))
        fail_messages.append(message)

    try:
        # Now that configs are lined up, verify required values in configs
        if state == 'present':
            art_sec.validate_config_values(artifactory_url,
                                           sec_dict)
            if not target_exists:
                art_sec.validate_config_required_keys(artifactory_url,
                                                      sec_dict)
    except art_base.ConfigValueTypeMismatch as cvtm:
        fail_messages.append(cvtm.message + ". ")
    except art_base.InvalidConfigurationData as icd:
        fail_messages.append(icd.message + ". ")
    except art_base.InvalidArtifactoryURL as iau:
        fail_messages.append(iau.message + ". ")

    # Populate failure messages
    failure_message = "".join(fail_messages)

    # Fail fast here, conflicting config values or invalid urls should not
    # be resolved.
    if failure_message:
        module.fail_json(msg=failure_message, **result)

    if module.check_mode:
        result['message'] = 'check_mode success'
        module.exit_json(**result)

    target_not_exists_msg = ("Target '%s' does not exist." % name)
    resp_is_invalid_failure = ("An unknown error occurred while attempting to "
                               "'%s' target '%s'. Response should "
                               "not be None.")
    try:
        if state == 'list':
            result['message'] = ("List of all targets against "
                                 "artifactory_url: %s" % artifactory_url)
            resp = art_sec.get_targets()
            result['config'] = json.loads(resp.read())
        elif state == 'read':
            if not target_exists:
                result['message'] = target_not_exists_msg
            else:
                resp = art_sec.get_target_config()
                if resp:
                    result['message'] = ("Successfully read config "
                                         "on target '%s'." % name)
                    result['config'] = json.loads(resp.read())
                    result['changed'] = True
                else:
                    failure_message = (resp_is_invalid_failure
                                       % (state, name))
        elif state == 'present':
            # If the target doesn't exist, create it.
            # If the target does exist, perform an update on it ONLY if
            # configuration supplied has values that don't match the remote
            # config. Some values may be read only, but these will not be
            # caught up front.
            if not target_exists:
                result['message'] = ('Attempting to create target: %s'
                                     % name)
                resp = art_sec.create_target()
                if resp:
                    result['message'] = resp.read()
                    result['changed'] = True
                else:
                    failure_message = (resp_is_invalid_failure
                                       % (state, name))
            else:
                result['message'] = ('Attempting to update target: %s'
                                     % name)
                current_config = art_sec.get_target_config()
                current_config = json.loads(current_config.read())
                desired_config = ast.literal_eval(sec_config)
                # Compare desired config with current config against target.
                # If config values are identical, don't update.
                resp = None
                for key in current_config:
                    if key in desired_config:
                        if desired_config[key] != current_config[key]:
                            if 'permissions' in artifactory_url:
                                # For certain Artifactory API interactions
                                # Replace via PUT is required instead of
                                # Update via POST
                                resp = art_sec.replace_target()
                            else:
                                resp = art_sec.update_target_config()
                if resp:
                    result['message'] = ("Successfully updated config "
                                         "on target '%s'." % name)
                    result['changed'] = True
                else:
                    # Config values were identical.
                    result['message'] = ("Target '%s' was not updated because "
                                         "config was identical." % name)
            # Attach the target config to result
            current_config = art_sec.get_target_config()
            result['config'] = json.loads(current_config.read())
        elif state == 'absent':
            if not target_exists:
                result['message'] = target_not_exists_msg
            else:
                # save config for output on successful delete so it can be
                # used later in play if recreating targets
                current_config = art_sec.get_target_config()
                resp = art_sec.delete_target()
                if resp:
                    result['message'] = ("Successfully deleted target '%s'."
                                         % name)
                    result['changed'] = True
                    result['config'] = json.loads(current_config.read())
                else:
                    failure_message = (resp_is_invalid_failure
                                       % (state, name))
    except urllib_error.HTTPError as http_e:
        message = ("HTTP response code was '%s'. Response message was"
                   " '%s'. " % (http_e.getcode(), http_e.read()))
        failure_message = message
    except urllib_error.URLError as url_e:
        message = ("A generic URLError was thrown. URLError: %s"
                   % str(url_e))
        failure_message = message
    except SyntaxError as s_e:
        message = ("%s. Response from artifactory was malformed: '%s' . "
                   % (str(s_e), resp))
        failure_message = message
    except ValueError as v_e:
        message = ("%s. Response from artifactory was malformed: '%s' . "
                   % (str(v_e), resp))
        failure_message = message
    except art_base.ConfigValueTypeMismatch as cvtm:
        failure_message = cvtm.message
    except art_base.InvalidConfigurationData as icd:
        failure_message = icd.message

    if failure_message:
        module.fail_json(msg=failure_message, **result)

    module.exit_json(**result)