Add API schema for v2.1/v3 remote_consoles API

By defining the API schema, it is possible to separate the validation
code from the API method. The API method can be more simple.
In addition, a response of API validation error can be consistent for
the whole Nova API.

Partially implements blueprint v3-api-schema

Change-Id: Icdb1f87903e3c4345e6533c08b2f2d2f9d36316e
This commit is contained in:
Haiwei Xu 2013-12-26 22:25:35 +09:00
parent b2ce11bab0
commit 383caeb889
3 changed files with 103 additions and 0 deletions

View File

@ -15,8 +15,10 @@
import webob
from nova.api.openstack import common
from nova.api.openstack.compute.schemas.v3 import remote_consoles
from nova.api.openstack import extensions
from nova.api.openstack import wsgi
from nova.api import validation
from nova import compute
from nova import exception
from nova.openstack.common.gettextutils import _
@ -33,6 +35,7 @@ class RemoteConsolesController(wsgi.Controller):
@extensions.expected_errors((400, 404, 409, 501))
@wsgi.action('get_vnc_console')
@validation.schema(remote_consoles.get_vnc_console)
def get_vnc_console(self, req, id, body):
"""Get text console output."""
context = req.environ['nova.context']
@ -63,6 +66,7 @@ class RemoteConsolesController(wsgi.Controller):
@extensions.expected_errors((400, 404, 409, 501))
@wsgi.action('get_spice_console')
@validation.schema(remote_consoles.get_spice_console)
def get_spice_console(self, req, id, body):
"""Get text console output."""
context = req.environ['nova.context']
@ -94,6 +98,7 @@ class RemoteConsolesController(wsgi.Controller):
@extensions.expected_errors((400, 404, 409, 501))
@wsgi.action('get_rdp_console')
@validation.schema(remote_consoles.get_rdp_console)
def get_rdp_console(self, req, id, body):
"""Get text console output."""
context = req.environ['nova.context']

View File

@ -0,0 +1,70 @@
# Copyright 2014 NEC Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
get_vnc_console = {
'type': 'object',
'properties': {
'get_vnc_console': {
'type': 'object',
'properties': {
'type': {
'type': 'string',
'enum': ['novnc', 'xvpvnc'],
},
},
'required': ['type'],
'additionalProperties': False,
},
},
'required': ['get_vnc_console'],
'additionalProperties': False,
}
get_spice_console = {
'type': 'object',
'properties': {
'get_spice_console': {
'type': 'object',
'properties': {
'type': {
'type': 'string',
'enum': ['spice-html5'],
},
},
'required': ['type'],
'additionalProperties': False,
},
},
'required': ['get_spice_console'],
'additionalProperties': False,
}
get_rdp_console = {
'type': 'object',
'properties': {
'get_rdp_console': {
'type': 'object',
'properties': {
'type': {
'type': 'string',
'enum': ['rdp-html5'],
},
},
'required': ['type'],
'additionalProperties': False,
},
},
'required': ['get_rdp_console'],
'additionalProperties': False,
}

View File

@ -209,6 +209,15 @@ class ConsolesExtensionTest(test.NoDBTestCase):
res = req.get_response(self.app)
self.assertEqual(res.status_int, 501)
def test_get_vnc_console_with_undefined_param(self):
body = {'get_vnc_console': {'type': 'novnc', 'undefined': 'foo'}}
req = webob.Request.blank('/v3/servers/1/action')
req.method = "POST"
req.body = jsonutils.dumps(body)
req.headers["content-type"] = "application/json"
res = req.get_response(self.app)
self.assertEqual(400, res.status_int)
def test_get_spice_console(self):
body = {'get_spice_console': {'type': 'spice-html5'}}
req = webob.Request.blank('/v3/servers/1/action')
@ -307,6 +316,16 @@ class ConsolesExtensionTest(test.NoDBTestCase):
res = req.get_response(self.app)
self.assertEqual(400, res.status_int)
def test_get_spice_console_with_undefined_param(self):
body = {'get_spice_console': {'type': 'spice-html5',
'undefined': 'foo'}}
req = webob.Request.blank('/v3/servers/1/action')
req.method = "POST"
req.body = jsonutils.dumps(body)
req.headers["content-type"] = "application/json"
res = req.get_response(self.app)
self.assertEqual(400, res.status_int)
def test_get_rdp_console(self):
body = {'get_rdp_console': {'type': 'rdp-html5'}}
req = webob.Request.blank('/v3/servers/1/action')
@ -391,3 +410,12 @@ class ConsolesExtensionTest(test.NoDBTestCase):
res = req.get_response(self.app)
self.assertEqual(400, res.status_int)
def test_get_rdp_console_with_undefined_param(self):
body = {'get_rdp_console': {'type': 'rdp-html5', 'undefined': 'foo'}}
req = webob.Request.blank('/v3/servers/1/action')
req.method = "POST"
req.body = jsonutils.dumps(body)
req.headers["content-type"] = "application/json"
res = req.get_response(self.app)
self.assertEqual(400, res.status_int)