Merge "Move tags validation code to json schema"
This commit is contained in:
commit
bce8e4b9e3
@ -10,20 +10,17 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
from nova.api.validation import parameter_types
|
||||||
|
from nova.objects import instance
|
||||||
|
|
||||||
update_all = {
|
update_all = {
|
||||||
"definitions": {
|
|
||||||
"tag": {
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"title": "Server tags",
|
"title": "Server tags",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"tags": {
|
"tags": {
|
||||||
"type": "array",
|
"type": "array",
|
||||||
"items": {
|
"items": parameter_types.tag,
|
||||||
"$ref": "#/definitions/tag"
|
"maxItems": instance.MAX_TAG_COUNT
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'required': ['tags'],
|
'required': ['tags'],
|
||||||
|
@ -109,12 +109,6 @@ class ServerTagsController(wsgi.Controller):
|
|||||||
% objects.instance.MAX_TAG_COUNT)
|
% objects.instance.MAX_TAG_COUNT)
|
||||||
raise webob.exc.HTTPBadRequest(explanation=msg)
|
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):
|
if id in _get_tags_names(tags):
|
||||||
# NOTE(snikitin): server already has specified tag
|
# NOTE(snikitin): server already has specified tag
|
||||||
return webob.Response(status_int=204)
|
return webob.Response(status_int=204)
|
||||||
@ -139,33 +133,6 @@ class ServerTagsController(wsgi.Controller):
|
|||||||
context.can(st_policies.POLICY_ROOT % 'update_all')
|
context.can(st_policies.POLICY_ROOT % 'update_all')
|
||||||
self._check_instance_in_valid_state(context, server_id, 'update tags')
|
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:
|
try:
|
||||||
tags = objects.TagList.create(context, server_id, body['tags'])
|
tags = objects.TagList.create(context, server_id, body['tags'])
|
||||||
except exception.InstanceNotFound as e:
|
except exception.InstanceNotFound as e:
|
||||||
|
@ -23,6 +23,7 @@ import six
|
|||||||
|
|
||||||
from nova import db
|
from nova import db
|
||||||
from nova.i18n import _
|
from nova.i18n import _
|
||||||
|
from nova.objects import tag
|
||||||
|
|
||||||
|
|
||||||
class ValidationRegex(object):
|
class ValidationRegex(object):
|
||||||
@ -398,5 +399,6 @@ personality = {
|
|||||||
|
|
||||||
tag = {
|
tag = {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
|
"maxLength": tag.MAX_TAG_LENGTH,
|
||||||
"pattern": "^[^,/]*$"
|
"pattern": "^[^,/]*$"
|
||||||
}
|
}
|
||||||
|
@ -103,14 +103,15 @@ class ServerTagsTest(test.TestCase):
|
|||||||
|
|
||||||
req = self._get_request(
|
req = self._get_request(
|
||||||
'/v2/fake/servers/%s/tags' % UUID, 'PUT')
|
'/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)
|
req, UUID, body=fake_tags)
|
||||||
|
|
||||||
def test_update_all_forbidden_characters(self):
|
def test_update_all_forbidden_characters(self):
|
||||||
self.stub_out('nova.api.openstack.common.get_instance', return_server)
|
self.stub_out('nova.api.openstack.common.get_instance', return_server)
|
||||||
req = self._get_request('/v2/fake/servers/%s/tags' % UUID, 'PUT')
|
req = self._get_request('/v2/fake/servers/%s/tags' % UUID, 'PUT')
|
||||||
for tag in ['tag,1', 'tag/1']:
|
for tag in ['tag,1', 'tag/1']:
|
||||||
self.assertRaises(exc.HTTPBadRequest,
|
self.assertRaises(exception.ValidationError,
|
||||||
self.controller.update_all,
|
self.controller.update_all,
|
||||||
req, UUID, body={'tags': [tag, 'tag2']})
|
req, UUID, body={'tags': [tag, 'tag2']})
|
||||||
|
|
||||||
@ -124,7 +125,8 @@ class ServerTagsTest(test.TestCase):
|
|||||||
self.stub_out('nova.api.openstack.common.get_instance', return_server)
|
self.stub_out('nova.api.openstack.common.get_instance', return_server)
|
||||||
req = self._get_request('/v2/fake/servers/%s/tags' % UUID, 'PUT')
|
req = self._get_request('/v2/fake/servers/%s/tags' % UUID, 'PUT')
|
||||||
tag = "a" * (tag_obj.MAX_TAG_LENGTH + 1)
|
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]})
|
req, UUID, body={'tags': [tag]})
|
||||||
|
|
||||||
def test_update_all_invalid_tag_list_type(self):
|
def test_update_all_invalid_tag_list_type(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user