]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Splitting meethod __call__() of CreateTerraformHandler into two parts.
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 6 Sep 2021 14:11:38 +0000 (16:11 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 6 Sep 2021 14:11:38 +0000 (16:11 +0200)
lib/cr_tf/handler.py

index 09a34d11c59e90f6f1059403b490e6c7f1c3039b..ed45dc4564ac976be5306fe365b7204d4275fa22 100644 (file)
@@ -215,6 +215,9 @@ class CreateTerraformHandler(BaseHandler):
 
         self.vsphere_folders = []
 
+        self.vsphere_user = None
+        self.vsphere_password = None
+
         self.used_networks = {}
         self.used_dc_clusters = {}
         self.used_datastores = {}
@@ -489,8 +492,8 @@ class CreateTerraformHandler(BaseHandler):
             self.vsphere = None
 
     # -------------------------------------------------------------------------
-    def __call__(self, yaml_file):
-        """Executing the underlying action."""
+    def first_call(self, yaml_file):
+        """First steps until reading the YAML file."""
 
         if not self.initialized:
             raise HandlerError(_("{}-object not initialized.").format(self.__class__.__name__))
@@ -503,6 +506,25 @@ class CreateTerraformHandler(BaseHandler):
 
             self.exec_read_yaml(yaml_file)
 
+            print()
+            LOG.info(_("Initialising VSPhere handlers."))
+            self.init_vspheres(yaml_file)
+
+            return True
+
+        except AbortExecution as e:
+            LOG.warn(str(e))
+            return false
+
+    # -------------------------------------------------------------------------
+    def __call__(self, yaml_file):
+        """Executing the underlying action."""
+
+        if not self.initialized:
+            raise HandlerError(_("{}-object not initialized.").format(self.__class__.__name__))
+
+        try:
+
             if self.simulate:
                 print()
                 msg_a = _("Simulation mode")
@@ -531,8 +553,8 @@ class CreateTerraformHandler(BaseHandler):
             self.exec_pdns_zones()
 
             print()
-            LOG.info(_("Initialising VSPhere handlers."))
-            self.init_vspheres(yaml_file)
+            LOG.info(_("Cpllecting first information from vSPhere."))
+            self.test_vsphere_handlers()
             self.exec_collect_folders(yaml_file)
             self.assign_default_vmw_values()
 
@@ -737,23 +759,10 @@ class CreateTerraformHandler(BaseHandler):
                 msg = _("VSPhere {!r} not defined in configuration.").format(vname)
                 raise ExpectedHandlerError(msg)
 
-            vsphere_user = self.config.vsphere[vname].user
-            vsphere_password = self.config.vsphere[vname].password
-
-            if not vsphere_user:
-                msg = '\n' + _("Please input the {}:").format(self.colored(
-                        _('vSphere user name'), 'AQUA'))
-                print(msg)
-                vsphere_user = input(_('vSphere user name') + ': ')
-                if vsphere_user == '':
-                    msg = _("No {} given.").format(_('vSphere user name'))
-                    raise ExpectedHandlerError(msg)
-
-            if not vsphere_password:
-                prompt = _("User password")
-                item = _('Password for user {u!r} of vSphere {n} on {h!r}').format(
-                        u=vsphere_user, n=vname, h=self.config.vsphere[vname].host)
-                vsphere_password = self.get_secret(prompt=prompt, item_name=item)
+            if not self.vsphere_user and self.config.vsphere[vname].user:
+                self.vsphere_user = self.config.vsphere[vname].user
+            if not self.vsphere_password and self.config.vsphere[vname].password:
+                self.vsphere_password = self.config.vsphere[vname].password
 
             try:
                 params = {
@@ -762,8 +771,8 @@ class CreateTerraformHandler(BaseHandler):
                     'base_dir': self.base_dir,
                     'host': self.config.vsphere[vname].host,
                     'port': self.config.vsphere[vname].port,
-                    'user': vsphere_user,
-                    'password': vsphere_password,
+                    'user': self.vsphere_user,
+                    'password': self.vsphere_password,
                     'dc': self.config.vsphere[vname].dc,
                     'simulate': self.simulate,
                     'force': self.force,
@@ -779,12 +788,25 @@ class CreateTerraformHandler(BaseHandler):
                     LOG.debug(msg)
 
                 vsphere = VsphereServer(**params)
+                self.vsphere[vname] = vsphere
+
+            except VSphereExpectedError as e:
+                raise ExpectedHandlerError(str(e))
+
+    # -------------------------------------------------------------------------ยท
+    def test_vsphere_handlers(self):
+
+        for vname in self.vsphere.keys():
+
+            try:
+
+                vsphere = self.vsphere[vname]
+
                 vsphere.get_about()
                 if self.verbose > 2:
                     msg = _("Created {}-object:").format('VsphereServer')
                     msg += '\n' + pp(vsphere.as_dict())
                     LOG.debug(msg)
-                self.vsphere[vname] = vsphere
 
             except VSphereExpectedError as e:
                 raise ExpectedHandlerError(str(e))