From a7ddb8f383534bc93795c9ec8cb60d1391906092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Tue, 23 May 2017 14:56:21 -0400 Subject: [PATCH] Add new command to resize an instance Change-Id: Iee7e6f41d94d1371c4bb45e03190cc0d5943317e --- almanachclient/commands/resize_instance.py | 33 ++++++++++++++ almanachclient/shell.py | 2 + .../tests/commands/test_resize_instance.py | 45 +++++++++++++++++++ almanachclient/tests/v1/test_client.py | 16 +++++++ almanachclient/v1/client.py | 9 ++++ doc/source/usage.rst | 17 +++++++ 6 files changed, 122 insertions(+) create mode 100644 almanachclient/commands/resize_instance.py create mode 100644 almanachclient/tests/commands/test_resize_instance.py diff --git a/almanachclient/commands/resize_instance.py b/almanachclient/commands/resize_instance.py new file mode 100644 index 0000000..95dd5f3 --- /dev/null +++ b/almanachclient/commands/resize_instance.py @@ -0,0 +1,33 @@ +# Copyright 2017 INAP +# +# 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. + +from cliff.command import Command +from dateutil import parser as date_parser + + +class ResizeInstanceCommand(Command): + """Resize instance""" + + def get_parser(self, prog_name): + parser = super().get_parser(prog_name) + parser.add_argument('instance_id', help='Instance ID') + parser.add_argument('flavor', help='Flavor') + parser.add_argument('--date', help='Resize date (now if missing)') + return parser + + def take_action(self, parsed_args): + self.app.get_client().resize_instance(parsed_args.instance_id, + parsed_args.flavor, + date_parser.parse(parsed_args.date) if parsed_args.date else None) + return 'Success' diff --git a/almanachclient/shell.py b/almanachclient/shell.py index 7f1eb36..2b1ff75 100644 --- a/almanachclient/shell.py +++ b/almanachclient/shell.py @@ -29,6 +29,7 @@ from almanachclient.commands.list_entity import ListEntityCommand from almanachclient.commands.list_instance import ListInstanceCommand from almanachclient.commands.list_volume_type import ListVolumeTypeCommand from almanachclient.commands.list_volumes import ListVolumeCommand +from almanachclient.commands.resize_instance import ResizeInstanceCommand from almanachclient.commands.update_instance_entity import UpdateInstanceEntityCommand from almanachclient.commands.version import VersionCommand from almanachclient.keystone_client import KeystoneClient @@ -48,6 +49,7 @@ class AlmanachCommandManager(commandmanager.CommandManager): 'list-instances': ListInstanceCommand, 'create-instance': CreateInstanceCommand, 'delete-instance': DeleteInstanceCommand, + 'resize-instance': ResizeInstanceCommand, 'get-entity': GetEntityCommand, 'list-entities': ListEntityCommand, 'update instance': UpdateInstanceEntityCommand, diff --git a/almanachclient/tests/commands/test_resize_instance.py b/almanachclient/tests/commands/test_resize_instance.py new file mode 100644 index 0000000..d0f58e0 --- /dev/null +++ b/almanachclient/tests/commands/test_resize_instance.py @@ -0,0 +1,45 @@ +# Copyright 2017 INAP +# +# 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. + +from argparse import Namespace +import datetime +from unittest import mock + +from almanachclient.commands.resize_instance import ResizeInstanceCommand + +from almanachclient.tests import base + + +class TestResizeInstanceCommand(base.TestCase): + + def setUp(self): + super().setUp() + self.app = mock.Mock() + self.app_args = mock.Mock() + self.args = Namespace(instance_id='some uuid', flavor='some flavor', date=None) + + self.client = mock.Mock() + self.app.get_client.return_value = self.client + self.command = ResizeInstanceCommand(self.app, self.app_args) + + def test_execute_command(self): + self.assertEqual('Success', self.command.take_action(self.args)) + self.client.resize_instance.assert_called_once_with('some uuid', 'some flavor', None) + + def test_execute_command_with_date(self): + self.args.date = '2017-01-01' + self.assertEqual('Success', self.command.take_action(self.args)) + self.client.resize_instance.assert_called_once_with('some uuid', + 'some flavor', + datetime.datetime(2017, 1, 1, 0, 0)) diff --git a/almanachclient/tests/v1/test_client.py b/almanachclient/tests/v1/test_client.py index cd36517..d22ffea 100644 --- a/almanachclient/tests/v1/test_client.py +++ b/almanachclient/tests/v1/test_client.py @@ -104,6 +104,22 @@ class TestClient(base.TestCase): data=json.dumps({'name': 'some entity'}), headers=self.headers) + @mock.patch('requests.put') + def test_resize_instance(self, requests): + date = datetime.now() + requests.return_value = self.response + + self.response.headers['Content-Length'] = 0 + self.response.status_code = 200 + + self.assertTrue(self.client.resize_instance('my_instance_id', 'another flavor', date)) + + requests.assert_called_once_with('{}{}'.format(self.url, '/v1/instance/my_instance_id/resize'), + params=None, + data=json.dumps({'flavor': 'another flavor', + 'date': date.strftime(Client.DATE_FORMAT_BODY)}), + headers=self.headers) + @mock.patch('requests.get') def test_get_volume_types(self, requests): expected = [{'volume_type_id': 'some uuid', 'volume_type_name': 'some volume'}] diff --git a/almanachclient/v1/client.py b/almanachclient/v1/client.py index 196105b..12ef465 100644 --- a/almanachclient/v1/client.py +++ b/almanachclient/v1/client.py @@ -74,6 +74,15 @@ class Client(HttpClient): self._delete('{}/{}/instance/{}'.format(self.url, self.api_version, instance_id), data=data) return True + def resize_instance(self, instance_id, flavor, date=None): + data = { + 'flavor': flavor, + 'date': self._format_body_datetime(date or datetime.now()), + } + + self._put('{}/{}/instance/{}/resize'.format(self.url, self.api_version, instance_id), data=data) + return True + def get_entity(self, entity_id): url = '{}/{}/entity/{}'.format(self.url, self.api_version, entity_id) return self._get(url) diff --git a/doc/source/usage.rst b/doc/source/usage.rst index 43acc4d..ca693e8 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -170,6 +170,23 @@ Arguments: * :code:`instance_id`: Instance ID (UUID) * :code:`end`: End date (ISO8601 format) +Resize Instance +--------------- + +Usage: :code:`almanach resize-instance --date + +.. code:: bash + + almanach resize-instance 8c3bc3aa-28d6-4863-b5ae-72e1b415f79d New_Flavor + + Success + +Arguments: + +* :code:`instance_id`: Instance ID (UUID) +* :code:`flavor`: Flavor (string) +* :code:`date`: Resize date (ISO8601 format), if not specified the current datetime is used + List Volumes ------------