Remove redundant get_obj method (#26819)

Fix removes redundant get_obj method in favor of
find_datacenter_by_name API. Also, strict checking for
datacenter object.

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/18727/merge
Abhijeet Kasurde 7 years ago committed by jctanner
parent 065c80c081
commit 8b7a58c961

@ -67,6 +67,16 @@ RETURN = """
""" """
import os import os
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
from ansible.module_utils.vmware import (
connect_to_api,
gather_vm_facts,
get_all_objs,
compile_folder_path_for_object,
vmware_argument_spec,
find_datacenter_by_name
)
HAS_PYVMOMI = False HAS_PYVMOMI = False
try: try:
@ -77,11 +87,6 @@ try:
except ImportError: except ImportError:
pass pass
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native
from ansible.module_utils.vmware import (connect_to_api, gather_vm_facts, get_all_objs,
compile_folder_path_for_object, vmware_argument_spec)
class PyVmomiHelper(object): class PyVmomiHelper(object):
def __init__(self, module): def __init__(self, module):
@ -186,37 +191,31 @@ class PyVmomiHelper(object):
def getfolders(self): def getfolders(self):
if not self.datacenter: if not self.datacenter:
self.get_datacenter() self.datacenter = find_datacenter_by_name(self.content, self.params['datacenter'])
if self.datacenter is None:
self.module.fail_json(msg="Unable to find datacenter %(datacenter)s" % self.params)
self.folders = self._build_folder_tree(self.datacenter.vmFolder) self.folders = self._build_folder_tree(self.datacenter.vmFolder)
self._build_folder_map(self.folders) self._build_folder_map(self.folders)
def get_datacenter(self): @staticmethod
self.datacenter = get_obj( def compile_folder_path_for_object(vobj):
self.content, """ make a /vm/foo/bar/baz like folder path for an object """
[vim.Datacenter],
self.params['datacenter'] paths = []
) if isinstance(vobj, vim.Folder):
paths.append(vobj.name)
def get_obj(content, vimtype, name): thisobj = vobj
""" while hasattr(thisobj, 'parent'):
Return an object by name, if name is None the thisobj = thisobj.parent
first found object is returned if isinstance(thisobj, vim.Folder):
""" paths.append(thisobj.name)
obj = None paths.reverse()
container = content.viewManager.CreateContainerView( if paths[0] == 'Datacenters':
content.rootFolder, vimtype, True) paths.remove('Datacenters')
for c in container.view: return '/' + '/'.join(paths)
if name:
if c.name == name:
obj = c
break
else:
obj = c
break
container.Destroy()
return obj
def main(): def main():

Loading…
Cancel
Save