From 7e8f03e3d9e998eb66cf11d39c970d655b95feda Mon Sep 17 00:00:00 2001
From: Dimitris Aragiorgis <dimara@grnet.gr>
Date: Mon, 11 Feb 2013 14:42:38 +0200
Subject: [PATCH] Make use of HooksDict() for networks

This can be used in hypervisor code as well. For consistency
export *NETWORK_NAME and not *NETWORK throughout the code.

Signed-off-by: Dimitris Aragiorgis <dimara@grnet.gr>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 lib/cmdlib.py               |  2 +-
 lib/hypervisor/hv_kvm.py    | 27 +--------------------------
 lib/objects.py              |  4 ++--
 man/ganeti-os-interface.rst |  2 +-
 4 files changed, 5 insertions(+), 30 deletions(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 3704ec78d..749ba6e93 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -1512,7 +1512,7 @@ def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
         # FIXME: broken network reference: the instance NIC specifies a
         # network, but the relevant network entry was not in the config. This
         # should be made impossible.
-        env["INSTANCE_NIC%d_NETWORK" % idx] = net
+        env["INSTANCE_NIC%d_NETWORK_NAME" % idx] = net
       if mode == constants.NIC_MODE_BRIDGED:
         env["INSTANCE_NIC%d_BRIDGE" % idx] = link
   else:
diff --git a/lib/hypervisor/hv_kvm.py b/lib/hypervisor/hv_kvm.py
index 2fe5b434e..d0f5a0046 100644
--- a/lib/hypervisor/hv_kvm.py
+++ b/lib/hypervisor/hv_kvm.py
@@ -148,29 +148,6 @@ def _OpenTap(vnet_hdr=True):
   return (ifname, tapfd)
 
 
-def _BuildNetworkEnv(name, network, gateway, network6, gateway6,
-                     mac_prefix, tags, env):
-  """Build environment variables concerning a Network.
-
-  """
-  if name:
-    env["NETWORK_NAME"] = name
-  if network:
-    env["NETWORK_SUBNET"] = network
-  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
-
-
 class QmpMessage:
   """QEMU Messaging Protocol (QMP) message.
 
@@ -854,9 +831,7 @@ class KVMHypervisor(hv_base.BaseHypervisor):
 
     if nic.network:
       n = objects.Network.FromDict(nic.netinfo)
-      _BuildNetworkEnv(nic.network, n.network, n.gateway,
-                       n.network6, n.gateway6,
-                       n.mac_prefix, n.tags, env)
+      env.update(n.HooksDict())
 
     if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
       env["BRIDGE"] = nic.nicparams[constants.NIC_LINK]
diff --git a/lib/objects.py b/lib/objects.py
index de466c453..2f90ea5ce 100644
--- a/lib/objects.py
+++ b/lib/objects.py
@@ -2043,7 +2043,7 @@ class Network(TaggableObject):
     "ext_reservations",
     ] + _TIMESTAMPS + _UUID
 
-  def HooksDict(self, prefix):
+  def HooksDict(self, prefix=""):
     """Export a dictionary used by hooks with a network's information.
 
     @type prefix: String
@@ -2051,7 +2051,7 @@ class Network(TaggableObject):
 
     """
     result = {
-      "%sNETWORK" % prefix: self.name,
+      "%sNETWORK_NAME" % prefix: self.name,
       "%sNETWORK_UUID" % prefix: self.uuid,
       "%sNETWORK_TAGS" % prefix: " ".join(self.tags),
     }
diff --git a/man/ganeti-os-interface.rst b/man/ganeti-os-interface.rst
index ed5872fdf..ec8ec860c 100644
--- a/man/ganeti-os-interface.rst
+++ b/man/ganeti-os-interface.rst
@@ -118,7 +118,7 @@ NIC_%N_FRONTEND_TYPE
     instance, this can be one of: ``rtl8139``, ``ne2k_pci``,
     ``ne2k_isa``, ``paravirtual``.
 
-NIC_%d_NETWORK
+NIC_%d_NETWORK_NAME
     (Optional) If a NIC network is specified, the network's name.
 
 NIC_%d_NETWORK_UUID
-- 
GitLab