From fe96220b7c7555beb584bd141fcba461fc0b45e2 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Fri, 19 Oct 2007 11:48:00 +0000
Subject: [PATCH] Abstract more strings values into constants

Currently, the disk types are defined using constants in the code.
Convert those into constants so that we can easily find them and check
their usage.

Note that we don't rename the values of the constants as they are used
in the configuration file, and as such it's best to leave them as they
are.

Reviewed-by: imsnah
---
 lib/backend.py       |  2 +-
 lib/bdev.py          |  7 ++++---
 lib/cmdlib.py        | 33 +++++++++++++++++----------------
 lib/config.py        |  2 +-
 lib/constants.py     |  5 +++++
 lib/objects.py       | 16 ++++++++--------
 scripts/gnt-instance |  2 +-
 7 files changed, 37 insertions(+), 30 deletions(-)

diff --git a/lib/backend.py b/lib/backend.py
index d2c154ae1..78ce3816d 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -1093,7 +1093,7 @@ def SnapshotBlockDevice(disk):
         if child.size == disk.size:
           # return implies breaking the loop
           return SnapshotBlockDevice(child)
-  elif disk.dev_type == "lvm":
+  elif disk.dev_type == constants.LD_LV:
     r_dev = _RecursiveFindBD(disk)
     if r_dev is not None:
       # let's stay on the safe side and ask for the full size, for now
diff --git a/lib/bdev.py b/lib/bdev.py
index 94ce4548c..473e4a407 100644
--- a/lib/bdev.py
+++ b/lib/bdev.py
@@ -28,6 +28,7 @@ import errno
 from ganeti import utils
 from ganeti import logger
 from ganeti import errors
+from ganeti import constants
 
 
 class BlockDev(object):
@@ -1522,9 +1523,9 @@ class DRBDev(BlockDev):
 
 
 DEV_MAP = {
-  "lvm": LogicalVolume,
-  "md_raid1": MDRaid1,
-  "drbd": DRBDev,
+  constants.LD_LV: LogicalVolume,
+  constants.LD_MD_R1: MDRaid1,
+  constants.LD_DRBD7: DRBDev,
   }
 
 
diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 2fce8a2a5..e316e7e38 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -2620,11 +2620,11 @@ def _GenerateMDDRBDBranch(cfg, primary, secondary, size, names):
   """
   port = cfg.AllocatePort()
   vgname = cfg.GetVGName()
-  dev_data = objects.Disk(dev_type="lvm", size=size,
+  dev_data = objects.Disk(dev_type=constants.LD_LV, size=size,
                           logical_id=(vgname, names[0]))
-  dev_meta = objects.Disk(dev_type="lvm", size=128,
+  dev_meta = objects.Disk(dev_type=constants.LD_LV, size=128,
                           logical_id=(vgname, names[1]))
-  drbd_dev = objects.Disk(dev_type="drbd", size=size,
+  drbd_dev = objects.Disk(dev_type=constants.LD_DRBD7, size=size,
                           logical_id = (primary, secondary, port),
                           children = [dev_data, dev_meta])
   return drbd_dev
@@ -2646,10 +2646,10 @@ def _GenerateDiskTemplate(cfg, template_name,
       raise errors.ProgrammerError("Wrong template configuration")
 
     names = _GenerateUniqueNames(cfg, [".sda", ".sdb"])
-    sda_dev = objects.Disk(dev_type="lvm", size=disk_sz,
+    sda_dev = objects.Disk(dev_type=constants.LD_LV, size=disk_sz,
                            logical_id=(vgname, names[0]),
                            iv_name = "sda")
-    sdb_dev = objects.Disk(dev_type="lvm", size=swap_sz,
+    sdb_dev = objects.Disk(dev_type=constants.LD_LV, size=swap_sz,
                            logical_id=(vgname, names[1]),
                            iv_name = "sdb")
     disks = [sda_dev, sdb_dev]
@@ -2660,18 +2660,18 @@ def _GenerateDiskTemplate(cfg, template_name,
 
     names = _GenerateUniqueNames(cfg, [".sda_m1", ".sda_m2",
                                        ".sdb_m1", ".sdb_m2"])
-    sda_dev_m1 = objects.Disk(dev_type="lvm", size=disk_sz,
+    sda_dev_m1 = objects.Disk(dev_type=constants.LD_LV, size=disk_sz,
                               logical_id=(vgname, names[0]))
-    sda_dev_m2 = objects.Disk(dev_type="lvm", size=disk_sz,
+    sda_dev_m2 = objects.Disk(dev_type=constants.LD_LV, size=disk_sz,
                               logical_id=(vgname, names[1]))
-    md_sda_dev = objects.Disk(dev_type="md_raid1", iv_name = "sda",
+    md_sda_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name = "sda",
                               size=disk_sz,
                               children = [sda_dev_m1, sda_dev_m2])
-    sdb_dev_m1 = objects.Disk(dev_type="lvm", size=swap_sz,
+    sdb_dev_m1 = objects.Disk(dev_type=constants.LD_LV, size=swap_sz,
                               logical_id=(vgname, names[2]))
-    sdb_dev_m2 = objects.Disk(dev_type="lvm", size=swap_sz,
+    sdb_dev_m2 = objects.Disk(dev_type=constants.LD_LV, size=swap_sz,
                               logical_id=(vgname, names[3]))
-    md_sdb_dev = objects.Disk(dev_type="md_raid1", iv_name = "sdb",
+    md_sdb_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name = "sdb",
                               size=swap_sz,
                               children = [sdb_dev_m1, sdb_dev_m2])
     disks = [md_sda_dev, md_sdb_dev]
@@ -2683,11 +2683,11 @@ def _GenerateDiskTemplate(cfg, template_name,
                                        ".sdb_data", ".sdb_meta"])
     drbd_sda_dev = _GenerateMDDRBDBranch(cfg, primary_node, remote_node,
                                          disk_sz, names[0:2])
-    md_sda_dev = objects.Disk(dev_type="md_raid1", iv_name="sda",
+    md_sda_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name="sda",
                               children = [drbd_sda_dev], size=disk_sz)
     drbd_sdb_dev = _GenerateMDDRBDBranch(cfg, primary_node, remote_node,
                                          swap_sz, names[2:4])
-    md_sdb_dev = objects.Disk(dev_type="md_raid1", iv_name="sdb",
+    md_sdb_dev = objects.Disk(dev_type=constants.LD_MD_R1, iv_name="sdb",
                               children = [drbd_sdb_dev], size=swap_sz)
     disks = [md_sda_dev, md_sdb_dev]
   else:
@@ -3256,7 +3256,8 @@ class LURemoveMDDRBDComponent(LogicalUnit):
       raise errors.OpPrereqError("Can't find this device ('%s') in the"
                                  " instance." % self.op.disk_name)
     for child in disk.children:
-      if child.dev_type == "drbd" and child.logical_id[2] == self.op.disk_id:
+      if (child.dev_type == constants.LD_DRBD7 and
+          child.logical_id[2] == self.op.disk_id):
         break
     else:
       raise errors.OpPrereqError("Can't find the device with this port.")
@@ -3479,7 +3480,7 @@ class LUQueryInstanceData(NoHooksLU):
     """
     self.cfg.SetDiskID(dev, instance.primary_node)
     dev_pstatus = rpc.call_blockdev_find(instance.primary_node, dev)
-    if dev.dev_type == "drbd":
+    if dev.dev_type == constants.LD_DRBD7:
       # we change the snode then (otherwise we use the one passed in)
       if dev.logical_id[0] == instance.primary_node:
         snode = dev.logical_id[1]
@@ -3741,7 +3742,7 @@ class LUExportInstance(LogicalUnit):
             logger.Error("could not snapshot block device %s on node %s" %
                          (disk.logical_id[1], src_node))
           else:
