diff --git a/almanachclient/commands/list_instance.py b/almanachclient/commands/list_instance.py new file mode 100644 index 0000000..7741ba1 --- /dev/null +++ b/almanachclient/commands/list_instance.py @@ -0,0 +1,47 @@ +# 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 ListInstanceCommand(Lister): + """Show all instances for a given tenant""" + + columns = ('Instance ID', 'Name', 'Start', 'End', 'Flavor', 'Image Meta') + + 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_instances(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('flavor'), + entity.get('image_meta', entity.get('os')))) + return rows diff --git a/almanachclient/shell.py b/almanachclient/shell.py index b6e6a05..2ca2356 100644 --- a/almanachclient/shell.py +++ b/almanachclient/shell.py @@ -25,6 +25,7 @@ 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_entity import ListEntityCommand +from almanachclient.commands.list_instance import ListInstanceCommand from almanachclient.commands.list_volume_type import ListVolumeTypeCommand from almanachclient.commands.update_instance_entity import UpdateInstanceEntityCommand from almanachclient.commands.version import VersionCommand @@ -41,6 +42,7 @@ class AlmanachCommandManager(commandmanager.CommandManager): 'delete-volume-type': DeleteVolumeTypeCommand, 'list-volume-types': ListVolumeTypeCommand, 'get-volume-type': GetVolumeTypeCommand, + 'list-instances': ListInstanceCommand, 'create-instance': CreateInstanceCommand, 'delete-instance': DeleteInstanceCommand, 'list-entities': ListEntityCommand, diff --git a/almanachclient/tests/commands/test_list_instance.py b/almanachclient/tests/commands/test_list_instance.py new file mode 100644 index 0000000..7ac5ebb --- /dev/null +++ b/almanachclient/tests/commands/test_list_instance.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_instance import ListInstanceCommand + +from almanachclient.tests import base + + +class TestListInstanceCommand(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 = ListInstanceCommand(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_instances.return_value = [{'entity_id': 'some uuid', 'project_id': 'tenant id'}] + + expected = (('Instance ID', 'Name', 'Start', 'End', 'Flavor', 'Image Meta'), + [('some uuid', None, None, None, None, None)]) + + self.assertEqual(expected, self.command.take_action(self.args)) + + self.client.get_instances.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 34f7c00..8e2237d 100644 --- a/almanachclient/tests/v1/test_client.py +++ b/almanachclient/tests/v1/test_client.py @@ -176,3 +176,21 @@ class TestClient(base.TestCase): headers=self.headers, data=json.dumps(payload), params=None) + + @mock.patch('requests.get') + def test_get_instances(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_instances('my_tenant_id', start, end)) + + requests.assert_called_once_with('{}{}'.format(self.url, '/v1/project/my_tenant_id/instances'), + params=params, + headers=self.headers) diff --git a/almanachclient/v1/client.py b/almanachclient/v1/client.py index 08bc13d..67567c2 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_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)} + return self._get(url, params) + def create_instance(self, tenant_id, instance_id, name, flavor, start, image_meta=None): url = '{}/{}/project/{}/instance'.format(self.url, self.api_version, tenant_id) image_meta = image_meta or {} diff --git a/doc/source/usage.rst b/doc/source/usage.rst index e0b0c36..5c23d0b 100644 --- a/doc/source/usage.rst +++ b/doc/source/usage.rst @@ -33,7 +33,7 @@ Usage: :code:`almanach endpoint` http://almanach.example.org -Get tenant entities +Get Tenant Entities ------------------- Usage: :code:`almanach list-entities ` @@ -50,6 +50,35 @@ Usage: :code:`almanach list-entities ` | 3e3b22e6-a10c-4c00-b8e5-05fcc8422b11 | volume | vol01 | 2017-05-15 19:11:14+00:00 | None | {'attached_to': [], 'volume_type': 'solidfire0'} | +--------------------------------------+----------+--------+---------------------------+------+---------------------------------------------------------------------------------------+ +Arguments: + +* :code:`tenant_id`: Tenant ID (UUID) +* :code:`start`: Start date (ISO8601 format) +* :code:`end`: End date (ISO8601 format) + + +List Instances Entities +----------------------- + +Usage: :code:`almanach list-instances ` + +.. code:: bash + + almanach list-entities bca89ae64dba46b8b74653d8d9ae8364 2016-01-01 2017-05-30 + + +--------------------------------------+--------+---------------------------+----------------------------------+---------+------------------------------------------------------------+ + | Instance ID | Name | Start | End | Flavor | Image Meta | + +--------------------------------------+--------+---------------------------+----------------------------------+---------+------------------------------------------------------------+ + | f0690323-c394-4848-a272-964aad6431aa | vm02 | 2017-05-15 18:31:42+00:00 | None | A1.1 | {'distro': 'centos', 'version': '7', 'os_type': 'linux'} | + | 8c3bc3aa-28d6-4863-b5ae-72e1b415f79d | vm01 | 2017-05-09 14:19:14+00:00 | 2017-05-17 09:37:47.775000+00:00 | A1.1 | {'distro': 'centos', 'version': '7', 'os_type': 'linux'} | + +--------------------------------------+--------+---------------------------+----------------------------------+---------+------------------------------------------------------------+ + +Arguments: + +* :code:`tenant_id`: Tenant ID (UUID) +* :code:`start`: Start date (ISO8601 format) +* :code:`end`: End date (ISO8601 format) + Create Instance Entity ----------------------