Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/modules/content_library_subscriptions_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: content_library_subscriptions_info
12 short_description: Returns information about the specified subscription of the published
13 library.
14 description: Returns information about the specified subscription of the published
15 library.
16 options:
17 library:
18 description:
19 - Identifier of the published library.
20 - The parameter must be the id of a resource returned by M(vmware.vmware_rest.content_library_info).
21 This parameter is mandatory.
22 required: true
23 type: str
24 session_timeout:
25 description:
26 - 'Timeout settings for client session. '
27 - 'The maximal number of seconds for the whole operation including connection
28 establishment, request sending and response. '
29 - The default value is 300s.
30 type: float
31 version_added: 2.1.0
32 subscription:
33 description:
34 - Identifier of the subscription.
35 - The parameter must be the id of a resource returned by M(vmware.vmware_rest.content_library_subscriptions_info).
36 Required with I(state=['get'])
37 type: str
38 vcenter_hostname:
39 description:
40 - The hostname or IP address of the vSphere vCenter
41 - If the value is not specified in the task, the value of environment variable
42 C(VMWARE_HOST) will be used instead.
43 required: true
44 type: str
45 vcenter_password:
46 description:
47 - The vSphere vCenter password
48 - If the value is not specified in the task, the value of environment variable
49 C(VMWARE_PASSWORD) will be used instead.
50 required: true
51 type: str
52 vcenter_rest_log_file:
53 description:
54 - 'You can use this optional parameter to set the location of a log file. '
55 - 'This file will be used to record the HTTP REST interaction. '
56 - 'The file will be stored on the host that runs the module. '
57 - 'If the value is not specified in the task, the value of '
58 - environment variable C(VMWARE_REST_LOG_FILE) will be used instead.
59 type: str
60 vcenter_username:
61 description:
62 - The vSphere vCenter username
63 - If the value is not specified in the task, the value of environment variable
64 C(VMWARE_USER) will be used instead.
65 required: true
66 type: str
67 vcenter_validate_certs:
68 default: true
69 description:
70 - Allows connection when SSL certificates are not valid. Set to C(false) when
71 certificates are not trusted.
72 - If the value is not specified in the task, the value of environment variable
73 C(VMWARE_VALIDATE_CERTS) will be used instead.
74 type: bool
75 author:
76 - Ansible Cloud Team (@ansible-collections)
77 version_added: 4.0.0
78 requirements:
79 - vSphere 7.0.3 or greater
80 - python >= 3.6
81 - aiohttp
82 notes:
83 - Tested on vSphere 7.0.3
84 """
85
86 EXAMPLES = r"""
87 """
88
89 RETURN = r"""
90 """
91
92 # This structure describes the format of the data expected by the end-points
93 PAYLOAD_FORMAT = {
94 "get": {
95 "query": {},
96 "body": {},
97 "path": {"library": "library", "subscription": "subscription"},
98 },
99 "list": {"query": {}, "body": {}, "path": {"library": "library"}},
100 } # pylint: disable=line-too-long
101
102 from ansible.module_utils.basic import env_fallback
103 import os
104
105 if os.getenv("VMWARE_ENABLE_TURBO", False):
106 try:
107 from ansible_collections.cloud.common.plugins.module_utils.turbo.module import (
108 AnsibleTurboModule as AnsibleModule,
109 )
110
111 AnsibleModule.collection_name = "vmware.vmware_rest"
112 except ImportError:
113 from ansible.module_utils.basic import AnsibleModule
114 else:
115 from ansible.module_utils.basic import AnsibleModule
116
117 from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import (
118 build_full_device_list,
119 exists,
120 gen_args,
121 open_session,
122 session_timeout,
123 update_changed_flag,
124 )
125
126
127 def prepare_argument_spec():
128 argument_spec = {
129 "vcenter_hostname": dict(
130 type="str",
131 required=True,
132 fallback=(env_fallback, ["VMWARE_HOST"]),
133 ),
134 "vcenter_username": dict(
135 type="str",
136 required=True,
137 fallback=(env_fallback, ["VMWARE_USER"]),
138 ),
139 "vcenter_password": dict(
140 type="str",
141 required=True,
142 no_log=True,
143 fallback=(env_fallback, ["VMWARE_PASSWORD"]),
144 ),
145 "vcenter_validate_certs": dict(
146 type="bool",
147 required=False,
148 default=True,
149 fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]),
150 ),
151 "vcenter_rest_log_file": dict(
152 type="str",
153 required=False,
154 fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]),
155 ),
156 "session_timeout": dict(
157 type="float",
158 required=False,
159 fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]),
160 ),
161 }
162
163 argument_spec["library"] = {"required": True, "type": "str"}
164 argument_spec["subscription"] = {"type": "str"}
165
166 return argument_spec
167
168
169 async def main():
170 required_if = list([])
171
172 module_args = prepare_argument_spec()
173 module = AnsibleModule(
174 argument_spec=module_args, required_if=required_if, supports_check_mode=True
175 )
176 if not module.params["vcenter_hostname"]:
177 module.fail_json("vcenter_hostname cannot be empty")
178 if not module.params["vcenter_username"]:
179 module.fail_json("vcenter_username cannot be empty")
180 if not module.params["vcenter_password"]:
181 module.fail_json("vcenter_password cannot be empty")
182 try:
183 session = await open_session(
184 vcenter_hostname=module.params["vcenter_hostname"],
185 vcenter_username=module.params["vcenter_username"],
186 vcenter_password=module.params["vcenter_password"],
187 validate_certs=module.params["vcenter_validate_certs"],
188 log_file=module.params["vcenter_rest_log_file"],
189 )
190 except Exception as err:
191 if hasattr(err, "get_message"):
192 module.fail_json(err.get_message())
193 else:
194 module.fail_json(str(err))
195 result = await entry_point(module, session)
196 module.exit_json(**result)
197
198
199 # template: info_list_and_get_module.j2
200 def build_url(params):
201 import yarl
202
203 if params.get("subscription"):
204 _in_query_parameters = PAYLOAD_FORMAT["get"]["query"].keys()
205 return yarl.URL(
206 (
207 "https://{vcenter_hostname}"
208 "/api/content/library/{library}/subscriptions/"
209 ).format(**params)
210 + params["subscription"]
211 + gen_args(params, _in_query_parameters),
212 encoded=True,
213 )
214 _in_query_parameters = PAYLOAD_FORMAT["list"]["query"].keys()
215 return yarl.URL(
216 (
217 "https://{vcenter_hostname}" "/api/content/library/{library}/subscriptions"
218 ).format(**params)
219 + gen_args(params, _in_query_parameters),
220 encoded=True,
221 )
222
223
224 async def entry_point(module, session):
225 url = build_url(module.params)
226 async with session.get(url, **session_timeout(module.params)) as resp:
227 _json = await resp.json()
228
229 if "value" not in _json: # 7.0.2+
230 _json = {"value": _json}
231
232 if module.params.get("subscription"):
233 _json["id"] = module.params.get("subscription")
234 elif module.params.get("label"): # TODO extend the list of filter
235 _json = await exists(module.params, session, str(url))
236 elif (
237 isinstance(_json["value"], list)
238 and len(_json["value"]) > 0
239 and isinstance(_json["value"][0], str)
240 ):
241 # this is a list of id, we fetch the details
242 full_device_list = await build_full_device_list(session, str(url), _json)
243 _json = {"value": [i["value"] for i in full_device_list]}
244
245 return await update_changed_flag(_json, resp.status, "get")
246
247
248 if __name__ == "__main__":
249 import asyncio
250
251 current_loop = asyncio.new_event_loop()
252 try:
253 asyncio.set_event_loop(current_loop)
254 current_loop.run_until_complete(main())
255 finally:
256 current_loop.close()