From 5974efeadf83729d5c438f9e13f6bb901ae3b8d1 Mon Sep 17 00:00:00 2001
From: Apollon Oikonomopoulos <apollon@noc.grnet.gr>
Date: Mon, 18 Apr 2011 16:33:19 +0300
Subject: [PATCH] lib.objects: Add network object definition
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Add a network object definition, with the appropriate slots, to lib.objects.

Also add a β€œnetworks” top-level configuration slot to host network definitions
and a β€œnetworks” slot to the NodeGroup objects to hold network ←→ node-group
mappings.

Signed-off-by: Apollon Oikonomopoulos <apollon@noc.grnet.gr>
---
 lib/objects.py | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/lib/objects.py b/lib/objects.py
index b3f425673..ad8ca6109 100644
--- a/lib/objects.py
+++ b/lib/objects.py
@@ -46,7 +46,7 @@ from socket import AF_INET
 
 
 __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
-           "OS", "Node", "NodeGroup", "Cluster", "FillDict"]
+           "OS", "Node", "NodeGroup", "Cluster", "FillDict", "Network"]
 
 _TIMESTAMPS = ["ctime", "mtime"]
 _UUID = ["uuid"]
@@ -323,6 +323,7 @@ class ConfigData(ConfigObject):
     "nodes",
     "nodegroups",
     "instances",
+    "networks",
     "serial_no",
     ] + _TIMESTAMPS
 
@@ -335,7 +336,7 @@ class ConfigData(ConfigObject):
     """
     mydict = super(ConfigData, self).ToDict()
     mydict["cluster"] = mydict["cluster"].ToDict()
-    for key in "nodes", "instances", "nodegroups":
+    for key in "nodes", "instances", "nodegroups", "networks":
       mydict[key] = self._ContainerToDicts(mydict[key])
 
     return mydict
@@ -350,6 +351,7 @@ class ConfigData(ConfigObject):
     obj.nodes = cls._ContainerFromDicts(obj.nodes, dict, Node)
     obj.instances = cls._ContainerFromDicts(obj.instances, dict, Instance)
     obj.nodegroups = cls._ContainerFromDicts(obj.nodegroups, dict, NodeGroup)
+    obj.networks = cls._ContainerFromDicts(obj.networks, dict, Network)
     return obj
 
   def HasAnyDiskOfType(self, dev_type):
@@ -386,11 +388,13 @@ class ConfigData(ConfigObject):
       # gives a good approximation.
       if self.HasAnyDiskOfType(constants.LD_DRBD8):
         self.cluster.drbd_usermode_helper = constants.DEFAULT_DRBD_HELPER
+    if self.networks is None:
+      self.networks = {}
 
 
 class NIC(ConfigObject):
   """Config object representing a network card."""
-  __slots__ = ["mac", "ip", "nicparams"]
+  __slots__ = ["mac", "ip", "network", "nicparams"]
 
   @classmethod
   def CheckParameterSyntax(cls, nicparams):
@@ -986,6 +990,7 @@ class NodeGroup(ConfigObject):
     "ndparams",
     "serial_no",
     "alloc_policy",
+    "networks",
     ] + _TIMESTAMPS + _UUID
 
   def ToDict(self):
@@ -1028,6 +1033,9 @@ class NodeGroup(ConfigObject):
     if self.mtime is None:
       self.mtime = time.time()
 
+    if self.networks is None:
+      self.networks = {}
+
   def FillND(self, node):
     """Return filled out ndparams for L{object.Node}
 
@@ -1087,7 +1095,6 @@ class Cluster(TaggableObject):
     "blacklisted_os",
     "primary_ip_family",
     "prealloc_wipe_disks",
-    "networks",
     ] + _TIMESTAMPS + _UUID
 
   def UpgradeConfig(self):
@@ -1174,10 +1181,6 @@ class Cluster(TaggableObject):
     if self.shared_file_storage_dir is None:
       self.shared_file_storage_dir = ""
 
-    # Network management
-    if self.networks is None:
-      self.networks = {}
-
   def ToDict(self):
     """Custom function for cluster.
 
@@ -1537,6 +1540,21 @@ class InstanceConsole(ConfigObject):
     return True
 
 
+class Network(ConfigObject):
+  """Object representing a network definition for ganeti.
+
+  """
+  __slots__ = [
+    "name",
+    "family",
+    "network",
+    "gateway",
+    "size",
+    "reservations",
+    "ext_reservations",
+    ] + _UUID
+
+
 class SerializableConfigParser(ConfigParser.SafeConfigParser):
   """Simple wrapper over ConfigParse that allows serialization.
 
-- 
GitLab