From 255e19d46c090d2fd5f4191e8da76ca53f78d5b5 Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Mon, 8 Jun 2009 12:36:14 +0100
Subject: [PATCH] Add NIC.CheckParameterSyntax

This function will be used to check the NIC parameters for validity.
Unittests are included.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/objects.py                 | 18 ++++++++++++++++++
 test/ganeti.config_unittest.py | 18 ++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/lib/objects.py b/lib/objects.py
index 1a6178d65..73f89d1af 100644
--- a/lib/objects.py
+++ b/lib/objects.py
@@ -312,6 +312,24 @@ class NIC(ConfigObject):
   """Config object representing a network card."""
   __slots__ = ["mac", "ip", "bridge"]
 
+  @classmethod
+  def CheckParameterSyntax(cls, nicparams):
+    """Check the given parameters for validity.
+
+    @type nicparams:  dict
+    @param nicparams: dictionary with parameter names/value
+    @raise errors.ConfigurationError: when a parameter is not valid
+
+    """
+    if nicparams[constants.NIC_MODE] not in constants.NIC_VALID_MODES:
+      err = "Invalid nic mode: %s" % nicparams[constants.NIC_MODE]
+      raise errors.ConfigurationError(err)
+
+    if (nicparams[constants.NIC_MODE] is constants.NIC_MODE_BRIDGED and
+        not nicparams[constants.NIC_LINK]):
+      err = "Missing bridged nic link"
+      raise errors.ConfigurationError(err)
+
 
 class Disk(ConfigObject):
   """Config object representing a block device."""
diff --git a/test/ganeti.config_unittest.py b/test/ganeti.config_unittest.py
index 24c9491ca..b9b293eb7 100755
--- a/test/ganeti.config_unittest.py
+++ b/test/ganeti.config_unittest.py
@@ -152,6 +152,24 @@ class TestConfigRunner(unittest.TestCase):
     # but the fake_instance update should still fail
     self.failUnlessRaises(errors.ConfigurationError, cfg.Update, fake_instance)
 
+  def testNICParameterSyntaxCheck(self):
+    """Test the NIC's CheckParameterSyntax function"""
+    mode = constants.NIC_MODE
+    link = constants.NIC_LINK
+    m_bridged = constants.NIC_MODE_BRIDGED
+    m_routed = constants.NIC_MODE_ROUTED
+    CheckSyntax = objects.NIC.CheckParameterSyntax
+
+    CheckSyntax(constants.NICC_DEFAULTS)
+    CheckSyntax({mode: m_bridged, link: 'br1'})
+    CheckSyntax({mode: m_routed, link: 'default'})
+    self.assertRaises(errors.ConfigurationError,
+                      CheckSyntax, {mode: '000invalid', link: 'any'})
+    self.assertRaises(errors.ConfigurationError,
+                      CheckSyntax, {mode: m_bridged, link: None})
+    self.assertRaises(errors.ConfigurationError,
+                      CheckSyntax, {mode: m_bridged, link: ''})
+
 
 if __name__ == '__main__':
   unittest.main()
-- 
GitLab