From: Frank Brehm Date: Thu, 22 Mar 2018 16:52:04 +0000 (+0100) Subject: Checking existence of given network. X-Git-Tag: 0.1.1~93 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=886362bd219babc2cc117f4ea45060ce07f11352;p=pixelpark%2Fcreate-vmware-tpl.git Checking existence of given network. --- diff --git a/lib/cr_vmware_tpl/app.py b/lib/cr_vmware_tpl/app.py index 0e6ed1c..350be0e 100644 --- a/lib/cr_vmware_tpl/app.py +++ b/lib/cr_vmware_tpl/app.py @@ -32,10 +32,10 @@ from .obj import PpBaseObject from .config import CrTplConfiguration -from .handler import TempVmExistsError, NoDatastoreFoundError +from .handler import TempVmExistsError, NoDatastoreFoundError, NetworkNotExistingError from .handler import CrTplHandler -__version__ = '0.3.2' +__version__ = '0.3.3' LOG = logging.getLogger(__name__) @@ -433,7 +433,7 @@ class CrTplApplication(PpBaseObject): try: self.handler() - except (TempVmExistsError, NoDatastoreFoundError) as e: + except (TempVmExistsError, NoDatastoreFoundError, NetworkNotExistingError) 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 aed1ee3..681513e 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.2' +__version__ = '0.3.3' LOG = logging.getLogger(__name__) @@ -81,6 +81,22 @@ class NoDatastoreFoundError(HandlerError): return msg +# ============================================================================= +class NetworkNotExistingError(HandlerError): + """Special error class for the case, if the expected network is not existing.""" + + # ------------------------------------------------------------------------- + def __init__(self, net_name): + + self.net_name = net_name + + # ------------------------------------------------------------------------- + def __str__(self): + + msg = "The network {!r} is not existing.".format(self.net_name) + return msg + + # ============================================================================= class CannotConnectError(HandlerError): """Special error class for the case, it cannot connect @@ -127,6 +143,7 @@ class CrTplHandler(PpBaseObject): self.server_instance = None self.tpl_vm_folder = None self.tpl_data_store = None + self.tpl_network = None if initialized: self.initialized = True @@ -165,6 +182,7 @@ class CrTplHandler(PpBaseObject): self.ensure_vm_folder() self.check_for_temp_tpl_vm() self.select_data_store() + self.check_network() finally: LOG.debug("Disconnecting from vSphere host {h}:{p} ...".format( h=self.config.vsphere_host, p=self.config.vsphere_port)) @@ -274,6 +292,43 @@ class CrTplHandler(PpBaseObject): return None + # ------------------------------------------------------------------------- + def check_network(self): + + content = self.server_instance.RetrieveContent() + dc = self.get_obj(content, [vim.Datacenter], self.config.dc) + + LOG.debug("Checking existence of network {!r} ...".format(self.config.network)) + + net = None + for child in dc.networkFolder.childEntity: + net = self._get_network(child) + if net: + break + + if not net: + raise NetworkNotExistingError(self.config.network) + LOG.debug("Found network {!r}.".format(self.config.network)) + self.tpl_network = net + + # ------------------------------------------------------------------------- + def _get_network(self, child, depth=1): + + if hasattr(child, 'childEntity'): + if depth > self.max_depth: + return None + for sub_child in child.childEntity: + net = self._get_network(sub_child, depth + 1) + if net: + return net + return None + + if isinstance(child, (vim.Network, vim.DistributedVirtualSwitch)): + if child.summary.name == self.config.network: + return child + + return None + # ------------------------------------------------------------------------- def select_data_store(self):