better support for reporting available api versions
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Fri, 8 Feb 2013 03:57:08 +0000 (22:57 -0500)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Fri, 8 Feb 2013 03:57:08 +0000 (22:57 -0500)
config/api_versions.xml [new file with mode: 0644]
setup.py
sfa/managers/aggregate_manager.py
sfa/server/api_versions.py [new file with mode: 0644]

diff --git a/config/api_versions.xml b/config/api_versions.xml
new file mode 100644 (file)
index 0000000..6b30f4e
--- /dev/null
@@ -0,0 +1,3 @@
+<api_versions>
+    <api_version version="" url=""/>   
+</api_versions>
index 5c12af0..1fe5a33 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -50,6 +50,7 @@ initscripts = [ 'sfa', 'sfa-cm' ]
 data_files = [ ('/etc/sfa/', [ 'config/aggregates.xml',
                               'config/registries.xml',
                               'config/default_config.xml',
+                              'config/api_versions.xml',
                               'config/sfi_config',
                               'config/topology',
                               'sfa/managers/pl/pl.rng',
index cb8b2a6..6893131 100644 (file)
@@ -5,6 +5,7 @@ from sfa.util.xrn import Xrn
 from sfa.util.callids import Callids
 from sfa.util.sfalogging import logger
 from sfa.util.faults import SfaInvalidArgument, InvalidRSpecVersion
+from sfa.server.api_versions import ApiVersions
 
 
 class AggregateManager:
@@ -49,13 +50,15 @@ class AggregateManager:
         xrn=Xrn(api.hrn, type='authority')
         version = version_core()
         cred_types = [{'geni_type': 'geni_sfa', 'geni_version': str(i)} for i in range(4)[-2:]]
+        geni_api_versions = ApiVersions().get_versions()
+        geni_api_versions.append({'3': 'http://%s:%s' % (socket.gethostname(), api.config.sfa_aggregate_port)})
         version_generic = {
             'testbed': self.driver.testbed_name(),
             'interface':'aggregate',
             'hrn':xrn.get_hrn(),
             'urn':xrn.get_urn(),
             'geni_api': 3,
-            'geni_api_versions': {'3': 'http://%s:%s' % (socket.gethostname(), api.config.sfa_aggregate_port)},
+            'geni_api_versions': geni_api_versions,
             'geni_single_allocation': 0, # Accept operations that act on as subset of slivers in a given state.
             'geni_allocate': 'geni_many',# Multiple slivers can exist and be incrementally added, including those which connect or overlap in some way.
             'geni_credential_types': cred_types,
diff --git a/sfa/server/api_versions.py b/sfa/server/api_versions.py
new file mode 100644 (file)
index 0000000..2f1ccf7
--- /dev/null
@@ -0,0 +1,47 @@
+import os
+from sfa.util.xml import XML
+from sfa.util.config import Config
+
+class ApiVersions:
+
+    required_fields = ['version', 'url']
+    
+    template = """<api_versions>
+<api_version name="" version="" url="" />
+</api_versions>""" 
+
+    def __init__(self, string=None, filename=None, create=False):
+        self.xml = None
+
+        if create:
+            self.create()
+        elif string:
+            self.load(string)
+        elif filename:
+            self.load(filename)
+        else:
+            # load the default file
+            c = Config()
+            api_versions_file = os.path.sep.join([c.config_path, 'api_versions.xml'])
+            self.load(api_versions_file)
+        
+    def create(self):
+        self.xml = XML(string=ApiVersions.template)
+
+    def load(self, source):
+        self.xml = XML(source)
+
+    def get_versions(self):
+        versions = []
+        for value in self.xml.todict().values():
+            if not value:
+                continue
+            if isinstance(value, list):
+                for item in value:
+                    if isinstance(item, dict) and \
+                       set(ApiVersions.required_fields).issubset(item.keys()):
+                        api_version = {str(item['version']): item['url']}
+                        versions.append(api_version)
+        return versions  
+                
+