diff --git a/almanachclient/commands/resize_volume.py b/almanachclient/commands/resize_volume.py new file mode 100644 index 0000000..fce07c2 --- /dev/null +++ b/almanachclient/commands/resize_volume.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 ResizeVolumeCommand(Command): + """Resize volume""" + + def get_parser(self, prog_name): + parser = super().get_parser(prog_name) + parser.add_argument('volume_id', help='Volume ID') + parser.add_argument('size', help='Size') + parser.add_argument('--date', help='Resize date (now if missing)') + return parser + + def take_action(self, parsed_args): + self.app.get_client().resize_volume(parsed_args.volume_id, + parsed_args.size, + 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 2b1ff75..7a8dacc 100644 --- a/almanachclient/shell.py +++ b/almanachclient/shell.py @@ -30,6 +30,7 @@ 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.resize_volume import ResizeVolumeCommand from almanachclient.commands.update_instance_entity import UpdateInstanceEntityCommand from almanachclient.commands.version import VersionCommand from almanachclient.keystone_client import KeystoneClient @@ -46,6 +47,7 @@ class AlmanachCommandManager(commandmanager.CommandManager): 'list-volume-types': ListVolumeTypeCommand, 'get-volume-type': GetVolumeTypeCommand, 'list-volumes': ListVolumeCommand, + 'resize-volume': ResizeVolumeCommand, 'list-instances': ListInstanceCommand, 'create-instance': CreateInstanceCommand, 'delete-instance': DeleteInstanceCommand, diff --git a/almanachclient/tests/commands/test_resize_volume.py b/almanachclient/tests/commands/test_resize_volume.py new file mode 100644 index 0000000..bd96e25 --- /dev/null +++ b/almanachclient/tests/commands/test_resize_volume.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_volume import ResizeVolumeCommand + +from almanachclient.tests import base + + +class TestResizeVolumeCommand(base.TestCase): + + def setUp(self): + super().setUp() + self.app = mock.Mock() + self.app_args = mock.Mock() + self.args = Namespace(volume_id='some uuid', size=2, date=None) + + self.client = mock.Mock() + self.app.get_client.return_value = self.client + self.command = ResizeVolumeCommand(self.app, self.app_args) + + def test_execute_command(self): + self.assertEqual('Success', self.command.take_action(self.args)) + self.client.resize_volume.assert_called_once_with('some uuid', 2, 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_volume.assert_called_once_with('some uuid', + 2, + datetime.datetime(2017, 1, 1, 0, 0)) diff --git a/almanachclient/tests/v1/test_client.py b/almanachclient/tests/v1/test_client.py index d22ffea..280ba1f 100644 --- a/almanachclient/tests/v1/test_client.py +++ b/almanachclient/tests/v1/test_client.py @@ -234,3 +234,19 @@ class TestClient(base.TestCase): requests.assert_called_once_with('{}{}'.format(self.url, '/v1/project/my_tenant_id/volumes'), params=params, headers=self.headers) + + @mock.patch('requests.put') + def test_resize_volume(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_volume('my_volume_id', 3, date)) + + requests.assert_called_once_with('{}{}'.format(self.url, '/v1/volume/my_volume_id/resize'), + params=None, + data=json.dumps({'size': 3, + 'date': date.strftime(Client.DATE_FORMAT_BODY)}), + headers=self.headers) diff --git a/almanachclient/v1/client.py b/almanachclient/v1/client.py index fac14ef..8672452 100644 --- a/almanachclient/v1/client.py +++ b/almanachclient/v1/client.py @@ -89,6 +89,15 @@ class Client(HttpClient): params = {'start': self._format_qs_datetime(start), 'end': self._format_qs_datetime(end)} return self._get(url, params) + def resize_volume(self, volume_id, size, date=None): + data = { + 'size': size, + 'date': self._format_body_datetime(date or datetime.now()), + } + + self._put('{}/{}/volume/{}/resize'.format(self.url, self.api_version, volume_id), data=data) + return True + def get_instances(self, tenant_id, start, end): """List instances for a tenant. diff --git a/doc/source/usage.rst b/doc/source/usage.rst index ca693e8..731ba3b 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -208,6 +208,23 @@ Arguments: * :code:`start`: Start date (ISO8601 format) * :code:`end`: End date (ISO8601 format) +Resize Volume +------------- + +Usage: :code:`almanach resize-volume --date + +.. code:: bash + + almanach resize-volume 8c3bc3aa-28d6-4863-b5ae-72e1b415f79d 2 + + Success + +Arguments: + +* :code:`volume_id`: Volume ID (UUID) +* :code:`size`: Volume size (integer) +* :code:`date`: Resize date (ISO8601 format), if not specified the current datetime is used + List Volume Types -----------------