Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/vcenter_network_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright: (c) 2021, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # template: header.j2
6 # This module is autogenerated using the ansible.content_builder.
7 # See: https://github.com/ansible-community/ansible.content_builder
8
9
10 DOCUMENTATION = r"""
11 module: vcenter_network_info
12 short_description: Returns information about at most 1000 visible (subject to permission
13 checks) networks in vCenter matching the Network.FilterSpec.
14 description: Returns information about at most 1000 visible (subject to permission
15 checks) networks in vCenter matching the Network.FilterSpec.
16 options:
17 datacenters:
18 aliases:
19 - filter_datacenters
20 description:
21 - Datacenters that must contain the network for the network to match the filter.
22 - If unset or empty, networks in any datacenter match the filter.
23 - When clients pass a value of this structure as a parameter, the field must
24 contain the id of resources returned by M(vmware.vmware_rest.vcenter_datacenter_info).
25 elements: str
26 type: list
27 folders:
28 aliases:
29 - filter_folders
30 description:
31 - Folders that must contain the network for the network to match the filter.
32 - If unset or empty, networks in any folder match the filter.
33 - When clients pass a value of this structure as a parameter, the field must
34 contain the id of resources returned by M(vmware.vmware_rest.vcenter_folder_info).
35 elements: str
36 type: list
37 names:
38 aliases:
39 - filter_names
40 description:
41 - Names that networks must have to match the filter (see I(name)).
42 - If unset or empty, networks with any name match the filter.
43 elements: str
44 type: list
45 networks:
46 description:
47 - Identifiers of networks that can match the filter.
48 - If unset or empty, networks with any identifier match the filter.
49 - When clients pass a value of this structure as a parameter, the field must
50 contain the id of resources returned by M(vmware.vmware_rest.vcenter_network_info).
51 elements: str
52 type: list
53 session_timeout:
54 description:
55 - 'Timeout settings for client session. '
56 - 'The maximal number of seconds for the whole operation including connection
57 establishment, request sending and response. '
58 - The default value is 300s.
59 type: float
60 version_added: 2.1.0
61 types:
62 aliases:
63 - filter_types
64 description:
65 - Types that networks must have to match the filter (see I(type)).
66 - If unset, networks with any type match the filter.
67 elements: str
68 type: list
69 vcenter_hostname:
70 description:
71 - The hostname or IP address of the vSphere vCenter
72 - If the value is not specified in the task, the value of environment variable
73 C(VMWARE_HOST) will be used instead.
74 required: true
75 type: str
76 vcenter_password:
77 description:
78 - The vSphere vCenter password
79 - If the value is not specified in the task, the value of environment variable
80 C(VMWARE_PASSWORD) will be used instead.
81 required: true
82 type: str
83 vcenter_rest_log_file:
84 description:
85 - 'You can use this optional parameter to set the location of a log file. '
86 - 'This file will be used to record the HTTP REST interaction. '
87 - 'The file will be stored on the host that runs the module. '
88 - 'If the value is not specified in the task, the value of '
89 - environment variable C(VMWARE_REST_LOG_FILE) will be used instead.
90 type: str
91 vcenter_username:
92 description:
93 - The vSphere vCenter username
94 - If the value is not specified in the task, the value of environment variable
95 C(VMWARE_USER) will be used instead.
96 required: true
97 type: str
98 vcenter_validate_certs:
99 default: true
100 description:
101 - Allows connection when SSL certificates are not valid. Set to C(false) when
102 certificates are not trusted.
103 - If the value is not specified in the task, the value of environment variable
104 C(VMWARE_VALIDATE_CERTS) will be used instead.
105 type: bool
106 author:
107 - Ansible Cloud Team (@ansible-collections)
108 version_added: 0.1.0
109 requirements:
110 - vSphere 7.0.3 or greater
111 - python >= 3.6
112 - aiohttp
113 notes:
114 - Tested on vSphere 7.0.3
115 """
116
117 EXAMPLES = r"""
118 - name: Get the dvswitch called my-portgroup
119 vmware.vmware_rest.vcenter_network_info:
120 filter_types: DISTRIBUTED_PORTGROUP
121 filter_names: my portrgoup
122 register: my_portgroup
123 """
124 RETURN = r"""
125 # content generated by the update_return_section callback# task: Get a list of the networks
126 value:
127 description: Get a list of the networks
128 returned: On success
129 sample:
130 - name: VM Network
131 network: network-1016
132 type: STANDARD_PORTGROUP
133 - name: second_vswitch
134 network: network-1018
135 type: STANDARD_PORTGROUP
136 - name: dvswitch1-DVUplinks-1020
137 network: dvportgroup-1021
138 type: DISTRIBUTED_PORTGROUP
139 - name: my portrgoup
140 network: dvportgroup-1022
141 type: DISTRIBUTED_PORTGROUP
142 type: list
143 """
144
145
146 # This structure describes the format of the data expected by the end-points
147 PAYLOAD_FORMAT = {
148 "list": {
149 "query": {
150 "datacenters": "datacenters",
151 "folders": "folders",
152 "names": "names",
153 "networks": "networks",
154 "types": "types",
155 },
156 "body": {},
157 "path": {},
158 }
159 } # pylint: disable=line-too-long
160
161 from ansible.module_utils.basic import env_fallback
162 import os
163
164 if os.getenv("VMWARE_ENABLE_TURBO", False):
165 try:
166 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
167 AnsibleTurboModule as AnsibleModule,
168 )
169
170 AnsibleModule.collection_name = "vmware.vmware_rest"
171 except ImportError:
172 from ansible.module_utils.basic import AnsibleModule
173 else:
174 from ansible.module_utils.basic import AnsibleModule
175
176 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
177 gen_args,
178 open_session,
179 session_timeout,
180 update_changed_flag,
181 )
182
183
184 def prepare_argument_spec():
185 argument_spec = {
186 "vcenter_hostname": dict(
187 type="str",
188 required=True,
189 fallback=(env_fallback, ["VMWARE_HOST"]),
190 ),
191 "vcenter_username": dict(
192 type="str",
193 required=True,
194 fallback=(env_fallback, ["VMWARE_USER"]),
195 ),
196 "vcenter_password": dict(
197 type="str",
198 required=True,
199 no_log=True,
200 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
201 ),
202 "vcenter_validate_certs": dict(
203 type="bool",
204 required=False,
205 default=True,
206 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
207 ),
208 "vcenter_rest_log_file": dict(
209 type="str",
210 required=False,
211 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
212 ),
213 "session_timeout": dict(
214 type="float",
215 required=False,
216 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
217 ),
218 }
219
220 argument_spec["datacenters"] = {
221 "aliases": ["filter_datacenters"],
222 "type": "list",
223 "elements": "str",
224 }
225 argument_spec["folders"] = {
226 "aliases": ["filter_folders"],
227 "type": "list",
228 "elements": "str",
229 }
230 argument_spec["names"] = {
231 "aliases": ["filter_names"],
232 "type": "list",
233 "elements": "str",
234 }
235 argument_spec["networks"] = {"type": "list", "elements": "str"}
236 argument_spec["types"] = {
237 "aliases": ["filter_types"],
238 "type": "list",
239 "elements": "str",
240 }
241
242 return argument_spec
243
244
245 async def main():
246 required_if = list([])
247
248 module_args = prepare_argument_spec()
249 module = AnsibleModule(
250 argument_spec=module_args, required_if=required_if, supports_check_mode=True
251 )
252 if not module.params["vcenter_hostname"]:
253 module.fail_json("vcenter_hostname cannot be empty")
254 if not module.params["vcenter_username"]:
255 module.fail_json("vcenter_username cannot be empty")
256 if not module.params["vcenter_password"]:
257 module.fail_json("vcenter_password cannot be empty")
258 try:
259 session = await open_session(
260 vcenter_hostname=module.params["vcenter_hostname"],
261 vcenter_username=module.params["vcenter_username"],
262 vcenter_password=module.params["vcenter_password"],
263 validate_certs=module.params["vcenter_validate_certs"],
264 log_file=module.params["vcenter_rest_log_file"],
265 )
266 except Exception as err:
267 if hasattr(err, "get_message"):
268 module.fail_json(err.get_message())
269 else:
270 module.fail_json(str(err))
271 result = await entry_point(module, session)
272 module.exit_json(**result)
273
274
275 # template: info_list_and_get_module.j2
276 def build_url(params):
277 import yarl
278
279 _in_query_parameters = PAYLOAD_FORMAT["list"]["query"].keys()
280 return yarl.URL(
281 ("https://{vcenter_hostname}" "/api/vcenter/network").format(**params)
282 + gen_args(params, _in_query_parameters),
283 encoded=True,
284 )
285
286
287 async def entry_point(module, session):
288 url = build_url(module.params)
289 async with session.get(url, **session_timeout(module.params)) as resp:
290 _json = await resp.json()
291
292 if "value" not in _json: # 7.0.2+
293 _json = {"value": _json}
294
295 return await update_changed_flag(_json, resp.status, "get")
296
297
298 if __name__ == "__main__":
299 import asyncio
300
301 current_loop = asyncio.new_event_loop()
302 try:
303 asyncio.set_event_loop(current_loop)
304 current_loop.run_until_complete(main())
305 finally:
306 current_loop.close()