]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Applying flake8 rules to lib/cr_tf/handler.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 6 Nov 2019 16:01:33 +0000 (17:01 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 6 Nov 2019 16:01:33 +0000 (17:01 +0100)
lib/cr_tf/handler.py

index 338eed2c97c3f365bacc5377f4322929668f872d..56fcbdd0013e07f02563de20124c56258aa0853c 100644 (file)
@@ -15,7 +15,6 @@ import re
 import socket
 import ipaddress
 import shutil
-import glob
 import stat
 import textwrap
 import copy
@@ -61,7 +60,7 @@ from .terraform.disk import TerraformDisk
 
 from .xlate import XLATOR
 
-__version__ = '3.1.9'
+__version__ = '3.2.0'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -664,7 +663,8 @@ class CreateTerraformHandler(BaseHandler):
                 found_vspheres.append(vname)
         if len(found_vspheres) > 1:
             yaml_file_rel = os.path.relpath(str(yaml_file), os.getcwd())
-            msg = _("There is only one, unique VSPhere definition allowed in a project file.") + '\n'
+            msg = _("There is only one, unique VSPhere definition allowed in a project file.")
+            msg += '\n'
             msg += _("In {f!r} were found {nr} different VSPhere definitions:").format(
                 f=yaml_file_rel, nr=len(found_vspheres))
             for vname in sorted(found_vspheres, key=str.lower):
@@ -1578,109 +1578,117 @@ class CreateTerraformHandler(BaseHandler):
 
         i = -1
         for iface in vm.interfaces:
-
             i += 1
-            if self.verbose > 1:
-                LOG.debug(_("Checking interface {i} of VM {n!r} ...").format(
-                    i=i, n=vm.name))
-            if not iface.address:
-                LOG.error(_("Interface {i} of VM {n!r} has no defined address.").format(
-                    i=i, n=vm.name))
-                self.eval_errors += 1
-                continue
+            self._validate_interface_of_vm(
+                vm_name=vm.name, iface=iface, vs_name=vs_name, vm_cluster=vm_cluster, i=i)
 
-            if not iface.fqdn:
-                LOG.error(_("Interface {i} of VM {n!r} has no defined FQDN.").format(
-                    i=i, n=vm.name))
-                self.eval_errors += 1
-                continue
+    # -------------------------------------------------------------------------·
+    def _validate_interface_of_vm(self, vm_name, iface, vs_name, vm_cluster, i=0):
 
-            if iface.fqdn in self.fqdns:
-                LOG.error(_(
-                    "FQDN {f!r} already defined for VM {va!r}({ia}) should be set "
-                    "for interface {ib} of {vb!r}.").format(
-                    f=iface.fqdn, va=self.fqdns[iface.fqdn][0], ia=self.fqdns[iface.fqdn][1],
-                    ib=i, vb=vm.name))
-                self.eval_errors += 1
-                continue
+        vsphere = self.vsphere[vs_name]
 
-            self.fqdns[iface.fqdn] = (vm.name, i)
+        if self.verbose > 1:
+            LOG.debug(_("Checking interface {i} of VM {n!r} ...").format(
+                i=i, n=vm_name))
 
-            if iface.address_v4:
-                if iface.address_v4 in self.addresses:
-                    LOG.error(_(
-                        "IPv4 address {a} already defined for VM {va!r}({ia}) should be set "
-                        "for interface {ib} of {vb!r}.").format(
-                        a=iface.address_v4, va=self.fqdns[iface.fqdn][0],
-                        ia=self.fqdns[iface.fqdn][1], ib=i, vb=vm.name))
-                    self.eval_errors += 1
-                    continue
-                self.addresses[iface.address_v4] = (vm.name, i)
-                pair = (iface.fqdn, iface.address_v4)
-                self.dns_mapping['forward'].append(pair)
-                pair = (iface.address_v4, iface.fqdn)
-                self.dns_mapping['reverse'].append(pair)
+        if not iface.address:
+            LOG.error(_("Interface {i} of VM {n!r} has no defined address.").format(
+                i=i, n=vm_name))
+            self.eval_errors += 1
+            return
 
-            if iface.address_v6:
-                if iface.address_v6 in self.addresses:
-                    LOG.error(_(
-                        "IPv6 address {a} already defined for VM {va!r}({ia}) should be set "
-                        "for interface {ib} of {vb!r}.").format(
-                        a=iface.address_v6, va=self.fqdns[iface.fqdn][0],
-                        ia=self.fqdns[iface.fqdn][1], ib=i, vb=vm.name))
-                    self.eval_errors += 1
-                    continue
-                self.addresses[iface.address_v6] = (vm.name, i)
-                pair = (iface.fqdn, iface.address_v6)
-                self.dns_mapping['forward'].append(pair)
-                pair = (iface.address_v6, iface.fqdn)
-                self.dns_mapping['reverse'].append(pair)
-
-            network = iface.network
-            if network:
-                if network not in vsphere.networks:
-                    LOG.error(_(
-                        "Could not find network {n!r} for VM {v!r}, interface {i}.").format(
-                        n=network, v=vm.name, i=i))
-                    self.eval_errors += 1
-                    continue
-            else:
-                network = vsphere.networks.get_network_for_ip(
-                    iface.address_v4, iface.address_v6)
-                if not network:
-                    self.eval_errors += 1
-                    continue
-                iface.network = network
-            LOG.debug(_("Found network {n!r} for interface {i} of VM {v!r}.").format(
-                n=network, i=i, v=vm.name))
+        if not iface.fqdn:
+            LOG.error(_("Interface {i} of VM {n!r} has no defined FQDN.").format(
+                i=i, n=vm_name))
+            self.eval_errors += 1
+            return
+
+        if iface.fqdn in self.fqdns:
+            LOG.error(_(
+                "FQDN {f!r} already defined for VM {va!r}({ia}) should be set "
+                "for interface {ib} of {vb!r}.").format(
+                f=iface.fqdn, va=self.fqdns[iface.fqdn][0], ia=self.fqdns[iface.fqdn][1],
+                ib=i, vb=vm_name))
+            self.eval_errors += 1
+            return
 
-            if network not in vm_cluster.networks:
+        self.fqdns[iface.fqdn] = (vm_name, i)
+
+        if iface.address_v4:
+            if iface.address_v4 in self.addresses:
                 LOG.error(_(
-                    "Network {n!r} for interface {i} of VM {v!r} not available in "
-                    "cluster {c!r}.").format(n=network, v=vm.name, i=i, c=vm_cluster.name))
+                    "IPv4 address {a} already defined for VM {va!r}({ia}) should be set "
+                    "for interface {ib} of {vb!r}.").format(
+                    a=iface.address_v4, va=self.fqdns[iface.fqdn][0],
+                    ia=self.fqdns[iface.fqdn][1], ib=i, vb=vm_name))
                 self.eval_errors += 1
