コード例 #1
0
def main():
    fields = dict(operation=dict(type='str', required=True),
                  data=dict(type='dict'),
                  query_params=dict(type='dict'),
                  path_params=dict(type='dict'),
                  register_as=dict(type='str'),
                  filters=dict(type='dict'))
    module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
    params = module.params

    connection = Connection(module._socket_path)

    op_name = params['operation']
    op_spec = connection.get_operation_spec(op_name)
    if op_spec is None:
        module.fail_json(msg='Invalid operation name provided: %s' % op_name)

    data, query_params, path_params = params['data'], params[
        'query_params'], params['path_params']

    try:
        validate_params(connection, op_name, query_params, path_params, data,
                        op_spec)
    except ValidationError as e:
        module.fail_json(msg=e.args[0])

    try:
        if module.check_mode:
            module.exit_json(changed=False)

        resource = BaseConfigurationResource(connection)
        url = op_spec[OperationField.URL]

        if is_add_operation(op_name, op_spec):
            resp = resource.add_object(url, data, path_params, query_params)
        elif is_edit_operation(op_name, op_spec):
            resp = resource.edit_object(url, data, path_params, query_params)
        elif is_delete_operation(op_name, op_spec):
            resp = resource.delete_object(url, path_params)
        elif is_find_by_filter_operation(op_name, op_spec, params):
            resp = resource.get_objects_by_filter(url, params['filters'],
                                                  path_params, query_params)
        else:
            resp = resource.send_request(url, op_spec[OperationField.METHOD],
                                         data, path_params, query_params)

        module.exit_json(changed=resource.config_changed,
                         response=resp,
                         ansible_facts=construct_ansible_facts(
                             resp, module.params))
    except FtdConfigurationError as e:
        module.fail_json(
            msg=
            'Failed to execute %s operation because of the configuration error: %s'
            % (op_name, e))
    except FtdServerError as e:
        module.fail_json(
            msg=
            'Server returned an error trying to execute %s operation. Status code: %s. '
            'Server response: %s' % (op_name, e.code, e.response))
コード例 #2
0
def main():
    fields = dict(
        operation=dict(type='str', required=True),
        file_to_upload=dict(type='path', required=True),
        register_as=dict(type='str'),
    )
    module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
    params = module.params
    connection = Connection(module._socket_path)

    op_spec = connection.get_operation_spec(params['operation'])
    if op_spec is None:
        module.fail_json(msg='Operation with specified name is not found: %s' %
                         params['operation'])
    if not is_upload_operation(op_spec):
        module.fail_json(
            msg=
            'Invalid upload operation: %s. The operation must make POST request and return UploadStatus model.'
            % params['operation'])

    try:
        if module.check_mode:
            module.exit_json()
        resp = connection.upload_file(params['file_to_upload'],
                                      op_spec[OperationField.URL])
        module.exit_json(changed=True,
                         response=resp,
                         ansible_facts=construct_ansible_facts(
                             resp, module.params))
    except FtdServerError as e:
        module.fail_json(
            msg='Upload request for %s operation failed. Status code: %s. '
            'Server response: %s' % (params['operation'], e.code, e.response))
コード例 #3
0
ファイル: ftd_file_download.py プロジェクト: zship/ansible
def main():
    fields = dict(operation=dict(type='str', required=True),
                  path_params=dict(type='dict'),
                  destination=dict(type='path', required=True))
    module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
    params = module.params
    connection = Connection(module._socket_path)

    op_name = params['operation']
    op_spec = connection.get_operation_spec(op_name)
    if op_spec is None:
        module.fail_json(msg='Operation with specified name is not found: %s' %
                         op_name)
    if not is_download_operation(op_spec):
        module.fail_json(
            msg=
            'Invalid download operation: %s. The operation must make GET request and return a file.'
            % op_name)

    try:
        path_params = params['path_params']
        validate_params(connection, op_name, path_params)
        if module.check_mode:
            module.exit_json(changed=False)
        connection.download_file(op_spec[OperationField.URL],
                                 params['destination'], path_params)
        module.exit_json(changed=False)
    except FtdServerError as e:
        module.fail_json(
            msg='Download request for %s operation failed. Status code: %s. '
            'Server response: %s' % (op_name, e.code, e.response))
    except ValidationError as e:
        module.fail_json(msg=e.args[0])