]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Rewritten validation datastores
authorFrank Brehm <frank@brehm-online.com>
Fri, 18 Oct 2019 10:36:37 +0000 (12:36 +0200)
committerFrank Brehm <frank@brehm-online.com>
Fri, 18 Oct 2019 10:36:37 +0000 (12:36 +0200)
lib/cr_tf/handler.py

index ebf8e93146f49bb1c57ecf1d89c5a22d12ca051c..0cff480e534782b6618138c6323d99b89c3bec6e 100644 (file)
@@ -61,7 +61,7 @@ from .terraform.disk import TerraformDisk
 
 from .xlate import XLATOR
 
-__version__ = '2.9.8'
+__version__ = '2.9.9'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -168,9 +168,9 @@ class CreateTerraformHandler(BaseHandler):
 
         self.vsphere_folders = []
 
-        self.used_networks = []
+        self.used_networks = {}
         self.used_dc_clusters = {}
-        self.used_datastores = []
+        self.used_datastores = {}
         self.project_dir = None
         self.project_name = None
 
@@ -1280,7 +1280,7 @@ class CreateTerraformHandler(BaseHandler):
     def validate_storages(self):
 
         self._validate_ds_clusters()
-        self._validate_datastores()
+        self._validate_datastores()
 
         if self.verbose:
             if self.used_dc_clusters:
@@ -1294,7 +1294,11 @@ class CreateTerraformHandler(BaseHandler):
             else:
                 LOG.debug(_("No datastore clusters are used."))
             if self.used_datastores:
-                out = '\n'.join(map(lambda x: '  * {}'.format(x), self.used_datastores))
+                out_lines = []
+                for vs_name in self.used_datastores:
+                    for ds in self.used_datastores[vs_name]:
+                        out_lines.append('  * VSphere {v!r}: {ds}'.format(v=vs_name, ds=ds))
+                out = '\n'.join(out_lines)
                 LOG.debug(_("Used datastors:") + "\n" + out)
             else:
                 LOG.debug(_("No datastores are used."))
@@ -1392,8 +1396,11 @@ class CreateTerraformHandler(BaseHandler):
                 disk = vm.disks[unit_number]
                 needed_gb += disk.size_gb
 
+        vs_name = vm.vsphere
+        vsphere = self.vsphere[vs_name]
+
         vm_cluster = None
-        for cluster in self.vsphere.clusters:
+        for cluster in vsphere.clusters:
             if cluster.name.lower() == vm.cluster.lower():
                 vm_cluster = cluster
                 break
@@ -1404,11 +1411,12 @@ class CreateTerraformHandler(BaseHandler):
 
         if vm.datastore:
             found = False
-            for ds_name in self.vsphere.datastores:
+            found_ds_name = None
+            for ds_name in vsphere.datastores:
                 if ds_name.lower() == vm.datastore.lower():
                     if self.verbose > 2:
-                        LOG.debug(_("Found datastore {d!r} for VM {n!r}.").format(
-                            n=vm.name, d=vm.datastore))
+                        LOG.debug(_("Found datastore {d!r} for VM {n!r} in VSPhere {v!r}.").format(
+                            n=vm.name, d=vm.datastore, v=vs_name))
                     if ds_name not in vm_cluster.datastores:
                         LOG.warn(_("Datastore {d!r} not available in cluster {c!r}.").format(
                             d=ds_name, c=vm.cluster))
@@ -1417,7 +1425,7 @@ class CreateTerraformHandler(BaseHandler):
                         LOG.debug(_("Setting datastore for VM {n!r} to {d!r} ...").format(
                             n=vm.name, d=ds_name))
                         vm.datastore = ds_name
-                    ds = self.vsphere.datastores[ds_name]
+                    ds = vsphere.datastores[ds_name]
                     if ds.avail_space_gb < needed_gb:
                         LOG.error(_(
                             "Datastore {d!r} has not sufficient space for storage of VM "
@@ -1427,20 +1435,28 @@ class CreateTerraformHandler(BaseHandler):
                     else:
                         ds.calculated_usage += needed_gb
                     found = True
+                    found_ds_name = ds_name
                     break
             if not found:
-                LOG.error(_("Datastore {d!r} of VM {n!r} not found.").format(
-                    n=vm.name, d=vm.datastore))
+                LOG.error(_("Datastore {d!r} of VM {n!r} not found in VSPhere {v!r}.").format(
+                    n=vm.name, d=vm.datastore, v=vs_name))
                 self.eval_errors += 1
+            if vs_name not in self.used_datastores:
+                self.used_datastores[vs_name] = []
+            if found_ds_name not in self.used_datastores[vs_name]:
+                self.used_datastores[vs_name].append(found_ds_name)
             return
 
-        ds_name = self.vsphere.datastores.find_ds(
+        ds_name = vsphere.datastores.find_ds(
             needed_gb, vm.ds_type, use_ds=copy.copy(vm_cluster.datastores), no_k8s=True)
         if ds_name:
-            LOG.debug(_("Found datastore {d!r} for VM {v!r}.").format(d=ds_name, v=vm.name))
+            LOG.debug(_("Found datastore {d!r} for VM {n!r} in VSPhere {v!r}.").format(
+                d=ds_name, n=vm.name, v=vs_name))
             vm.datastore = ds_name
-            if ds_name not in self.used_datastores:
-                self.used_datastores.append(ds_name)
+            if vs_name not in self.used_datastores:
+                self.used_datastores[vs_name] = []
+            if ds_name not in self.used_datastores[vs_name]:
+                self.used_datastores[vs_name].append(ds_name)
         else:
             self.eval_errors += 1