-                continue
-            LOG.debug(_("Network {n!r} is available in cluster {c!r}.").format(
-                n=network, c=vm_cluster.name))
-
-            net = vsphere.networks[network]
-            if not iface.gateway:
-                LOG.debug(_("Setting gateway of interface {i} of VM {v!r} to {g}.").format(
-                    i=i, v=vm.name, g=net.gateway))
-                iface.gateway = net.gateway
-
-            if net.network:
-                if net.network.version == 4:
-                    if iface.netmask_v4 is None:
-                        iface.netmask_v4 = net.network.prefixlen
-                else:
-                    if iface.netmask_v6 is None:
-                        iface.netmask_v6 = net.network.prefixlen
+                return
+            self.addresses[iface.address_v4] = (vm_name, i)
+            pair = (iface.fqdn, iface.address_v4)
+            self.dns_mapping['forward'].append(pair)
+            pair = (iface.address_v4, iface.fqdn)
+            self.dns_mapping['reverse'].append(pair)
+
+        if iface.address_v6:
+            if iface.address_v6 in self.addresses:
+                LOG.error(_(
+                    "IPv6 address {a} already defined for VM {va!r}({ia}) should be set "
+                    "for interface {ib} of {vb!r}.").format(
+                    a=iface.address_v6, va=self.fqdns[iface.fqdn][0],
+                    ia=self.fqdns[iface.fqdn][1], ib=i, vb=vm_name))
+                self.eval_errors += 1
+                return
+            self.addresses[iface.address_v6] = (vm_name, i)
+            pair = (iface.fqdn, iface.address_v6)
+            self.dns_mapping['forward'].append(pair)
+            pair = (iface.address_v6, iface.fqdn)
+            self.dns_mapping['reverse'].append(pair)
+
+        network = iface.network
+        if network:
+            if network not in vsphere.networks:
+                LOG.error(_(
+                    "Could not find network {n!r} for VM {v!r}, interface {i}.").format(
+                    n=network, v=vm_name, i=i))
+                self.eval_errors += 1
+                return
+        else:
+            network = vsphere.networks.get_network_for_ip(
+                iface.address_v4, iface.address_v6)
+            if not network:
+                self.eval_errors += 1
+                return
+            iface.network = network
+        LOG.debug(_("Found network {n!r} for interface {i} of VM {v!r}.").format(
+            n=network, i=i, v=vm_name))
+
+        if network not in vm_cluster.networks:
+            LOG.error(_(
+                "Network {n!r} for interface {i} of VM {v!r} not available in "
+                "cluster {c!r}.").format(n=network, v=vm_name, i=i, c=vm_cluster.name))
+            self.eval_errors += 1
+            return
+        LOG.debug(_("Network {n!r} is available in cluster {c!r}.").format(
+            n=network, c=vm_cluster.name))
+
+        net = vsphere.networks[network]
+        if not iface.gateway:
+            LOG.debug(_("Setting gateway of interface {i} of VM {v!r} to {g}.").format(
+                i=i, v=vm_name, g=net.gateway))
+            iface.gateway = net.gateway
+
+        if net.network:
+            if net.network.version == 4:
+                if iface.netmask_v4 is None:
+                    iface.netmask_v4 = net.network.prefixlen
+            else:
+                if iface.netmask_v6 is None:
+                    iface.netmask_v6 = net.network.prefixlen
 
