From eedec59003c0a6ddb1988033954d8211b5001854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Tue, 16 May 2017 14:07:22 -0400 Subject: [PATCH] Add new commands to create and delete volume types --- almanachclient/commands/create_volume_type.py | 30 +++++++++++++++ almanachclient/commands/delete_volume_type.py | 28 ++++++++++++++ almanachclient/commands/endpoint.py | 2 +- .../{list_entities.py => list_entity.py} | 0 almanachclient/commands/version.py | 2 +- almanachclient/http_client.py | 13 ++++++- almanachclient/shell.py | 6 ++- .../tests/commands/test_create_volume_type.py | 37 +++++++++++++++++++ .../tests/commands/test_delete_volume_type.py | 37 +++++++++++++++++++ ...t_list_entities.py => test_list_entity.py} | 2 +- almanachclient/tests/v1/test_client.py | 28 +++++++++++++- almanachclient/v1/client.py | 10 +++++ doc/source/usage.rst | 22 +++++++++++ 13 files changed, 211 insertions(+), 6 deletions(-) create mode 100644 almanachclient/commands/create_volume_type.py create mode 100644 almanachclient/commands/delete_volume_type.py rename almanachclient/commands/{list_entities.py => list_entity.py} (100%) create mode 100644 almanachclient/tests/commands/test_create_volume_type.py create mode 100644 almanachclient/tests/commands/test_delete_volume_type.py rename almanachclient/tests/commands/{test_list_entities.py => test_list_entity.py} (96%) diff --git a/almanachclient/commands/create_volume_type.py b/almanachclient/commands/create_volume_type.py new file mode 100644 index 0000000..cd9c89e --- /dev/null +++ b/almanachclient/commands/create_volume_type.py @@ -0,0 +1,30 @@ +# 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 CreateVolumeTypeCommand(Command): + """Create volume type""" + + def get_parser(self, prog_name): + parser = super().get_parser(prog_name) + parser.add_argument('volume_type_id', help='Volume Type ID') + parser.add_argument('volume_type_name', help='Volume Type Name') + return parser + + def take_action(self, parsed_args): + self.app.get_client().create_volume_type(parsed_args.volume_type_id, + parsed_args.volume_type_name) + return 'Success' diff --git a/almanachclient/commands/delete_volume_type.py b/almanachclient/commands/delete_volume_type.py new file mode 100644 index 0000000..710453f --- /dev/null +++ b/almanachclient/commands/delete_volume_type.py @@ -0,0 +1,28 @@ +# 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 DeleteVolumeTypeCommand(Command): + """Delete volume type""" + + def get_parser(self, prog_name): + parser = super().get_parser(prog_name) + parser.add_argument('volume_type_id', help='Volume Type ID') + return parser + + def take_action(self, parsed_args): + self.app.get_client().delete_volume_type(parsed_args.volume_type_id) + return 'Success' diff --git a/almanachclient/commands/endpoint.py b/almanachclient/commands/endpoint.py index 3569da2..5a24053 100644 --- a/almanachclient/commands/endpoint.py +++ b/almanachclient/commands/endpoint.py @@ -19,4 +19,4 @@ class EndpointCommand(Command): """Show Almanach Endpoint URL""" def take_action(self, parsed_args): - self.app.stdout.write('{}\n'.format(self.app.get_client().get_url())) + return self.app.get_client().get_url() diff --git a/almanachclient/commands/list_entities.py b/almanachclient/commands/list_entity.py similarity index 100% rename from almanachclient/commands/list_entities.py rename to almanachclient/commands/list_entity.py diff --git a/almanachclient/commands/version.py b/almanachclient/commands/version.py index 41b0aed..c9f1c27 100644 --- a/almanachclient/commands/version.py +++ b/almanachclient/commands/version.py @@ -20,4 +20,4 @@ class VersionCommand(Command): def take_action(self, parsed_args): info = self.app.get_client().get_info() - self.app.stdout.write('{}\n'.format(info.get('info', {}).get('version'))) + return info.get('info', {}).get('version') diff --git a/almanachclient/http_client.py b/almanachclient/http_client.py index a4d770b..337c0b1 100644 --- a/almanachclient/http_client.py +++ b/almanachclient/http_client.py @@ -41,8 +41,19 @@ class HttpClient(metaclass=abc.ABCMeta): params=params, data=json.dumps(data))) + 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) + + def _delete(self, url, params=None): + logger.debug(url) + return self._parse_response(requests.delete(url, headers=self._get_headers(), params=params), 202) + def _parse_response(self, response, expected_status=200): - body = response.json() + body = response.json() if len(response.text) > 0 else '' if response.status_code != expected_status: raise exceptions.HTTPError('{} ({})'.format(body.get('error') or 'HTTP Error', response.status_code)) diff --git a/almanachclient/shell.py b/almanachclient/shell.py index 0a8bf17..d6b200d 100644 --- a/almanachclient/shell.py +++ b/almanachclient/shell.py @@ -18,9 +18,11 @@ import sys from cliff import app from cliff import commandmanager +from almanachclient.commands.create_volume_type import CreateVolumeTypeCommand +from almanachclient.commands.delete_volume_type import DeleteVolumeTypeCommand from almanachclient.commands.endpoint import EndpointCommand from almanachclient.commands.get_volume_type import GetVolumeTypeCommand -from almanachclient.commands.list_entities import ListEntityCommand +from almanachclient.commands.list_entity import ListEntityCommand from almanachclient.commands.list_volume_type import ListVolumeTypeCommand from almanachclient.commands.update_instance_entity import UpdateInstanceEntityCommand from almanachclient.commands.version import VersionCommand @@ -33,6 +35,8 @@ class AlmanachCommandManager(commandmanager.CommandManager): SHELL_COMMANDS = { 'version': VersionCommand, 'endpoint': EndpointCommand, + 'create-volume-type': CreateVolumeTypeCommand, + 'delete-volume-type': DeleteVolumeTypeCommand, 'list-volume-types': ListVolumeTypeCommand, 'get-volume-type': GetVolumeTypeCommand, 'list-entities': ListEntityCommand, diff --git a/almanachclient/tests/commands/test_create_volume_type.py b/almanachclient/tests/commands/test_create_volume_type.py new file mode 100644 index 0000000..e9202a1 --- /dev/null +++ b/almanachclient/tests/commands/test_create_volume_type.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.create_volume_type import CreateVolumeTypeCommand + +from almanachclient.tests import base + + +class TestCreateVolumeTypeCommand(base.TestCase): + + def setUp(self): + super().setUp() + self.app = mock.Mock() + self.app_args = mock.Mock() + self.args = Namespace(volume_type_id='some uuid', volume_type_name='some name') + + self.client = mock.Mock() + self.app.get_client.return_value = self.client + self.command = CreateVolumeTypeCommand(self.app, self.app_args) + + def test_execute_command(self): + self.assertTrue(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_volume_type.py b/almanachclient/tests/commands/test_delete_volume_type.py new file mode 100644 index 0000000..85b98cc --- /dev/null +++ b/almanachclient/tests/commands/test_delete_volume_type.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_volume_type import DeleteVolumeTypeCommand + +from almanachclient.tests import base + + +class TestDeleteVolumeTypeCommand(base.TestCase): + + def setUp(self): + super().setUp() + self.app = mock.Mock() + self.app_args = mock.Mock() + self.args = Namespace(volume_type_id='some uuid') + + self.client = mock.Mock() + self.app.get_client.return_value = self.client + self.command = DeleteVolumeTypeCommand(self.app, self.app_args) + + def test_execute_command(self): + self.assertTrue(self.command.take_action(self.args)) + self.client.delete_volume_type.assert_called_once_with('some uuid') diff --git a/almanachclient/tests/commands/test_list_entities.py b/almanachclient/tests/commands/test_list_entity.py similarity index 96% rename from almanachclient/tests/commands/test_list_entities.py rename to almanachclient/tests/commands/test_list_entity.py index 829e919..0fee6e6 100644 --- a/almanachclient/tests/commands/test_list_entities.py +++ b/almanachclient/tests/commands/test_list_entity.py @@ -16,7 +16,7 @@ from argparse import Namespace import datetime from unittest import mock -from almanachclient.commands.list_entities import ListEntityCommand +from almanachclient.commands.list_entity import ListEntityCommand from almanachclient.tests import base diff --git a/almanachclient/tests/v1/test_client.py b/almanachclient/tests/v1/test_client.py index a183e35..c945ea2 100644 --- a/almanachclient/tests/v1/test_client.py +++ b/almanachclient/tests/v1/test_client.py @@ -25,7 +25,7 @@ class TestClient(base.TestCase): def setUp(self): super().setUp() - self.response = mock.Mock() + self.response = mock.Mock(text='sample data') self.url = 'http://almanach_url' self.token = 'token' self.headers = {'Content-Type': 'application/json', @@ -112,3 +112,29 @@ class TestClient(base.TestCase): self.assertEqual(expected, self.client.get_volume_type('some-uuid')) requests.assert_called_once_with('{}{}'.format(self.url, '/v1/volume_type/some-uuid'), headers=self.headers, params=None) + + @mock.patch('requests.post') + def test_create_volume_type(self, requests): + data = {'type_id': 'some uuid', 'type_name': 'some name'} + self.response.text = '' + + requests.return_value = self.response + self.response.status_code = 201 + + self.assertTrue(self.client.create_volume_type('some uuid', 'some name')) + requests.assert_called_once_with('{}{}'.format(self.url, '/v1/volume_type'), + headers=self.headers, + data=json.dumps(data), + params=None) + + @mock.patch('requests.delete') + def test_delete_volume_type(self, requests): + self.response.text = '' + + requests.return_value = self.response + self.response.status_code = 202 + + 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, + params=None) diff --git a/almanachclient/v1/client.py b/almanachclient/v1/client.py index 1bd06f4..6ed6e8c 100644 --- a/almanachclient/v1/client.py +++ b/almanachclient/v1/client.py @@ -33,6 +33,16 @@ class Client(HttpClient): def get_volume_type(self, volume_type_id): return self._get('{}/{}/volume_type/{}'.format(self.url, self.api_version, volume_type_id)) + def create_volume_type(self, volume_type_id, volume_type_name): + url = '{}/{}/volume_type'.format(self.url, self.api_version) + data = {'type_id': volume_type_id, 'type_name': volume_type_name} + self._post(url, data) + return True + + def delete_volume_type(self, volume_type_id): + self._delete('{}/{}/volume_type/{}'.format(self.url, self.api_version, volume_type_id)) + 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)} diff --git a/doc/source/usage.rst b/doc/source/usage.rst index 3e029ac..492e846 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -109,3 +109,25 @@ Usage: :code:`almanach-client get-volume-type ` | Volume Type ID | f3786e9f-f8e6-4944-a3bc-e11b9f112706 | | Volume Type Name | solidfire0 | +------------------+--------------------------------------+ + +Create Volume Type +------------------ + +Usage: :code:`almanach-client create-volume-type ` + +.. code:: bash + + almanach-client create-volume-type f1c2db7b-946e-47a4-b443-914a669a6672 my_volume_type + + Success + +Delete Volume Type +------------------ + +Usage: :code:`almanach-client delete-volume-type ` + +.. code:: bash + + almanach-client delete-volume-type f1c2db7b-946e-47a4-b443-914a669a6672 + + Success