3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from cmframework.server.cmhttperrors import CMHTTPErrors
18 from cmframework.server import cmrestapiv1
21 class CMRestAPIFactory(object):
22 def __init__(self, processor, base_url):
24 api = cmrestapiv1.CMRestAPIV1(processor)
25 self.apis[api.get_version()] = api
26 self.base_url = base_url
28 def get_apis(self, rpc):
30 Request: GET http://<cm-vip:port>/cm/apis
36 "href": "<http address for the api>"
37 "min-version": "<mimimum version required to support this version>",
38 "status": "<supported|deprecated|unsupported|current>"
45 logging.debug('get_apis called')
48 for key, value in self.apis.iteritems():
50 version['id'] = value.get_version()
51 version['href'] = '%s%s/' % (self.base_url, key)
52 version['min-version'] = value.get_minimum_version()
53 version['status'] = value.get_status()
54 versions.append(version)
55 reply['versions'] = versions
56 rpc.rep_status = CMHTTPErrors.get_ok_status()
57 rpc.rep_body = json.dumps(reply)
58 logging.debug('returning, rpc=%s', str(rpc))
59 except Exception as exp: # pylint: disable=broad-except
60 logging.error('Got exception %s', str(exp))
61 rpc.rep_status = CMHTTPErrors.get_internal_error_status()
63 rpc.rep_status += str(exp)
65 def get_api(self, version):
66 logging.debug('get_api called with version %s', version)
69 api = self.apis[version]
71 logging.warn('Could not find API with version %s', version)
75 if __name__ == '__main__':
76 rest_api_factory = CMRestAPIFactory(None, None)