Add command to list volume types
This commit is contained in:
		
							
								
								
									
										31
									
								
								almanachclient/commands/list_volume_type.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								almanachclient/commands/list_volume_type.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| # 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.lister import Lister | ||||
|  | ||||
|  | ||||
| class ListVolumeTypeCommand(Lister): | ||||
|     """List volume types""" | ||||
|  | ||||
|     columns = ('Volume Type ID', 'Volume Type Name') | ||||
|  | ||||
|     def take_action(self, parsed_args): | ||||
|         volume_types = self.app.get_client().get_volume_types() | ||||
|         return self.columns, self._format_volume_types(volume_types) | ||||
|  | ||||
|     def _format_volume_types(self, volume_types): | ||||
|         rows = [] | ||||
|         for volume_type in volume_types: | ||||
|             rows.append((volume_type.get('volume_type_id'), volume_type.get('volume_type_name'))) | ||||
|         return rows | ||||
| @@ -20,6 +20,7 @@ from cliff import commandmanager | ||||
|  | ||||
| from almanachclient.commands.endpoint import EndpointCommand | ||||
| from almanachclient.commands.list_entities import ListEntityCommand | ||||
| from almanachclient.commands.list_volume_type import ListVolumeTypeCommand | ||||
| from almanachclient.commands.update_instance_entity import UpdateInstanceEntityCommand | ||||
| from almanachclient.commands.version import VersionCommand | ||||
| from almanachclient.keystone_client import KeystoneClient | ||||
| @@ -31,7 +32,8 @@ class AlmanachCommandManager(commandmanager.CommandManager): | ||||
|     SHELL_COMMANDS = { | ||||
|         'version': VersionCommand, | ||||
|         'endpoint': EndpointCommand, | ||||
|         'list entities': ListEntityCommand, | ||||
|         'list-volume-types': ListVolumeTypeCommand, | ||||
|         'list-entities': ListEntityCommand, | ||||
|         'update instance': UpdateInstanceEntityCommand, | ||||
|     } | ||||
|  | ||||
|   | ||||
							
								
								
									
										42
									
								
								almanachclient/tests/commands/test_list_volume_type.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								almanachclient/tests/commands/test_list_volume_type.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| # 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 unittest import mock | ||||
|  | ||||
| from almanachclient.commands.list_volume_type import ListVolumeTypeCommand | ||||
|  | ||||
| from almanachclient.tests import base | ||||
|  | ||||
|  | ||||
| class TestListVolumeTypeCommand(base.TestCase): | ||||
|  | ||||
|     def setUp(self): | ||||
|         super().setUp() | ||||
|         self.app = mock.Mock() | ||||
|         self.app_args = mock.Mock() | ||||
|         self.args = mock.Mock() | ||||
|  | ||||
|         self.client = mock.Mock() | ||||
|         self.app.get_client.return_value = self.client | ||||
|         self.command = ListVolumeTypeCommand(self.app, self.app_args) | ||||
|  | ||||
|     def test_execute_command(self): | ||||
|         self.client.get_volume_types.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_types.assert_called_once_with() | ||||
| @@ -91,3 +91,16 @@ class TestClient(base.TestCase): | ||||
|                                          params=None, | ||||
|                                          data=json.dumps({'name': 'some entity'}), | ||||
|                                          headers=self.headers) | ||||
|  | ||||
|     @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 | ||||
|  | ||||
|         self.assertEqual(expected, self.client.get_volume_types()) | ||||
|         requests.assert_called_once_with('{}{}'.format(self.url, '/v1/volume_types'), | ||||
|                                          headers=self.headers, params=None) | ||||
|   | ||||
| @@ -27,6 +27,9 @@ class Client(HttpClient): | ||||
|     def get_info(self): | ||||
|         return self._get('{}/{}/info'.format(self.url, self.api_version)) | ||||
|  | ||||
|     def get_volume_types(self): | ||||
|         return self._get('{}/{}/volume_types'.format(self.url, self.api_version)) | ||||
|  | ||||
|     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)} | ||||
|   | ||||
| @@ -36,11 +36,11 @@ Usage: :code:`almanach-client endpoint` | ||||
| Get tenant entities | ||||
| ------------------- | ||||
|  | ||||
| Usage: :code:`almanach-client list entities <tenant_id> <start> <end>` | ||||
| Usage: :code:`almanach-client list-entities <tenant_id> <start> <end>` | ||||
|  | ||||
| .. code:: bash | ||||
|  | ||||
|     almanach-client list entities bca89ae64dba46b8b74653d8d9ae8364 2016-01-01 2017-05-30 | ||||
|     almanach-client list-entities bca89ae64dba46b8b74653d8d9ae8364 2016-01-01 2017-05-30 | ||||
|  | ||||
|     +--------------------------------------+----------+--------+---------------------------+------+---------------------------------------------------------------------------------------+ | ||||
|     | Entity ID                            | Type     | Name   | Start                     | End  | Properties                                                                            | | ||||
| @@ -53,11 +53,11 @@ Usage: :code:`almanach-client list entities <tenant_id> <start> <end>` | ||||
| Update Instance Entity | ||||
| ---------------------- | ||||
|  | ||||
| Usage: :code:`almanach-client update instance <instance_id> --start <start> --end <end> --name <name> --flavor <flavor>` | ||||
| Usage: :code:`almanach-client update-instance <instance_id> --start <start> --end <end> --name <name> --flavor <flavor>` | ||||
|  | ||||
| .. code:: bash | ||||
|  | ||||
|     almanach-client update instance 8c3bc3aa-28d6-4863-b5ae-72e1b415f79d --name vm03 | ||||
|     almanach-client update-instance 8c3bc3aa-28d6-4863-b5ae-72e1b415f79d --name vm03 | ||||
|  | ||||
|     +-------------+----------------------------------------------------------+ | ||||
|     | Field       | Value                                                    | | ||||
| @@ -78,3 +78,18 @@ Arguments: | ||||
| * :code:`end`: End date (ISO8601 format) | ||||
| * :code:`name`: Instance name (string) | ||||
| * :code:`flavor`: Flavor (string) | ||||
|  | ||||
| List Volume Types | ||||
| ----------------- | ||||
|  | ||||
| Usage: :code:`almanach-client list-volume-types` | ||||
|  | ||||
| .. code:: bash | ||||
|  | ||||
|     almanach-client list-volume-types | ||||
|  | ||||
|     +--------------------------------------+------------------+ | ||||
|     | Volume Type ID                       | Volume Type Name | | ||||
|     +--------------------------------------+------------------+ | ||||
|     | f3786e9f-f8e6-4944-a3bc-e11b9f112706 | solidfire0       | | ||||
|     +--------------------------------------+------------------+ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Frédéric Guillot
					Frédéric Guillot