diff --git a/almanachclient/commands/list_entity.py b/almanachclient/commands/list_entity.py index 45ad0b3..0c71077 100644 --- a/almanachclient/commands/list_entity.py +++ b/almanachclient/commands/list_entity.py @@ -41,9 +41,12 @@ class ListEntityCommand(Lister): entity_type = entity.get('entity_type') if entity_type == 'instance': - properties = dict(flavor=entity.get('flavor'), image=entity.get('image_meta', entity.get('os'))) + properties = dict(flavor=entity.get('flavor'), + image=entity.get('image_meta', entity.get('os'))) elif entity_type == 'volume': - properties = dict(volume_type=entity.get('volume_type'), attached_to=entity.get('attached_to')) + properties = dict(volume_type=entity.get('volume_type'), + size=entity.get('size'), + attached_to=entity.get('attached_to')) else: properties = None diff --git a/almanachclient/commands/list_volumes.py b/almanachclient/commands/list_volumes.py new file mode 100644 index 0000000..891d78f --- /dev/null +++ b/almanachclient/commands/list_volumes.py @@ -0,0 +1,48 @@ +# 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 +from dateutil import parser + + +class ListVolumeCommand(Lister): + """Show all volumes for a given tenant""" + + columns = ('Volume ID', 'Name', 'Start', 'End', 'Type', 'Size', 'Attachments') + + def get_parser(self, prog_name): + parser = super().get_parser(prog_name) + parser.add_argument('tenant_id', help='Tenant ID') + parser.add_argument('start', help='Start Date') + parser.add_argument('end', help='End Date') + return parser + + def take_action(self, parsed_args): + start = parser.parse(parsed_args.start) + end = parser.parse(parsed_args.end) + entities = self.app.get_client().get_volumes(parsed_args.tenant_id, start, end) + return self.columns, self._format_rows(entities) + + def _format_rows(self, entities): + rows = [] + + for entity in entities: + rows.append((entity.get('entity_id'), + entity.get('name'), + entity.get('start'), + entity.get('end'), + entity.get('volume_type'), + entity.get('size'), + entity.get('attached_to'))) + return rows diff --git a/almanachclient/shell.py b/almanachclient/shell.py index 2ca2356..e0ddb07 100644 --- a/almanachclient/shell.py +++ b/almanachclient/shell.py @@ -27,6 +27,7 @@ from almanachclient.commands.get_volume_type import GetVolumeTypeCommand from almanachclient.commands.list_entity import ListEntityCommand from almanachclient.commands.list_instance import ListInstanceCommand from almanachclient.commands.list_volume_type import ListVolumeTypeCommand +from almanachclient.commands.list_volumes import ListVolumeCommand from almanachclient.commands.update_instance_entity import UpdateInstanceEntityCommand from almanachclient.commands.version import VersionCommand from almanachclient.keystone_client import KeystoneClient @@ -42,6 +43,7 @@ class AlmanachCommandManager(commandmanager.CommandManager): 'delete-volume-type': DeleteVolumeTypeCommand, 'list-volume-types': ListVolumeTypeCommand, 'get-volume-type': GetVolumeTypeCommand, + 'list-volumes': ListVolumeCommand, 'list-instances': ListInstanceCommand, 'create-instance': CreateInstanceCommand, 'delete-instance': DeleteInstanceCommand, diff --git a/almanachclient/tests/commands/test_list_volume.py b/almanachclient/tests/commands/test_list_volume.py new file mode 100644 index 0000000..d5f074d --- /dev/null +++ b/almanachclient/tests/commands/test_list_volume.py @@ -0,0 +1,49 @@ +# 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 +import datetime +from unittest import mock + +from almanachclient.commands.list_volumes import ListVolumeCommand + +from almanachclient.tests import base + + +class TestListVolumeCommand(base.TestCase): + + def setUp(self): + super().setUp() + self.app = mock.Mock() + self.app_args = mock.Mock() + self.args = Namespace(tenant_id=None, start=None, end=None) + + self.client = mock.Mock() + self.app.get_client.return_value = self.client + self.command = ListVolumeCommand(self.app, self.app_args) + + def test_execute_command(self): + self.args.tenant_id = 'some uuid' + self.args.start = '2017-01-01' + self.args.end = '2017-01-30' + self.client.get_volumes.return_value = [{'entity_id': 'some uuid', 'project_id': 'tenant id'}] + + expected = (('Volume ID', 'Name', 'Start', 'End', 'Type', 'Size', 'Attachments'), + [('some uuid', None, None, None, None, None, None)]) + + self.assertEqual(expected, self.command.take_action(self.args)) + + self.client.get_volumes.assert_called_once_with(self.args.tenant_id, + datetime.datetime(2017, 1, 1, 0, 0), + datetime.datetime(2017, 1, 30, 0, 0)) diff --git a/almanachclient/tests/v1/test_client.py b/almanachclient/tests/v1/test_client.py index 8e2237d..32c91b2 100644 --- a/almanachclient/tests/v1/test_client.py +++ b/almanachclient/tests/v1/test_client.py @@ -158,15 +158,6 @@ class TestClient(base.TestCase): def test_create_instance(self, requests): self.response.text = '' date = datetime.now() - payload = { - "flavor": "flavor", - "id": "instance_id", - "name": "name", - "created_at": date.strftime(Client.DATE_FORMAT_BODY), - "os_distro": None, - "os_type": None, - "os_version": None, - } requests.return_value = self.response self.response.status_code = 201 @@ -174,7 +165,7 @@ class TestClient(base.TestCase): self.assertTrue(self.client.create_instance('tenant_id', 'instance_id', 'name', 'flavor', date)) requests.assert_called_once_with('{}{}'.format(self.url, '/v1/project/tenant_id/instance'), headers=self.headers, - data=json.dumps(payload), + data=mock.ANY, params=None) @mock.patch('requests.get') @@ -194,3 +185,21 @@ class TestClient(base.TestCase): requests.assert_called_once_with('{}{}'.format(self.url, '/v1/project/my_tenant_id/instances'), params=params, headers=self.headers) + + @mock.patch('requests.get') + def test_get_volumes(self, requests): + expected = [mock.Mock()] + + requests.return_value = self.response + self.response.json.return_value = expected + self.response.status_code = 200 + + start = datetime.now() + end = datetime.now() + params = dict(start=start.strftime(Client.DATE_FORMAT_QS), end=end.strftime(Client.DATE_FORMAT_QS)) + + self.assertEqual(expected, self.client.get_volumes('my_tenant_id', start, end)) + + requests.assert_called_once_with('{}{}'.format(self.url, '/v1/project/my_tenant_id/volumes'), + params=params, + headers=self.headers) diff --git a/almanachclient/v1/client.py b/almanachclient/v1/client.py index 67567c2..9a13504 100644 --- a/almanachclient/v1/client.py +++ b/almanachclient/v1/client.py @@ -45,6 +45,11 @@ class Client(HttpClient): self._delete('{}/{}/volume_type/{}'.format(self.url, self.api_version, volume_type_id)) return True + def get_volumes(self, tenant_id, start, end): + url = '{}/{}/project/{}/volumes'.format(self.url, self.api_version, tenant_id) + params = {'start': self._format_qs_datetime(start), 'end': self._format_qs_datetime(end)} + return self._get(url, params) + def get_instances(self, tenant_id, start, end): url = '{}/{}/project/{}/instances'.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 5c23d0b..ff9bb94 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -47,7 +47,7 @@ Usage: :code:`almanach list-entities ` +--------------------------------------+----------+--------+---------------------------+------+---------------------------------------------------------------------------------------+ | 8c3bc3aa-28d6-4863-b5ae-72e1b415f79d | instance | vm01 | 2017-05-09 14:19:14+00:00 | None | {'image': {'distro': 'centos', 'version': '7', 'os_type': 'linux'}, 'flavor': 'A1.1'} | | f0690323-c394-4848-a272-964aad6431aa | instance | vm02 | 2017-05-15 18:31:42+00:00 | None | {'image': {'distro': 'centos', 'version': '7', 'os_type': 'linux'}, 'flavor': 'A1.1'} | - | 3e3b22e6-a10c-4c00-b8e5-05fcc8422b11 | volume | vol01 | 2017-05-15 19:11:14+00:00 | None | {'attached_to': [], 'volume_type': 'solidfire0'} | + | 3e3b22e6-a10c-4c00-b8e5-05fcc8422b11 | volume | vol01 | 2017-05-15 19:11:14+00:00 | None | {'size': 1, 'attached_to': [], 'volume_type': 'solidfire0'} | +--------------------------------------+----------+--------+---------------------------+------+---------------------------------------------------------------------------------------+ Arguments: @@ -151,6 +151,27 @@ Arguments: * :code:`instance_id`: Instance ID (UUID) * :code:`end`: End date (ISO8601 format) +List Volumes +------------ + +Usage: :code:`almanach list-volumes ` + +.. code:: bash + + almanach list-volumes bca89ae64dba46b8b74653d8d9ae8364 2016-01-01 2017-09-01 + + +--------------------------------------+------+---------------------------+------+------------+------+-------------+ + | Volume ID | Name | Start | End | Type | Size | Attachments | + +--------------------------------------+------+---------------------------+------+------------+------+-------------+ + | 3e3b22e6-a10c-4c00-b8e5-05fcc8422b11 | vol1 | 2017-05-15 19:11:14+00:00 | None | solidfire0 | 1 | [] | + +--------------------------------------+------+---------------------------+------+------------+------+-------------+ + +Arguments: + +* :code:`tenant_id`: Tenant ID (UUID) +* :code:`start`: Start date (ISO8601 format) +* :code:`end`: End date (ISO8601 format) + List Volume Types -----------------