-            new_dev = objects.Disk(dev_type="lvm", size=disk.size,
+            new_dev = objects.Disk(dev_type=constants.LD_LV, size=disk.size,
                                       logical_id=(vgname, new_dev_name),
                                       physical_id=(vgname, new_dev_name),
                                       iv_name=disk.iv_name)
diff --git a/lib/config.py b/lib/config.py
index 81b41e75c..e30931668 100644
--- a/lib/config.py
+++ b/lib/config.py
@@ -198,7 +198,7 @@ class ConfigWriter:
 
     if disk.logical_id is None and disk.physical_id is not None:
       return
-    if disk.dev_type == "drbd":
+    if disk.dev_type == constants.LD_DRBD7:
       pnode, snode, port = disk.logical_id
       if node_name not in (pnode, snode):
         raise errors.ConfigurationError("DRBD device not knowing node %s" %
diff --git a/lib/constants.py b/lib/constants.py
index d1237de90..cffe2bd51 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -79,6 +79,11 @@ DT_PLAIN = "plain"
 DT_LOCAL_RAID1 = "local_raid1"
 DT_REMOTE_RAID1 = "remote_raid1"
 
+# logical disk types
+LD_LV = "lvm"
+LD_MD_R1 = "md_raid1"
+LD_DRBD7 = "drbd"
+
 # instance creation modem
 INSTANCE_CREATE = "create"
 INSTANCE_IMPORT = "import"
diff --git a/lib/objects.py b/lib/objects.py
index d404d6b9f..f5da82a73 100644
--- a/lib/objects.py
+++ b/lib/objects.py
@@ -314,15 +314,15 @@ class Disk(ConfigObject):
 
   def CreateOnSecondary(self):
     """Test if this device needs to be created on a secondary node."""
-    return self.dev_type in ("drbd", "lvm")
+    return self.dev_type in (constants.LD_DRBD7, constants.LD_LV)
 
   def AssembleOnSecondary(self):
     """Test if this device needs to be assembled on a secondary node."""
-    return self.dev_type in ("drbd", "lvm")
+    return self.dev_type in (constants.LD_DRBD7, constants.LD_LV)
 
   def OpenOnSecondary(self):
     """Test if this device needs to be opened on a secondary node."""
-    return self.dev_type in ("lvm",)
+    return self.dev_type in (constants.LD_LV,)
 
   def GetNodes(self, node):
     """This function returns the nodes this device lives on.
@@ -333,9 +333,9 @@ class Disk(ConfigObject):
     devices needs to (or can) be assembled.
 
     """
-    if self.dev_type == "lvm" or self.dev_type == "md_raid1":
+    if self.dev_type == constants.LD_LV or self.dev_type == constants.LD_MD_R1:
       result = [node]
-    elif self.dev_type == "drbd":
+    elif self.dev_type == constants.LD_DRBD7:
       result = [self.logical_id[0], self.logical_id[1]]
       if node not in result:
         raise errors.ConfigurationError("DRBD device passed unknown node")
@@ -436,7 +436,7 @@ class Instance(TaggableObject):
     """
     def _Helper(primary, sec_nodes, device):
       """Recursively computes secondary nodes given a top device."""
-      if device.dev_type == 'drbd':
+      if device.dev_type == constants.LD_DRBD7:
         nodea, nodeb, dummy = device.logical_id
         if nodea == primary:
           candidate = nodeb
@@ -485,10 +485,10 @@ class Instance(TaggableObject):
       devs = self.disks
 
     for dev in devs:
-      if dev.dev_type == "lvm":
+      if dev.dev_type == constants.LD_LV:
         lvmap[node].append(dev.logical_id[1])
 
-      elif dev.dev_type == "drbd":
+      elif dev.dev_type == constants.LD_DRBD7:
         if dev.logical_id[0] not in lvmap:
           lvmap[dev.logical_id[0]] = []
 
diff --git a/scripts/gnt-instance b/scripts/gnt-instance
index fe5a9536e..9d78dff99 100755
--- a/scripts/gnt-instance
+++ b/scripts/gnt-instance
@@ -503,7 +503,7 @@ def _FormatBlockDevInfo(buf, dev, indent_level):
     else:
       (path, major, minor, syncp, estt, degr) = status
       buf.write("%s (%d:%d)" % (path, major, minor))
-      if dtype in ("md_raid1", "drbd"):
+      if dtype in (constants.LD_MD_R1, constants.LD_DRBD7):
         if syncp is not None:
           sync_text = "*RECOVERING* %5.2f%%," % syncp
           if estt:
-- 
GitLab