To do so network object should be available just after CheckPrereq().
Thus move object creation away from Exec() in LUNetworkAdd.

Signed-off-by: Dimitris Aragiorgis <[email protected]>
---
 lib/cmdlib.py |   88 +++++++++------------------------------------------------
 1 file changed, 14 insertions(+), 74 deletions(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index e4df32f..3e7f3da 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -1398,47 +1398,6 @@ def _ExpandInstanceName(cfg, name):
   return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
 
 
-def _BuildNetworkHookEnv(name, subnet, gateway, network6, gateway6,
-                         mac_prefix, tags):
-  """Builds network related env variables for hooks
-
-  This builds the hook environment from individual variables.
-
-  @type name: string
-  @param name: the name of the network
-  @type subnet: string
-  @param subnet: the ipv4 subnet
-  @type gateway: string
-  @param gateway: the ipv4 gateway
-  @type network6: string
-  @param network6: the ipv6 subnet
-  @type gateway6: string
-  @param gateway6: the ipv6 gateway
-  @type mac_prefix: string
-  @param mac_prefix: the mac_prefix
-  @type tags: list
-  @param tags: the tags of the network
-
-  """
-  env = {}
-  if name:
-    env["NETWORK_NAME"] = name
-  if subnet:
-    env["NETWORK_SUBNET"] = subnet
-  if gateway:
-    env["NETWORK_GATEWAY"] = gateway
-  if network6:
-    env["NETWORK_SUBNET6"] = network6
-  if gateway6:
-    env["NETWORK_GATEWAY6"] = gateway6
-  if mac_prefix:
-    env["NETWORK_MAC_PREFIX"] = mac_prefix
-  if tags:
-    env["NETWORK_TAGS"] = " ".join(tags)
-
-  return env
-
-
 def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
                           minmem, maxmem, vcpus, nics, disk_template, disks,
                           bep, hvp, hypervisor_name, tags):
@@ -16260,35 +16219,28 @@ class LUNetworkAdd(LogicalUnit):
     for tag in self.op.tags:
       objects.TaggableObject.ValidateTag(tag)
 
+    self.nobj = objects.Network(name=self.op.network_name,
+                                network=self.op.network,
+                                gateway=self.op.gateway,
+                                network6=self.op.network6,
+                                gateway6=self.op.gateway6,
+                                mac_prefix=self.op.mac_prefix,
+                                uuid=self.network_uuid,
+                                tags=self.op.tags)
+
   def BuildHooksEnv(self):
     """Build hooks env.
 
     """
-    args = {
-      "name": self.op.network_name,
-      "subnet": self.op.network,
-      "gateway": self.op.gateway,
-      "network6": self.op.network6,
-      "gateway6": self.op.gateway6,
-      "mac_prefix": self.op.mac_prefix,
-      "tags": self.op.tags,
-      }
-    return _BuildNetworkHookEnv(**args) # pylint: disable=W0142
+    return self.nobj.HooksDict() # pylint: disable=W0142
 
   def Exec(self, feedback_fn):
     """Add the ip pool to the cluster.
 
     """
-    nobj = objects.Network(name=self.op.network_name,
-                           network=self.op.network,
-                           gateway=self.op.gateway,
-                           network6=self.op.network6,
-                           gateway6=self.op.gateway6,
-                           mac_prefix=self.op.mac_prefix,
-                           uuid=self.network_uuid)
     # Initialize the associated address pool
     try:
-      pool = network.AddressPool.InitializePool(nobj)
+      pool = network.AddressPool.InitializePool(self.nobj)
     except errors.AddressPoolError, err:
       raise errors.OpExecError("Cannot create IP address pool for network"
                                " '%s': %s" % (self.op.network_name, err))
@@ -16325,11 +16277,7 @@ class LUNetworkAdd(LogicalUnit):
           raise errors.OpExecError("Cannot reserve IP address '%s': %s" %
                                    (ip, err))
 
-    if self.op.tags:
-      for tag in self.op.tags:
-        nobj.AddTag(tag)
-
-    self.cfg.AddNetwork(nobj, self.proc.GetECId(), check_uuid=False)
+    self.cfg.AddNetwork(self.nobj, self.proc.GetECId(), check_uuid=False)
     del self.remove_locks[locking.LEVEL_NETWORK]
 
 
@@ -16456,20 +16404,12 @@ class LUNetworkSetParams(LogicalUnit):
       else:
         self.network6 = self.op.network6
 
+
   def BuildHooksEnv(self):
     """Build hooks env.
 
     """
-    args = {
-      "name": self.op.network_name,
-      "subnet": self.network.network,
-      "gateway": self.gateway,
-      "network6": self.network6,
-      "gateway6": self.gateway6,
-      "mac_prefix": self.mac_prefix,
-      "tags": self.tags,
-      }
-    return _BuildNetworkHookEnv(**args) # pylint: disable=W0142
+    return self.nobj.HooksDict()
 
   def BuildHooksNodes(self):
     """Build hooks nodes.
-- 
1.7.10.4

Reply via email to