]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Improving validation of given clusters
authorFrank Brehm <frank@brehm-online.com>
Fri, 18 Oct 2019 08:04:00 +0000 (10:04 +0200)
committerFrank Brehm <frank@brehm-online.com>
Fri, 18 Oct 2019 08:04:00 +0000 (10:04 +0200)
lib/cr_tf/handler.py

index 6c4e5aeb8b282f162107441591b57a809fd0efc3..c2f03ea206bd36cceb91c47299bb268f69b56635 100644 (file)
@@ -27,6 +27,8 @@ from subprocess import PIPE
 
 from distutils.version import LooseVersion
 
+from operator import attrgetter
+
 # Third party modules
 import pytz
 import yaml
@@ -59,7 +61,7 @@ from .terraform.disk import TerraformDisk
 
 from .xlate import XLATOR
 
-__version__ = '2.9.5'
+__version__ = '2.9.6'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -242,8 +244,9 @@ class CreateTerraformHandler(BaseHandler):
         if self.pdns:
             self.pdns.simulate = self.simulate
 
-        if self.vsphere:
-            self.vsphere.simulate = self.simulate
+        for vsphere_name in self.vsphere.keys():
+            if self.vsphere[vsphere_name]:
+                self.vsphere[vsphere_name].simulate = self.simulate
 
     # -----------------------------------------------------------
     @property
@@ -782,7 +785,7 @@ class CreateTerraformHandler(BaseHandler):
                 self.eval_errors).format(n=self.eval_errors)
             raise ExpectedHandlerError(msg)
 
-        self.validate_vms()
+        self.validate_vms()
 
         LOG.info(_("Finished step {!r}.").format('validate-yaml'))
         if self.stop_at_step == 'validate-yaml':
@@ -1154,27 +1157,37 @@ class CreateTerraformHandler(BaseHandler):
 
         for vm in self.vms:
 
+            vname = vm.vsphere
+            if vname not in clusters:
+                clusters[vname] = {}
+
             if vm.cluster in clusters:
-                clusters[vm.cluster].append(vm.name)
+                clusters[vname][vm.cluster].append(vm.name)
             else:
-                clusters[vm.cluster] = [vm.name]
+                clusters[vname][vm.cluster] = [vm.name]
 
-        for cluster in clusters.keys():
+        for vname in clusters.keys():
+            for cluster in clusters[vname].keys():
 
-            cl = str(cluster)
-            LOG.debug(_("Checking existence of computing cluster {!r} ...").format(cl))
+                vms = clusters[vname][cluster]
 
-            vmw_cluster = self.vsphere.get_cluster_by_name(cl)
-            if vmw_cluster:
-                if self.verbose > 2:
-                    LOG.debug(
-                        _("Found computing cluster {cl!r} (defined for VMs {vms}).").format(
-                            cl=vmw_cluster.name, vms=pp(clusters[cluster])))
-            else:
-                LOG.error(
-                    _("Computing cluster {cl!r} (defined for VMs {vms}) not found.").format(
-                        cl=cl, vms=pp(clusters[cluster])))
-                self.eval_errors += 1
+                cl = str(cluster)
+                LOG.debug(_(
+                    "Checking existence of computing cluster {c!r} in VSPhere {v!r} ...").format(
+                    c=cl, v=vname))
+
+                vsphere = self.vsphere[vname]
+                vmw_cluster = vsphere.get_cluster_by_name(cl)
+                if vmw_cluster:
+                    if self.verbose > 1:
+                        LOG.debug(_(
+                            "Found computing cluster {cl!r} in VSPhere {v!r} (defined for VMs "
+                            "{vms}).").format(cl=vmw_cluster.name, v=vname, vms=pp(vms)))
+                else:
+                    LOG.error(_(
+                        "Computing cluster {cl!r} (defined for VMs {vms}) in VSPhere {v!r} not "
+                        "found.").format(cl=cl, vms=pp(vms), v=vname))
+                    self.eval_errors += 1
 
     # -------------------------------------------------------------------------ยท
     def validate_vms(self):
@@ -1183,7 +1196,7 @@ class CreateTerraformHandler(BaseHandler):
         LOG.info(_("Validating existence of VMs in VMWare."))
         vms2perform = []
 
-        for vm in sorted(self.vms, key=lambda x: x.tf_name):
+        for vm in sorted(self.vms, key=attrgetter('tf_name')):
 
             print(" * {} ".format(vm.fqdn), end='', flush=True)
             if self.verbose: