VMware: vcenter_license fixes (#50794)

* Updated document fragment
* Used PyVmomi Helper class
* Added check for vcenter server

Fixes: #50752

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2019-01-14 21:50:21 +05:30 committed by GitHub
parent 8dfdd9abf3
commit 85dd0a7e74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 26 deletions

View file

@ -43,7 +43,7 @@ notes:
an evaluation license only.
- The evaluation license (00000-00000-00000-00000-00000) is not listed
when unused.
extends_documentation_fragment: vmware.documentation
extends_documentation_fragment: vmware.vcenter_documentation
'''
EXAMPLES = r'''
@ -83,24 +83,27 @@ except ImportError:
HAS_PYVMOMI = False
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vmware import connect_to_api, vmware_argument_spec
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec
def find_key(licenses, license):
for item in licenses:
if item.licenseKey == license:
return item
return None
class VcenterLicenseMgr(PyVmomi):
def __init__(self, module):
super(VcenterLicenseMgr, self).__init__(module)
def find_key(self, licenses, license):
for item in licenses:
if item.licenseKey == license:
return item
return None
def list_keys(licenses):
keys = []
for item in licenses:
# Filter out evaluation license key
if item.used is None:
continue
keys.append(item.licenseKey)
return keys
def list_keys(self, licenses):
keys = []
for item in licenses:
# Filter out evaluation license key
if item.used is None:
continue
keys.append(item.licenseKey)
return keys
def main():
@ -132,13 +135,14 @@ def main():
diff=dict(),
)
if not HAS_PYVMOMI:
module.fail_json(msg='pyvmomi is required for this module')
pyv = VcenterLicenseMgr(module)
if not pyv.is_vcenter():
module.fail_json(msg="vcenter_license is meant for vCenter, hostname %s "
"is not vCenter server." % module.params.get('hostname'))
content = connect_to_api(module)
lm = content.licenseManager
lm = pyv.content.licenseManager
result['licenses'] = list_keys(lm.licenses)
result['licenses'] = pyv.list_keys(lm.licenses)
if module._diff:
result['diff']['before'] = '\n'.join(result['licenses']) + '\n'
@ -151,22 +155,22 @@ def main():
lm.AddLicense(license, labels)
# Automatically assign to current vCenter, if needed
key = find_key(lm.licenses, license)
if content.about.name in key.name:
key = pyv.find_key(lm.licenses, license)
if pyv.content.about.name in key.name:
try:
lam = lm.licenseAssignmentManager
lam.UpdateAssignedLicense(entity=content.about.instanceUuid, licenseKey=license)
lam.UpdateAssignedLicense(entity=pyv.content.about.instanceUuid, licenseKey=license)
except Exception:
module.warn('Could not assign "%s" (%s) to vCenter.' % (license, key.name))
result['licenses'] = list_keys(lm.licenses)
result['licenses'] = pyv.list_keys(lm.licenses)
if module._diff:
result['diff']['after'] = '\n'.join(result['licenses']) + '\n'
elif state == 'absent' and license in result['licenses']:
# Check if key is in use
key = find_key(lm.licenses, license)
key = pyv.find_key(lm.licenses, license)
if key.used > 0:
module.fail_json(msg='Cannot remove key "%s", still in use %s time(s).' % (license, key.used))
@ -175,7 +179,7 @@ def main():
result['licenses'].remove(license)
else:
lm.RemoveLicense(license)
result['licenses'] = list_keys(lm.licenses)
result['licenses'] = pyv.list_keys(lm.licenses)
if module._diff:
result['diff']['after'] = '\n'.join(result['licenses']) + '\n'

View file

@ -1,5 +1,6 @@
# Copyright: (c) 2016, Charles Paul <cpaul@ansible.com>
# Copyright: (c) 2018, Ansible Project
# Copyright: (c) 2019, Abhijeet Kasurde <akasurde@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
@ -44,3 +45,44 @@ options:
default: 443
version_added: 2.5
'''
# This doc fragment is specific to vcenter modules like vcenter_license
VCENTER_DOCUMENTATION = '''
options:
hostname:
description:
- The hostname or IP address of the vSphere vCenter server.
- If the value is not specified in the task, the value of environment variable C(VMWARE_HOST) will be used instead.
- Environment variable supported added in version 2.6.
type: str
username:
description:
- The username of the vSphere vCenter server.
- If the value is not specified in the task, the value of environment variable C(VMWARE_USER) will be used instead.
- Environment variable supported added in version 2.6.
type: str
aliases: [ admin, user ]
password:
description:
- The password of the vSphere vCenter server.
- If the value is not specified in the task, the value of environment variable C(VMWARE_PASSWORD) will be used instead.
- Environment variable supported added in version 2.6.
type: str
aliases: [ pass, pwd ]
validate_certs:
description:
- Allows connection when SSL certificates are not valid. Set to C(false) when certificates are not trusted.
- If the value is not specified in the task, the value of environment variable C(VMWARE_VALIDATE_CERTS) will be used instead.
- Environment variable supported added in version 2.6.
- If set to C(yes), please make sure Python >= 2.7.9 is installed on the given machine.
type: bool
default: 'yes'
port:
description:
- The port number of the vSphere vCenter server.
- If the value is not specified in the task, the value of environment variable C(VMWARE_PORT) will be used instead.
- Environment variable supported added in version 2.6.
type: int
default: 443
version_added: 2.5
'''