# Path: aodh/api/controllers/v2/base.py
# class ClientSideError(wsme.exc.ClientSideError):
# class ProjectNotAuthorized(ClientSideError):
# class AdvEnum(wtypes.wsproperty):
# class Base(wtypes.DynamicBase):
# class Query(Base):
# class AlarmNotFound(ClientSideError):
# class AlarmRule(Base):
#     def __init__(self, error, status_code=400):
#     def __init__(self, id, aspect='project'):
#     def __init__(self, name, *args, **kwargs):
#     def _get(self, parent):
#     def _set(self, parent, value):
#     def from_db_model(cls, m):
#     def from_db_and_links(cls, m, links):
#     def as_dict(self, db_model):
#     def as_dict_from_keys(self, keys):
#     def to_dict(self):
#     def get_op(self):
#     def set_op(self, value):
#     def __repr__(self):
#     def sample(cls):
#     def as_dict(self):
#     def get_value(self, forced_type=None):
#     def __init__(self, alarm, auth_project):
#     def validate_alarm(alarm):
#     def create_hook(alarm):
#     def update_hook(alarm):
# 
# Path: aodh/i18n.py
# DOMAIN = 'aodh'
# def translate(value, user_locale):
# def get_available_languages():


import voluptuous
import wsme
from wsme import types as wtypes
from aodh.api.controllers.v2 import base
from aodh.i18n import _


class AlarmEventRule(base.AlarmRule):
    """Alarm Event Rule.

    Describe when to trigger the alarm based on an event
    """

    event_type = wsme.wsattr(wtypes.text)
    "The type of event (default is '*')"

    query = wsme.wsattr([base.Query])
    "The query to find the event (default is [])"

    def __init__(self, event_type=None, query=None):
        event_type = event_type or '*'
        query = [base.Query(**q) for q in query or []]
        super(AlarmEventRule, self).__init__(event_type=event_type,
                                             query=query)

    @classmethod
    def validate_alarm(cls, alarm):
        super(AlarmEventRule, cls).validate_alarm(alarm)
        for i in alarm.event_rule.query:
            i.get_value()
            try:
                _q_validator({"field": i.field, "op": i.op,
                              "value": i.type})
            except voluptuous.MultipleInvalid as e:
                raise base.ClientSideError(

# Path: aodh/api/controllers/v2/base.py
# class ClientSideError(wsme.exc.ClientSideError):
# class ProjectNotAuthorized(ClientSideError):
# class AdvEnum(wtypes.wsproperty):
# class Base(wtypes.DynamicBase):
# class Query(Base):
# class AlarmNotFound(ClientSideError):
# class AlarmRule(Base):
#     def __init__(self, error, status_code=400):
#     def __init__(self, id, aspect='project'):
#     def __init__(self, name, *args, **kwargs):
#     def _get(self, parent):
#     def _set(self, parent, value):
#     def from_db_model(cls, m):
#     def from_db_and_links(cls, m, links):
#     def as_dict(self, db_model):
#     def as_dict_from_keys(self, keys):
#     def to_dict(self):
#     def get_op(self):
#     def set_op(self, value):
#     def __repr__(self):
#     def sample(cls):
#     def as_dict(self):
#     def get_value(self, forced_type=None):
#     def __init__(self, alarm, auth_project):
#     def validate_alarm(alarm):
#     def create_hook(alarm):
#     def update_hook(alarm):
# 
# Path: aodh/i18n.py
# DOMAIN = 'aodh'
# def translate(value, user_locale):
# def get_available_languages():


# Path: aodh/api/controllers/v2/alarm_rules/event.py
import voluptuous
import wsme
from wsme import types as wtypes
from aodh.api.controllers.v2 import base
from aodh.i18n import _



class AlarmEventRule(base.AlarmRule):
    """Alarm Event Rule.

    Describe when to trigger the alarm based on an event
    """

    event_type = wsme.wsattr(wtypes.text)
    "The type of event (default is '*')"

    query = wsme.wsattr([base.Query])
    "The query to find the event (default is [])"

    def __init__(self, event_type=None, query=None):
        event_type = event_type or '*'
        query = [base.Query(**q) for q in query or []]
        super(AlarmEventRule, self).__init__(event_type=event_type,
                                             query=query)

    @classmethod
    def validate_alarm(cls, alarm):
        super(AlarmEventRule, cls).validate_alarm(alarm)
        for i in alarm.event_rule.query:
            i.get_value()
            try:
                _q_validator({"field": i.field, "op": i.op,
                              "value": i.type})
            except voluptuous.MultipleInvalid as e:
                raise base.ClientSideError(

                    _("Query value or traits invalid: %s") % str(e))