Merge "Add API schema for v2.1 "create a server" API"

This commit is contained in:
Jenkins 2014-09-03 03:13:30 +00:00 committed by Gerrit Code Review
commit 133e0a1f45
4 changed files with 49 additions and 45 deletions

View File

@ -343,20 +343,8 @@ class ServersController(wsgi.Controller):
# fixed IP address is optional
# if the fixed IP address is not provided then
# it will use one of the available IP address from the network
try:
request.address = network.get('fixed_ip', None)
except ValueError:
msg = (_("Invalid fixed IP address (%s)") %
networks.get('fixed_ip'))
raise exc.HTTPBadRequest(explanation=msg)
try:
request.port_id = network.get('port', None)
except ValueError:
msg = _("Bad port format: port uuid is "
"not in proper format "
"(%s)") % network.get('port')
raise exc.HTTPBadRequest(explanation=msg)
request.address = network.get('fixed_ip', None)
request.port_id = network.get('port', None)
if request.port_id:
request.network_id = None
@ -435,14 +423,7 @@ class ServersController(wsgi.Controller):
context = req.environ['nova.context']
server_dict = body['server']
password = self._get_server_admin_password(server_dict)
if 'name' not in server_dict:
msg = _("Server name is not defined")
raise exc.HTTPBadRequest(explanation=msg)
name = server_dict['name']
self._validate_server_name(name)
name = name.strip()
# Arguments to be passed to instance create function
create_kwargs = {}
@ -509,9 +490,6 @@ class ServersController(wsgi.Controller):
raise exc.HTTPForbidden(
explanation=error.format_message(),
headers={'Retry-After': 0})
except exception.InvalidMetadataSize as error:
raise exc.HTTPRequestEntityTooLarge(
explanation=error.format_message())
except exception.ImageNotFound as error:
msg = _("Can not find requested image")
raise exc.HTTPBadRequest(explanation=msg)
@ -813,12 +791,7 @@ class ServersController(wsgi.Controller):
raise exc.HTTPBadRequest(explanation=msg)
def _flavor_id_from_req_data(self, data):
try:
flavor_ref = data['server']['flavorRef']
except (TypeError, KeyError):
msg = _("Missing flavorRef attribute")
raise exc.HTTPBadRequest(explanation=msg)
flavor_ref = data['server']['flavorRef']
return common.get_id_from_href(flavor_ref)
@extensions.expected_errors((400, 401, 403, 404, 409))

View File

@ -12,17 +12,43 @@
# License for the specific language governing permissions and limitations
# under the License.
from nova.api.validation import parameter_types
base_create = {
'type': 'object',
'properties': {
'server': {
'type': 'object',
'properties': {
# TODO(oomichi): To focus the schema extension, now these
# properties are not defined. After it, we need to define
# them.
# 'name': ...
'name': parameter_types.name,
'imageRef': parameter_types.image_ref,
'flavorRef': parameter_types.flavor_ref,
'adminPass': parameter_types.admin_password,
'metadata': parameter_types.metadata,
'networks': {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'fixed_ip': {
'type': ['string', 'null'],
'oneOf': [
{'format': 'ipv4'},
{'format': 'ipv6'}
]
},
'port': {
'type': ['string', 'null'],
'format': 'uuid'
},
'uuid': {'type': 'string'},
},
'additionalProperties': False,
}
}
},
'required': ['name', 'flavorRef'],
# TODO(oomichi): After all extension schema patches are merged,
# this code should be enabled. If enabling before merger, API
# extension parameters would be considered as bad parameters.
@ -30,7 +56,7 @@ base_create = {
},
},
'required': ['server'],
# TODO(oomichi): ditto, enable here after all extension schema
# patches are merged.
# TODO(oomichi): Now v3 code will be used for v2.1 only and v2.1 needs
# to allow additionalProperties for some extensions.
# 'additionalProperties': False,
}

View File

