From 8e5577f52f660960738f1fc0384dff77fa30fc8a Mon Sep 17 00:00:00 2001
From: Ken'ichi Ohmichi <oomichi@mxs.nes.nec.co.jp>
Date: Thu, 10 Apr 2014 20:46:13 +0900
Subject: [PATCH] Allow -1 as the length of "get console output" API

Current "get console output" API does not allow -1 as the length and
returns a BadRequest response if receiving it. The other APIs(quota)
considers -1 as an unlimited value.
This patch allows -1 as the length for API consistency.

DocImpact

Change-Id: Idf88a238d1b0e545ebab5be872269b1b1030cc56
Related-Bug: #1295426
---
 .../api/openstack/compute/plugins/v3/console_output.py |  4 ++++
 .../api/openstack/compute/schemas/v3/console_output.py |  5 +++--
 .../compute/plugins/v3/test_console_output.py          | 18 ++++++++++++++++--
 3 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/nova/api/openstack/compute/plugins/v3/console_output.py b/nova/api/openstack/compute/plugins/v3/console_output.py
index 843fe76..8e118d4 100644
--- a/nova/api/openstack/compute/plugins/v3/console_output.py
+++ b/nova/api/openstack/compute/plugins/v3/console_output.py
@@ -44,6 +44,10 @@ class ConsoleOutputController(wsgi.Controller):
 
         instance = common.get_instance(self.compute_api, context, id)
         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
 
         try:
             output = self.compute_api.get_console_output(context,
diff --git a/nova/api/openstack/compute/schemas/v3/console_output.py b/nova/api/openstack/compute/schemas/v3/console_output.py
index d3f5897..c3e7040 100644
--- a/nova/api/openstack/compute/schemas/v3/console_output.py
+++ b/nova/api/openstack/compute/schemas/v3/console_output.py
@@ -20,8 +20,9 @@ get_console_output = {
             'properties': {
                 'length': {
                     'type': ['integer', 'string'],
-                    'minimum': 0,
-                    'pattern': '^[0-9]+$',
+                    'pattern': '^-?[0-9]+$',
+                    # NOTE: -1 means an unlimited length.
+                    'minimum': -1,
                 },
             },
             'additionalProperties': False,
diff --git a/nova/tests/api/openstack/compute/plugins/v3/test_console_output.py b/nova/tests/api/openstack/compute/plugins/v3/test_console_output.py
index 00c4ad5..312ce15 100644
--- a/nova/tests/api/openstack/compute/plugins/v3/test_console_output.py
+++ b/nova/tests/api/openstack/compute/plugins/v3/test_console_output.py
@@ -92,6 +92,20 @@ class ConsoleOutputExtensionTest(test.NoDBTestCase):
         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)
@@ -132,8 +146,8 @@ class ConsoleOutputExtensionTest(test.NoDBTestCase):
         res = req.get_response(self.app)
         self.assertEqual(res.status_int, 501)
 
-    def test_get_console_output_with_negative_length(self):
-        req = self._create_request(length_dict={'length': -1})
+    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)
 
-- 
1.9.0

