def test_construct_ansible_facts_should_not_make_default_fact_with_no_type():
    response = {
        'id': '123',
        'type': 'bar'
    }

    assert {} == construct_ansible_facts(response, {})
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)
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, {})
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)
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, {})
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)