| 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_configuration |
| 12 |
short_description: Updates the configuration |
| 13 |
description: Updates the configuration. The update is incremental. Any field in the |
| 14 |
ConfigurationModel structure that is unset will not be modified. Note that this |
| 15 |
update operation doesn't guarantee an atomic change of all the properties. In |
| 16 |
the case of a system crash or failure, some of the properties could be left unchanged |
| 17 |
while others may be updated. |
| 18 |
options: |
| 19 |
automatic_sync_enabled: |
| 20 |
description: |
| 21 |
- 'Whether automatic synchronization is enabled. ' |
| 22 |
- ' When automatic synchronization is enabled, the Content Library Service |
| 23 |
will automatically synchronize all subscribed libraries on a daily basis. |
| 24 |
Subscribed libraries with the SubscriptionInfo.automatic-sync-enabled |
| 25 |
flag turned on will be synchronized every hour between ConfigurationModel.automatic-sync-start-hour |
| 26 |
and ConfigurationModel.automatic-sync-stop-hour.' |
| 27 |
- '' |
| 28 |
- This field is not used for the create operation. It will always be present |
| 29 |
in the result of the get or list operations. It is optional for the update |
| 30 |
operation. |
| 31 |
type: bool |
| 32 |
automatic_sync_start_hour: |
| 33 |
description: |
| 34 |
- The hour at which the automatic synchronization will start. This value is |
| 35 |
between 0 (midnight) and 23 inclusive. |
| 36 |
- This field is not used for the create operation. It will always be present |
| 37 |
in the result of the get or list operations. It is optional for the update |
| 38 |
operation. |
| 39 |
type: int |
| 40 |
automatic_sync_stop_hour: |
| 41 |
description: |
| 42 |
- The hour at which the automatic synchronization will stop. Any active synchronization |
| 43 |
operation will continue to run, however no new synchronization operations |
| 44 |
will be triggered after the stop hour. This value is between 0 (midnight) |
| 45 |
and 23 inclusive. |
| 46 |
- This field is not used for the create operation. It will always be present |
| 47 |
in the result of the get or list operations. It is optional for the update |
| 48 |
operation. |
| 49 |
type: int |
| 50 |
maximum_concurrent_item_syncs: |
| 51 |
description: |
| 52 |
- 'The maximum allowed number of library items to synchronize concurrently |
| 53 |
from remote libraries. This must be a positive number. The service may |
| 54 |
not be able to guarantee the requested concurrency if there is no available |
| 55 |
capacity. ' |
| 56 |
- ' This setting is global across all subscribed libraries.' |
| 57 |
- '' |
| 58 |
- This field is not used for the create operation. It will always be present |
| 59 |
in the result of the get or list operations. It is optional for the update |
| 60 |
operation. |
| 61 |
type: int |
| 62 |
session_timeout: |
| 63 |
description: |
| 64 |
- 'Timeout settings for client session. ' |
| 65 |
- 'The maximal number of seconds for the whole operation including connection |
| 66 |
establishment, request sending and response. ' |
| 67 |
- The default value is 300s. |
| 68 |
type: float |
| 69 |
version_added: 2.1.0 |
| 70 |
state: |
| 71 |
choices: |
| 72 |
- present |
| 73 |
default: present |
| 74 |
description: [] |
| 75 |
type: str |
| 76 |
vcenter_hostname: |
| 77 |
description: |
| 78 |
- The hostname or IP address of the vSphere vCenter |
| 79 |
- If the value is not specified in the task, the value of environment variable |
| 80 |
C(VMWARE_HOST) will be used instead. |
| 81 |
required: true |
| 82 |
type: str |
| 83 |
vcenter_password: |
| 84 |
description: |
| 85 |
- The vSphere vCenter password |
| 86 |
- If the value is not specified in the task, the value of environment variable |
| 87 |
C(VMWARE_PASSWORD) will be used instead. |
| 88 |
required: true |
| 89 |
type: str |
| 90 |
vcenter_rest_log_file: |
| 91 |
description: |
| 92 |
- 'You can use this optional parameter to set the location of a log file. ' |
| 93 |
- 'This file will be used to record the HTTP REST interaction. ' |
| 94 |
- 'The file will be stored on the host that runs the module. ' |
| 95 |
- 'If the value is not specified in the task, the value of ' |
| 96 |
- environment variable C(VMWARE_REST_LOG_FILE) will be used instead. |
| 97 |
type: str |
| 98 |
vcenter_username: |
| 99 |
description: |
| 100 |
- The vSphere vCenter username |
| 101 |
- If the value is not specified in the task, the value of environment variable |
| 102 |
C(VMWARE_USER) will be used instead. |
| 103 |
required: true |
| 104 |
type: str |
| 105 |
vcenter_validate_certs: |
| 106 |
default: true |
| 107 |
description: |
| 108 |
- Allows connection when SSL certificates are not valid. Set to C(false) when |
| 109 |
certificates are not trusted. |
| 110 |
- If the value is not specified in the task, the value of environment variable |
| 111 |
C(VMWARE_VALIDATE_CERTS) will be used instead. |
| 112 |
type: bool |
| 113 |
author: |
| 114 |
- Ansible Cloud Team (@ansible-collections) |
| 115 |
version_added: 2.0.0 |
| 116 |
requirements: |
| 117 |
- vSphere 7.0.3 or greater |
| 118 |
- python >= 3.6 |
| 119 |
- aiohttp |
| 120 |
notes: |
| 121 |
- Tested on vSphere 7.0.3 |
| 122 |
""" |
| 123 |
|
| 124 |
EXAMPLES = r""" |
| 125 |
- name: Turn on the autmatic syncrhronization |
| 126 |
vmware.vmware_rest.content_configuration: |
| 127 |
automatic_sync_enabled: true |
| 128 |
""" |
| 129 |
RETURN = r""" |
| 130 |
# content generated by the update_return_section callback# task: Turn on the autmatic syncrhronization |
| 131 |
id: |
| 132 |
description: moid of the resource |
| 133 |
returned: On success |
| 134 |
sample: null |
| 135 |
type: dict |
| 136 |
value: |
| 137 |
description: Turn on the autmatic syncrhronization |
| 138 |
returned: On success |
| 139 |
sample: |
| 140 |
automatic_sync_enabled: 1 |
| 141 |
automatic_sync_start_hour: 20 |
| 142 |
automatic_sync_stop_hour: 7 |
| 143 |
maximum_concurrent_item_syncs: 5 |
| 144 |
type: dict |
| 145 |
""" |
| 146 |
|
| 147 |
|
| 148 |
# This structure describes the format of the data expected by the end-points |
| 149 |
PAYLOAD_FORMAT = { |
| 150 |
"update": { |
| 151 |
"query": {}, |
| 152 |
"body": { |
| 153 |
"automatic_sync_enabled": "automatic_sync_enabled", |
| 154 |
"automatic_sync_start_hour": "automatic_sync_start_hour", |
| 155 |
"automatic_sync_stop_hour": "automatic_sync_stop_hour", |
| 156 |
"maximum_concurrent_item_syncs": "maximum_concurrent_item_syncs", |
| 157 |
}, |
| 158 |
"path": {}, |
| 159 |
} |
| 160 |
} # pylint: disable=line-too-long |
| 161 |
|
| 162 |
from ansible.module_utils.basic import env_fallback |
| 163 |
import os |
| 164 |
|
| 165 |
if os.getenv("VMWARE_ENABLE_TURBO", False): |
| 166 |
try: |
| 167 |
from ansible_collections.cloud.common.plugins.module_utils.turbo.module import ( |
| 168 |
AnsibleTurboModule as AnsibleModule, |
| 169 |
) |
| 170 |
|
| 171 |
AnsibleModule.collection_name = "vmware.vmware_rest" |
| 172 |
except ImportError: |
| 173 |
from ansible.module_utils.basic import AnsibleModule |
| 174 |
else: |
| 175 |
from ansible.module_utils.basic import AnsibleModule |
| 176 |
|
| 177 |
from ansible_collections.vmware.vmware_rest.plugins.module_utils.vmware_rest import ( |
| 178 |
open_session, |
| 179 |
prepare_payload, |
| 180 |
session_timeout, |
| 181 |
update_changed_flag, |
| 182 |
) |
| 183 |
|
| 184 |
|
| 185 |
def prepare_argument_spec(): |
| 186 |
argument_spec = { |
| 187 |
"vcenter_hostname": dict( |
| 188 |
type="str", |
| 189 |
required=True, |
| 190 |
fallback=(env_fallback, ["VMWARE_HOST"]), |
| 191 |
), |
| 192 |
"vcenter_username": dict( |
| 193 |
type="str", |
| 194 |
required=True, |
| 195 |
fallback=(env_fallback, ["VMWARE_USER"]), |
| 196 |
), |
| 197 |
"vcenter_password": dict( |
| 198 |
type="str", |
| 199 |
required=True, |
| 200 |
no_log=True, |
| 201 |
fallback=(env_fallback, ["VMWARE_PASSWORD"]), |
| 202 |
), |
| 203 |
"vcenter_validate_certs": dict( |
| 204 |
type="bool", |
| 205 |
required=False, |
| 206 |
default=True, |
| 207 |
fallback=(env_fallback, ["VMWARE_VALIDATE_CERTS"]), |
| 208 |
), |
| 209 |
"vcenter_rest_log_file": dict( |
| 210 |
type="str", |
| 211 |
required=False, |
| 212 |
fallback=(env_fallback, ["VMWARE_REST_LOG_FILE"]), |
| 213 |
), |
| 214 |
"session_timeout": dict( |
| 215 |
type="float", |
| 216 |
required=False, |
| 217 |
fallback=(env_fallback, ["VMWARE_SESSION_TIMEOUT"]), |
| 218 |
), |
| 219 |
} |
| 220 |
|
| 221 |
argument_spec["automatic_sync_enabled"] = {"type": "bool"} |
| 222 |
argument_spec["automatic_sync_start_hour"] = {"type": "int"} |
| 223 |
argument_spec["automatic_sync_stop_hour"] = {"type": "int"} |
| 224 |
argument_spec["maximum_concurrent_item_syncs"] = {"type": "int"} |
| 225 |
argument_spec["state"] = { |
| 226 |
"type": "str", |
| 227 |
"choices": ["present"], |
| 228 |
"default": "present", |
| 229 |
} |
| 230 |
|
| 231 |
return argument_spec |
| 232 |
|
| 233 |
|
| 234 |
async def main(): |
| 235 |
required_if = list([]) |
| 236 |
|
| 237 |
module_args = prepare_argument_spec() |
| 238 |
module = AnsibleModule( |
| 239 |
argument_spec=module_args, required_if=required_if, supports_check_mode=False |
| 240 |
) |
| 241 |
if not module.params["vcenter_hostname"]: |
| 242 |
module.fail_json("vcenter_hostname cannot be empty") |
| 243 |
if not module.params["vcenter_username"]: |
| 244 |
module.fail_json("vcenter_username cannot be empty") |
| 245 |
if not module.params["vcenter_password"]: |
| 246 |
module.fail_json("vcenter_password cannot be empty") |
| 247 |
try: |
| 248 |
session = await open_session( |
| 249 |
vcenter_hostname=module.params["vcenter_hostname"], |
| 250 |
vcenter_username=module.params["vcenter_username"], |
| 251 |
vcenter_password=module.params["vcenter_password"], |
| 252 |
validate_certs=module.params["vcenter_validate_certs"], |
| 253 |
log_file=module.params["vcenter_rest_log_file"], |
| 254 |
) |
| 255 |
except Exception as err: |
| 256 |
if hasattr(err, "get_message"): |
| 257 |
module.fail_json(err.get_message()) |
| 258 |
else: |
| 259 |
module.fail_json(str(err)) |
| 260 |
result = await entry_point(module, session) |
| 261 |
module.exit_json(**result) |
| 262 |
|
| 263 |
|
| 264 |
# template: default_module.j2 |
| 265 |
def build_url(params): |
| 266 |
return ("https://{vcenter_hostname}" "/api/content/configuration").format(**params) |
| 267 |
|
| 268 |
|
| 269 |
async def entry_point(module, session): |
| 270 |
|
| 271 |
if module.params["state"] == "present": |
| 272 |
if "_create" in globals(): |
| 273 |
operation = "create" |
| 274 |
else: |
| 275 |
operation = "update" |
| 276 |
elif module.params["state"] == "absent": |
| 277 |
operation = "delete" |
| 278 |
else: |
| 279 |
operation = module.params["state"] |
| 280 |
|
| 281 |
func = globals()["_" + operation] |
| 282 |
|
| 283 |
return await func(module.params, session) |
| 284 |
|
| 285 |
|
| 286 |
async def _update(params, session): |
| 287 |
payload = prepare_payload(params, PAYLOAD_FORMAT["update"]) |
| 288 |
_url = ("https://{vcenter_hostname}" "/api/content/configuration").format(**params) |
| 289 |
async with session.get(_url, **session_timeout(params)) as resp: |
| 290 |
_json = await resp.json() |
| 291 |
if "value" in _json: |
| 292 |
value = _json["value"] |
| 293 |
else: # 7.0.2 and greater |
| 294 |
value = _json |
| 295 |
for k, v in value.items(): |
| 296 |
if k in payload: |
| 297 |
if isinstance(payload[k], dict) and isinstance(v, dict): |
| 298 |
to_delete = True |
| 299 |
for _k in list(payload[k].keys()): |
| 300 |
if payload[k][_k] != v.get(_k): |
| 301 |
to_delete = False |
| 302 |
if to_delete: |
| 303 |
del payload[k] |
| 304 |
elif payload[k] == v: |
| 305 |
del payload[k] |
| 306 |
elif payload[k] == {}: |
| 307 |
del payload[k] |
| 308 |
|
| 309 |
if payload == {} or payload == {"spec": {}}: |
| 310 |
# Nothing has changed |
| 311 |
if "value" not in _json: # 7.0.2 |
| 312 |
_json = {"value": _json} |
| 313 |
_json["id"] = params.get("None") |
| 314 |
return await update_changed_flag(_json, resp.status, "get") |
| 315 |
async with session.patch(_url, json=payload, **session_timeout(params)) as resp: |
| 316 |
try: |
| 317 |
if resp.headers["Content-Type"] == "application/json": |
| 318 |
_json = await resp.json() |
| 319 |
except KeyError: |
| 320 |
_json = {} |
| 321 |
if "value" not in _json: # 7.0.2 |
| 322 |
_json = {"value": _json} |
| 323 |
|
| 324 |
# e.g: content_configuration |
| 325 |
if not _json and resp.status == 204: |
| 326 |
async with session.get(_url, **session_timeout(params)) as resp_get: |
| 327 |
_json_get = await resp_get.json() |
| 328 |
if _json_get: |
| 329 |
_json = _json_get |
| 330 |
|
| 331 |
_json["id"] = params.get("None") |
| 332 |
return await update_changed_flag(_json, resp.status, "update") |
| 333 |
|
| 334 |
|
| 335 |
if __name__ == "__main__": |
| 336 |
import asyncio |
| 337 |
|
| 338 |
current_loop = asyncio.new_event_loop() |
| 339 |
try: |
| 340 |
asyncio.set_event_loop(current_loop) |
| 341 |
current_loop.run_until_complete(main()) |
| 342 |
finally: |
| 343 |
current_loop.close() |