diff --git a/gnocchi/resource_type.py b/gnocchi/resource_type.py index 367acb88f..79c0e8b7f 100644 --- a/gnocchi/resource_type.py +++ b/gnocchi/resource_type.py @@ -40,6 +40,16 @@ class InvalidResourceAttributeName(Exception): self.name = name +class InvalidResourceAttributeValue(ValueError): + """Error raised when the resource attribute min is greater than max""" + def __init__(self, min, max): + super(InvalidResourceAttributeValue, self).__init__( + "Resource attribute value min (or min_length) %s must be less " + "than or equal to max (or max_length) %s!" % (str(min), str(max))) + self.min = min + self.max = max + + class CommonAttributeSchema(object): meta_schema_ext = {} schema_ext = None @@ -80,10 +90,12 @@ class StringSchema(CommonAttributeSchema): def __init__(self, min_length, max_length, *args, **kwargs): super(StringSchema, self).__init__(*args, **kwargs) + if min_length > max_length: + raise InvalidResourceAttributeValue(min_length, max_length) + self.min_length = min_length self.max_length = max_length - # TODO(sileht): ensure min_length <= max_length meta_schema_ext = { voluptuous.Required('min_length', default=0): voluptuous.All(int, voluptuous.Range(min=0, max=255)), @@ -115,10 +127,12 @@ class NumberSchema(CommonAttributeSchema): def __init__(self, min, max, *args, **kwargs): super(NumberSchema, self).__init__(*args, **kwargs) + if max is not None and min > max: + raise InvalidResourceAttributeValue(min, max) + self.min = min self.max = max - # TODO(sileht): ensure min_length <= max_length meta_schema_ext = { voluptuous.Required('min', default=None): voluptuous.Any( None, numbers.Real), diff --git a/gnocchi/rest/__init__.py b/gnocchi/rest/__init__.py index f80e0a0f9..3ac684038 100644 --- a/gnocchi/rest/__init__.py +++ b/gnocchi/rest/__init__.py @@ -790,6 +790,8 @@ class ResourceTypesController(rest.RestController): rt = schema.resource_type_from_dict(**body) except resource_type.InvalidResourceAttributeName as e: abort(400, e) + except resource_type.InvalidResourceAttributeValue as e: + abort(400, e) enforce("create resource type", body) try: diff --git a/gnocchi/tests/gabbi/gabbits/resource-type.yaml b/gnocchi/tests/gabbi/gabbits/resource-type.yaml index 2204bbaac..9fa32a5cd 100644 --- a/gnocchi/tests/gabbi/gabbits/resource-type.yaml +++ b/gnocchi/tests/gabbi/gabbits/resource-type.yaml @@ -58,6 +58,36 @@ tests: # - "Invalid input: not a valid value for dictionary value @ data[u'attributes'][u'foo'][u'type']" - "Invalid input:" + - name: post resource type bad min_length value + POST: $LAST_URL + request_headers: + x-roles: admin + content-type: application/json + data: + name: my_custom_resource + attributes: + name: + type: string + required: true + max_length: 2 + min_length: 5 + status: 400 + + - name: post resource type bad min value + POST: $LAST_URL + request_headers: + x-roles: admin + content-type: application/json + data: + name: my_custom_resource + attributes: + int: + type: number + required: false + max: 3 + min: 8 + status: 400 + # Create a type - name: post resource type