Exemple #1
0
def list_directory(module, domain, dir_path):
    connection = Connection(module._socket_path)
    dir_req = DirectoryRequest()
    dir_req.set_path(domain=domain, dir_path=dir_path)

    dir_resp = connection.get_resource_or_none(dir_req.path)
    if dir_resp is None:
        return []
    data = dir_resp['filestore']['location']
    if 'file' in data and isinstance(data['file'], dict):
        return [data['file']]
    elif 'file' in data and isinstance(data['file'], list):
        return data['file']
    else:
        return []
Exemple #2
0
def ensure_directory(module, domain, dir_path, state='present'):
    # Directory is a top_dir, nothing to ensure.
    diff = None
    result = {}
    result['changed'] = False

    if len(dir_path.split('/')) == 1:
        result = {}
        result['path'] = dir_path
        result['diff'] = {}
        return result

    connection = Connection(module._socket_path)
    dir_req = DirectoryRequest()
    dir_req.set_body(dir_path=dir_path)
    dir_req.set_path(domain=domain, dir_path=dir_path)
    dir_state = connection.get_resource_or_none(dir_req.path)

    if dir_state and state == 'present':
        result['path'] = dir_path
        diff = {}
        result['response'] = None
    elif dir_state is None and state == 'present':
        if not module.check_mode:
            resp = connection.send_request(**dir_req.post())
            result['response'] = resp
        result['changed'] = True
        diff = {'before': None, 'after': dir_path}
        result['path'] = dir_path
    elif dir_state and state == 'absent':
        if not module.check_mode:
            resp = connection.send_request(**dir_req.delete())
            result['response'] = resp
        diff = {'before': dir_path, 'after': None}
        result['changed'] = True
    elif dir_state is None and state == 'absent':
        diff = {'before': None, 'after': None}
        result['response'] = None

    if module._diff:
        result['diff'] = diff
    return result
Exemple #3
0
def ensure_file(module, domain, file_path, data, state):
    result = {}
    result['changed'] = False
    connection = Connection(module._socket_path)
    parent_dir = posixpath.split(file_path)[0]
    top_dir = file_path.split(
        '/'
    )[0] or parent_dir  # Handles the case where the parent dir is also the root directory.
    diff = None

    # Ensure the parent directory is present before uploading file
    # If file state is 'absent' do nothing.
    if state != 'absent':
        result['directory'] = ensure_directory(module, domain, parent_dir)

    files = list_directory(module=module, domain=domain, dir_path=parent_dir)

    file_req = build_file_request(domain, file_path, data)

    if not has_file(files, file_path) and state == 'present':
        if not module.check_mode:
            file_create_resp = connection.send_request(**file_req.post())
            result['response'] = file_create_resp
            result['path'] = file_create_resp['_links']['location']['href']
        result['diff'] = {'before': None, 'after': file_path}
        result['changed'] = True

    elif has_file(files, file_path) and state == 'present':
        # Compare the files, can't compare cert/sharedcert.
        if 'sharecert' not in top_dir and 'cert' not in top_dir:
            resp = connection.get_resource_or_none(file_req.path)
            from_data = base64.b64decode(resp['file'])

            try:
                diff = file_diff(
                    from_data,
                    data,
                    file_path,
                )
            except UnicodeDecodeError as e:
                # File seems to be binary
                diff = 'Not possible to compare a binary file.'

            # Compare md5, if data is different update the file
            to_md5 = hashlib.md5()
            to_md5.update(data)

            from_md5 = hashlib.md5()
            from_md5.update(from_data)
            if to_md5.hexdigest() != from_md5.hexdigest():
                if not module.check_mode:
                    update_resp = connection.send_request(**file_req.put())
                    result['response'] = update_resp
                result['changed'] = True

        # The requested file already exists in cert/shared cert
        # Not updating a file as there is no way to restore/backout
        # unless you have the original cert/key or secure backups.
        elif has_file(files, file_path):
            result['path'] = file_path
            result[
                'msg'] = 'Files are in cert / sharedcert directories, not overwiting existing crypto files.'
            return result
        else:
            raise NotImplementedError(
                "This condition was not expected, this is likely a bug.")
    elif not has_file(files, file_path) and state == 'absent':
        diff = {'before': None, 'after': None}
    elif has_file(files, file_path) and state == 'absent':
        diff = {'before': file_path, 'after': None}
        delete_resp = connection.send_request(**file_req.delete())
        result['changed'] = True
        result['response'] = delete_resp

    if module._diff:
        result['diff'] = diff

    return result
Exemple #4
0
def ensure_config(module, domain, config, state):
    result = {}
    result['changed'] = False
    connection = Connection(module._socket_path)
    class_name = class_name_from_config(config)
    name = name_from_config(config, class_name)
    clean_dp_dict(
        config
    )  # Remove keys that aren't valid for configuration. (href, links, self, etc...)
    req = ConfigRequest()
    req.path = join_path(domain, class_name, name, base_path='/mgmt/config/')

    before = connection.get_resource_or_none(req.path)
    if before is not None:
        clean_dp_dict(before)
        diff = recursive_diff(before, config)
    else:
        diff = config
    result['before'] = before

    if module._diff:
        result['diff'] = diff

    # Determine the correct request to execute depending on desired state.
    request = None
    if state == 'merged':
        if before is None:
            req.body = config
            request = req.post
        elif diff and len(list(diff[1][class_name])) > 0:
            req.body = dict_merge(before, config)
            request = req.put
    elif state == 'replaced':
        if before is None:
            req.body = config
            request = req.post
        elif diff and len(list(diff[1][class_name])) > 0:
            req.body = config
            request = req.put
    elif state == 'deleted':
        if before is None:
            request = None
        else:
            request = req.delete

    if module.check_mode:
        if request:
            result['changed'] = True
        module.exit_json(**result)

    if request:
        try:
            response = connection.send_request(**request())
            result['config'] = connection.get_resource_or_none(req.path)
            clean_dp_dict(result['config'])
        except ConnectionError as e:
            err = to_text(e)
            result['request'] = request()
            result['error'] = err
            result['changed'] = False
            module.fail_json(msg=to_text(e), **result)

        result['response'] = response
        result['changed'] = True

    return result