]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Fixing lib/cr_tf/handler.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 6 Nov 2019 13:15:00 +0000 (14:15 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 6 Nov 2019 13:15:00 +0000 (14:15 +0100)
lib/cr_tf/handler.py

index 6f05a4b938caa2244516b79e71272ca9ff7f43ae..ba238ae34b840abeaf84d7ede72fe7bd6a191b25 100644 (file)
@@ -61,7 +61,7 @@ from .terraform.disk import TerraformDisk
 
 from .xlate import XLATOR
 
-__version__ = '3.1.3'
+__version__ = '3.1.4'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -2087,11 +2087,14 @@ class CreateTerraformHandler(BaseHandler):
     # --------------------------------------------------------------------------
     def create_varfiles(self):
 
-        # vs_name = str([self.vsphere.keys()][0])
+        LOG.debug(_("Creating {!r} ...").format('terraform.tfvars'))
+
         vs_name = None
         for vs_name in self.vsphere.keys():
             break
-        LOG.debug(_("Creating varfile for VSPhere {!r} ...").format(vs_name))
+        if self.verbose > 1:
+            LOG.debug(_("Creating {w} for VSPhere {v!r} ...").format(
+                w='dcfile', v=vs_name))
 
         vs_host = self.config.vsphere[vs_name].host
         vs_port = self.config.vsphere[vs_name].port
@@ -2118,7 +2121,6 @@ class CreateTerraformHandler(BaseHandler):
 
         ''')
 
-        LOG.debug(_("Creating {!r} ...").format('terraform.tfvars'))
         if self.simulate:
             if self.verbose:
                 print(content)
@@ -2220,7 +2222,14 @@ class CreateTerraformHandler(BaseHandler):
     # --------------------------------------------------------------------------
     def create_dcfile(self):
 
+        vs_name = None
+        for vs_name in self.vsphere.keys():
+            break
+
         LOG.debug(_("Creating {!r} ...").format('dc.tf'))
+        if self.verbose > 1:
+            LOG.debug(_("Creating {w} for VSPhere {v!r} ...").format(
+                w='dcfile', v=vs_name))
 
         content = textwrap.dedent('''\
         # filename: dc.tf
@@ -2246,7 +2255,7 @@ class CreateTerraformHandler(BaseHandler):
 
         ''')
 
-        for cluster in self.vsphere.clusters:
+        for cluster in self.vsphere[vs_name].clusters:
             tpl = textwrap.dedent('''\
             data "vsphere_resource_pool" "{pv}" {{
               name          = "{pn}"
@@ -2259,7 +2268,7 @@ class CreateTerraformHandler(BaseHandler):
 
         if self.used_dc_clusters:
             for dsc_name in sorted(self.used_dc_clusters, key=str.lower):
-                dsc_tf_name = self.vsphere.ds_cluster_mapping[dsc_name]
+                dsc_tf_name = self.vsphere[vs_name].ds_cluster_mapping[dsc_name]
                 tpl = textwrap.dedent('''\
                 data "vsphere_datastore_cluster" "{tn}" {{
                   name          = "{n}"
@@ -2271,7 +2280,7 @@ class CreateTerraformHandler(BaseHandler):
 
         if self.used_datastores:
             for ds_name in sorted(self.used_datastores, key=str.lower):
-                ds_tf_name = self.vsphere.ds_mapping[ds_name]
+                ds_tf_name = self.vsphere[vs_name].ds_mapping[ds_name]
                 tpl = textwrap.dedent('''\
                 data "vsphere_datastore" "{tn}" {{
                   name          = "{n}"
@@ -2282,7 +2291,7 @@ class CreateTerraformHandler(BaseHandler):
                 content += tpl.format(tn=ds_tf_name, n=ds_name)
 
         for net_name in sorted(self.used_networks, key=str.lower):
-            net_tf_name = self.vsphere.network_mapping[net_name]
+            net_tf_name = self.vsphere[vs_name].network_mapping[net_name]
             tpl = textwrap.dedent('''\
             data "vsphere_network" "{tn}" {{
               name          = "{n}"
@@ -2330,9 +2339,13 @@ class CreateTerraformHandler(BaseHandler):
           }}
         ''')
 
+        project = self.full_project_name
+        if not project:
+            project = self.project_name
+
         content = tpl.format(
             host=self.config.tf_backend_host, scheme=self.config.tf_backend_scheme,
-            prefix=self.config.tf_backend_path_prefix, project=self.project_name)
+            prefix=self.config.tf_backend_path_prefix, project=project)
 
         if self.min_version_terraform:
             content += '  required_version = ">= {}"\n'.format(str(self.min_version_terraform))
@@ -2408,6 +2421,8 @@ class CreateTerraformHandler(BaseHandler):
     # --------------------------------------------------------------------------
     def _create_instfile_general(self, vm, guest_id, tpl_vm):
 
+        vs_name = vm.vsphere
+
         # ## General definitions of VM
         if self.verbose > 1:
             LOG.debug(_("Generating global definitions of {!r}.").format(vm.name))
@@ -2416,7 +2431,7 @@ class CreateTerraformHandler(BaseHandler):
 
         ''').format(vm.name)
 
-        cluster = self.vsphere.get_cluster_by_name(vm.cluster)
+        cluster = self.vsphere[vs_name].get_cluster_by_name(vm.cluster)
         if not cluster:
             msg = _("Cluster {!r} not found - this shouldn't be happened.").format(
                 vm.cluster)
@@ -2430,12 +2445,12 @@ class CreateTerraformHandler(BaseHandler):
         ''').format(tn=vm.tf_name, n=vm.name, pv=cluster.resource_pool_var)
 
         if vm.ds_cluster:
-            dsc_tf_name = self.vsphere.ds_cluster_mapping[vm.ds_cluster]
+            dsc_tf_name = self.vsphere[vs_name].ds_cluster_mapping[vm.ds_cluster]
             tpl = '  datastore_cluster_id   = data.vsphere_datastore_cluster.{}.id\n'
             content += tpl.format(dsc_tf_name)
 
         if vm.datastore:
-            ds_tf_name = self.vsphere.ds_mapping[vm.datastore]
+            ds_tf_name = self.vsphere[vs_name].ds_mapping[vm.datastore]
             tpl = '  datastore_id           = data.vsphere_datastore.{}.id\n'
             content += tpl.format(ds_tf_name)
 
@@ -2468,12 +2483,14 @@ class CreateTerraformHandler(BaseHandler):
     # --------------------------------------------------------------------------
     def _create_instfile_if(self, vm, iface, i, tpl_vm):
 
+        vs_name = vm.vsphere
+
         # ## Interface definition
 
         if self.verbose > 1:
             LOG.debug(_("Generating interface definition {i} of {v!r}.").format(i=i, v=vm.name))
         nw = iface.network
-        nw_name = self.vsphere.network_mapping[nw]
+        nw_name = self.vsphere[vs_name].network_mapping[nw]
 
         content = textwrap.indent(textwrap.dedent('''\
         network_interface {{
@@ -2766,11 +2783,12 @@ class CreateTerraformHandler(BaseHandler):
 
             for vm in self.existing_vms:
 
+                vs_name = vm.vsphere
                 print()
                 LOG.info(_("Importing VM {!r}.").format(vm['name']))
                 vm_obj = 'vsphere_virtual_machine.{}'.format(vm['tf_name'])
                 path = '/{dc}/{f}/{p}/{n}'.format(
-                    dc=self.vsphere.dc, f=self.vsphere.dc_obj.vm_folder,
+                    dc=self.vsphere[vs_name].dc, f=self.vsphere[vs_name].dc_obj.vm_folder,
                     p=vm['path'], n=vm['name'])
                 cmd = [str(self.terraform_cmd), 'import', vm_obj, path]
                 result = self.run(