class V2Controller(object): """Version 2 API controller root.""" resources = resources.ResourcesController() meters = meters.MetersController() samples = samples.SamplesController() alarms = alarms.AlarmsController() event_types = events.EventTypesController() events = events.EventsController() query = query.QueryController() capabilities = capabilities.CapabilitiesController()
class V2Controller(object): """Version 2 API controller root.""" event_types = events.EventTypesController() events = events.EventsController() capabilities = capabilities.CapabilitiesController() def __init__(self): self._gnocchi_is_enabled = None self._aodh_is_enabled = None self._aodh_url = None @property def gnocchi_is_enabled(self): if self._gnocchi_is_enabled is None: if cfg.CONF.api.gnocchi_is_enabled is not None: self._gnocchi_is_enabled = cfg.CONF.api.gnocchi_is_enabled elif ("gnocchi" not in cfg.CONF.dispatcher or "database" in cfg.CONF.dispatcher): self._gnocchi_is_enabled = False else: try: ks = keystone_client.get_client() ks.service_catalog.url_for(service_type='metric') except exceptions.EndpointNotFound: self._gnocchi_is_enabled = False except exceptions.ClientException: LOG.warn( _LW("Can't connect to keystone, assuming gnocchi " "is disabled and retry later")) else: self._gnocchi_is_enabled = True LOG.warn( _LW("ceilometer-api started with gnocchi " "enabled. The resources/meters/samples " "URLs are disabled.")) return self._gnocchi_is_enabled @property def aodh_url(self): if self._aodh_url is None: if cfg.CONF.api.aodh_is_enabled is False: self._aodh_url = "" elif cfg.CONF.api.aodh_url is not None: self._aodh_url = self._normalize_aodh_url( cfg.CONF.api.aodh_url) else: try: ks = keystone_client.get_client() self._aodh_url = self._normalize_aodh_url( ks.service_catalog.url_for(service_type='alarming')) except exceptions.EndpointNotFound: self._aodh_url = "" except exceptions.ClientException: LOG.warn( _LW("Can't connect to keystone, " "assuming aodh is disabled and retry later.")) else: LOG.warn( _LW("ceilometer-api started with aodh enabled. " "Alarms URLs will be redirected to aodh " "endpoint.")) return self._aodh_url @pecan.expose() def _lookup(self, kind, *remainder): if (kind in ['meters', 'resources', 'samples'] and self.gnocchi_is_enabled): gnocchi_abort() elif kind == 'meters': return meters.MetersController(), remainder elif kind == 'resources': return resources.ResourcesController(), remainder elif kind == 'samples': return samples.SamplesController(), remainder elif kind == 'query': return QueryController( gnocchi_is_enabled=self.gnocchi_is_enabled, aodh_url=self.aodh_url, ), remainder elif kind == 'alarms' and self.aodh_url: aodh_redirect(self.aodh_url) elif kind == 'alarms': return alarms.AlarmsController(), remainder else: pecan.abort(404) @staticmethod def _normalize_aodh_url(url): if url.endswith("/"): return url[:-1] return url
class V2Controller(object): """Version 2 API controller root.""" capabilities = capabilities.CapabilitiesController() def __init__(self): self._gnocchi_is_enabled = None self._aodh_is_enabled = None self._aodh_url = None self._panko_is_enabled = None self._panko_url = None @property def gnocchi_is_enabled(self): if self._gnocchi_is_enabled is None: if pecan.request.cfg.api.gnocchi_is_enabled is not None: self._gnocchi_is_enabled = ( pecan.request.cfg.api.gnocchi_is_enabled) elif ("gnocchi" not in pecan.request.cfg.meter_dispatchers or "database" in pecan.request.cfg.meter_dispatchers): self._gnocchi_is_enabled = False else: try: catalog = keystone_client.get_service_catalog( keystone_client.get_client(pecan.request.cfg)) catalog.url_for(service_type='metric') except exceptions.EndpointNotFound: self._gnocchi_is_enabled = False except exceptions.ClientException: LOG.warning("Can't connect to keystone, assuming " "gnocchi is disabled and retry later") else: self._gnocchi_is_enabled = True LOG.warning("ceilometer-api started with gnocchi " "enabled. The resources/meters/samples " "URLs are disabled.") return self._gnocchi_is_enabled @property def aodh_url(self): if self._aodh_url is None: if pecan.request.cfg.api.aodh_is_enabled is False: self._aodh_url = "" elif pecan.request.cfg.api.aodh_url is not None: self._aodh_url = self._normalize_url( pecan.request.cfg.api.aodh_url) else: try: catalog = keystone_client.get_service_catalog( keystone_client.get_client(pecan.request.cfg)) self._aodh_url = self._normalize_url( catalog.url_for(service_type='alarming')) except exceptions.EndpointNotFound: self._aodh_url = "" except exceptions.ClientException: LOG.warning("Can't connect to keystone, assuming aodh " "is disabled and retry later.") else: LOG.warning("ceilometer-api started with aodh " "enabled. Alarms URLs will be redirected " "to aodh endpoint.") return self._aodh_url @property def panko_url(self): if self._panko_url is None: if pecan.request.cfg.api.panko_is_enabled is False: self._panko_url = "" elif pecan.request.cfg.api.panko_url is not None: self._panko_url = self._normalize_url( pecan.request.cfg.api.panko_url) else: try: catalog = keystone_client.get_service_catalog( keystone_client.get_client(pecan.request.cfg)) self._panko_url = self._normalize_url( catalog.url_for(service_type='event')) except exceptions.EndpointNotFound: self._panko_url = "" except exceptions.ClientException: LOG.warning("Can't connect to keystone, assuming Panko " "is disabled and retry later.") else: LOG.warning("ceilometer-api started with Panko " "enabled. Events URLs will be redirected " "to Panko endpoint.") return self._panko_url @pecan.expose() def _lookup(self, kind, *remainder): if (kind in ['meters', 'resources', 'samples'] and self.gnocchi_is_enabled): if kind == 'meters' and pecan.request.method == 'POST': direct = pecan.request.params.get('direct', '') if strutils.bool_from_string(direct): pecan.abort( 400, _('direct option cannot be true when ' 'Gnocchi is enabled.')) return meters.MetersController(), remainder gnocchi_abort() elif kind == 'meters': return meters.MetersController(), remainder elif kind == 'resources': return resources.ResourcesController(), remainder elif kind == 'samples': return samples.SamplesController(), remainder elif kind == 'query': return QueryController( gnocchi_is_enabled=self.gnocchi_is_enabled, aodh_url=self.aodh_url, ), remainder elif kind == 'alarms' and (not self.aodh_url): aodh_abort() elif kind == 'alarms' and self.aodh_url: _redirect(self.aodh_url) elif kind == 'events' and self.panko_url: return _redirect(self.panko_url) elif kind == 'event_types' and self.panko_url: return _redirect(self.panko_url) else: pecan.abort(404) @staticmethod def _normalize_url(url): if url.endswith("/"): return url[:-1] return url