From 2c0be3d0633a2b30210a5392a9db2014b57655bb Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 29 Jul 2010 18:08:11 +0200
Subject: [PATCH] Add check for RAPI paths to start with /2

During a discussion in July 2010 it was decided that we'll stabilize on /2. See
message ID <20100716180012.GA9423@google.com> for reference.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/rapi/connector.py |  8 ++++++--
 test/docs_unittest.py | 15 ++++++++++++---
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/lib/rapi/connector.py b/lib/rapi/connector.py
index ff61d1460..d0b18eba3 100644
--- a/lib/rapi/connector.py
+++ b/lib/rapi/connector.py
@@ -159,6 +159,10 @@ def GetHandlers(node_name_pattern, instance_name_pattern, job_id_pattern):
   """Returns all supported resources and their handlers.
 
   """
+  # Important note: New resources should always be added under /2. During a
+  # discussion in July 2010 it was decided that having per-resource versions
+  # is more flexible and future-compatible than versioning the whole remote
+  # API.
   return {
     "/": R_root,
 
@@ -213,9 +217,9 @@ def GetHandlers(node_name_pattern, instance_name_pattern, job_id_pattern):
       rlib2.R_2_instances_name_migrate,
 
     "/2/jobs": rlib2.R_2_jobs,
-    re.compile(r'/2/jobs/(%s)$' % job_id_pattern):
+    re.compile(r"^/2/jobs/(%s)$" % job_id_pattern):
       rlib2.R_2_jobs_id,
-    re.compile(r'/2/jobs/(%s)/wait$' % job_id_pattern):
+    re.compile(r"^/2/jobs/(%s)/wait$" % job_id_pattern):
       rlib2.R_2_jobs_id_wait,
 
     "/2/tags": rlib2.R_2_tags,
diff --git a/test/docs_unittest.py b/test/docs_unittest.py
index 95d81a877..03278d363 100755
--- a/test/docs_unittest.py
+++ b/test/docs_unittest.py
@@ -92,11 +92,16 @@ class TestDocs(unittest.TestCase):
 
       prevline = line
 
+    prefix_exception = frozenset(["/", "/version", "/2"])
+
     undocumented = []
 
     for key, handler in resources.iteritems():
       # Regex objects
       if hasattr(key, "match"):
+        self.assert_(key.pattern.startswith("^/2/"),
+                     msg="Pattern %r does not start with '^/2/'" % key.pattern)
+
         found = False
         for title in titles:
           if (title.startswith("``") and
@@ -107,10 +112,14 @@ class TestDocs(unittest.TestCase):
 
         if not found:
           # TODO: Find better way of identifying resource
-          undocumented.append(str(handler))
+          undocumented.append(key.pattern)
+
+      else:
+        self.assert_(key.startswith("/2/") or key in prefix_exception,
+                     msg="Path %r does not start with '/2/'" % key)
 
-      elif ("``%s``" % key) not in titles:
-        undocumented.append(key)
+        if ("``%s``" % key) not in titles:
+          undocumented.append(key)
 
     self.failIf(undocumented,
                 msg=("Missing RAPI resource documentation for %s" %
-- 
GitLab