The various APIs of OpenStack learning
In a word: OpenStack separate projects provide services externally through the RESTful API, while process communications on each host in the project are communicated primarily through RPC.
OpenStack Independent Project External interface (RESTFUL API)
Summary:aste.deploy Module loading api-paste.init defined, WSGI app for service; The Wsgi app is encapsulated by Webob Library to simplify the definition and writing of Wsgi app, and the routes library is used to define a URL to a specific function mapping within the service. Deploy also has URL to service mapping function, but this mapping hierarchy is a bit higher, can be understood as two times accurate mapping.
personally feel that the future version of this framework will also become a module package in Olso.
quite a lot of related articles on the network is good, here on the two chapters representative, afraid of the link will not find:
Python paste modules and routes modules
The original author wrote a simple-restful server architecture himself. But to run up, you need to improve the next. As a practice, modify, understand OpenStack RESTFUL framework is good.
Python paste module and routes module "turn"
When reading OpenStack source code, found that its components are basically using paste modules and routes modules to build the server and processing routing information, in order to familiarize themselves with these modules to use these modules to write a simple server, The architecture of this server is consistent with the design of the glance component.
First use paste to create an app, and then Wsgiref.simple_server start him up, as the app's function is determined by the configuration file
1 config = "Python_paste.ini"
2 appname = "Common"
3 Wsgi_app = Loadapp ("config:%s"% Os.path.abspath (config), app Name)
4 server = Make_server (' localhost ', 80,wsgi_app)
5 server.serve_forever ()
Python_paste.ini
[Composite:common]
Use = Egg:paste#urlmap
/:showversion
/log:showversion_log
/v1:apiv1app
[Pipeline:showversion_log ]
pipeline = Filter_log showversion
[Filter:filter_log]
#filter2 deal with Time,read args belowmanage< C13/>paste.filter_factory = Manage:LogFilter.factory
[App:apiv1app]
paste.app_factory = v1.router: Myrouterapp.factory
[app:showversion]
Version = 1.0.0
paste.app_factory = Manage: Showversion.factory
/:showversion: Print version number
/log:showversion_log: Print version number of the same time do the corresponding logging
/v1:apiv1app: Imitate glance in the multi-version API, Forward all V1 requests to apiv1app processing.
1 class Showversion (object):
2 "'
3 app 4" '
5 def __init__ (self,version):
6 self.version = version
7 def __call__ (self,environ,start_response):
8 res = Response ()
9 res.status = ' OK ' res.content_type = ' text/plain ' one content = [] content.append ("%s\n"% self.version) res.body = ' \ n '. Join (content) return Res (environ,start_response) @classmethod def Factory (Cls,global_conf,**kwargs): print ' factory ' Kwargs: ", Kwargs return showversion (kwargs[' version '])
1 class Logfilter (object):
2 "'
3 Log
4" '
5 def __init__ (Self,app):
6 Self.app = App
7 def __call__ (self,environ,start_response):
8 print ' You can write log. '
9 return Self.app (environ,start_response) @classmethod def factory (Cls,global_ Conf,**kwargs):
return Logfilter
When this URI is/and/log, the normal response of the server can be obtained. But/V1 has not yet been realized. Have to admire the powerful python, so a few lines to achieve a Web server basic functionality, the following introduction routes, with routes can achieve more elegant URI routing mechanism.
Note: Here on the paste profile is not much, we go to paste official website view can, write very detailed, this is not difficult, do not need to go to one of the introductions.
Create a new Python module v1, v1 inside the new two files router.py and wsgi.py,wsgi.py is a common file, and business-related, we just need to focus on the implementation of the router.py can be very simple and convenient to use.
router.py:
1 Import wsgi
2
3 class Controllertest (object):
4 def __init__ (self):
5 print " Controllertest!!!! "
6 def Test (self,req):
7 print "req", req
8 return {
9 ' name ': "Test", ' Properties ': "Test"
class Myrouterapp (Wsgi. Router):
" app" ' def __init__ (self,mapper):
18 controller = controllertest () mapper.connect ('/test ', Controller=wsgi. Resource (Controller), action= ' test ', conditions={' method ': [' Get ']}) Super (Myrouterapp, self). __init__ (Mapper)
wsgi.py:
1 import datetime 2 Import JSON 3 import routes 4 import routes.middleware 5 Impo RT Webob 6 Import webob.dec 7 Import Webob.exc 8 9 class Apimapper (routes. Mapper): "" "Handle route matching when the URL is ' because routes.
Mapper returns the error in the this case. "" "Def Routematch (self, Url=none, environ=none): If URL is" ": the result = S Elf._match ("", environ) return result[0], result[1] routes. Mapper.routematch (self, url, environ) class Router (object): Def __init__ (self, Mapper): Mappe R.redirect ("", "/") Self.map =