From: Frank Brehm Date: Wed, 21 Mar 2018 11:51:21 +0000 (+0100) Subject: Ensuring existence of template folder. X-Git-Tag: 0.1.1~99 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=aa8559e6e21db4e6609a8b4c713420c1c79dd7c3;p=pixelpark%2Fcreate-vmware-tpl.git Ensuring existence of template folder. --- diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 8db51e5..bb36a3f 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -12,10 +12,14 @@ from __future__ import absolute_import import sys import os import logging +import ssl # Third party modules import six +from pyVmomi import vim + +from pyVim.connect import SmartConnect, Disconnect # Own modules from .errors import FunctionNotImplementedError, PpError @@ -24,7 +28,7 @@ from .obj import PpBaseObject from .config import CrTplConfiguration -__version__ = '0.1.1' +__version__ = '0.2.1' LOG = logging.getLogger(__name__) @@ -36,6 +40,26 @@ class HandlerError(PpError, RuntimeError): pass +# ============================================================================= +class CannotConnectError(HandlerError): + """Special error class for the case, it cannot connect + to the given vSphere server.""" + + # ------------------------------------------------------------------------- + def __init__(self, host, port, user): + + self.host = host + self.port = port + self.user = user + + # ------------------------------------------------------------------------- + def __str__(self): + + msg = "Could not connect to the vSphere host {h}:{p} as user {u!r}.".format( + h=self.host, p=self.port, u=self.user) + return msg + + # ============================================================================= class CrTplHandler(PpBaseObject): """ @@ -56,6 +80,7 @@ class CrTplHandler(PpBaseObject): ) self.config = config + self.server_instance = None if initialized: self.initialized = True @@ -72,8 +97,69 @@ class CrTplHandler(PpBaseObject): "self.config is not a CrTplConfiguration-instance, but a " "{}-instance instead.").format(self.config.__class__.__name__)) - LOG.debug("Starting ...") + LOG.debug("Connecting to vSphere host {h}:{p} as {u!r} ...".format( + h=self.config.vsphere_host, p=self.config.vsphere_port, + u=self.config.vsphere_user)) + + ssl_context = None + if hasattr(ssl, '_create_unverified_context'): + ssl_context = ssl._create_unverified_context() + + self.server_instance = SmartConnect( + host=self.config.vsphere_host, port=self.config.vsphere_port, + user=self.config.vsphere_user, pwd=self.config.password, + sslContext=ssl_context) + + if not self.server_instance: + raise CannotConnectError( + host=self.config.vsphere_host, port=self.config.vsphere_port, + user=self.config.vsphere_user) + + try: + self.create_folder() + finally: + Disconnect(self.server_instance) + + # ------------------------------------------------------------------------- + def get_obj(self, content, vimtype, name): + + obj = None + container = content.viewManager.CreateContainerView( + content.rootFolder, vimtype, True) + for c in container.view: + if c.name == name: + obj = c + break + + return obj + + # ------------------------------------------------------------------------- + def _create_folder(self, host_folder, folder_name, ftype=None): + + if not ftype: + ftype = host_folder.__class__.__name__ + + LOG.debug("Creating {t}-folder {f!r} in data center {d!r} ...".format( + t=ftype, f=folder_name, d=self.config.dc)) + host_folder.CreateFolder(folder_name) + LOG.info("Successfully created the {t}-folder {f!r} in data center {d!r}.".format( + t=ftype, f=folder_name, d=self.config.dc)) + + # ------------------------------------------------------------------------- + def create_folder(self): + + content = self.server_instance.RetrieveContent() + dc = self.get_obj(content, [vim.Datacenter], self.config.dc) + + 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') + return True # =============================================================================