| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, Ansible Project |
| 5 |
# Copyright: (c) 2019, Pavan Bidkar <[email protected]> |
| 6 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: vmware_content_library_manager |
| 16 |
deprecated: |
| 17 |
removed_in: 7.0.0 |
| 18 |
why: This module has been moved to the L(new vmware.vmware collection,https://forum.ansible.com/t/5880) |
| 19 |
alternative: Use M(vmware.vmware.local_content_library) and M(vmware.vmware.subscribed_content_library) instead. |
| 20 |
short_description: Create, update and delete VMware content library |
| 21 |
description: |
| 22 |
- Module to manage VMware content Library |
| 23 |
- Content Library feature is introduced in vSphere 6.0 version, so this module is not supported in the earlier versions of vSphere. |
| 24 |
author: |
| 25 |
- Pavan Bidkar (@pgbidkar) |
| 26 |
requirements: |
| 27 |
- vSphere Automation SDK |
| 28 |
options: |
| 29 |
library_name: |
| 30 |
description: |
| 31 |
- The name of VMware content library to manage. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
library_description: |
| 35 |
description: |
| 36 |
- The content library description. |
| 37 |
- This is required only if O(state=present). |
| 38 |
- This parameter is ignored, when O(state=absent). |
| 39 |
- Process of updating content library only allows description change. |
| 40 |
type: str |
| 41 |
required: false |
| 42 |
library_type: |
| 43 |
description: |
| 44 |
- The content library type. |
| 45 |
- This is required only if O(state=resent). |
| 46 |
- This parameter is ignored, when O(state=absent). |
| 47 |
type: str |
| 48 |
required: false |
| 49 |
default: 'local' |
| 50 |
choices: [ 'local', 'subscribed' ] |
| 51 |
datastore_name: |
| 52 |
description: |
| 53 |
- Name of the datastore on which backing content library is created. |
| 54 |
- This is required if O(state=present). |
| 55 |
- This parameter is ignored, when O(state=absent). |
| 56 |
- Currently only datastore backing creation is supported. |
| 57 |
type: str |
| 58 |
required: false |
| 59 |
aliases: ['datastore'] |
| 60 |
subscription_url: |
| 61 |
description: |
| 62 |
- The url of the content library to subscribe to. |
| 63 |
- This is required if O(library_type=subscribed). |
| 64 |
- This parameter is ignored, when O(state=absent). |
| 65 |
type: str |
| 66 |
default: '' |
| 67 |
required: false |
| 68 |
ssl_thumbprint: |
| 69 |
description: |
| 70 |
- The SHA1 SSL thumbprint of the subscribed content library to subscribe to. |
| 71 |
- This is required if O(library_type=subscribed) and the library is https. |
| 72 |
- This parameter is ignored, when O(state=absent). |
| 73 |
- 'The information can be extracted using openssl using the following example: |
| 74 |
C(echo | openssl s_client -connect test-library.com:443 |& openssl x509 -fingerprint -noout)' |
| 75 |
type: str |
| 76 |
default: '' |
| 77 |
required: false |
| 78 |
update_on_demand: |
| 79 |
description: |
| 80 |
- Whether to download all content on demand. |
| 81 |
- If set to V(true), all content will be downloaded on demand. |
| 82 |
- If set to V(false) content will be downloaded ahead of time. |
| 83 |
- This is required if O(library_type=subscribed). |
| 84 |
- This parameter is ignored, when O(state=absent). |
| 85 |
type: bool |
| 86 |
default: false |
| 87 |
state: |
| 88 |
description: |
| 89 |
- The state of content library. |
| 90 |
- If set to V(present) and library does not exists, then content library is created. |
| 91 |
- If set to V(present) and library exists, then content library is updated. |
| 92 |
- If set to V(absent) and library exists, then content library is deleted. |
| 93 |
- If set to V(absent) and library does not exists, no action is taken. |
| 94 |
type: str |
| 95 |
required: false |
| 96 |
default: 'present' |
| 97 |
choices: [ 'present', 'absent' ] |
| 98 |
extends_documentation_fragment: |
| 99 |
- vmware.vmware.base_options |
| 100 |
- vmware.vmware.additional_rest_options |
| 101 |
|
| 102 |
''' |
| 103 |
EXAMPLES = r''' |
| 104 |
- name: Create Local Content Library |
| 105 |
community.vmware.vmware_content_library_manager: |
| 106 |
hostname: '{{ vcenter_hostname }}' |
| 107 |
username: '{{ vcenter_username }}' |
| 108 |
password: '{{ vcenter_password }}' |
| 109 |
library_name: test-content-lib |
| 110 |
library_description: 'Library with Datastore Backing' |
| 111 |
library_type: local |
| 112 |
datastore_name: datastore |
| 113 |
state: present |
| 114 |
delegate_to: localhost |
| 115 |
|
| 116 |
- name: Create Subscribed Content Library |
| 117 |
community.vmware.vmware_content_library_manager: |
| 118 |
hostname: '{{ vcenter_hostname }}' |
| 119 |
username: '{{ vcenter_username }}' |
| 120 |
password: '{{ vcenter_password }}' |
| 121 |
library_name: test-content-lib |
| 122 |
library_description: 'Subscribed Library with Datastore Backing' |
| 123 |
library_type: subscribed |
| 124 |
datastore_name: datastore |
| 125 |
subscription_url: 'https://library.url' |
| 126 |
ssl_thumbprint: 'aa:bb:cc:dd:ee:ff:gg:hh:ii:jj:kk:ll:mm:nn:oo:pp:qq:rr:ss:tt' |
| 127 |
update_on_demand: true |
| 128 |
state: present |
| 129 |
delegate_to: localhost |
| 130 |
|
| 131 |
- name: Update Content Library |
| 132 |
community.vmware.vmware_content_library_manager: |
| 133 |
hostname: '{{ vcenter_hostname }}' |
| 134 |
username: '{{ vcenter_username }}' |
| 135 |
password: '{{ vcenter_password }}' |
| 136 |
library_name: test-content-lib |
| 137 |
library_description: 'Library with Datastore Backing' |
| 138 |
state: present |
| 139 |
delegate_to: localhost |
| 140 |
|
| 141 |
- name: Delete Content Library |
| 142 |
community.vmware.vmware_content_library_manager: |
| 143 |
hostname: '{{ vcenter_hostname }}' |
| 144 |
username: '{{ vcenter_username }}' |
| 145 |
password: '{{ vcenter_password }}' |
| 146 |
library_name: test-content-lib |
| 147 |
state: absent |
| 148 |
delegate_to: localhost |
| 149 |
''' |
| 150 |
|
| 151 |
RETURN = r''' |
| 152 |
content_library_info: |
| 153 |
description: library creation success and library_id |
| 154 |
returned: on success |
| 155 |
type: dict |
| 156 |
sample: { |
| 157 |
"library_id": "d0b92fa9-7039-4f29-8e9c-0debfcb22b72", |
| 158 |
"library_description": 'Test description', |
| 159 |
"library_type": 'LOCAL', |
| 160 |
"msg": "Content Library 'demo-local-lib-4' created.", |
| 161 |
} |
| 162 |
''' |
| 163 |
|
| 164 |
import uuid |
| 165 |
from ansible.module_utils.basic import AnsibleModule |
| 166 |
from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient |
| 167 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 168 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import rest_compatible_argument_spec |
| 169 |
|
| 170 |
HAS_VAUTOMATION_PYTHON_SDK = False |
| 171 |
try: |
| 172 |
from com.vmware.content_client import LibraryModel |
| 173 |
from com.vmware.content.library_client import StorageBacking, SubscriptionInfo |
| 174 |
from com.vmware.vapi.std.errors_client import ResourceInaccessible |
| 175 |
HAS_VAUTOMATION_PYTHON_SDK = True |
| 176 |
except ImportError: |
| 177 |
pass |
| 178 |
|
| 179 |
|
| 180 |
class VmwareContentLibCreate(VmwareRestClient): |
| 181 |
def __init__(self, module): |
| 182 |
"""Constructor.""" |
| 183 |
super(VmwareContentLibCreate, self).__init__(module) |
| 184 |
self.content_service = self.api_client |
| 185 |
self.local_libraries = dict() |
| 186 |
# Track all existing library names, to block update/delete if duplicates exist |
| 187 |
self.existing_library_names = [] |
| 188 |
self.library_name = self.params.get('library_name') |
| 189 |
self.library_description = self.params.get('library_description') |
| 190 |
self.library_type = self.params.get('library_type') |
| 191 |
self.library_types = dict() |
| 192 |
self.subscription_url = self.params.get('subscription_url') |
| 193 |
self.ssl_thumbprint = self.params.get('ssl_thumbprint') |
| 194 |
self.datastore_name = self.params.get('datastore_name') |
| 195 |
self.update_on_demand = self.params.get('update_on_demand') |
| 196 |
self.library_types = { |
| 197 |
'local': self.content_service.content.LocalLibrary, |
| 198 |
'subscribed': self.content_service.content.SubscribedLibrary |
| 199 |
} |
| 200 |
|
| 201 |
# Import objects of both types to prevent duplicate names |
| 202 |
self.get_all_libraries(self.library_types['local']) |
| 203 |
self.get_all_libraries(self.library_types['subscribed']) |
| 204 |
|
| 205 |
# Set library type for create/update actions |
| 206 |
self.library_service = self.library_types[self.library_type] |
| 207 |
self.pyv = PyVmomi(module=module) |
| 208 |
|
| 209 |
def process_state(self): |
| 210 |
""" |
| 211 |
Manage states of Content Library |
| 212 |
""" |
| 213 |
self.desired_state = self.params.get('state') |
| 214 |
library_states = { |
| 215 |
'absent': { |
| 216 |
'present': self.state_destroy_library, |
| 217 |
'absent': self.state_exit_unchanged, |
| 218 |
}, |
| 219 |
'present': { |
| 220 |
'present': self.state_update_library, |
| 221 |
'absent': self.state_create_library, |
| 222 |
} |
| 223 |
} |
| 224 |
library_states[self.desired_state][self.check_content_library_status()]() |
| 225 |
|
| 226 |
def get_all_libraries(self, library_service): |
| 227 |
content_libs = library_service.list() |
| 228 |
if content_libs: |
| 229 |
for content_lib in content_libs: |
| 230 |
lib_details = library_service.get(content_lib) |
| 231 |
lib_dict = dict( |
| 232 |
lib_name=lib_details.name, |
| 233 |
lib_description=lib_details.description, |
| 234 |
lib_id=lib_details.id, |
| 235 |
lib_type=lib_details.type |
| 236 |
) |
| 237 |
if lib_details.type == "SUBSCRIBED": |
| 238 |
lib_dict["lib_sub_url"] = lib_details.subscription_info.subscription_url |
| 239 |
lib_dict["lib_sub_on_demand"] = lib_details.subscription_info.on_demand |
| 240 |
lib_dict["lib_sub_ssl_thumbprint"] = lib_details.subscription_info.ssl_thumbprint |
| 241 |
|
| 242 |
self.local_libraries[lib_details.name] = lib_dict |
| 243 |
self.existing_library_names.append(lib_details.name) |
| 244 |
|
| 245 |
def check_content_library_status(self): |
| 246 |
""" |
| 247 |
Check if Content Library exists or not |
| 248 |
Returns: 'present' if library found, else 'absent' |
| 249 |
|
| 250 |
""" |
| 251 |
ret = 'present' if self.library_name in self.local_libraries else 'absent' |
| 252 |
return ret |
| 253 |
|
| 254 |
def fail_when_duplicated(self): |
| 255 |
if self.existing_library_names.count(self.library_name) > 1: |
| 256 |
self.module.fail_json(msg="Operation cannot continue, library [%s] is not unique" % self.library_name) |
| 257 |
|
| 258 |
def state_exit_unchanged(self): |
| 259 |
""" |
| 260 |
Return unchanged state |
| 261 |
|
| 262 |
""" |
| 263 |
self.module.exit_json(changed=False) |
| 264 |
|
| 265 |
def set_subscription_spec(self): |
| 266 |
if "https:" in self.subscription_url and not self.ssl_thumbprint: |
| 267 |
self.module.fail_json(msg="While using HTTPS, a SSL thumbprint must be provided.") |
| 268 |
subscription_info = SubscriptionInfo() |
| 269 |
subscription_info.on_demand = self.update_on_demand |
| 270 |
subscription_info.automatic_sync_enabled = True |
| 271 |
subscription_info.subscription_url = self.subscription_url |
| 272 |
|
| 273 |
if "https:" in self.subscription_url: |
| 274 |
subscription_info.ssl_thumbprint = self.ssl_thumbprint |
| 275 |
return subscription_info |
| 276 |
|
| 277 |
def create_update(self, spec, library_id=None, update=False): |
| 278 |
""" |
| 279 |
Create or update call and exit cleanly if call completes |
| 280 |
""" |
| 281 |
if self.module.check_mode: |
| 282 |
action = 'would be updated' if update else 'would be created' |
| 283 |
else: |
| 284 |
try: |
| 285 |
if update: |
| 286 |
self.library_service.update(library_id, spec) |
| 287 |
action = "updated" |
| 288 |
else: |
| 289 |
library_id = self.library_service.create( |
| 290 |
create_spec=spec, |
| 291 |
client_token=str(uuid.uuid4()) |
| 292 |
) |
| 293 |
action = "created" |
| 294 |
except ResourceInaccessible as e: |
| 295 |
message = ("vCenter Failed to make connection to %s with exception: %s " |
| 296 |
"If using HTTPS, check that the SSL thumbprint is valid" % (self.subscription_url, str(e))) |
| 297 |
self.module.fail_json(msg=message) |
| 298 |
|
| 299 |
content_library_info = dict( |
| 300 |
msg="Content Library '%s' %s." % (spec.name, action), |
| 301 |
library_id=library_id, |
| 302 |
library_description=self.library_description, |
| 303 |
library_type=spec.type, |
| 304 |
) |
| 305 |
if spec.type == "SUBSCRIBED": |
| 306 |
content_library_info["library_subscription_url"] = spec.subscription_info.subscription_url |
| 307 |
content_library_info["library_subscription_on_demand"] = spec.subscription_info.on_demand |
| 308 |
content_library_info["library_subscription_ssl_thumbprint"] = spec.subscription_info.ssl_thumbprint |
| 309 |
self.module.exit_json( |
| 310 |
changed=True, |
| 311 |
content_library_info=content_library_info |
| 312 |
) |
| 313 |
|
| 314 |
def state_create_library(self): |
| 315 |
# Fail if no datastore is specified |
| 316 |
if not self.datastore_name: |
| 317 |
self.module.fail_json(msg="datastore_name must be specified for create operations") |
| 318 |
# Find the datastore by the given datastore name |
| 319 |
datastore_id = self.pyv.find_datastore_by_name(datastore_name=self.datastore_name) |
| 320 |
if not datastore_id: |
| 321 |
self.module.fail_json(msg="Failed to find the datastore %s" % self.datastore_name) |
| 322 |
self.datastore_id = datastore_id._moId |
| 323 |
# Build the storage backing for the library to be created |
| 324 |
storage_backings = [] |
| 325 |
storage_backing = StorageBacking(type=StorageBacking.Type.DATASTORE, datastore_id=self.datastore_id) |
| 326 |
storage_backings.append(storage_backing) |
| 327 |
|
| 328 |
# Build the specification for the library to be created |
| 329 |
create_spec = LibraryModel() |
| 330 |
create_spec.name = self.library_name |
| 331 |
create_spec.description = self.library_description |
| 332 |
self.library_types = {'local': create_spec.LibraryType.LOCAL, |
| 333 |
'subscribed': create_spec.LibraryType.SUBSCRIBED} |
| 334 |
create_spec.type = self.library_types[self.library_type] |
| 335 |
create_spec.storage_backings = storage_backings |
| 336 |
|
| 337 |
# Build subscribed specification |
| 338 |
if self.library_type == "subscribed": |
| 339 |
subscription_info = self.set_subscription_spec() |
| 340 |
subscription_info.authentication_method = SubscriptionInfo.AuthenticationMethod.NONE |
| 341 |
create_spec.subscription_info = subscription_info |
| 342 |
|
| 343 |
self.create_update(spec=create_spec) |
| 344 |
|
| 345 |
def state_update_library(self): |
| 346 |
""" |
| 347 |
Update Content Library |
| 348 |
|
| 349 |
""" |
| 350 |
self.fail_when_duplicated() |
| 351 |
changed = False |
| 352 |
library_id = self.local_libraries[self.library_name]['lib_id'] |
| 353 |
|
| 354 |
library_update_spec = LibraryModel() |
| 355 |
|
| 356 |
# Ensure library types are consistent |
| 357 |
existing_library_type = self.local_libraries[self.library_name]['lib_type'].lower() |
| 358 |
if existing_library_type != self.library_type: |
| 359 |
self.module.fail_json(msg="Library [%s] is of type %s, cannot be changed to %s" % |
| 360 |
(self.library_name, existing_library_type, self.library_type)) |
| 361 |
|
| 362 |
# Compare changeable subscribed attributes |
| 363 |
if self.library_type == "subscribed": |
| 364 |
existing_subscription_url = self.local_libraries[self.library_name]['lib_sub_url'] |
| 365 |
sub_url_changed = (existing_subscription_url != self.subscription_url) |
| 366 |
|
| 367 |
existing_on_demand = self.local_libraries[self.library_name]['lib_sub_on_demand'] |
| 368 |
sub_on_demand_changed = (existing_on_demand != self.update_on_demand) |
| 369 |
|
| 370 |
sub_ssl_thumbprint_changed = False |
| 371 |
if "https:" in self.subscription_url and self.ssl_thumbprint: |
| 372 |
existing_ssl_thumbprint = self.local_libraries[self.library_name]['lib_sub_ssl_thumbprint'] |
| 373 |
sub_ssl_thumbprint_changed = (existing_ssl_thumbprint != self.ssl_thumbprint) |
| 374 |
|
| 375 |
if sub_url_changed or sub_on_demand_changed or sub_ssl_thumbprint_changed: |
| 376 |
subscription_info = self.set_subscription_spec() |
| 377 |
library_update_spec.subscription_info = subscription_info |
| 378 |
changed = True |
| 379 |
|
| 380 |
# Compare description |
| 381 |
library_desc = self.local_libraries[self.library_name]['lib_description'] |
| 382 |
desired_lib_desc = self.params.get('library_description') |
| 383 |
if library_desc != desired_lib_desc: |
| 384 |
library_update_spec.description = desired_lib_desc |
| 385 |
changed = True |
| 386 |
|
| 387 |
if changed: |
| 388 |
library_update_spec.name = self.library_name |
| 389 |
self.create_update(spec=library_update_spec, library_id=library_id, update=True) |
| 390 |
|
| 391 |
content_library_info = dict(msg="Content Library %s is unchanged." % self.library_name, library_id=library_id) |
| 392 |
self.module.exit_json(changed=False, |
| 393 |
content_library_info=dict(msg=content_library_info, library_id=library_id)) |
| 394 |
|
| 395 |
def state_destroy_library(self): |
| 396 |
""" |
| 397 |
Delete Content Library |
| 398 |
|
| 399 |
""" |
| 400 |
self.fail_when_duplicated() |
| 401 |
library_id = self.local_libraries[self.library_name]['lib_id'] |
| 402 |
# Setup library service based on existing object type to allow library_type to unspecified |
| 403 |
library_service = self.library_types[self.local_libraries[self.library_name]['lib_type'].lower()] |
| 404 |
if self.module.check_mode: |
| 405 |
action = 'would be deleted' |
| 406 |
else: |
| 407 |
action = 'deleted' |
| 408 |
library_service.delete(library_id=library_id) |
| 409 |
self.module.exit_json( |
| 410 |
changed=True, |
| 411 |
content_library_info=dict( |
| 412 |
msg="Content Library '%s' %s." % (self.library_name, action), |
| 413 |
library_id=library_id |
| 414 |
) |
| 415 |
) |
| 416 |
|
| 417 |
|
| 418 |
def main(): |
| 419 |
argument_spec = rest_compatible_argument_spec() |
| 420 |
argument_spec.update( |
| 421 |
library_name=dict(type='str', required=True), |
| 422 |
library_description=dict(type='str', required=False), |
| 423 |
library_type=dict(type='str', required=False, choices=['local', 'subscribed'], default='local'), |
| 424 |
datastore_name=dict(type='str', required=False, aliases=['datastore']), |
| 425 |
state=dict(type='str', choices=['present', 'absent'], default='present', required=False), |
| 426 |
subscription_url=dict(type='str', default='', required=False), |
| 427 |
ssl_thumbprint=dict(type='str', default='', required=False), |
| 428 |
update_on_demand=dict(type='bool', default=False, required=False), |
| 429 |
) |
| 430 |
module = AnsibleModule( |
| 431 |
argument_spec=argument_spec, |
| 432 |
supports_check_mode=True, |
| 433 |
required_if=[ |
| 434 |
('library_type', 'subscribed', ['subscription_url']), |
| 435 |
], |
| 436 |
) |
| 437 |
|
| 438 |
vmware_contentlib_create = VmwareContentLibCreate(module) |
| 439 |
vmware_contentlib_create.process_state() |
| 440 |
|
| 441 |
|
| 442 |
if __name__ == '__main__': |
| 443 |
main() |