Exemplo n.º 1
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))
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)
    resource = BaseConfigurationResource(connection, module.check_mode)
    op_name = params['operation']
    try:
        resp = resource.execute_operation(op_name, params)
        module.exit_json(changed=resource.config_changed, response=resp,
                         ansible_facts=construct_ansible_facts(resp, module.params))
    except FtdInvalidOperationNameError as e:
        module.fail_json(msg='Invalid operation name provided: %s' % e.operation_name)
    except FtdConfigurationError as e:
        module.fail_json(msg='Failed to execute %s operation because of the configuration error: %s' % (op_name, e.msg))
    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))
    except FtdUnexpectedResponse as e:
        module.fail_json(msg=e.args[0])
    except ValidationError as e:
        module.fail_json(msg=e.args[0])
    except CheckModeException:
        module.exit_json(changed=False)
Exemplo n.º 3
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))
Exemplo n.º 4
0
def test_construct_ansible_facts_should_ignore_items_with_no_register_as():
    response = {
        'items': [{
            'id': '123',
            'name': 'foo',
            'type': 'bar'
        }, {
            'id': '123',
            'name': 'foo',
            'type': 'bar'
        }]
    }

    assert {} == construct_ansible_facts(response, {})
Exemplo n.º 5
0
def test_construct_ansible_facts_should_extract_items():
    response = {
        'items': [{
            'id': '123',
            'name': 'foo',
            'type': 'bar'
        }, {
            'id': '123',
            'name': 'foo',
            'type': 'bar'
        }]
    }
    params = {'register_as': 'fact_name'}

    assert {
        'fact_name': response['items']
    } == construct_ansible_facts(response, params)
Exemplo n.º 6
0
def test_construct_ansible_facts_should_use_register_as_when_given():
    response = {'id': '123', 'name': 'foo', 'type': 'bar'}
    params = {'register_as': 'fact_name'}

    assert {'fact_name': response} == construct_ansible_facts(response, params)
Exemplo n.º 7
0
def test_construct_ansible_facts_should_not_make_default_fact_with_no_type():
    response = {'id': '123', 'type': 'bar'}

    assert {} == construct_ansible_facts(response, {})
Exemplo n.º 8
0
def test_construct_ansible_facts_should_make_default_fact_with_name_and_type():
    response = {'id': '123', 'name': 'foo', 'type': 'bar'}

    assert {'bar_foo': response} == construct_ansible_facts(response, {})