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
from .config import CrTplConfiguration
-__version__ = '0.1.1'
+__version__ = '0.2.1'
LOG = logging.getLogger(__name__)
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):
"""
)
self.config = config
+ self.server_instance = None
if initialized:
self.initialized = True
"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
# =============================================================================