Add command to delete an instance

This commit is contained in:
Frédéric Guillot 2017-05-17 09:46:54 -04:00
parent b6694e8ae0
commit c6faa327dd
9 changed files with 120 additions and 12 deletions

View File

@ -0,0 +1,29 @@
# 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
class DeleteInstanceCommand(Command):
"""Delete instance"""
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument('instance_id', help='Instance ID')
parser.add_argument('--end', help='End date')
return parser
def take_action(self, parsed_args):
self.app.get_client().delete_instance(parsed_args.instance_id, parsed_args.end)
return 'Success'

View File

@ -43,14 +43,19 @@ class HttpClient(metaclass=abc.ABCMeta):
def _post(self, url, data, params=None):
logger.debug(url)
return self._parse_response(requests.post(url,
headers=self._get_headers(),
params=params,
data=json.dumps(data)), 201)
response = requests.post(url,
headers=self._get_headers(),
params=params,
data=json.dumps(data))
return self._parse_response(response, 201)
def _delete(self, url, params=None):
def _delete(self, url, params=None, data=None):
logger.debug(url)
return self._parse_response(requests.delete(url, headers=self._get_headers(), params=params), 202)
response = requests.delete(url,
headers=self._get_headers(),
params=params,
data=json.dumps(data) if data else None)
return self._parse_response(response, 202)
def _parse_response(self, response, expected_status=200):
body = response.json() if len(response.text) > 0 else ''

View File

@ -19,6 +19,7 @@ from cliff import app
from cliff import commandmanager
from almanachclient.commands.create_volume_type import CreateVolumeTypeCommand
from almanachclient.commands.delete_instance import DeleteInstanceCommand
from almanachclient.commands.delete_volume_type import DeleteVolumeTypeCommand
from almanachclient.commands.endpoint import EndpointCommand
from almanachclient.commands.get_volume_type import GetVolumeTypeCommand
@ -39,6 +40,7 @@ class AlmanachCommandManager(commandmanager.CommandManager):
'delete-volume-type': DeleteVolumeTypeCommand,
'list-volume-types': ListVolumeTypeCommand,
'get-volume-type': GetVolumeTypeCommand,
'delete-instance': DeleteInstanceCommand,
'list-entities': ListEntityCommand,
'update instance': UpdateInstanceEntityCommand,
}

View File

@ -33,5 +33,5 @@ class TestCreateVolumeTypeCommand(base.TestCase):
self.command = CreateVolumeTypeCommand(self.app, self.app_args)
def test_execute_command(self):
self.assertTrue(self.command.take_action(self.args))
self.assertEqual('Success', self.command.take_action(self.args))
self.client.create_volume_type.assert_called_once_with('some uuid', 'some name')

View File

@ -0,0 +1,37 @@
# 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
from unittest import mock
from almanachclient.commands.delete_instance import DeleteInstanceCommand
from almanachclient.tests import base
class TestDeleteInstanceCommand(base.TestCase):
def setUp(self):
super().setUp()
self.app = mock.Mock()
self.app_args = mock.Mock()
self.args = Namespace(instance_id='some uuid', end=None)
self.client = mock.Mock()
self.app.get_client.return_value = self.client
self.command = DeleteInstanceCommand(self.app, self.app_args)
def test_execute_command(self):
self.assertEqual('Success', self.command.take_action(self.args))
self.client.delete_instance.assert_called_once_with('some uuid', None)

View File

@ -33,5 +33,5 @@ class TestDeleteVolumeTypeCommand(base.TestCase):
self.command = DeleteVolumeTypeCommand(self.app, self.app_args)
def test_execute_command(self):
self.assertTrue(self.command.take_action(self.args))
self.assertEqual('Success', self.command.take_action(self.args))
self.client.delete_volume_type.assert_called_once_with('some uuid')

View File

@ -137,4 +137,19 @@ class TestClient(base.TestCase):
self.assertTrue(self.client.delete_volume_type('some uuid'))
requests.assert_called_once_with('{}{}'.format(self.url, '/v1/volume_type/some uuid'),
headers=self.headers,
data=None,
params=None)
@mock.patch('requests.delete')
def test_delete_instance(self, requests):
self.response.text = ''
date = datetime.now()
requests.return_value = self.response
self.response.status_code = 202
self.assertTrue(self.client.delete_instance('some uuid', date))
requests.assert_called_once_with('{}{}'.format(self.url, '/v1/instance/some uuid'),
headers=self.headers,
data=json.dumps({'date': date.strftime(Client.DATE_FORMAT_BODY)}),
params=None)

View File

@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from datetime import datetime
from almanachclient.http_client import HttpClient
@ -43,6 +45,11 @@ class Client(HttpClient):
self._delete('{}/{}/volume_type/{}'.format(self.url, self.api_version, volume_type_id))
return True
def delete_instance(self, instance_id, end=None):
data = {'date': self._format_body_datetime(end or datetime.now())}
self._delete('{}/{}/instance/{}'.format(self.url, self.api_version, instance_id), data=data)
return True
def get_tenant_entities(self, tenant_id, start, end):
url = '{}/{}/project/{}/entities'.format(self.url, self.api_version, tenant_id)
params = {'start': self._format_qs_datetime(start), 'end': self._format_qs_datetime(end)}
@ -57,8 +64,8 @@ class Client(HttpClient):
return self._put(url, kwargs)
def _format_body_datetime(self, value):
return value.strftime(self.DATE_FORMAT_BODY)
def _format_body_datetime(self, dt):
return dt.strftime(self.DATE_FORMAT_BODY)
def _format_qs_datetime(self, value):
return value.strftime(self.DATE_FORMAT_QS)
def _format_qs_datetime(self, dt):
return dt.strftime(self.DATE_FORMAT_QS)

View File

@ -79,6 +79,19 @@ Arguments:
* :code:`name`: Instance name (string)
* :code:`flavor`: Flavor (string)
Delete Instance
---------------
Usage: :code:`almanach delete-instance <instance_id> --end <end>
.. code:: bash
almanach delete-instance 8c3bc3aa-28d6-4863-b5ae-72e1b415f79d
Success
* :code:`end`: End date, if not specified the current date time is used (ISO8601 format)
List Volume Types
-----------------