Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_vsan_hcl_db.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Hewlett Packard Enterprise Development LP
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12
13 DOCUMENTATION = r'''
14 ---
15 module: vmware_vsan_hcl_db
16 version_added: '3.5.0'
17 short_description: Manages the vSAN Hardware Compatibility List (HCL) database
18 description:
19 - Manages vSAN HCL db on vSphere
20 - DB file can be downloaded from https://partnerweb.vmware.com/service/vsan/all.json
21 author:
22 - Philipp Fruck (@p-fruck)
23 requirements:
24 - vSAN Management SDK, which needs to be downloaded from VMware and installed manually.
25 options:
26 source:
27 description:
28 - The path to the HCL db file
29 type: str
30 required: true
31 extends_documentation_fragment:
32 - vmware.vmware.base_options
33 '''
34
35 EXAMPLES = r'''
36 - name: Fetch HCL db file
37 ansible.builtin.get_url:
38 url: https://partnerweb.vmware.com/service/vsan/all.json
39 dest: hcl_db.json
40 force: true
41 delegate_to: localhost
42
43 - name: Upload HCL db file to vCenter
44 community.vmware.vmware_vsan_hcl_db:
45 hostname: "{{ vcenter_hostname }}"
46 username: "{{ vcenter_username }}"
47 password: "{{ vcenter_password }}"
48 source: hcl_db.json
49 delegate_to: localhost
50 '''
51
52 RETURN = r'''#
53 '''
54
55 import base64
56 import gzip
57 import traceback
58
59 VSANPYTHONSDK_IMP_ERR = None
60 try:
61 import vsanapiutils
62 HAS_VSANPYTHONSDK = True
63 except ImportError:
64 VSANPYTHONSDK_IMP_ERR = traceback.format_exc()
65 HAS_VSANPYTHONSDK = False
66
67 from ansible.module_utils.basic import AnsibleModule
68 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi
69 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
70
71
72 class VsanApi(PyVmomi):
73 def __init__(self, module):
74 super(VsanApi, self).__init__(module)
75
76 client_stub = self.si._GetStub()
77 ssl_context = client_stub.schemeArgs.get('context')
78 apiVersion = vsanapiutils.GetLatestVmodlVersion(module.params['hostname'])
79 vcMos = vsanapiutils.GetVsanVcMos(client_stub, context=ssl_context, version=apiVersion)
80 self.vsanClusterHealthSystem = vcMos['vsan-cluster-health-system']
81
82 def upload_hcl_db(self, content):
83 compressed = gzip.compress(content)
84 payload_b64 = base64.b64encode(compressed).decode('ascii')
85 self.vsanClusterHealthSystem.VsanVcUploadHclDb(db=payload_b64)
86
87
88 def main():
89 argument_spec = base_argument_spec()
90
91 argument_spec.update(dict(
92 source=dict(type='str', required=True)
93 ))
94
95 module = AnsibleModule(
96 argument_spec=argument_spec,
97 )
98
99 source = module.params['source']
100 api = VsanApi(module)
101
102 try:
103 with open(source, 'rb') as f:
104 api.upload_hcl_db(f.read())
105 except Exception as e:
106 module.fail_json(msg=str(e))
107
108 module.exit_json(changed=True)
109
110
111 if __name__ == '__main__':
112 main()