]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Checking existence of given network.
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 22 Mar 2018 16:52:04 +0000 (17:52 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 22 Mar 2018 16:52:04 +0000 (17:52 +0100)
lib/cr_vmware_tpl/app.py
lib/cr_vmware_tpl/handler.py

index 0e6ed1c5fa44d4104559752943be9f83951707ff..350be0ead9398f4f7c5cdcc18c4445bb50f238d1 100644 (file)
@@ -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)
 
index aed1ee3d15d4db49a7f3f124a59ca1a5d7e0d51c..681513efe19898e53931bc8d7a713fc57311b973 100644 (file)
@@ -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):