From ea3c89089daaa69687e77acece6b09502a9eeebb Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 23 Mar 2018 12:03:03 +0100 Subject: [PATCH] Simplified error handling of handler class, searching for the default cluster object for getting a resource pool --- lib/cr_vmware_tpl/app.py | 7 +++--- lib/cr_vmware_tpl/handler.py | 48 ++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/lib/cr_vmware_tpl/app.py b/lib/cr_vmware_tpl/app.py index 350be0e..6bc5f28 100644 --- a/lib/cr_vmware_tpl/app.py +++ b/lib/cr_vmware_tpl/app.py @@ -32,10 +32,9 @@ from .obj import PpBaseObject from .config import CrTplConfiguration -from .handler import TempVmExistsError, NoDatastoreFoundError, NetworkNotExistingError -from .handler import CrTplHandler +from .handler import ExpectedHandlerError, CrTplHandler -__version__ = '0.3.3' +__version__ = '0.3.4' LOG = logging.getLogger(__name__) @@ -433,7 +432,7 @@ class CrTplApplication(PpBaseObject): try: self.handler() - except (TempVmExistsError, NoDatastoreFoundError, NetworkNotExistingError) as e: + except ExpectedHandlerError as e: self.handle_error(str(e), "Temporary VM") self.exit(5) diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 681513e..13798ea 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -30,7 +30,7 @@ from .obj import PpBaseObject from .config import CrTplConfiguration -__version__ = '0.3.3' +__version__ = '0.4.1' LOG = logging.getLogger(__name__) @@ -43,7 +43,14 @@ class HandlerError(PpError, RuntimeError): # ============================================================================= -class TempVmExistsError(HandlerError): +class ExpectedHandlerError(HandlerError): + """Base class for all errors, which could be expected in application object + and displayed without stack trace.""" + + pass + +# ============================================================================= +class TempVmExistsError(ExpectedHandlerError): """Special error class for the case, if the temporary VM is already existing.""" # ------------------------------------------------------------------------- @@ -59,7 +66,7 @@ class TempVmExistsError(HandlerError): # ============================================================================= -class NoDatastoreFoundError(HandlerError): +class NoDatastoreFoundError(ExpectedHandlerError): """Special error class for the case, that no SAN based data store was with enogh free space was found.""" @@ -82,7 +89,7 @@ class NoDatastoreFoundError(HandlerError): # ============================================================================= -class NetworkNotExistingError(HandlerError): +class NetworkNotExistingError(ExpectedHandlerError): """Special error class for the case, if the expected network is not existing.""" # ------------------------------------------------------------------------- @@ -98,7 +105,7 @@ class NetworkNotExistingError(HandlerError): # ============================================================================= -class CannotConnectError(HandlerError): +class CannotConnectError(ExpectedHandlerError): """Special error class for the case, it cannot connect to the given vSphere server.""" @@ -144,6 +151,7 @@ class CrTplHandler(PpBaseObject): self.tpl_vm_folder = None self.tpl_data_store = None self.tpl_network = None + self.tpl_cluster = None if initialized: self.initialized = True @@ -179,6 +187,7 @@ class CrTplHandler(PpBaseObject): user=self.config.vsphere_user) try: + self.get_cluster() self.ensure_vm_folder() self.check_for_temp_tpl_vm() self.select_data_store() @@ -192,8 +201,7 @@ class CrTplHandler(PpBaseObject): def get_obj(self, content, vimtype, name): obj = None - container = content.viewManager.CreateContainerView( - content.rootFolder, vimtype, True) + container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True) for c in container.view: if c.name == name: obj = c @@ -201,6 +209,32 @@ class CrTplHandler(PpBaseObject): return obj + # ------------------------------------------------------------------------- + def get_obj_list(self, content, vimtype): + + result = [] + + container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True) + for c in container.view: + result.append(c) + + return result + + # ------------------------------------------------------------------------- + def get_cluster(self, content=None): + + if not content: + content = self.server_instance.RetrieveContent() + + cluster_list = self.get_obj_list(content, [vim.ClusterComputeResource]) + if not len(cluster_list): + raise HandlerError( + "Strange: did not found a cluster object in data center {!r}.".format( + self.config.dc)) + + self.tpl_cluster = cluster_list[0] + LOG.debug("Using cluster {!r}.".format(self.tpl_cluster.name)) + # ------------------------------------------------------------------------- def _create_folder(self, host_folder, folder_name, ftype=None): -- 2.39.5