From 17ba7bc92e1fa500b34f516af9febdc43b23c670 Mon Sep 17 00:00:00 2001 From: Simon Chang Date: Fri, 14 Oct 2016 14:40:17 -0400 Subject: [PATCH] Add compute instance ID and volume ID to trove show output The Compute ID (server_id) and Volume ID (volume_id) associated with a trove instance are useful information for an administrator. This commit add these fields to the trove show output. They will only be visible to users with admin rights. Change-Id: I4a39b59ae610803f5aaf849f2e20ebb6e4ea1565 Closes-Bug: 1633581 --- ...et-instance-details-response-json-http.txt | 2 +- ...db-mgmt-get-instance-details-response.json | 4 ++- ...mgmt-instance-index-response-json-http.txt | 2 +- .../db-mgmt-instance-index-response.json | 4 ++- ...nce-show-comp-vol-id-964db9f52a5ac9c1.yaml | 4 +++ trove/instance/views.py | 6 ++++ .../unittests/cluster/test_cluster_views.py | 8 ++++-- .../unittests/instance/test_instance_views.py | 28 ++++++++++++++++--- 8 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 releasenotes/notes/instance-show-comp-vol-id-964db9f52a5ac9c1.yaml diff --git a/api-ref/source/samples/db-mgmt-get-instance-details-response-json-http.txt b/api-ref/source/samples/db-mgmt-get-instance-details-response-json-http.txt index 4aa235cb2f..feb89a88ad 100644 --- a/api-ref/source/samples/db-mgmt-get-instance-details-response-json-http.txt +++ b/api-ref/source/samples/db-mgmt-get-instance-details-response-json-http.txt @@ -1,5 +1,5 @@ HTTP/1.1 200 OK Content-Type: application/json -Content-Length: 1556 +Content-Length: 1676 Date: Mon, 18 Mar 2013 19:09:17 GMT diff --git a/api-ref/source/samples/db-mgmt-get-instance-details-response.json b/api-ref/source/samples/db-mgmt-get-instance-details-response.json index 2d08d35e68..203159d617 100644 --- a/api-ref/source/samples/db-mgmt-get-instance-details-response.json +++ b/api-ref/source/samples/db-mgmt-get-instance-details-response.json @@ -56,6 +56,7 @@ "status": "ACTIVE", "tenant_id": "3000" }, + "server_id": "44b277eb-39be-4921-be31-3d61b43651d7", "service_status": "ACTIVE", "status": "ACTIVE", "task_description": "No tasks for the instance.", @@ -75,6 +76,7 @@ "status": "in-use", "total": 4.0, "used": 0.16 - } + }, + "volume_id": "VOL_44b277eb-39be-4921-be31-3d61b43651d7" } } diff --git a/api-ref/source/samples/db-mgmt-instance-index-response-json-http.txt b/api-ref/source/samples/db-mgmt-instance-index-response-json-http.txt index 9acc82364c..875f0f20a3 100644 --- a/api-ref/source/samples/db-mgmt-instance-index-response-json-http.txt +++ b/api-ref/source/samples/db-mgmt-instance-index-response-json-http.txt @@ -1,5 +1,5 @@ HTTP/1.1 200 OK Content-Type: application/json -Content-Length: 1105 +Content-Length: 1225 Date: Mon, 18 Mar 2013 19:09:17 GMT diff --git a/api-ref/source/samples/db-mgmt-instance-index-response.json b/api-ref/source/samples/db-mgmt-instance-index-response.json index a29a2fcebb..5736bb17c0 100644 --- a/api-ref/source/samples/db-mgmt-instance-index-response.json +++ b/api-ref/source/samples/db-mgmt-instance-index-response.json @@ -45,6 +45,7 @@ "status": "ACTIVE", "tenant_id": "3000" }, + "server_id": "44b277eb-39be-4921-be31-3d61b43651d7", "service_status": "ACTIVE", "status": "ACTIVE", "task_description": "No tasks for the instance.", @@ -52,7 +53,8 @@ "updated": "2014-10-30T12:30:00", "volume": { "size": 4 - } + }, + "volume_id": "VOL_44b277eb-39be-4921-be31-3d61b43651d7" } ] } diff --git a/releasenotes/notes/instance-show-comp-vol-id-964db9f52a5ac9c1.yaml b/releasenotes/notes/instance-show-comp-vol-id-964db9f52a5ac9c1.yaml new file mode 100644 index 0000000000..d704ea25a6 --- /dev/null +++ b/releasenotes/notes/instance-show-comp-vol-id-964db9f52a5ac9c1.yaml @@ -0,0 +1,4 @@ +--- +other: + - Add Compute ID (server_id) and Volume ID (volume_id) + to trove show output for admin users. Bug #1633581 diff --git a/trove/instance/views.py b/trove/instance/views.py index 73531eae23..6721ec10ce 100644 --- a/trove/instance/views.py +++ b/trove/instance/views.py @@ -16,6 +16,7 @@ from oslo_log import log as logging from trove.common.views import create_links +from trove.common import wsgi from trove.instance import models LOG = logging.getLogger(__name__) @@ -27,6 +28,7 @@ class InstanceView(object): def __init__(self, instance, req=None): self.instance = instance self.req = req + self.context = req.environ[wsgi.CONTEXT_KEY] def data(self): instance_dict = { @@ -122,6 +124,10 @@ class InstanceDetailView(InstanceView): if self.instance.shard_id: result['instance']['shard_id'] = self.instance.shard_id + if self.context.is_admin: + result['instance']['server_id'] = self.instance.server_id + result['instance']['volume_id'] = self.instance.volume_id + return result def _build_fault_info(self): diff --git a/trove/tests/unittests/cluster/test_cluster_views.py b/trove/tests/unittests/cluster/test_cluster_views.py index 45a2127c18..1479bae26c 100644 --- a/trove/tests/unittests/cluster/test_cluster_views.py +++ b/trove/tests/unittests/cluster/test_cluster_views.py @@ -127,6 +127,10 @@ class ClusterInstanceDetailViewTest(trove_testtools.TestCase): self.instance.get_visible_ip_addresses = lambda: ["1.2.3.4"] self.instance.slave_of_id = None self.instance.slaves = None + self.context = trove_testtools.TroveTestContext(self) + self.req = Mock() + self.req.environ = Mock() + self.req.environ.__getitem__ = Mock(return_value=self.context) def tearDown(self): super(ClusterInstanceDetailViewTest, self).tearDown() @@ -135,7 +139,7 @@ class ClusterInstanceDetailViewTest(trove_testtools.TestCase): @patch.object(ClusterInstanceDetailView, '_build_flavor_links') @patch.object(ClusterInstanceDetailView, '_build_configuration_info') def test_data(self, *args): - view = ClusterInstanceDetailView(self.instance, Mock()) + view = ClusterInstanceDetailView(self.instance, self.req) result = view.data() self.assertEqual(self.instance.created, result['instance']['created']) self.assertEqual(self.instance.updated, result['instance']['updated']) @@ -150,7 +154,7 @@ class ClusterInstanceDetailViewTest(trove_testtools.TestCase): @patch.object(ClusterInstanceDetailView, '_build_configuration_info') def test_data_ip(self, *args): self.instance.hostname = None - view = ClusterInstanceDetailView(self.instance, Mock()) + view = ClusterInstanceDetailView(self.instance, self.req) result = view.data() self.assertEqual(self.instance.created, result['instance']['created']) self.assertEqual(self.instance.updated, result['instance']['updated']) diff --git a/trove/tests/unittests/instance/test_instance_views.py b/trove/tests/unittests/instance/test_instance_views.py index e8458c42f0..e41836d8e6 100644 --- a/trove/tests/unittests/instance/test_instance_views.py +++ b/trove/tests/unittests/instance/test_instance_views.py @@ -63,6 +63,8 @@ class InstanceDetailViewTest(trove_testtools.TestCase): self.instance.slave_of_id = None self.instance.slaves = [] self.instance.locality = 'affinity' + self.instance.server_id = 'server_abc' + self.instance.volume_id = 'volume_abc' self.fault_message = 'Error' self.fault_details = 'details' self.fault_date = 'now' @@ -70,6 +72,10 @@ class InstanceDetailViewTest(trove_testtools.TestCase): self.instance.fault.message = self.fault_message self.instance.fault.details = self.fault_details self.instance.fault.updated = self.fault_date + self.context = trove_testtools.TroveTestContext(self) + self.req = Mock() + self.req.environ = Mock() + self.req.environ.__getitem__ = Mock(return_value=self.context) def tearDown(self): super(InstanceDetailViewTest, self).tearDown() @@ -78,7 +84,7 @@ class InstanceDetailViewTest(trove_testtools.TestCase): InstanceDetailView._build_configuration_info = self.build_config_method def test_data_hostname(self): - view = InstanceDetailView(self.instance, Mock()) + view = InstanceDetailView(self.instance, self.req) result = view.data() self.assertEqual(self.instance.created, result['instance']['created']) self.assertEqual(self.instance.updated, result['instance']['updated']) @@ -90,7 +96,7 @@ class InstanceDetailViewTest(trove_testtools.TestCase): def test_data_ip(self): self.instance.hostname = None - view = InstanceDetailView(self.instance, Mock()) + view = InstanceDetailView(self.instance, self.req) result = view.data() self.assertEqual(self.instance.created, result['instance']['created']) self.assertEqual(self.instance.updated, result['instance']['updated']) @@ -101,13 +107,13 @@ class InstanceDetailViewTest(trove_testtools.TestCase): def test_locality(self): self.instance.hostname = None - view = InstanceDetailView(self.instance, Mock()) + view = InstanceDetailView(self.instance, self.req) result = view.data() self.assertEqual(self.instance.locality, result['instance']['locality']) def test_fault(self): - view = InstanceDetailView(self.instance, Mock()) + view = InstanceDetailView(self.instance, self.req) result = view.data() self.assertEqual(self.fault_message, result['instance']['fault']['message']) @@ -115,3 +121,17 @@ class InstanceDetailViewTest(trove_testtools.TestCase): result['instance']['fault']['details']) self.assertEqual(self.fault_date, result['instance']['fault']['created']) + + def test_admin_view(self): + self.context.is_admin = True + view = InstanceDetailView(self.instance, self.req) + result = view.data() + self.assertIn('server_id', result['instance']) + self.assertIn('volume_id', result['instance']) + + def test_non_admin_view(self): + self.context.is_admin = False + view = InstanceDetailView(self.instance, self.req) + result = view.data() + self.assertNotIn('server_id', result['instance']) + self.assertNotIn('volume_id', result['instance'])