"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-4.0.0/monasca_api/v2/common/schemas/notifications_request_body_schema.py" (13 May 2020, 3362 Bytes) of package /linux/misc/openstack/monasca-api-4.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 "notifications_request_body_schema.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.1.0_vs_4.0.0.

    1 # (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP
    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 monasca_api.v2.common.validation as validation
   16 from oslo_log import log
   17 import six
   18 import six.moves.urllib.parse as urlparse
   19 from voluptuous import All
   20 from voluptuous import Any
   21 from voluptuous import Length
   22 from voluptuous import Marker
   23 from voluptuous import Required
   24 from voluptuous import Schema
   25 
   26 from monasca_api.v2.common.schemas import exceptions
   27 
   28 LOG = log.getLogger(__name__)
   29 
   30 schemes = ['http', 'https']
   31 
   32 notification_schema = {
   33     Required('name'): Schema(All(Any(str, six.text_type), Length(max=250))),
   34     Required('type'): Schema(Any(str, six.text_type)),
   35     Required('address'): Schema(All(Any(str, six.text_type), Length(max=512))),
   36     Marker('period'): All(Any(int, str))}
   37 
   38 request_body_schema = Schema(Any(notification_schema))
   39 
   40 
   41 def parse_and_validate(msg, valid_periods, require_all=False):
   42     try:
   43         request_body_schema(msg)
   44     except Exception as ex:
   45         LOG.exception(ex)
   46         raise exceptions.ValidationException(str(ex))
   47 
   48     if 'period' not in msg:
   49         if require_all:
   50             raise exceptions.ValidationException("Period is required")
   51         else:
   52             msg['period'] = 0
   53     else:
   54         msg['period'] = _parse_and_validate_period(msg['period'], valid_periods)
   55 
   56     notification_type = str(msg['type']).upper()
   57 
   58     if notification_type == 'EMAIL':
   59         _validate_email(msg['address'])
   60     elif notification_type == 'WEBHOOK':
   61         _validate_url(msg['address'])
   62 
   63 
   64 def _validate_email(address):
   65     if not validation.validate_email_address(address):
   66         raise exceptions.ValidationException("Address {} is not of correct format".format(address))
   67 
   68 
   69 def _validate_url(address):
   70     try:
   71         parsed = urlparse.urlparse(address)
   72     except Exception:
   73         raise exceptions.ValidationException("Address {} is not of correct format".format(address))
   74 
   75     if not parsed.scheme:
   76         raise exceptions.ValidationException("Address {} does not have URL scheme".format(address))
   77     if not parsed.netloc:
   78         raise exceptions.ValidationException("Address {} does not have network location"
   79                                              .format(address))
   80     if parsed.scheme not in schemes:
   81         raise exceptions.ValidationException("Address {} scheme is not in {}"
   82                                              .format(address, schemes))
   83 
   84 
   85 def _parse_and_validate_period(period, valid_periods):
   86     try:
   87         period = int(period)
   88     except Exception:
   89         raise exceptions.ValidationException("Period {} must be a valid integer".format(period))
   90     if period != 0 and period not in valid_periods:
   91         raise exceptions.ValidationException(
   92             "{} is not in the configured list of valid periods: {}".format(period, valid_periods))
   93     return period