"Fossies" - the Fresh Open Source Software Archive

Member "monasca-events-api-2.0.0/monasca_events_api/app/controller/v1/events.py" (14 Oct 2020, 2925 Bytes) of package /linux/misc/openstack/monasca-events-api-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "events.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.2.0_vs_0.3.0.

    1 # Copyright 2018 FUJITSU LIMITED
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # a copy of the License at
    6 #
    7 #      http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 import falcon
   16 
   17 from monasca_events_api.app.common import helpers
   18 from monasca_events_api.app.controller.v1 import body_validation
   19 from monasca_events_api.app.controller.v1 import bulk_processor
   20 from monasca_events_api.app.core.model import prepare_message_to_sent
   21 from oslo_log import log
   22 
   23 LOG = log.getLogger(__name__)
   24 
   25 
   26 class Events(object):
   27     """Events.
   28 
   29     Events acts as a RESTful endpoint accepting messages contains
   30     collected events from the OpenStack message bus.
   31     Works as getaway for any further processing for accepted data.
   32     """
   33     VERSION = 'v1.0'
   34     SUPPORTED_CONTENT_TYPES = {'application/json'}
   35 
   36     def __init__(self):
   37         super(Events, self).__init__()
   38 
   39         self._processor = bulk_processor.EventsBulkProcessor()
   40 
   41     def on_post(self, req, res):
   42         """Accepts sent events as json.
   43 
   44         Accepts events sent to resource which should be sent
   45         to Kafka queue.
   46 
   47         :param req: current request
   48         :param res: current response
   49         """
   50         policy_action = 'events_api:agent_required'
   51 
   52         try:
   53             req.validate(self.SUPPORTED_CONTENT_TYPES)
   54             request_body = helpers.read_json_msg_body(req)
   55             req.can(policy_action)
   56             project_id = req.project_id
   57             body_validation.validate_body(request_body)
   58             messages = prepare_message_to_sent(request_body)
   59             self._processor.send_message(messages, event_project_id=project_id)
   60             res.status = falcon.HTTP_200
   61         except falcon.HTTPUnprocessableEntity as ex:
   62             LOG.error('Entire bulk package was rejected, unsupported body')
   63             LOG.exception(ex)
   64             raise ex
   65         except falcon.HTTPUnsupportedMediaType as ex:
   66             LOG.error('Entire bulk package was rejected, '
   67                       'unsupported media type')
   68             LOG.exception(ex)
   69             raise ex
   70         except Exception as ex:
   71             LOG.error('Entire bulk package was rejected')
   72             LOG.exception(ex)
   73             _title = ex.title if hasattr(ex, 'title') else None
   74             _descr = ex.description if hasattr(ex, 'description') else None
   75             raise falcon.HTTPError(falcon.HTTP_400,
   76                                    title=_title, description=_descr)
   77 
   78     @property
   79     def version(self):
   80         return getattr(self, 'VERSION')