コード例 #1
0
def test_description():
    marker = Marker(Schema(str), description='Hello')
    assert marker.description == 'Hello'

    optional = Optional('key', description='Hello')
    assert optional.description == 'Hello'

    exclusive = Exclusive('alpha', 'angles', description='Hello')
    assert exclusive.description == 'Hello'

    required = Required('key', description='Hello')
    assert required.description == 'Hello'
コード例 #2
0
from voluptuous import Length
from voluptuous import Marker
from voluptuous import Required
from voluptuous import Schema

from monasca_api.v2.common.schemas import exceptions

LOG = log.getLogger(__name__)

schemes = ['http', 'https']

notification_schema = {
    Required('name'): Schema(All(Any(str, six.text_type), Length(max=250))),
    Required('type'): Schema(Any(str, six.text_type)),
    Required('address'): Schema(All(Any(str, six.text_type), Length(max=512))),
    Marker('period'): All(Any(int, str))
}

request_body_schema = Schema(Any(notification_schema))


def parse_and_validate(msg, valid_periods, require_all=False):
    try:
        request_body_schema(msg)
    except Exception as ex:
        LOG.exception(ex)
        raise exceptions.ValidationException(str(ex))

    if 'period' not in msg:
        if require_all:
            raise exceptions.ValidationException("Period is required")
コード例 #3
0
def list_item_length(v):
    if not isinstance(v, list):
        raise Invalid('Not a list: {}'.format(type(v)))
    for i in v:
        if not isinstance(i, (str, unicode)):
            raise Invalid(
                'list item <{}> -> {} not one of (str, unicode)'.format(
                    i, type(i)))
        if len(i) > MAX_ITEM_LENGTH:
            raise Invalid('length {} > {}'.format(len(i), MAX_ITEM_LENGTH))


alarm_definition_schema = {
    Required('name'): All(Any(str, unicode), Length(max=255)),
    Required('expression'): All(Any(str, unicode)),
    Marker('description'): All(Any(str, unicode), Length(max=255)),
    Marker('severity'): All(Upper, Any('LOW', 'MEDIUM', 'HIGH', 'CRITICAL')),
    Marker('match_by'): Any([unicode], [str]),
    Marker('ok_actions'): list_item_length,
    Marker('alarm_actions'): list_item_length,
    Marker('undetermined_actions'): list_item_length,
    Marker('actions_enabled'): bool
}


def validate(msg, require_all=False):
    try:
        request_body_schema = Schema(alarm_definition_schema,
                                     required=require_all,
                                     extra=True)
        request_body_schema(msg)
コード例 #4
0
ファイル: contract.py プロジェクト: worldomonation/mozci
         Optional("classification_note"):
         str,
         Optional("duration"):
         int,
     }]),
 ),
 Contract(
     name="push_tasks_tags",
     description=
     "Data about the tags associated with tasks on a given push.",
     validate_in=Schema({
         Required("branch"): str,
         Required("rev"): str,
     }),
     validate_out=Schema({
         Marker(str, description="task id"): {
             Marker(str, description="tag name"):
             Marker(str, description="tag value")
         }
     }),
 ),
 Contract(
     name="push_test_groups",
     description="Data about the test groups that ran on a given push.",
     validate_in=Schema({
         Required("branch"): str,
         Required("rev"): str,
     }),
     validate_out=Schema({
         Marker(str, description="task id"): {
             Marker(str, description="group name"):
コード例 #5
0
def validate_ok_action_list(v):
    validate_action_list(v, 'OK')


def validate_alarm_action_list(v):
    validate_action_list(v, 'ALARM')


def validate_undetermined_action_list(v):
    validate_action_list(v, 'UNDETERMINED')


alarm_definition_schema = {
    Required('name'): All(Any(str, six.text_type), Length(max=255)),
    Required('expression'): All(Any(str, six.text_type)),
    Marker('description'): All(Any(str, six.text_type), Length(max=255)),
    Marker('severity'): All(Upper, Any('LOW', 'MEDIUM', 'HIGH', 'CRITICAL')),
    Marker('match_by'): Any([six.text_type], [str]),
    Marker('ok_actions'): validate_ok_action_list,
    Marker('alarm_actions'): validate_alarm_action_list,
    Marker('undetermined_actions'): validate_undetermined_action_list,
    Marker('actions_enabled'): bool
}


def validate(msg, require_all=False):
    try:
        request_body_schema = Schema(alarm_definition_schema,
                                     required=require_all,
                                     extra=True)
        request_body_schema(msg)