diff --git a/almanachclient/commands/delete_instance.py b/almanachclient/commands/delete_instance.py new file mode 100644 index 0000000..2817461 --- /dev/null +++ b/almanachclient/commands/delete_instance.py @@ -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' diff --git a/almanachclient/http_client.py b/almanachclient/http_client.py index 337c0b1..b1e090b 100644 --- a/almanachclient/http_client.py +++ b/almanachclient/http_client.py @@ -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 '' diff --git a/almanachclient/shell.py b/almanachclient/shell.py index d6b200d..e952d56 100644 --- a/almanachclient/shell.py +++ b/almanachclient/shell.py @@ -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, } diff --git a/almanachclient/tests/commands/test_create_volume_type.py b/almanachclient/tests/commands/test_create_volume_type.py index e9202a1..d84335b 100644 --- a/almanachclient/tests/commands/test_create_volume_type.py +++ b/almanachclient/tests/commands/test_create_volume_type.py @@ -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') diff --git a/almanachclient/tests/commands/test_delete_instance.py b/almanachclient/tests/commands/test_delete_instance.py new file mode 100644 index 0000000..bdd0f81 --- /dev/null +++ b/almanachclient/tests/commands/test_delete_instance.py @@ -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) diff --git a/almanachclient/tests/commands/test_delete_volume_type.py b/almanachclient/tests/commands/test_delete_volume_type.py index 85b98cc..408db0a 100644 --- a/almanachclient/tests/commands/test_delete_volume_type.py +++ b/almanachclient/tests/commands/test_delete_volume_type.py @@ -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') diff --git a/almanachclient/tests/v1/test_client.py b/almanachclient/tests/v1/test_client.py index c945ea2..ef325e1 100644 --- a/almanachclient/tests/v1/test_client.py +++ b/almanachclient/tests/v1/test_client.py @@ -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) diff --git a/almanachclient/v1/client.py b/almanachclient/v1/client.py index 6ed6e8c..1771335 100644 --- a/almanachclient/v1/client.py +++ b/almanachclient/v1/client.py @@ -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) diff --git a/doc/source/usage.rst b/doc/source/usage.rst index 8da6d02..b1db0c8 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -79,6 +79,19 @@ Arguments: * :code:`name`: Instance name (string) * :code:`flavor`: Flavor (string) +Delete Instance +--------------- + +Usage: :code:`almanach delete-instance --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 -----------------