From 48048bf5ea19046d369d02160a0e5b878afbf31c Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 21 Mar 2018 11:36:36 +0100 Subject: [PATCH] Adding and using lib/cr_vmware_tpl/handler.py --- lib/cr_vmware_tpl/app.py | 10 ++++- lib/cr_vmware_tpl/handler.py | 86 ++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 lib/cr_vmware_tpl/handler.py diff --git a/lib/cr_vmware_tpl/app.py b/lib/cr_vmware_tpl/app.py index 67dfc67..1edc615 100644 --- a/lib/cr_vmware_tpl/app.py +++ b/lib/cr_vmware_tpl/app.py @@ -32,7 +32,9 @@ from .obj import PpBaseObject from .config import CrTplConfiguration -__version__ = '0.2.3' +from .handler import CrTplHandler + +__version__ = '0.3.1' LOG = logging.getLogger(__name__) @@ -157,6 +159,9 @@ class CrTplApplication(PpBaseObject): self.config = CrTplConfiguration( appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + self.handler = CrTplHandler( + appname=self.appname, verbose=self.verbose, base_dir=self.base_dir) + self.post_init() # ----------------------------------------------------------- @@ -400,6 +405,8 @@ class CrTplApplication(PpBaseObject): h=self.config.vsphere_host, u=self.config.vsphere_user) self.config.password = getpass.getpass(prompt=prompt) + self.handler.config = self.config + self.handler.initialized = True self.initialized = True # ------------------------------------------------------------------------- @@ -422,6 +429,7 @@ class CrTplApplication(PpBaseObject): """ LOG.info("Starting ...") + self.handler() # ------------------------------------------------------------------------- def __call__(self): diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py new file mode 100644 index 0000000..8db51e5 --- /dev/null +++ b/lib/cr_vmware_tpl/handler.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank.brehm@pixelpark.com +@copyright: © 2018 by Frank Brehm, Berlin +@summary: A handler module for creating the VMWare template +""" +from __future__ import absolute_import + +# Standard module +import sys +import os +import logging + +# Third party modules +import six + + +# Own modules +from .errors import FunctionNotImplementedError, PpError + +from .obj import PpBaseObject + +from .config import CrTplConfiguration + +__version__ = '0.1.1' +LOG = logging.getLogger(__name__) + + +# ============================================================================= +class HandlerError(PpError, RuntimeError): + """Base error class for all exceptions happened during + execution this handler""" + + pass + + +# ============================================================================= +class CrTplHandler(PpBaseObject): + """ + A handler class for creating a vSphere template. + """ + + # ------------------------------------------------------------------------- + def __init__( + self, appname=None, verbose=0, version=__version__, base_dir=None, + config=None, initialized=False): + + super(CrTplHandler, self).__init__( + appname=appname, + verbose=verbose, + version=version, + base_dir=base_dir, + initialized=False, + ) + + self.config = config + + if initialized: + self.initialized = True + + # ------------------------------------------------------------------------- + def __call__(self): + """Executing the underlying action.""" + + if not self.initialized: + raise HandlerError("{}-object not initialized.".format(self.__class__.__name__)) + + if not isinstance(self.config, CrTplConfiguration): + raise HandlerError(( + "self.config is not a CrTplConfiguration-instance, but a " + "{}-instance instead.").format(self.config.__class__.__name__)) + + LOG.debug("Starting ...") + + +# ============================================================================= + +if __name__ == "__main__": + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list -- 2.39.5