1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3#
4# Copyright (c) 2016 Red Hat, Inc.
5#
6# This file is part of Ansible
7#
8# Ansible is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# Ansible is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22from __future__ import (absolute_import, division, print_function)
23__metaclass__ = type
24
25DOCUMENTATION = '''
26---
27module: ovirt_network_info
28short_description: Retrieve information about one or more oVirt/RHV networks
29version_added: "1.0.0"
30author:
31- "Ondra Machacek (@machacekondra)"
32- "Martin Necas (@mnecas)"
33description:
34    - "Retrieve information about one or more oVirt/RHV networks."
35    - This module was called C(ovirt_network_facts) before Ansible 2.9, returning C(ansible_facts).
36      Note that the M(ovirt.ovirt.ovirt_network_info) module no longer returns C(ansible_facts)!
37notes:
38    - "This module returns a variable C(ovirt_networks), which
39       contains a list of networks. You need to register the result with
40       the I(register) keyword to use it."
41options:
42    pattern:
43        description:
44            - "Search term which is accepted by oVirt/RHV search backend."
45            - "For example to search network starting with string vlan1 use: name=vlan1*"
46        type: str
47extends_documentation_fragment: ovirt.ovirt.ovirt_info
48'''
49
50
51EXAMPLES = '''
52# Examples don't contain auth parameter for simplicity,
53# look at ovirt_auth module to see how to reuse authentication:
54
55# Gather information about all networks which names start with C(vlan1):
56- ovirt.ovirt.ovirt_network_info:
57    pattern: name=vlan1*
58  register: result
59- ansible.builtin.debug:
60    msg: "{{ result.ovirt_networks }}"
61'''
62
63
64RETURN = '''
65ovirt_networks:
66    description: "List of dictionaries describing the networks. Network attributes are mapped to dictionary keys,
67                  all networks attributes can be found at following url: http://ovirt.github.io/ovirt-engine-api-model/master/#types/network."
68    returned: On success.
69    type: list
70'''
71
72import traceback
73
74from ansible.module_utils.basic import AnsibleModule
75from ansible_collections.ovirt.ovirt.plugins.module_utils.ovirt import (
76    check_sdk,
77    create_connection,
78    get_dict_of_struct,
79    ovirt_info_full_argument_spec,
80)
81
82
83def main():
84    argument_spec = ovirt_info_full_argument_spec(
85        pattern=dict(default='', required=False),
86    )
87    module = AnsibleModule(argument_spec)
88    check_sdk(module)
89    if module.params['fetch_nested'] or module.params['nested_attributes']:
90        module.deprecate(
91            "The 'fetch_nested' and 'nested_attributes' are deprecated please use 'follow' parameter",
92            version='2.0.0',
93            collection_name='ovirt.ovirt'
94        )
95
96    try:
97        auth = module.params.pop('auth')
98        connection = create_connection(auth)
99        networks_service = connection.system_service().networks_service()
100        networks = networks_service.list(search=module.params['pattern'])
101        result = dict(
102            ovirt_networks=[
103                get_dict_of_struct(
104                    struct=c,
105                    connection=connection,
106                    fetch_nested=module.params.get('fetch_nested'),
107                    attributes=module.params.get('nested_attributes'),
108                ) for c in networks
109            ],
110        )
111        module.exit_json(changed=False, **result)
112    except Exception as e:
113        module.fail_json(msg=str(e), exception=traceback.format_exc())
114    finally:
115        connection.close(logout=auth.get('token') is None)
116
117
118if __name__ == '__main__':
119    main()
120