From 40fa3cf9716df140f3af767ebf762700af4072e8 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 18 Oct 2019 10:04:00 +0200 Subject: [PATCH] Improving validation of given clusters --- lib/cr_tf/handler.py | 55 +++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/cr_tf/handler.py b/lib/cr_tf/handler.py index 6c4e5ae..c2f03ea 100644 --- a/lib/cr_tf/handler.py +++ b/lib/cr_tf/handler.py @@ -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: -- 2.39.5