rapi: Merge tag helpers into resource
authorMichael Hanselmann <hansmi@google.com>
Thu, 8 Sep 2011 09:34:26 +0000 (11:34 +0200)
committerMichael Hanselmann <hansmi@google.com>
Fri, 9 Sep 2011 14:40:31 +0000 (16:40 +0200)
They were only used in one place and upcoming changes, enabling testing
of resource classes, will be easier with this merge.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: RenĂ© Nussbaumer <rn@google.com>

lib/rapi/baserlib.py
lib/rapi/rlib2.py

index 5f43e0a..59cc6d3 100644 (file)
@@ -32,9 +32,6 @@ import logging
 from ganeti import luxi
 from ganeti import rapi
 from ganeti import http
-from ganeti import ssconf
-from ganeti import constants
-from ganeti import opcodes
 from ganeti import errors
 
 
@@ -91,49 +88,6 @@ def MapFields(names, data):
   return dict(zip(names, data))
 
 
-def _Tags_GET(kind, name):
-  """Helper function to retrieve tags.
-
-  """
-  if kind in (constants.TAG_INSTANCE,
-              constants.TAG_NODEGROUP,
-              constants.TAG_NODE):
-    if not name:
-      raise http.HttpBadRequest("Missing name on tag request")
-    cl = GetClient()
-    if kind == constants.TAG_INSTANCE:
-      fn = cl.QueryInstances
-    elif kind == constants.TAG_NODEGROUP:
-      fn = cl.QueryGroups
-    else:
-      fn = cl.QueryNodes
-    result = fn(names=[name], fields=["tags"], use_locking=False)
-    if not result or not result[0]:
-      raise http.HttpBadGateway("Invalid response from tag query")
-    tags = result[0][0]
-  elif kind == constants.TAG_CLUSTER:
-    ssc = ssconf.SimpleStore()
-    tags = ssc.GetClusterTags()
-
-  return list(tags)
-
-
-def _Tags_PUT(kind, tags, name, dry_run):
-  """Helper function to set tags.
-
-  """
-  return SubmitJob([opcodes.OpTagsSet(kind=kind, name=name,
-                                      tags=tags, dry_run=dry_run)])
-
-
-def _Tags_DELETE(kind, tags, name, dry_run):
-  """Helper function to delete tags.
-
-  """
-  return SubmitJob([opcodes.OpTagsDel(kind=kind, name=name,
-                                      tags=tags, dry_run=dry_run)])
-
-
 def MapBulkFields(itemslist, fields):
   """Map value to field name in to one dictionary.
 
index b6d9abd..82b2d9e 100644 (file)
@@ -1380,8 +1380,33 @@ class _R_Tags(baserlib.R_Generic):
     Example: ["tag1", "tag2", "tag3"]
 
     """
-    # pylint: disable-msg=W0212
-    return baserlib._Tags_GET(self.TAG_LEVEL, name=self.name)
+    kind = self.TAG_LEVEL
+
+    if kind in (constants.TAG_INSTANCE,
+                constants.TAG_NODEGROUP,
+                constants.TAG_NODE):
+      if not self.name:
+        raise http.HttpBadRequest("Missing name on tag request")
+
+      cl = baserlib.GetClient()
+      if kind == constants.TAG_INSTANCE:
+        fn = cl.QueryInstances
+      elif kind == constants.TAG_NODEGROUP:
+        fn = cl.QueryGroups
+      else:
+        fn = cl.QueryNodes
+      result = fn(names=[self.name], fields=["tags"], use_locking=False)
+      if not result or not result[0]:
+        raise http.HttpBadGateway("Invalid response from tag query")
+      tags = result[0][0]
+
+    elif kind == constants.TAG_CLUSTER:
+      assert not self.name
+      # TODO: Use query API?
+      ssc = ssconf.SimpleStore()
+      tags = ssc.GetClusterTags()
+
+    return list(tags)
 
   def PUT(self):
     """Add a set of tags.
@@ -1394,9 +1419,9 @@ class _R_Tags(baserlib.R_Generic):
     if "tag" not in self.queryargs:
       raise http.HttpBadRequest("Please specify tag(s) to add using the"
                                 " the 'tag' parameter")
-    return baserlib._Tags_PUT(self.TAG_LEVEL,
-                              self.queryargs["tag"], name=self.name,
-                              dry_run=bool(self.dryRun()))
+    op = opcodes.OpTagsSet(kind=self.TAG_LEVEL, name=self.name,
+                           tags=self.queryargs["tag"], dry_run=self.dryRun())
+    return baserlib.SubmitJob([op])
 
   def DELETE(self):
     """Delete a tag.
@@ -1411,10 +1436,9 @@ class _R_Tags(baserlib.R_Generic):
       # no we not gonna delete all tags
       raise http.HttpBadRequest("Cannot delete all tags - please specify"
                                 " tag(s) using the 'tag' parameter")
-    return baserlib._Tags_DELETE(self.TAG_LEVEL,
-                                 self.queryargs["tag"],
-                                 name=self.name,
-                                 dry_run=bool(self.dryRun()))
+    op = opcodes.OpTagsDel(kind=self.TAG_LEVEL, name=self.name,
+                           tags=self.queryargs["tag"], dry_run=self.dryRun())
+    return baserlib.SubmitJob([op])
 
 
 class R_2_instances_name_tags(_R_Tags):