Merge "Validation Check for 'query' params of alarm type 'event'"

This commit is contained in:
Zuul 2018-07-13 07:58:36 +00:00 committed by Gerrit Code Review
commit 444817f0de
3 changed files with 82 additions and 0 deletions

View File

@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import voluptuous
import wsme
from wsme import types as wtypes
@ -20,6 +21,13 @@ from aodh.api.controllers.v2 import base
from aodh.i18n import _
# Schema validation for the event type query.
_q_validator = voluptuous.Schema(
{"field": voluptuous.Match(r"^[a-zA-Z.',0-9_-]*$"),
"op": voluptuous.In(base.operation_kind),
"value": voluptuous.In(["string", "integer", "float", "boolean", ""])})
class AlarmEventRule(base.AlarmRule):
"""Alarm Event Rule.
@ -40,8 +48,15 @@ class AlarmEventRule(base.AlarmRule):
@classmethod
def validate_alarm(cls, alarm):
super(AlarmEventRule, cls).validate_alarm(alarm)
for i in alarm.event_rule.query:
i._get_value_as_type()
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))
@property
def default_description(self):

View File

@ -28,6 +28,72 @@ tests:
response_headers:
allow: GET, POST
- name: try to POST an event type alarm
desc: what does POST response be
POST: /v2/alarms
request_headers:
content-type: application/json
data:
name: instance_off
type: event
event_rule:
query: [{'field': "{=:", 'op': "eq", 'type': "string", 'value': "sample_string"}]
status: 400
response_strings:
- "Query value or traits invalid:"
- name: try to POST an event type alarm2
desc: what does POST response be
POST: /v2/alarms
request_headers:
content-type: application/json
data:
name: instance_off
type: event
event_rule:
query: [{'field': "traits.instance_id", 'op': "eq", 'type': "", 'value': "default_string_datatype_isconsidered"}]
status: 201
- name: try to POST an event type alarm3
desc: what does POST response be
POST: /v2/alarms
request_headers:
content-type: application/json
data:
name: instance_off
type: event
event_rule:
query: [{'field': "traits.instance_id", 'op': "lt", 'type': "integer", 'value': "1234567"}]
status: 201
- name: try to POST an event type alarm4
desc: what does POST response be
POST: /v2/alarms
request_headers:
content-type: application/json
data:
name: instance_off
type: event
event_rule:
query: [{'field': "traits.instance_id", 'op': "lt", 'type': "integer", 'value': "hello"}]
status: 400
response_strings:
- "Unable to convert the value hello to the expected data type integer"
- name: try to POST an event type alarm5
desc: what does POST response be
POST: /v2/alarms
request_headers:
content-type: application/json
data:
name: instance_off
type: event
event_rule:
query: [{'field': "traits.instance_id", 'op': "ltt", 'type': "integer", 'value': "1234567"}]
status: 400
response_strings:
- "Query value or traits invalid:"
- name: createAlarm
desc: Creates an alarm.
POST: /v2/alarms

View File

@ -27,6 +27,7 @@ requests>=2.5.2
six>=1.9.0
stevedore>=1.5.0 # Apache-2.0
tooz>=1.28.0 # Apache-2.0
voluptuous>=0.8.10
WebOb>=1.2.3
WSME>=0.8
cachetools>=1.1.6