-            if vs_name not in self.used_networks:
-                self.used_networks[vs_name] = []
-            if network not in self.used_networks[vs_name]:
-                self.used_networks[vs_name].append(network)
+        if vs_name not in self.used_networks:
+            self.used_networks[vs_name] = []
+        if network not in self.used_networks[vs_name]:
+            self.used_networks[vs_name].append(network)
 
     # -------------------------------------------------------------------------·
     def validate_dns_mappings(self):
@@ -2078,7 +2086,6 @@ class CreateTerraformHandler(BaseHandler):
 
         print()
         print()
-        #LOG.info(_("Creating all necessary files for terraform."))
         msg = _("Creating all necessary files for terraform.")
         ll = 6
         if len(msg) > ll:
@@ -2110,7 +2117,6 @@ class CreateTerraformHandler(BaseHandler):
                 w='dcfile', v=vs_name))
 
         vs_host = self.config.vsphere[vs_name].host
-        vs_port = self.config.vsphere[vs_name].port
         vs_user = self.config.vsphere[vs_name].user
         vs_pwd = self.config.vsphere[vs_name].password
         vs_dc = self.config.vsphere[vs_name].dc
@@ -2388,7 +2394,6 @@ class CreateTerraformHandler(BaseHandler):
     def create_instance_file(self, vm):
 
         vs_name = vm.vsphere
-        vsphere = self.vsphere[vs_name]
 
         fname = 'instance.' + vm.name + '.tf'
         LOG.debug(_("Creating file {f!r} for VM instance {n!r}.").format(