Debian Codegraph @

ansible_collections/community/general/plugins/modules/oneview_ethernet_network_info.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2016-2017 Hewlett Packard Enterprise Development LP
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 module: oneview_ethernet_network_info
10 short_description: Retrieve the information about one or more of the OneView Ethernet Networks
11 description:
12 - Retrieve the information about one or more of the Ethernet Networks from OneView.
13 requirements:
14 - hpOneView >= 2.0.1
15 author:
16 - Felipe Bulsoni (@fgbulsoni)
17 - Thiago Miotto (@tmiotto)
18 - Adriane Cardozo (@adriane-cardozo)
19 attributes:
20 check_mode:
21 version_added: 3.3.0
22 # This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix
23 options:
24 name:
25 description:
26 - Ethernet Network name.
27 type: str
28 options:
29 description:
30 - 'List with options to gather additional information about an Ethernet Network and related resources. Options allowed:
31 V(associatedProfiles) and V(associatedUplinkGroups).'
32 type: list
33 elements: str
34 extends_documentation_fragment:
35 - community.general._oneview
36 - community.general._oneview.factsparams
37 - community.general._attributes
38 - community.general._attributes.info_module
39 """
40
41 EXAMPLES = r"""
42 - name: Gather information about all Ethernet Networks
43 community.general.oneview_ethernet_network_info:
44 config: /etc/oneview/oneview_config.json
45 delegate_to: localhost
46 register: result
47
48 - name: Print fetched information about Ethernet Networks
49 ansible.builtin.debug:
50 msg: "{{ result.ethernet_networks }}"
51
52 - name: Gather paginated and filtered information about Ethernet Networks
53 community.general.oneview_ethernet_network_info:
54 config: /etc/oneview/oneview_config.json
55 params:
56 start: 1
57 count: 3
58 sort: 'name:descending'
59 filter: 'purpose=General'
60 delegate_to: localhost
61 register: result
62
63 - name: Print fetched information about paginated and filtered list of Ethernet Networks
64 ansible.builtin.debug:
65 msg: "{{ result.ethernet_networks }}"
66
67 - name: Gather information about an Ethernet Network by name
68 community.general.oneview_ethernet_network_info:
69 config: /etc/oneview/oneview_config.json
70 name: Ethernet network name
71 delegate_to: localhost
72 register: result
73
74 - name: Print fetched information about Ethernet Network found by name
75 ansible.builtin.debug:
76 msg: "{{ result.ethernet_networks }}"
77
78 - name: Gather information about an Ethernet Network by name with options
79 community.general.oneview_ethernet_network_info:
80 config: /etc/oneview/oneview_config.json
81 name: eth1
82 options:
83 - associatedProfiles
84 - associatedUplinkGroups
85 delegate_to: localhost
86 register: result
87
88 - name: Print fetched information about Ethernet Network Associated Profiles
89 ansible.builtin.debug:
90 msg: "{{ result.enet_associated_profiles }}"
91
92 - name: Print fetched information about Ethernet Network Associated Uplink Groups
93 ansible.builtin.debug:
94 msg: "{{ result.enet_associated_uplink_groups }}"
95 """
96
97 RETURN = r"""
98 ethernet_networks:
99 description: Has all the OneView information about the Ethernet Networks.
100 returned: Always, but can be null.
101 type: dict
102
103 enet_associated_profiles:
104 description: Has all the OneView information about the profiles which are using the Ethernet network.
105 returned: When requested, but can be null.
106 type: dict
107
108 enet_associated_uplink_groups:
109 description: Has all the OneView information about the uplink sets which are using the Ethernet network.
110 returned: When requested, but can be null.
111 type: dict
112 """
113
114 from ansible_collections.community.general.plugins.module_utils._oneview import OneViewModuleBase
115
116
117 class EthernetNetworkInfoModule(OneViewModuleBase):
118 argument_spec = dict(name=dict(type="str"), options=dict(type="list", elements="str"), params=dict(type="dict"))
119
120 def __init__(self):
121 super().__init__(
122 additional_arg_spec=self.argument_spec,
123 supports_check_mode=True,
124 )
125
126 self.resource_client = self.oneview_client.ethernet_networks
127
128 def execute_module(self):
129 info = {}
130 if self.module.params["name"]:
131 ethernet_networks = self.resource_client.get_by("name", self.module.params["name"])
132
133 if self.module.params.get("options") and ethernet_networks:
134 info = self.__gather_optional_info(ethernet_networks[0])
135 else:
136 ethernet_networks = self.resource_client.get_all(**self.facts_params)
137
138 info["ethernet_networks"] = ethernet_networks
139
140 return dict(changed=False, **info)
141
142 def __gather_optional_info(self, ethernet_network):
143 info = {}
144
145 if self.options.get("associatedProfiles"):
146 info["enet_associated_profiles"] = self.__get_associated_profiles(ethernet_network)
147 if self.options.get("associatedUplinkGroups"):
148 info["enet_associated_uplink_groups"] = self.__get_associated_uplink_groups(ethernet_network)
149
150 return info
151
152 def __get_associated_profiles(self, ethernet_network):
153 associated_profiles = self.resource_client.get_associated_profiles(ethernet_network["uri"])
154 return [self.oneview_client.server_profiles.get(x) for x in associated_profiles]
155
156 def __get_associated_uplink_groups(self, ethernet_network):
157 uplink_groups = self.resource_client.get_associated_uplink_groups(ethernet_network["uri"])
158 return [self.oneview_client.uplink_sets.get(x) for x in uplink_groups]
159
160
161 def main():
162 EthernetNetworkInfoModule().run()
163
164
165 if __name__ == "__main__":
166 main()