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__)
try:
self.handler()
- except (TempVmExistsError, NoDatastoreFoundError) as e:
+ except (TempVmExistsError, NoDatastoreFoundError, NetworkNotExistingError) as e:
self.handle_error(str(e), "Temporary VM")
self.exit(5)
from .config import CrTplConfiguration
-__version__ = '0.3.2'
+__version__ = '0.3.3'
LOG = logging.getLogger(__name__)
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
self.server_instance = None
self.tpl_vm_folder = None
self.tpl_data_store = None
+ self.tpl_network = None
if initialized:
self.initialized = True
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))
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):