Convert console_output v3 plugin to v2.1
Changes required to have v3 plugin natively support the v2 API The get_console_output action is reverted to os-getConsoleOutput Partially implements blueprint v2-on-v3-api Change-Id: I71b0d5fd1a098052f8e11f60a19113b6d647ef2d
This commit is contained in:
parent
b4d2e58f01
commit
0c941a212b
@ -1,5 +1,5 @@
|
||||
{
|
||||
"get_console_output": {
|
||||
"os-getConsoleOutput": {
|
||||
"length": 50
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,8 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import re
|
||||
|
||||
import webob
|
||||
|
||||
from nova.api.openstack import common
|
||||
@ -35,7 +37,7 @@ class ConsoleOutputController(wsgi.Controller):
|
||||
self.compute_api = compute.API()
|
||||
|
||||
@extensions.expected_errors((400, 404, 409, 501))
|
||||
@wsgi.action('get_console_output')
|
||||
@wsgi.action('os-getConsoleOutput')
|
||||
@validation.schema(console_output.get_console_output)
|
||||
def get_console_output(self, req, id, body):
|
||||
"""Get text console output."""
|
||||
@ -44,22 +46,32 @@ class ConsoleOutputController(wsgi.Controller):
|
||||
|
||||
instance = common.get_instance(self.compute_api, context, id,
|
||||
want_objects=True)
|
||||
length = body['get_console_output'].get('length')
|
||||
if length is not None and int(length) == -1:
|
||||
# NOTE: -1 means an unlimited length. So here translates it to None
|
||||
# which also means an unlimited in the internal implementation.
|
||||
length = None
|
||||
length = body['os-getConsoleOutput'].get('length')
|
||||
# TODO(cyeoh): In a future API update accept a length of -1
|
||||
# as meaning unlimited length (convert to None)
|
||||
|
||||
try:
|
||||
output = self.compute_api.get_console_output(context,
|
||||
instance,
|
||||
length)
|
||||
# NOTE(cyeoh): This covers race conditions where the instance is
|
||||
# deleted between common.get_instance and get_console_output
|
||||
# being called
|
||||
except exception.InstanceNotFound as e:
|
||||
raise webob.exc.HTTPNotFound(explanation=e.format_message())
|
||||
except exception.InstanceNotReady as e:
|
||||
raise webob.exc.HTTPConflict(explanation=e.format_message())
|
||||
except NotImplementedError:
|
||||
msg = _("Unable to get console log, functionality not implemented")
|
||||
raise webob.exc.HTTPNotImplemented(explanation=msg)
|
||||
|
||||
# XML output is not correctly escaped, so remove invalid characters
|
||||
# NOTE(cyeoh): We don't support XML output with V2.1, but for
|
||||
# backwards compatibility reasons we continue to filter the output
|
||||
# We should remove this in the future
|
||||
remove_re = re.compile('[\x00-\x08\x0B-\x1F]')
|
||||
output = remove_re.sub('', output)
|
||||
|
||||
return {'output': output}
|
||||
|
||||
|
||||
|
@ -15,19 +15,22 @@
|
||||
get_console_output = {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'get_console_output': {
|
||||
'os-getConsoleOutput': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'length': {
|
||||
'type': ['integer', 'string'],
|
||||
'type': ['integer', 'string', 'null'],
|
||||
'pattern': '^-?[0-9]+$',
|
||||
# NOTE: -1 means an unlimited length.
|
||||
# TODO(cyeoh): None also means unlimited length
|
||||
# and is supported for v2 backwards compatibility
|
||||
# Should remove in the future with a microversion
|
||||
'minimum': -1,
|
||||
},
|
||||
},
|
||||
'additionalProperties': False,
|
||||
},
|
||||
},
|
||||
'required': ['get_console_output'],
|
||||
'required': ['os-getConsoleOutput'],
|
||||
'additionalProperties': False,
|
||||
}
|
||||
|
@ -15,8 +15,6 @@
|
||||
|
||||
import string
|
||||
|
||||
import webob
|
||||
|
||||
from nova.compute import api as compute_api
|
||||
from nova import exception
|
||||
from nova.openstack.common import jsonutils
|
||||
@ -46,32 +44,34 @@ def fake_get_console_output_all_characters(self, _ctx, _instance, _tail_len):
|
||||
return string.printable
|
||||
|
||||
|
||||
def fake_get(self, context, instance_uuid, want_objects=False):
|
||||
def fake_get(self, context, instance_uuid, want_objects=False,
|
||||
expected_attrs=None):
|
||||
return fake_instance.fake_instance_obj(context, **{'uuid': instance_uuid})
|
||||
|
||||
|
||||
def fake_get_not_found(*args, **kwargs):
|
||||
raise exception.NotFound()
|
||||
raise exception.InstanceNotFound(instance_id='fake')
|
||||
|
||||
|
||||
class ConsoleOutputExtensionTest(test.NoDBTestCase):
|
||||
class ConsoleOutputExtensionTestV21(test.NoDBTestCase):
|
||||
application_type = "application/json"
|
||||
action_url = '/v3/servers/1/action'
|
||||
|
||||
def setUp(self):
|
||||
super(ConsoleOutputExtensionTest, self).setUp()
|
||||
super(ConsoleOutputExtensionTestV21, self).setUp()
|
||||
self.stubs.Set(compute_api.API, 'get_console_output',
|
||||
fake_get_console_output)
|
||||
self.stubs.Set(compute_api.API, 'get', fake_get)
|
||||
self.flags(
|
||||
osapi_compute_extension=[
|
||||
'nova.api.openstack.compute.contrib.select_extensions'],
|
||||
osapi_compute_ext_list=['Console_output'])
|
||||
self.app = fakes.wsgi_app(init_only=('servers',))
|
||||
self.app = self._get_app()
|
||||
|
||||
def _get_app(self):
|
||||
return fakes.wsgi_app_v3(init_only=('servers',
|
||||
'os-console-output'))
|
||||
|
||||
def _get_response(self, length_dict=None):
|
||||
length_dict = length_dict or {}
|
||||
body = {'os-getConsoleOutput': length_dict}
|
||||
req = fakes.HTTPRequest.blank('/v2/fake/servers/1/action')
|
||||
req = fakes.HTTPRequest.blank(self.action_url)
|
||||
req.method = "POST"
|
||||
req.body = jsonutils.dumps(body)
|
||||
req.headers["content-type"] = self.application_type
|
||||
@ -124,7 +124,7 @@ class ConsoleOutputExtensionTest(test.NoDBTestCase):
|
||||
self.assertEqual(404, res.status_int)
|
||||
|
||||
def _get_console_output_bad_request_case(self, body):
|
||||
req = webob.Request.blank('/v2/fake/servers/1/action')
|
||||
req = fakes.HTTPRequest.blank(self.action_url)
|
||||
req.method = "POST"
|
||||
req.body = jsonutils.dumps(body)
|
||||
req.headers["content-type"] = "application/json"
|
||||
@ -158,3 +158,14 @@ class ConsoleOutputExtensionTest(test.NoDBTestCase):
|
||||
def test_get_console_output_with_boolean_length(self):
|
||||
res = self._get_response(length_dict={'length': True})
|
||||
self.assertEqual(400, res.status_int)
|
||||
|
||||
|
||||
class ConsoleOutputExtensionTestV2(ConsoleOutputExtensionTestV21):
|
||||
need_osapi_compute_extension = True
|
||||
action_url = '/v2/fake/servers/1/action'
|
||||
|
||||
def _get_app(self):
|
||||
self.flags(osapi_compute_extension=[
|
||||
'nova.api.openstack.compute.contrib.select_extensions'],
|
||||
osapi_compute_ext_list=['Console_output'])
|
||||
return fakes.wsgi_app(init_only=('servers',))
|
||||
|
@ -1,158 +0,0 @@
|
||||
# Copyright 2011 Eldar Nugaev
|
||||
# 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.
|
||||
|
||||
import string
|
||||
|
||||
from nova.compute import api as compute_api
|
||||
from nova import exception
|
||||
from nova.openstack.common import jsonutils
|
||||
from nova import test
|
||||
from nova.tests.api.openstack import fakes
|
||||
|
||||
|
||||
def fake_get_console_output(self, _context, _instance, tail_length):
|
||||
fixture = [str(i) for i in range(5)]
|
||||
|
||||
if tail_length is None:
|
||||
pass
|
||||
elif tail_length == 0:
|
||||
fixture = []
|
||||
else:
|
||||
fixture = fixture[-int(tail_length):]
|
||||
|
||||
return '\n'.join(fixture)
|
||||
|
||||
|
||||
def fake_get_console_output_not_ready(self, _context, _instance, tail_length):
|
||||
raise exception.InstanceNotReady(instance_id=_instance["uuid"])
|
||||
|
||||
|
||||
def fake_get_console_output_all_characters(self, _ctx, _instance, _tail_len):
|
||||
return string.printable
|
||||
|
||||
|
||||
def fake_get(self, context, instance_uuid, expected_attrs=None,
|
||||
want_objects=False):
|
||||
return {'uuid': instance_uuid}
|
||||
|
||||
|
||||
def fake_get_not_found(*args, **kwargs):
|
||||
raise exception.InstanceNotFound(instance_id='')
|
||||
|
||||
|
||||
class ConsoleOutputExtensionTest(test.NoDBTestCase):
|
||||
application_type = "application/json"
|
||||
|
||||
def setUp(self):
|
||||
super(ConsoleOutputExtensionTest, self).setUp()
|
||||
self.stubs.Set(compute_api.API, 'get_console_output',
|
||||
fake_get_console_output)
|
||||
self.stubs.Set(compute_api.API, 'get', fake_get)
|
||||
self.app = fakes.wsgi_app_v3(init_only=('servers',
|
||||
'os-console-output'))
|
||||
|
||||
def _create_request(self, length_dict=None):
|
||||
length_dict = length_dict or {}
|
||||
body = {'get_console_output': length_dict}
|
||||
req = fakes.HTTPRequestV3.blank('/v3/servers/1/action')
|
||||
req.method = "POST"
|
||||
req.body = jsonutils.dumps(body)
|
||||
req.headers["content-type"] = self.application_type
|
||||
return req
|
||||
|
||||
def test_get_text_console_instance_action(self):
|
||||
req = self._create_request(length_dict={})
|
||||
res = req.get_response(self.app)
|
||||
output = jsonutils.loads(res.body)
|
||||
self.assertEqual(res.status_int, 200)
|
||||
self.assertEqual(output, {'output': '0\n1\n2\n3\n4'})
|
||||
|
||||
def test_get_console_output_with_tail(self):
|
||||
req = self._create_request(length_dict={'length': 3})
|
||||
res = req.get_response(self.app)
|
||||
output = jsonutils.loads(res.body)
|
||||
self.assertEqual(res.status_int, 200)
|
||||
self.assertEqual(output, {'output': '2\n3\n4'})
|
||||
|
||||
def test_get_console_output_with_length_as_str(self):
|
||||
req = self._create_request(length_dict={'length': '3'})
|
||||
res = req.get_response(self.app)
|
||||
output = jsonutils.loads(res.body)
|
||||
self.assertEqual(res.status_int, 200)
|
||||
self.assertEqual(output, {'output': '2\n3\n4'})
|
||||
|
||||
def test_get_console_output_with_unlimited_length(self):
|
||||
req = self._create_request(length_dict={'length': -1})
|
||||
res = req.get_response(self.app)
|
||||
output = jsonutils.loads(res.body)
|
||||
self.assertEqual(res.status_int, 200)
|
||||
self.assertEqual(output, {'output': '0\n1\n2\n3\n4'})
|
||||
|
||||
def test_get_console_output_with_unlimited_length_as_str(self):
|
||||
req = self._create_request(length_dict={'length': '-1'})
|
||||
res = req.get_response(self.app)
|
||||
output = jsonutils.loads(res.body)
|
||||
self.assertEqual(res.status_int, 200)
|
||||
self.assertEqual(output, {'output': '0\n1\n2\n3\n4'})
|
||||
|
||||
def test_get_console_output_with_non_integer_length(self):
|
||||
req = self._create_request(length_dict={'length': 'NaN'})
|
||||
res = req.get_response(self.app)
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_get_text_console_no_instance(self):
|
||||
self.stubs.Set(compute_api.API, 'get', fake_get_not_found)
|
||||
req = self._create_request(length_dict={})
|
||||
res = req.get_response(self.app)
|
||||
self.assertEqual(res.status_int, 404)
|
||||
|
||||
def test_get_text_console_bad_body(self):
|
||||
body = {}
|
||||
req = fakes.HTTPRequestV3.blank('/v3/servers/1/action')
|
||||
req.method = "POST"
|
||||
req.body = jsonutils.dumps(body)
|
||||
req.headers["content-type"] = self.application_type
|
||||
|
||||
res = req.get_response(self.app)
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_get_console_output_not_ready(self):
|
||||
self.stubs.Set(compute_api.API, 'get_console_output',
|
||||
fake_get_console_output_not_ready)
|
||||
req = self._create_request(length_dict={'length': 3})
|
||||
res = req.get_response(self.app)
|
||||
self.assertEqual(res.status_int, 409)
|
||||
|
||||
def test_get_console_output_with_length_as_float(self):
|
||||
req = self._create_request(length_dict={'length': 2.5})
|
||||
res = req.get_response(self.app)
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_get_console_output_not_implemented(self):
|
||||
self.stubs.Set(compute_api.API, 'get_console_output',
|
||||
fakes.fake_not_implemented)
|
||||
req = self._create_request()
|
||||
res = req.get_response(self.app)
|
||||
self.assertEqual(res.status_int, 501)
|
||||
|
||||
def test_get_console_output_with_small_length(self):
|
||||
req = self._create_request(length_dict={'length': -2})
|
||||
res = req.get_response(self.app)
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_get_console_output_with_boolean_length(self):
|
||||
req = self._create_request(length_dict={'length': True})
|
||||
res = req.get_response(self.app)
|
||||
self.assertEqual(res.status_int, 400)
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"get_console_output": {
|
||||
"os-getConsoleOutput": {
|
||||
"length": 50
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user