from .config import CrTplConfiguration
-__version__ = '0.2.2'
+__version__ = '0.3.1'
LOG = logging.getLogger(__name__)
pass
+# =============================================================================
+class TempVmExistsError(HandlerError):
+ """Special error class for the case, if the temporary VM is already existing."""
+
+ # -------------------------------------------------------------------------
+ def __init__(self, vm_name):
+
+ self.vm_name = vm_name
+
+ # -------------------------------------------------------------------------
+ def __str__(self):
+
+ msg = "The temporary VM {!r} is already existing, cannot continue.".format(self.vm_name)
+ return msg
+
+
# =============================================================================
class CannotConnectError(HandlerError):
"""Special error class for the case, it cannot connect
try:
self.ensure_vm_folder()
- #self.list_vms()
+ self.check_for_temp_tpl_vm()
finally:
LOG.debug("Disconnecting from vSphere host {h}:{p} ...".format(
h=self.config.vsphere_host, p=self.config.vsphere_port))
self.tpl_vm_folder = tpl_vm_folder
return True
-# if self.get_obj(content, [vim.Folder], self.config.folder):
-# LOG.info("vSphere folder {f!r} in data center {d!r} already exists.".format(
-# f=self.config.folder, d=self.config.dc))
-# return True
-
- #self._create_folder(dc.hostFolder, self.config.folder, 'host')
self._create_folder(dc.vmFolder, self.config.folder, 'VM')
self.tpl_vm_folder = self.get_tpl_folder()
return True
# -------------------------------------------------------------------------
- def list_vms(self):
+ def check_for_temp_tpl_vm(self):
+
+ vm = self.get_temp_tpl_vm()
+ if vm:
+ if self.verbose > 1:
+ LOG.debug("Temporary VM {!r} exists, raising TempVmExistsError.".format(
+ self.config.template_vm))
+ raise TempVmExistsError(self.config.template_vm)
+
+ LOG.debug("Temporary VM {!r} does not exists, will be created.".format(
+ self.config.template_vm))
+
+ # -------------------------------------------------------------------------
+ def get_temp_tpl_vm(self):
content = self.server_instance.RetrieveContent()
+ dc = self.get_obj(content, [vim.Datacenter], self.config.dc)
- for child in content.rootFolder.childEntity:
- if hasattr(child, 'vmFolder'):
- datacenter = child
- vmfolder = datacenter.vmFolder
- for vm in vmfolder.childEntity:
- self._printvminfo(vm)
+ for child in dc.vmFolder.childEntity:
+ vm = self._get_temp_tpl_vm(child)
+ if vm:
+ return vm
# -------------------------------------------------------------------------
- def _printvminfo(self, vm, depth=1):
+ def _get_temp_tpl_vm(self, child, depth=1):
- if hasattr(vm, 'childEntity'):
+ if hasattr(child, 'childEntity'):
if depth > self.max_depth:
- return
- for child in vm.childEntity:
- self._printvminfo(child, depth + 1)
- return
-
- if hasattr(vm, 'summary'):
- summary = vm.summary
- vm_type = 'Virtual Machine'
- if summary.config.template:
- vm_type = 'Template'
- LOG.info("Found {t} {n!r}.".format(t=vm_type, n=summary.config.name))
- return
+ return None
+ for sub_child in child.childEntity:
+ vm = self._get_temp_tpl_vm(sub_child, depth + 1)
+ if vm:
+ return vm
+ return None
+
+ if hasattr(child, 'summary'):
+ summary = child.summary
+ if summary.config.name == self.config.template_vm:
+ return child
+
+ return None
+
# =============================================================================