Ceilometer RESTful frame

Source: Internet
Author: User

CEILOMETER-API uses pecan and flask to build restful APIs, and here's a brief introduction to the use of pecan and flask.

CEILOMTER-API Service Initiation Process

/usr/bin/ceilometer-api

... from ceilometer.cli import apiif __name__ = = "__main__":    sys.exit (API ())
...

/usr/lib/python2.6/site-packages/ceilometer/cli.py

...
Def API (): service.prepare_service () srv = App.build_server () srv.serve_forever ()
...

/usr/lib/python2.6/site-packages/ceilometer/api/app.py


...
Class Versionselectorapplication (object): def __init__ (self): pc = Get_pecan_config () pc.app.debug = Conf.debug Pc.app.enable_acl = (Conf.auth_strategy = = ' Keystone ') if Cfg. Conf.enable_v1_api: From ceilometer.api.v1 import app as V1app self.v1 = V1app.make_app (cfg. CONF, Enable_acl=pc.app.enable_acl) else: def not_found (environ, start_response): start_response (' 404 Not Found ', []) return [] self.v1 = not_found self.v2 = Setup_app (pecan_config=pc) def __call__ ( Self, Environ, start_response): if environ[' Path_info '].startswith ('/v1/'): return self.v1 (environ, start _response) return self.v2 (environ, start_response)
...

As shown, there are two versions of Ceilometer-api, V1 and V2, that use flask and pecan to build restful APIs, where V1 is Havana in deprecated.

Pecan is a lightweight web framework, home to http://pecan.readthedocs.org/, where URLs mapping such as:

Ceilometer uses the pecan code as follows:

/usr/lib/python2.6/site-packages/ceilometer/api/app.py

...
def setup_app (Pecan_config=none, Extra_hooks=none): # Fixme:replace Dbhook with a hooks. Transactionhook app_hooks = [Hooks. Confighook (), hooks. Dbhook (Storage.get_connection (CFG). CONF, ' metric '), Storage.get_connection (CFG. CONF, ' event '), hooks. Pipelinehook (), hooks. Translationhook ()] if Extra_hooks:app_hooks.extend (extra_hooks) if not pecan_config:pecan_config = g Et_pecan_config () pecan.configuration.set_config (Dict (pecan_config), overwrite=true) app = Pecan.make_app (PE Can_config.app.root, Static_root=pecan_config.app.static_root, Template_path=pecan_config.app.template_path, Debug=conf.debug, Force_canonical=getattr (Pecan_config.app, ' force_canonical ', True), Hooks=app_hook S, Wrap_app=middleware. Parsableerrormiddleware, Guess_content_type_from_ext=false) if GetAttr (Pecan_config.app, ' Enable_acl ', True): Return Acl.install (app, CFG. CONF) return app
...

Configuration of pecan in Ceilometer/usr/lib/python2.6/site-packages/ceilometer/api/config.py

...
App = { ' root ': ' Ceilometer.api.controllers.root.RootController ', ' modules ': [' Ceilometer.api '], ' Static_root ': '% (confdir) s/public ', ' template_path ': '% (confdir) s/ceilometer/api/templates ',}
...

rootcontroller/usr/lib/python2.6/site-packages/ceilometer/api/controllers/root.py

...
Class Rootcontroller (object): v2 = v2. V2controller () @pecan. Expose (Generic=true, template= ' index.html ') def index (self): # Fixme:return Version Information return Dict ()
...

The V2controller contains multiple resource controllers,/usr/lib/python2.6/site-packages/ceilometer/api/controllers/v2.py

Class V2controller (object): "" "    Version 2 API controller root.    " " resources = Resourcescontroller ()    meters = Meterscontroller ()    samples = Samplescontroller ()    alarms = Alarmscontroller ()    event_types = Eventtypescontroller ()    events = Eventscontroller ()    status = Statuscontroller ()    query = Querycontroller ()    capabilities = Capabilitiescontroller ()

The following is an analysis of Meterscontroller:/usr/lib/python2.6/site-packages/ceilometer/api/controllers/v2.py

class meterscontroller (rest.    Restcontroller): "" Works on Meters. "" "    @pecan. Expose () def _lookup (self, Meter_name, *remainder): Return Metercontroller (meter_name), remainder 
#wsme_pecan是来自wsmeext. Pecan @wsme_pecan. Wsexpose ([Meter], [Query]) def get_all (self, q=[]): "" "Return to all kno WN meters, based on the data recorded so far. :p Aram Q:filter rules for the meters to be returned. "" "#Timestamp field is not supported for Meter queries Kwargs = _query_to_kwargs (q, Pecan.request.metric_co Nn.get_meters, Allow_timestamps=false) return [Meter.from_db_model (m) For M in Pecan.request.metric_conn.get_meters (**kwargs)]

Wsme and Wsmeext.pecan are used here. Wsme, Web services made easy, simplifies the writing of rest Web service. can run on the top level of another frame, such as (Cornice, Flask, Pecan), and so on, this is to run on the Pecan to more simple build restapi, usage:

Wsmeext.pecan.wsexpose (Return_type, *arg_types, **options)

Flask, like Pecan, is a python-based open source micro-restful framework, Ceilometer uses flask code as follows:/usr/lib/python2.6/site-packages/ceilometer/api/v1/ app.py

...
def make_app (conf, enable_acl=true, attach_storage=true, sources_file= ' Sources.json '): app = flask. Flask (' Ceilometer.api ') app.register_blueprint (v1_blueprint.blueprint, url_prefix= '/v1 ') App.json_ Encoder = Jsonencoder try: with open (Sources_file, "R") as F: sources = Jsonutils.load (f) except IOError: sources = {} @app. Before_request def attach_config (): flask.request.cfg = conf flask.request.sources = Sources if attach_storage: @app. Before_request def attach_storage (): Flask.request.storage_conn = storage.get_connection (conf) # Install The middleware wrapper if Enable_ ACL: App.wsgi_app = Acl.install (App.wsgi_app, conf) return app
...
App.register_blueprint register a RESTful method, as shown in
Blueprint = Flask. Blueprint (' v1 ', __name__,                            template_folder= ' templates ',                            static_folder= ' static ') @blueprint. Route ('/meters  ') def list_meters_all (): "" "    Return a list of meters.    :p Aram Metadata.<key>: Match on the metadata within the Resource.                           (optional)    "" " RQ = flask.request    meters = rq.storage_conn.get_meters (        project=acl.get_limited_to_project (rq.headers),        metaquery=_get_metaquery (Rq.args))    Return Flask.jsonify (Meters=[m.as_dict () for m in meters])

Ceilometer RESTful frame

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.