Skip to content

Commit

Permalink
Merge "Move tags validation code to json schema"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Dec 14, 2016
2 parents 94fddf7 + 8f61868 commit bce8e4b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 44 deletions.
13 changes: 5 additions & 8 deletions nova/api/openstack/compute/schemas/server_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@
# License for the specific language governing permissions and limitations
# under the License.

from nova.api.validation import parameter_types
from nova.objects import instance

update_all = {
"definitions": {
"tag": {
"type": "string"
}
},
"title": "Server tags",
"type": "object",
"properties": {
"tags": {
"type": "array",
"items": {
"$ref": "#/definitions/tag"
}
"items": parameter_types.tag,
"maxItems": instance.MAX_TAG_COUNT
}
},
'required': ['tags'],
Expand Down
33 changes: 0 additions & 33 deletions nova/api/openstack/compute/server_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ def update(self, req, server_id, id, body):
% objects.instance.MAX_TAG_COUNT)
raise webob.exc.HTTPBadRequest(explanation=msg)

if len(id) > objects.tag.MAX_TAG_LENGTH:
msg = (_("Tag '%(tag)s' is too long. Maximum length of a tag "
"is %(length)d") % {'tag': id,
'length': objects.tag.MAX_TAG_LENGTH})
raise webob.exc.HTTPBadRequest(explanation=msg)

if id in _get_tags_names(tags):
# NOTE(snikitin): server already has specified tag
return webob.Response(status_int=204)
Expand All @@ -139,33 +133,6 @@ def update_all(self, req, server_id, body):
context.can(st_policies.POLICY_ROOT % 'update_all')
self._check_instance_in_valid_state(context, server_id, 'update tags')

invalid_tags = []
for tag in body['tags']:
try:
jsonschema.validate(tag, parameter_types.tag)
except jsonschema.ValidationError:
invalid_tags.append(tag)
if invalid_tags:
msg = (_("Tags '%s' are invalid. Each tag must be a string "
"without characters '/' and ','.") % invalid_tags)
raise webob.exc.HTTPBadRequest(explanation=msg)

tag_count = len(body['tags'])
if tag_count > objects.instance.MAX_TAG_COUNT:
msg = (_("The number of tags exceeded the per-server limit "
"%(max)d. The number of tags in request is %(count)d.")
% {'max': objects.instance.MAX_TAG_COUNT,
'count': tag_count})
raise webob.exc.HTTPBadRequest(explanation=msg)

long_tags = [
t for t in body['tags'] if len(t) > objects.tag.MAX_TAG_LENGTH]
if long_tags:
msg = (_("Tags %(tags)s are too long. Maximum length of a tag "
"is %(length)d") % {'tags': long_tags,
'length': objects.tag.MAX_TAG_LENGTH})
raise webob.exc.HTTPBadRequest(explanation=msg)

try:
tags = objects.TagList.create(context, server_id, body['tags'])
except exception.InstanceNotFound as e:
Expand Down
2 changes: 2 additions & 0 deletions nova/api/validation/parameter_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from nova import db
from nova.i18n import _
from nova.objects import tag


class ValidationRegex(object):
Expand Down Expand Up @@ -398,5 +399,6 @@ def valid_char(char):

tag = {
"type": "string",
"maxLength": tag.MAX_TAG_LENGTH,
"pattern": "^[^,/]*$"
}
8 changes: 5 additions & 3 deletions nova/tests/unit/api/openstack/compute/test_server_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,15 @@ def test_update_all_too_many_tags(self):

req = self._get_request(
'/v2/fake/servers/%s/tags' % UUID, 'PUT')
self.assertRaises(exc.HTTPBadRequest, self.controller.update_all,
self.assertRaises(exception.ValidationError,
self.controller.update_all,
req, UUID, body=fake_tags)

def test_update_all_forbidden_characters(self):
self.stub_out('nova.api.openstack.common.get_instance', return_server)
req = self._get_request('/v2/fake/servers/%s/tags' % UUID, 'PUT')
for tag in ['tag,1', 'tag/1']:
self.assertRaises(exc.HTTPBadRequest,
self.assertRaises(exception.ValidationError,
self.controller.update_all,
req, UUID, body={'tags': [tag, 'tag2']})

Expand All @@ -124,7 +125,8 @@ def test_update_all_too_long_tag(self):
self.stub_out('nova.api.openstack.common.get_instance', return_server)
req = self._get_request('/v2/fake/servers/%s/tags' % UUID, 'PUT')
tag = "a" * (tag_obj.MAX_TAG_LENGTH + 1)
self.assertRaises(exc.HTTPBadRequest, self.controller.update_all,
self.assertRaises(exception.ValidationError,
self.controller.update_all,
req, UUID, body={'tags': [tag]})

def test_update_all_invalid_tag_list_type(self):
Expand Down

0 comments on commit bce8e4b

Please sign in to comment.