diff --git a/almanachclient/commands/get_volume_type.py b/almanachclient/commands/get_volume_type.py new file mode 100644 index 0000000..466be3e --- /dev/null +++ b/almanachclient/commands/get_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.show import ShowOne + + +class GetVolumeTypeCommand(ShowOne): + """Get volume type""" + + columns = ('Volume Type ID', 'Volume Type Name') + + 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): + volume_type = self.app.get_client().get_volume_type(parsed_args.volume_type_id) + return self.columns, (volume_type.get('volume_type_id'), volume_type.get('volume_type_name')) diff --git a/almanachclient/shell.py b/almanachclient/shell.py index 056d45b..0a8bf17 100644 --- a/almanachclient/shell.py +++ b/almanachclient/shell.py @@ -19,6 +19,7 @@ from cliff import app from cliff import commandmanager 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_volume_type import ListVolumeTypeCommand from almanachclient.commands.update_instance_entity import UpdateInstanceEntityCommand @@ -33,6 +34,7 @@ class AlmanachCommandManager(commandmanager.CommandManager): 'version': VersionCommand, 'endpoint': EndpointCommand, 'list-volume-types': ListVolumeTypeCommand, + 'get-volume-type': GetVolumeTypeCommand, 'list-entities': ListEntityCommand, 'update instance': UpdateInstanceEntityCommand, } diff --git a/almanachclient/tests/commands/test_get_volume_type.py b/almanachclient/tests/commands/test_get_volume_type.py new file mode 100644 index 0000000..5e01028 --- /dev/null +++ b/almanachclient/tests/commands/test_get_volume_type.py @@ -0,0 +1,43 @@ +# 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.get_volume_type import GetVolumeTypeCommand + +from almanachclient.tests import base + + +class TestGetVolumeTypeCommand(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 = GetVolumeTypeCommand(self.app, self.app_args) + + def test_execute_command(self): + self.client.get_volume_type.return_value = {'volume_type_id': 'some uuid', + 'volume_type_name': 'some volume'} + + expected = (('Volume Type ID', 'Volume Type Name'), + ('some uuid', 'some volume')) + + self.assertEqual(expected, self.command.take_action(self.args)) + self.client.get_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 e9de665..a183e35 100644 --- a/almanachclient/tests/v1/test_client.py +++ b/almanachclient/tests/v1/test_client.py @@ -25,6 +25,7 @@ class TestClient(base.TestCase): def setUp(self): super().setUp() + self.response = mock.Mock() self.url = 'http://almanach_url' self.token = 'token' self.headers = {'Content-Type': 'application/json', @@ -36,35 +37,32 @@ class TestClient(base.TestCase): @mock.patch('requests.get') def test_get_info(self, requests): - response = mock.Mock() expected = { 'info': {'version': '1.2.3'}, "database": {'all_entities': 2, 'active_entities': 1} } - requests.return_value = response - response.json.return_value = expected - response.status_code = 200 + requests.return_value = self.response + self.response.json.return_value = expected + self.response.status_code = 200 self.assertEqual(expected, self.client.get_info()) requests.assert_called_once_with('{}{}'.format(self.url, '/v1/info'), headers=self.headers, params=None) @mock.patch('requests.get') def test_get_info_with_http_error(self, requests): - response = mock.Mock() - requests.return_value = response - response.status_code = 500 + requests.return_value = self.response + self.response.status_code = 500 self.assertRaises(exceptions.HTTPError, self.client.get_info) @mock.patch('requests.get') def test_get_tenant_entities(self, requests): - response = mock.Mock() expected = [mock.Mock()] - requests.return_value = response - response.json.return_value = expected - response.status_code = 200 + requests.return_value = self.response + self.response.json.return_value = expected + self.response.status_code = 200 start = datetime.now() end = datetime.now() @@ -78,12 +76,11 @@ class TestClient(base.TestCase): @mock.patch('requests.put') def test_update_instance_entity(self, requests): - response = mock.Mock() expected = dict(name='some entity') - requests.return_value = response - response.json.return_value = expected - response.status_code = 200 + requests.return_value = self.response + self.response.json.return_value = expected + self.response.status_code = 200 self.assertEqual(expected, self.client.update_instance_entity('my_instance_id', name='some entity')) @@ -94,13 +91,24 @@ class TestClient(base.TestCase): @mock.patch('requests.get') def test_get_volume_types(self, requests): - response = mock.Mock() expected = [{'volume_type_id': 'some uuid', 'volume_type_name': 'some volume'}] - requests.return_value = response - response.json.return_value = expected - response.status_code = 200 + requests.return_value = self.response + self.response.json.return_value = expected + self.response.status_code = 200 self.assertEqual(expected, self.client.get_volume_types()) requests.assert_called_once_with('{}{}'.format(self.url, '/v1/volume_types'), headers=self.headers, params=None) + + @mock.patch('requests.get') + def test_get_volume_type(self, requests): + expected = [{'volume_type_id': 'some uuid', 'volume_type_name': 'some volume'}] + + requests.return_value = self.response + self.response.json.return_value = expected + self.response.status_code = 200 + + 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) diff --git a/almanachclient/v1/client.py b/almanachclient/v1/client.py index 86b46d4..1bd06f4 100644 --- a/almanachclient/v1/client.py +++ b/almanachclient/v1/client.py @@ -30,6 +30,9 @@ class Client(HttpClient): def get_volume_types(self): return self._get('{}/{}/volume_types'.format(self.url, self.api_version)) + def get_volume_type(self, volume_type_id): + return self._get('{}/{}/volume_type/{}'.format(self.url, self.api_version, volume_type_id)) + 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 4528bd9..3e029ac 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -4,12 +4,12 @@ Command Line Usage Environment variables --------------------- -* `OS_AUTH_URL`: Keystone URL (v3 endpoint) -* `OS_AUTH_URL`: OpenStack region name -* `OS_USERNAME`: OpenStack username -* `OS_PASSWORD`: OpenStack password -* `ALMANACH_SERVICE`: Almanach catalog service name -* `ALMANACH_TOKEN`: Almanach private key +* :code:`OS_AUTH_URL`: Keystone URL (v3 endpoint) +* :code:`OS_AUTH_URL`: OpenStack region name +* :code:`OS_USERNAME`: OpenStack username +* :code:`OS_PASSWORD`: OpenStack password +* :code:`ALMANACH_SERVICE`: Almanach catalog service name +* :code:`ALMANACH_TOKEN`: Almanach API key Get server version ------------------ @@ -93,3 +93,19 @@ Usage: :code:`almanach-client list-volume-types` +--------------------------------------+------------------+ | f3786e9f-f8e6-4944-a3bc-e11b9f112706 | solidfire0 | +--------------------------------------+------------------+ + +Get Volume Type +--------------- + +Usage: :code:`almanach-client get-volume-type ` + +.. code:: bash + + almanach-client get-volume-type f3786e9f-f8e6-4944-a3bc-e11b9f112706 + + +------------------+--------------------------------------+ + | Field | Value | + +------------------+--------------------------------------+ + | Volume Type ID | f3786e9f-f8e6-4944-a3bc-e11b9f112706 | + | Volume Type Name | solidfire0 | + +------------------+--------------------------------------+