Add HT_KVM to HTS_REQ_PORT
[ganeti-local] / lib / rapi / connector.py
index ecf82c7..0d3a961 100644 (file)
@@ -29,7 +29,6 @@ from ganeti import constants
 from ganeti import http
 
 from ganeti.rapi import baserlib
-from ganeti.rapi import rlib1
 from ganeti.rapi import rlib2
 
 # the connection map is created at the end of this file
@@ -43,8 +42,7 @@ class Mapper:
   def __init__(self, connector=CONNECTOR):
     """Resource mapper constructor.
 
-    Args:
-      con: a dictionary, mapping method name with URL path regexp
+    @param connector: a dictionary, mapping method name with URL path regexp
 
     """
     self._connector = connector
@@ -52,14 +50,13 @@ class Mapper:
   def getController(self, uri):
     """Find method for a given URI.
 
-    Args:
-      uri: string with URI
+    @param uri: string with URI
 
-    Returns:
-      None if no method is found or a tuple containing the following fields:
-        methd: name of method mapped to URI
-        items: a list of variable intems in the path
-        args: a dictionary with additional parameters from URL
+    @return: None if no method is found or a tuple containing
+        the following fields:
+            - method: name of method mapped to URI
+            - items: a list of variable intems in the path
+            - args: a dictionary with additional parameters from URL
 
     """
     if '?' in uri:
@@ -88,7 +85,7 @@ class Mapper:
     if result:
       return result
     else:
-      raise http.HTTPNotFound()
+      raise http.HttpNotFound()
 
 
 class R_root(baserlib.R_Generic):
@@ -100,8 +97,7 @@ class R_root(baserlib.R_Generic):
   def GET(self):
     """Show the list of mapped resources.
 
-    Returns:
-      A dictionary with 'name' and 'uri' keys for each of them.
+    @return: a dictionary with 'name' and 'uri' keys for each of them.
 
     """
     root_pattern = re.compile('^R_([a-zA-Z0-9]+)$')
@@ -117,28 +113,53 @@ class R_root(baserlib.R_Generic):
     return baserlib.BuildUriList(rootlist, "/%s")
 
 
-CONNECTOR.update({
-  "/": R_root,
+def _getResources(id):
+  """Return a list of resources underneath given id.
+
+  This is to generalize querying of version resources lists.
+
+  @return: a list of resources names.
+
+  """
+  r_pattern = re.compile('^R_%s_([a-zA-Z0-9]+)$' % id)
+
+  rlist = []
+  for handler in CONNECTOR.values():
+    m = r_pattern.match(handler.__name__)
+    if m:
+      name = m.group(1)
+      rlist.append(name)
+
+  return rlist
+
 
-  "/version": rlib1.R_version,
+class R_2(baserlib.R_Generic):
+  """ /2 resourse.
 
-  "/tags": rlib1.R_tags,
-  "/info": rlib1.R_info,
+  """
+  DOC_URI = "/2"
 
-  "/nodes": rlib1.R_nodes,
-  re.compile(r'^/nodes/([\w\._-]+)$'): rlib1.R_nodes_name,
-  re.compile(r'^/nodes/([\w\._-]+)/tags$'): rlib1.R_nodes_name_tags,
+  def GET(self):
+    """Show the list of mapped resources.
 
-  "/instances": rlib1.R_instances,
-  re.compile(r'^/instances/([\w\._-]+)$'): rlib1.R_instances_name,
-  re.compile(r'^/instances/([\w\._-]+)/tags$'): rlib1.R_instances_name_tags,
+    @return: a dictionary with 'name' and 'uri' keys for each of them.
+
+    """
+    return baserlib.BuildUriList(_getResources("2"), "/2/%s")
+
+
+CONNECTOR.update({
+  "/": R_root,
 
-  "/os": rlib1.R_os,
+  "/version": rlib2.R_version,
 
+  "/2": R_2,
   "/2/jobs": rlib2.R_2_jobs,
   "/2/nodes": rlib2.R_2_nodes,
+  re.compile(r'^/2/nodes/([\w\._-]+)$'): rlib2.R_2_nodes_name,
+  re.compile(r'^/2/nodes/([\w\._-]+)/tags$'): rlib2.R_2_nodes_name_tags,
   "/2/instances": rlib2.R_2_instances,
-  re.compile(r'^/2/instances/([\w\._-]+)$'): rlib1.R_instances_name,
+  re.compile(r'^/2/instances/([\w\._-]+)$'): rlib2.R_2_instances_name,
   re.compile(r'^/2/instances/([\w\._-]+)/tags$'): rlib2.R_2_instances_name_tags,
   re.compile(r'^/2/instances/([\w\._-]+)/reboot$'):
       rlib2.R_2_instances_name_reboot,
@@ -147,4 +168,7 @@ CONNECTOR.update({
   re.compile(r'^/2/instances/([\w\._-]+)/startup$'):
       rlib2.R_2_instances_name_startup,
   re.compile(r'/2/jobs/(%s)$' % constants.JOB_ID_TEMPLATE): rlib2.R_2_jobs_id,
+  "/2/tags": rlib2.R_2_tags,
+  "/2/info": rlib2.R_2_info,
+  "/2/os": rlib2.R_2_os,
   })