From af3aea481c3b8528239770edcae75d406a57de81 Mon Sep 17 00:00:00 2001
From: Dimitris Aragiorgis <dimara@grnet.gr>
Date: Tue, 22 Jan 2013 15:58:32 +0200
Subject: [PATCH] Handle the result of QueryGroups() correctly
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If no group is given for the β€œgnt-network connectβ€œ/β€œβ€¦ disconnect”
commands, the client uses the result of β€œQueryGroups()” which is a list
of lists. Use β€œitertools.chain()” to handle the return value correctly.

Signed-off-by: Dimitris Aragiorgis <dimara@grnet.gr>
Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Dimitris Aragiorgis <dimara@grnet.gr>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/client/gnt_network.py | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/lib/client/gnt_network.py b/lib/client/gnt_network.py
index 34273707a..7210ab30c 100644
--- a/lib/client/gnt_network.py
+++ b/lib/client/gnt_network.py
@@ -25,6 +25,7 @@
 # W0614: Unused import %s from wildcard import (since we need cli)
 
 import textwrap
+import itertools
 
 from ganeti.cli import *
 from ganeti import constants
@@ -83,6 +84,23 @@ def AddNetwork(opts, args):
   SubmitOrSend(op, opts)
 
 
+def _GetDefaultGroups(cl, groups):
+  """Gets list of groups to operate on.
+
+  If C{groups} doesn't contain groups, a list of all groups in the cluster is
+  returned.
+
+  @type cl: L{luxi.Client}
+  @type groups: list
+  @rtype: list
+
+  """
+  if groups:
+    return groups
+
+  return list(itertools.chain(*cl.QueryGroups([], ["uuid"], False)))
+
+
 def ConnectNetwork(opts, args):
   """Map a network to a node group.
 
@@ -93,13 +111,10 @@ def ConnectNetwork(opts, args):
   @return: the desired exit code
 
   """
-  (network, mode, link) = args[:3]
-  groups = args[3:]
-
   cl = GetClient()
 
-  if not groups:
-    (groups, ) = cl.QueryGroups([], ["uuid"], False)
+  (network, mode, link) = args[:3]
+  groups = _GetDefaultGroups(cl, args[3:])
 
   # TODO: Change logic to support "--submit"
   for group in groups:
@@ -121,13 +136,10 @@ def DisconnectNetwork(opts, args):
   @return: the desired exit code
 
   """
-  (network, ) = args[:1]
-  groups = args[1:]
-
   cl = GetClient()
 
-  if not groups:
-    (groups, ) = cl.QueryGroups([], ["uuid"], False)
+  (network, ) = args[:1]
+  groups = _GetDefaultGroups(cl, args[1:])
 
   # TODO: Change logic to support "--submit"
   for group in groups:
-- 
GitLab