From bd061c35e1c1d44cbf9981e4911b445c28f23b6d Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Tue, 8 Jun 2010 17:40:40 +0100
Subject: [PATCH] Enforce consistency in disks and nics input dicts

With this change unknown disk and nic parameters will be refused, rather
than silently ignored, so that one can't pass them in by mistake and not
realize what went wrong.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/cmdlib.py    | 10 +++++++++-
 lib/constants.py | 12 ++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 05d5f4cb5..8f58243e3 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -6116,9 +6116,15 @@ class LUCreateInstance(LogicalUnit):
       # TODO: make the ip check more flexible and not depend on the name check
       raise errors.OpPrereqError("Cannot do ip checks without a name check",
                                  errors.ECODE_INVAL)
-    # check disk information: either all adopt, or no adopt
+
+    # check nics' parameter names
+    for nic in self.op.nics:
+      utils.ForceDictType(nic, constants.INIC_PARAMS_TYPES)
+
+    # check disks. parameter names and consistent adopt/no-adopt strategy
     has_adopt = has_no_adopt = False
     for disk in self.op.disks:
+      utils.ForceDictType(disk, constants.IDISK_PARAMS_TYPES)
       if "adopt" in disk:
         has_adopt = True
       else:
@@ -8170,6 +8176,7 @@ class LUSetInstanceParams(LogicalUnit):
     # Disk validation
     disk_addremove = 0
     for disk_op, disk_dict in self.op.disks:
+      utils.ForceDictType(disk_dict, constants.IDISK_PARAMS_TYPES)
       if disk_op == constants.DDM_REMOVE:
         disk_addremove += 1
         continue
@@ -8223,6 +8230,7 @@ class LUSetInstanceParams(LogicalUnit):
     # NIC validation
     nic_addremove = 0
     for nic_op, nic_dict in self.op.nics:
+      utils.ForceDictType(nic_dict, constants.INIC_PARAMS_TYPES)
       if nic_op == constants.DDM_REMOVE:
         nic_addremove += 1
         continue
diff --git a/lib/constants.py b/lib/constants.py
index 5d5435ef1..a188a771f 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -512,6 +512,18 @@ NICS_PARAMETER_TYPES = {
 
 NICS_PARAMETERS = frozenset(NICS_PARAMETER_TYPES.keys())
 
+IDISK_SIZE = "size"
+IDISK_MODE = "mode"
+IDISK_PARAMS = frozenset([IDISK_SIZE, IDISK_MODE])
+IDISK_PARAMS_TYPES = {IDISK_SIZE: VTYPE_SIZE, IDISK_MODE: VTYPE_STRING}
+INIC_MAC = "mac"
+INIC_IP = "ip"
+INIC_MODE = "mode"
+INIC_LINK = "link"
+INIC_BRIDGE = "bridge"
+INIC_PARAMS = frozenset([INIC_MAC, INIC_IP, INIC_MODE, INIC_LINK, INIC_BRIDGE])
+INIC_PARAMS_TYPES = dict([(name, VTYPE_STRING) for name in INIC_PARAMS])
+
 # Hypervisor constants
 HT_XEN_PVM = "xen-pvm"
 HT_FAKE = "fake"
-- 
GitLab