Add new command to resize an instance

Change-Id: Iee7e6f41d94d1371c4bb45e03190cc0d5943317e
This commit is contained in:
Frédéric Guillot 2017-05-23 14:56:21 -04:00
parent 8924fdca7a
commit a7ddb8f383
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 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'

View File

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

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

View File

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

View File

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

View File

@ -170,6 +170,23 @@ Arguments:
* :code:`instance_id`: Instance ID (UUID)
* :code:`end`: End date (ISO8601 format)
Resize Instance
---------------
Usage: :code:`almanach resize-instance <instance_id> <flavor> --date <resize_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
------------