Add new command to resize volumes

Change-Id: Ie7c6a5eb4649da1c18a78baeff71649a30433479
This commit is contained in:
Frédéric Guillot 2017-05-24 12:12:58 -04:00
parent 20e250c0f1
commit c352dbcc7e
6 changed files with 122 additions and 0 deletions

View File

@ -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'

View File

@ -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,

View File

@ -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))

View File

@ -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)

View File

@ -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.

View File

@ -208,6 +208,23 @@ Arguments:
* :code:`start`: Start date (ISO8601 format)
* :code:`end`: End date (ISO8601 format)
Resize Volume
-------------
Usage: :code:`almanach resize-volume <volume_id> <size> --date <resize_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
-----------------