| 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_info |
| 16 |
short_description: Gather information about VMWare Content Library |
| 17 |
description: |
| 18 |
- Module to list the content libraries. |
| 19 |
- Module to get information about specific content library. |
| 20 |
- Content Library feature is introduced in vSphere 6.0 version, so this module is not supported in the earlier versions of vSphere. |
| 21 |
author: |
| 22 |
- Pavan Bidkar (@pgbidkar) |
| 23 |
requirements: |
| 24 |
- vSphere Automation SDK |
| 25 |
options: |
| 26 |
library_id: |
| 27 |
description: |
| 28 |
- content library id for which details needs to be fetched. |
| 29 |
type: str |
| 30 |
required: false |
| 31 |
extends_documentation_fragment: |
| 32 |
- vmware.vmware.base_options |
| 33 |
- vmware.vmware.additional_rest_options |
| 34 |
|
| 35 |
''' |
| 36 |
|
| 37 |
EXAMPLES = r''' |
| 38 |
- name: Get List of Content Libraries |
| 39 |
community.vmware.vmware_content_library_info: |
| 40 |
hostname: '{{ vcenter_hostname }}' |
| 41 |
username: '{{ vcenter_username }}' |
| 42 |
password: '{{ vcenter_password }}' |
| 43 |
delegate_to: localhost |
| 44 |
|
| 45 |
- name: Get information about content library |
| 46 |
community.vmware.vmware_content_library_info: |
| 47 |
hostname: '{{ vcenter_hostname }}' |
| 48 |
username: '{{ vcenter_username }}' |
| 49 |
password: '{{ vcenter_password }}' |
| 50 |
library_id: '13b0f060-f4d3-4f84-b61f-0fe1b0c0a5a8' |
| 51 |
delegate_to: localhost |
| 52 |
''' |
| 53 |
|
| 54 |
RETURN = r''' |
| 55 |
content_lib_details: |
| 56 |
description: list of content library metadata |
| 57 |
returned: on success |
| 58 |
type: list |
| 59 |
sample: [ |
| 60 |
{ |
| 61 |
"library_creation_time": "2019-07-02T11:50:52.242000", |
| 62 |
"library_description": "new description", |
| 63 |
"library_id": "13b0f060-f4d3-4f84-b61f-0fe1b0c0a5a8", |
| 64 |
"library_name": "demo-local-lib", |
| 65 |
"library_publish_info": { |
| 66 |
"authentication_method": "NONE", |
| 67 |
"persist_json_enabled": false, |
| 68 |
"publish_url": null, |
| 69 |
"published": false, |
| 70 |
"user_name": null |
| 71 |
}, |
| 72 |
"library_server_guid": "0fd5813b-aac7-4b92-9fb7-f18f16565613", |
| 73 |
"library_type": "LOCAL", |
| 74 |
"library_version": "3" |
| 75 |
} |
| 76 |
] |
| 77 |
content_libs: |
| 78 |
description: list of content libraries |
| 79 |
returned: on success |
| 80 |
type: list |
| 81 |
sample: [ |
| 82 |
"ded9c4d5-0dcd-4837-b1d8-af7398511e33", |
| 83 |
"36b72549-14ed-4b5f-94cb-6213fecacc02" |
| 84 |
] |
| 85 |
''' |
| 86 |
|
| 87 |
from ansible.module_utils.basic import AnsibleModule |
| 88 |
from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient |
| 89 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import rest_compatible_argument_spec |
| 90 |
|
| 91 |
|
| 92 |
class VmwareContentLibInfo(VmwareRestClient): |
| 93 |
def __init__(self, module): |
| 94 |
"""Constructor.""" |
| 95 |
super(VmwareContentLibInfo, self).__init__(module) |
| 96 |
self.content_service = self.api_client |
| 97 |
self.local_content_libraries = self.content_service.content.LocalLibrary.list() |
| 98 |
if self.local_content_libraries is None: |
| 99 |
self.local_content_libraries = [] |
| 100 |
|
| 101 |
self.subscribed_content_libraries = self.content_service.content.SubscribedLibrary.list() |
| 102 |
if self.subscribed_content_libraries is None: |
| 103 |
self.subscribed_content_libraries = [] |
| 104 |
|
| 105 |
self.library_info = [] |
| 106 |
|
| 107 |
def get_all_content_libs(self): |
| 108 |
"""Method to retrieve List of content libraries.""" |
| 109 |
content_libraries = self.local_content_libraries + self.subscribed_content_libraries |
| 110 |
|
| 111 |
self.module.exit_json(changed=False, content_libs=content_libraries) |
| 112 |
|
| 113 |
def get_content_lib_details(self, library_id): |
| 114 |
"""Method to retrieve Details of contentlib with library_id""" |
| 115 |
lib_publish_info = None |
| 116 |
|
| 117 |
if library_id in self.local_content_libraries: |
| 118 |
try: |
| 119 |
lib_details = self.content_service.content.LocalLibrary.get(library_id) |
| 120 |
lib_publish_info = dict( |
| 121 |
persist_json_enabled=lib_details.publish_info.persist_json_enabled, |
| 122 |
authentication_method=lib_details.publish_info.authentication_method, |
| 123 |
publish_url=lib_details.publish_info.publish_url, |
| 124 |
published=lib_details.publish_info.published, |
| 125 |
user_name=lib_details.publish_info.user_name |
| 126 |
) |
| 127 |
except Exception as e: |
| 128 |
self.module.fail_json(exists=False, msg="%s" % self.get_error_message(e)) |
| 129 |
elif library_id in self.subscribed_content_libraries: |
| 130 |
try: |
| 131 |
lib_details = self.content_service.content.SubscribedLibrary.get(library_id) |
| 132 |
except Exception as e: |
| 133 |
self.module.fail_json(exists=False, msg="%s" % self.get_error_message(e)) |
| 134 |
else: |
| 135 |
self.module.fail_json(exists=False, msg="Library %s not found." % library_id) |
| 136 |
|
| 137 |
self.library_info.append( |
| 138 |
dict( |
| 139 |
library_name=lib_details.name, |
| 140 |
library_description=lib_details.description, |
| 141 |
library_id=lib_details.id, |
| 142 |
library_type=lib_details.type, |
| 143 |
library_creation_time=lib_details.creation_time, |
| 144 |
library_server_guid=lib_details.server_guid, |
| 145 |
library_version=lib_details.version, |
| 146 |
library_publish_info=lib_publish_info |
| 147 |
) |
| 148 |
) |
| 149 |
|
| 150 |
self.module.exit_json(exists=False, changed=False, content_lib_details=self.library_info) |
| 151 |
|
| 152 |
|
| 153 |
def main(): |
| 154 |
argument_spec = rest_compatible_argument_spec() |
| 155 |
argument_spec.update( |
| 156 |
library_id=dict(type='str', required=False), |
| 157 |
) |
| 158 |
module = AnsibleModule(argument_spec=argument_spec, |
| 159 |
supports_check_mode=True) |
| 160 |
|
| 161 |
vmware_contentlib_info = VmwareContentLibInfo(module) |
| 162 |
if module.params.get('library_id'): |
| 163 |
vmware_contentlib_info.get_content_lib_details(module.params['library_id']) |
| 164 |
else: |
| 165 |
vmware_contentlib_info.get_all_content_libs() |
| 166 |
|
| 167 |
|
| 168 |
if __name__ == '__main__': |
| 169 |
main() |