vmware_guest: small refactor (#2955)

* use connecion method from module_utils
* use resource group related to host system

Addresses #2911
pull/18777/head
jctanner 8 years ago committed by Matt Clay
parent 1702937ab3
commit 10e50652b2

@ -156,14 +156,11 @@ HAS_PYVMOMI = False
try: try:
import pyVmomi import pyVmomi
from pyVmomi import vim from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
HAS_PYVMOMI = True HAS_PYVMOMI = True
except ImportError: except ImportError:
pass pass
import atexit
import os import os
import ssl
import string import string
import time import time
@ -185,24 +182,7 @@ class PyVmomiHelper(object):
self.foldermap = None self.foldermap = None
def smartconnect(self): def smartconnect(self):
kwargs = {'host': self.params['hostname'], self.content = connect_to_api(self.module)
'user': self.params['username'],
'pwd': self.params['password']}
if hasattr(ssl, 'SSLContext'):
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_NONE
kwargs['sslContext'] = context
# CONNECT TO THE SERVER
try:
self.si = SmartConnect(**kwargs)
except Exception:
err = get_exception()
self.module.fail_json(msg="Cannot connect to %s: %s" %
(kwargs['host'], err))
atexit.register(Disconnect, self.si)
self.content = self.si.RetrieveContent()
def _build_folder_tree(self, folder, tree={}, treepath=None): def _build_folder_tree(self, folder, tree={}, treepath=None):
@ -342,7 +322,7 @@ class PyVmomiHelper(object):
if searchpath: if searchpath:
# get all objects for this path ... # get all objects for this path ...
fObj = self.si.content.searchIndex.FindByInventoryPath(searchpath) fObj = self.content.searchIndex.FindByInventoryPath(searchpath)
if fObj: if fObj:
if isinstance(fObj, vim.Datacenter): if isinstance(fObj, vim.Datacenter):
fObj = fObj.vmFolder fObj = fObj.vmFolder
@ -354,6 +334,7 @@ class PyVmomiHelper(object):
break break
else: else:
# FIXME - this is unused if folder has a default value
vmList = get_all_objs(self.content, [vim.VirtualMachine]) vmList = get_all_objs(self.content, [vim.VirtualMachine])
if name_match: if name_match:
if name_match == 'first': if name_match == 'first':
@ -549,7 +530,6 @@ class PyVmomiHelper(object):
# FIXME: cluster or hostsystem ... ? # FIXME: cluster or hostsystem ... ?
#cluster = get_obj(self.content, [vim.ClusterComputeResource], self.params['esxi']['hostname']) #cluster = get_obj(self.content, [vim.ClusterComputeResource], self.params['esxi']['hostname'])
hostsystem = get_obj(self.content, [vim.HostSystem], self.params['esxi_hostname']) hostsystem = get_obj(self.content, [vim.HostSystem], self.params['esxi_hostname'])
resource_pools = get_all_objs(self.content, [vim.ResourcePool])
# set the destination datastore in the relocation spec # set the destination datastore in the relocation spec
if self.params['disk']: if self.params['disk']:
@ -563,11 +543,22 @@ class PyVmomiHelper(object):
# create the relocation spec # create the relocation spec
relospec = vim.vm.RelocateSpec() relospec = vim.vm.RelocateSpec()
relospec.host = hostsystem
relospec.datastore = datastore relospec.datastore = datastore
# fixme ... use the pool from the cluster if given # Find the associated resourcepool for the host system
relospec.pool = resource_pools[0] # * FIXME: find resourcepool for clusters too
relospec.host = hostsystem resource_pool = None
resource_pools = get_all_objs(self.content, [vim.ResourcePool])
for rp in resource_pools.items():
if rp[0].parent == hostsystem.parent:
resource_pool = rp[0]
break
if resource_pool:
relospec.pool = resource_pool
else:
self.module.fail_json(msg="Failed to find a resource group for %s" \
% hostsystem.name)
clonespec_kwargs = {} clonespec_kwargs = {}
clonespec_kwargs['location'] = relospec clonespec_kwargs['location'] = relospec
@ -667,7 +658,6 @@ class PyVmomiHelper(object):
time.sleep(sleep) time.sleep(sleep)
thispoll += 1 thispoll += 1
#import epdb; epdb.st()
return facts return facts
@ -827,7 +817,6 @@ class PyVmomiHelper(object):
return result return result
def get_obj(content, vimtype, name): def get_obj(content, vimtype, name):
""" """
Return an object by name, if name is None the Return an object by name, if name is None the
@ -849,18 +838,6 @@ def get_obj(content, vimtype, name):
return obj return obj
def get_all_objs(content, vimtype):
"""
Get all the vsphere objects associated with a given type
"""
obj = []
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for c in container.view:
obj.append(c)
container.Destroy()
return obj
def main(): def main():
vm = None vm = None
@ -951,6 +928,7 @@ def main():
poweron=poweron, poweron=poweron,
wait_for_ip=module.params['wait_for_ip_address'] wait_for_ip=module.params['wait_for_ip_address']
) )
result['changed'] = True
elif module.params['state'] == 'absent': elif module.params['state'] == 'absent':
result = {'changed': False, 'failed': False} result = {'changed': False, 'failed': False}
else: else:
@ -966,7 +944,7 @@ def main():
module.exit_json(**result) module.exit_json(**result)
# this is magic, see lib/ansible/module_common.py from ansible.module_utils.vmware import *
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save