@ -86,6 +86,11 @@ image_ref = {
}
flavor_ref = {
'type': ['string', 'integer'],
}
metadata = {
'type': 'object',
'patternProperties': {

View File

@ -1968,7 +1968,7 @@ class ServersControllerCreateTest(test.TestCase):
self.body['server']['min_count'] = 1
self.body['server']['imageRef'] = image_href,
self.req.body = jsonutils.dumps(self.body)
self.assertRaises(webob.exc.HTTPBadRequest,
self.assertRaises(exception.ValidationError,
self.controller.create,
self.req, body=self.body)
# TODO(cyeoh): bp-v3-api-unittests
@ -2200,7 +2200,7 @@ class ServersControllerCreateTest(test.TestCase):
self.body['server']['name'] = 'X' * 256
self.body['server']['imageRef'] = image_href
self.req.body = jsonutils.dumps(self.body)
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create,
self.assertRaises(exception.ValidationError, self.controller.create,
self.req, body=self.body)
def test_create_instance_name_all_blank_spaces(self):
@ -2224,7 +2224,7 @@ class ServersControllerCreateTest(test.TestCase):
req.method = 'POST'
req.body = jsonutils.dumps(body)
req.headers["content-type"] = "application/json"
self.assertRaises(webob.exc.HTTPBadRequest,
self.assertRaises(exception.ValidationError,
self.controller.create, req, body=body)
def test_create_instance(self):
@ -2296,7 +2296,7 @@ class ServersControllerCreateTest(test.TestCase):
self.body['server']['metadata'] = {('a' * 260): '12345'}
self.req.body = jsonutils.dumps(self.body)
self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
self.assertRaises(exception.ValidationError,
self.controller.create, self.req, body=self.body)
def test_create_instance_metadata_value_too_long(self):
@ -2305,7 +2305,7 @@ class ServersControllerCreateTest(test.TestCase):
self.body['server']['imageRef'] = image_href
self.body['server']['metadata'] = {'key1': ('a' * 260)}
self.req.body = jsonutils.dumps(self.body)
self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
self.assertRaises(exception.ValidationError,
self.controller.create, self.req, body=self.body)
def test_create_instance_metadata_key_blank(self):
@ -2314,7 +2314,7 @@ class ServersControllerCreateTest(test.TestCase):
self.body['server']['imageRef'] = image_href
self.body['server']['metadata'] = {'': 'abcd'}
self.req.body = jsonutils.dumps(self.body)
self.assertRaises(webob.exc.HTTPBadRequest,
self.assertRaises(exception.ValidationError,
self.controller.create, self.req, body=self.body)
def test_create_instance_metadata_not_dict(self):
@ -2323,7 +2323,7 @@ class ServersControllerCreateTest(test.TestCase):
self.body['server']['imageRef'] = image_href
self.body['server']['metadata'] = 'string'
self.req.body = jsonutils.dumps(self.body)
self.assertRaises(webob.exc.HTTPBadRequest,
self.assertRaises(exception.ValidationError,
self.controller.create, self.req, body=self.body)
def test_create_instance_metadata_key_not_string(self):
@ -2332,7 +2332,7 @@ class ServersControllerCreateTest(test.TestCase):
self.body['server']['imageRef'] = image_href
self.body['server']['metadata'] = {1: 'test'}
self.req.body = jsonutils.dumps(self.body)
self.assertRaises(webob.exc.HTTPBadRequest,
self.assertRaises(exception.ValidationError,
self.controller.create, self.req, body=self.body)
def test_create_instance_metadata_value_not_string(self):
@ -2341,7 +2341,7 @@ class ServersControllerCreateTest(test.TestCase):
self.body['server']['imageRef'] = image_href
self.body['server']['metadata'] = {'test': ['a', 'list']}
self.req.body = jsonutils.dumps(self.body)
self.assertRaises(webob.exc.HTTPBadRequest,
self.assertRaises(exception.ValidationError,
self.controller.create, self.req, body=self.body)
def test_create_user_data_malformed_bad_request(self):