From 8344d16ec940cf194b8bf6f1811c3d5a0c6b15d8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= <fguillot@internap.com>
Date: Fri, 2 Jun 2017 11:31:37 -0400
Subject: [PATCH] Make end date optional for list-entities command

Change-Id: I2e1969d4922f4922ece2a7a63aa38949848e52dc
---
 almanachclient/commands/list_entity.py          |  4 ++--
 almanachclient/shell.py                         |  2 +-
 .../tests/commands/test_list_entity.py          | 14 ++++++++++++++
 almanachclient/tests/v1/test_client.py          | 17 +++++++++++++++++
 almanachclient/v1/client.py                     |  8 ++++++--
 doc/source/usage_cli.rst                        |  4 ++--
 6 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/almanachclient/commands/list_entity.py b/almanachclient/commands/list_entity.py
index 0c71077..6c24500 100644
--- a/almanachclient/commands/list_entity.py
+++ b/almanachclient/commands/list_entity.py
@@ -25,12 +25,12 @@ class ListEntityCommand(Lister):
         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')
+        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)
+        end = parser.parse(parsed_args.end) if parsed_args.end else None
         entities = self.app.get_client().get_tenant_entities(parsed_args.tenant_id, start, end)
         return self.columns, self._format_rows(entities)
 
diff --git a/almanachclient/shell.py b/almanachclient/shell.py
index a8a9799..3ab549a 100644
--- a/almanachclient/shell.py
+++ b/almanachclient/shell.py
@@ -86,7 +86,7 @@ class AlmanachApp(app.App):
                             help='Keystone V3 URL (Env: OS_AUTH_URL).')
 
         parser.add_argument('--os-region-name',
-                            default=os.environ.get('`'),
+                            default=os.environ.get('OS_REGION_NAME'),
                             help='OpenStack region name (Env: OS_REGION_NAME).')
 
         parser.add_argument('--os-password',
diff --git a/almanachclient/tests/commands/test_list_entity.py b/almanachclient/tests/commands/test_list_entity.py
index 0fee6e6..18310da 100644
--- a/almanachclient/tests/commands/test_list_entity.py
+++ b/almanachclient/tests/commands/test_list_entity.py
@@ -47,3 +47,17 @@ class TestListEntityCommand(base.TestCase):
         self.client.get_tenant_entities.assert_called_once_with(self.args.tenant_id,
                                                                 datetime.datetime(2017, 1, 1, 0, 0),
                                                                 datetime.datetime(2017, 1, 30, 0, 0))
+
+    def test_execute_command_without_end_date(self):
+        self.args.tenant_id = 'some uuid'
+        self.args.start = '2017-01-01'
+        self.client.get_tenant_entities.return_value = [{'entity_id': 'some uuid', 'project_id': 'tenant id'}]
+
+        expected = (('Entity ID', 'Type', 'Name', 'Start', 'End', 'Properties'),
+                    [('some uuid', None, None, None, None, None)])
+
+        self.assertEqual(expected, self.command.take_action(self.args))
+
+        self.client.get_tenant_entities.assert_called_once_with(self.args.tenant_id,
+                                                                datetime.datetime(2017, 1, 1, 0, 0),
+                                                                None)
diff --git a/almanachclient/tests/v1/test_client.py b/almanachclient/tests/v1/test_client.py
index 9890af8..3cad915 100644
--- a/almanachclient/tests/v1/test_client.py
+++ b/almanachclient/tests/v1/test_client.py
@@ -76,6 +76,23 @@ class TestClient(base.TestCase):
                                          params=params,
                                          headers=self.headers)
 
+    @mock.patch('requests.get')
+    def test_get_tenant_entities_without_end_date(self, requests):
+        expected = [mock.Mock()]
+
+        requests.return_value = self.response
+        self.response.json.return_value = expected
+        self.response.status_code = 200
+
+        start = datetime.now()
+        params = dict(start=start.strftime(Client.DATE_FORMAT_QS))
+
+        self.assertEqual(expected, self.client.get_tenant_entities('my_tenant_id', start))
+
+        requests.assert_called_once_with('{}{}'.format(self.url, '/v1/project/my_tenant_id/entities'),
+                                         params=params,
+                                         headers=self.headers)
+
     @mock.patch('requests.get')
     def test_get_entity(self, requests):
         expected = [mock.Mock()]
diff --git a/almanachclient/v1/client.py b/almanachclient/v1/client.py
index 0d09064..b30936b 100644
--- a/almanachclient/v1/client.py
+++ b/almanachclient/v1/client.py
@@ -253,7 +253,7 @@ class Client(HttpClient):
         url = '{}/{}/entity/{}'.format(self.url, self.api_version, entity_id)
         return self._get(url)
 
-    def get_tenant_entities(self, tenant_id, start, end):
+    def get_tenant_entities(self, tenant_id, start, end=None):
         """List instances and volumes for a tenant.
 
         :arg str tenant_id: Tenant UUID
@@ -263,7 +263,11 @@ class Client(HttpClient):
         :rtype: list
         """
         url = '{}/{}/project/{}/entities'.format(self.url, self.api_version, tenant_id)
-        params = {'start': self._format_qs_datetime(start), 'end': self._format_qs_datetime(end)}
+        params = {'start': self._format_qs_datetime(start)}
+
+        if end:
+            params['end'] = self._format_qs_datetime(end)
+
         return self._get(url, params)
 
     def update_instance_entity(self, instance_id, **kwargs):
diff --git a/doc/source/usage_cli.rst b/doc/source/usage_cli.rst
index 316897f..a444f1e 100644
--- a/doc/source/usage_cli.rst
+++ b/doc/source/usage_cli.rst
@@ -48,7 +48,7 @@ Usage: :code:`almanach endpoint`
 Get Tenant Entities
 -------------------
 
-Usage: :code:`almanach list-entities <tenant_id> <start> <end>`
+Usage: :code:`almanach list-entities <tenant_id> <start> --end <end>`
 
 .. code:: bash
 
@@ -66,7 +66,7 @@ Arguments:
 
 * :code:`tenant_id`: Tenant ID (UUID)
 * :code:`start`: Start date (ISO8601 format)
-* :code:`end`: End date (ISO8601 format)
+* :code:`end`: End date (ISO8601 format), optional
 
 Get one Entity
 --------------