From 8dd1267e6b459a8b488bae67ad5e2188ece09b58 Mon Sep 17 00:00:00 2001 From: Ondra Machacek Date: Thu, 12 Oct 2017 19:57:33 +0200 Subject: [PATCH] ovirt: Add new api facts module (#28875) * ovirt: Add new api facts module * Simplify boilerplate and add Python 3 import --- .../modules/cloud/ovirt/ovirt_api_facts.py | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 lib/ansible/modules/cloud/ovirt/ovirt_api_facts.py diff --git a/lib/ansible/modules/cloud/ovirt/ovirt_api_facts.py b/lib/ansible/modules/cloud/ovirt/ovirt_api_facts.py new file mode 100644 index 00000000000..32986db939e --- /dev/null +++ b/lib/ansible/modules/cloud/ovirt/ovirt_api_facts.py @@ -0,0 +1,89 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright (c) 2017 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} + + +DOCUMENTATION = ''' +--- +module: ovirt_api_facts +short_description: Retrieve facts about the oVirt/RHV API +author: "Ondra Machacek (@machacekondra)" +version_added: "2.5" +description: + - "Retrieve facts about the oVirt/RHV API." +notes: + - "This module creates a new top-level C(ovirt_api) fact, + which contains a information about oVirt/RHV API." +extends_documentation_fragment: ovirt_facts +''' + +EXAMPLES = ''' +# Examples don't contain auth parameter for simplicity, +# look at ovirt_auth module to see how to reuse authentication: + +# Gather facts oVirt API: +- ovirt_api_facts: +- debug: + var: ovirt_api +''' + +RETURN = ''' +ovirt_api: + description: "Dictionary describing the oVirt API information. + Api attributes are mapped to dictionary keys, + all API attributes can be found at following + url: https://ovirt.example.com/ovirt-engine/api/model#types/api." + returned: On success. + type: dictionary +''' + +import traceback + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.ovirt import ( + check_sdk, + create_connection, + get_dict_of_struct, + ovirt_facts_full_argument_spec, +) + + +def main(): + argument_spec = ovirt_facts_full_argument_spec() + module = AnsibleModule(argument_spec) + check_sdk(module) + + try: + auth = module.params.pop('auth') + connection = create_connection(auth) + api = connection.system_service().get() + module.exit_json( + changed=False, + ansible_facts=dict( + ovirt_api=get_dict_of_struct( + struct=api, + connection=connection, + fetch_nested=module.params.get('fetch_nested'), + attributes=module.params.get('nested_attributes'), + ) + ), + ) + except Exception as e: + module.fail_json(msg=str(e), exception=traceback.format_exc()) + finally: + connection.close(logout=auth.get('token') is None) + + +if __name__ == '__main__': + main()