From 8ea84225c39f9fca8a6b8808d5ee8b47976260fb Mon Sep 17 00:00:00 2001 From: Gabriel Hurley Date: Sun, 18 Mar 2012 16:47:33 -0700 Subject: [PATCH] Adds service name to services table. Also moves most of the logic to a Service wrapper class to make it reusable. Adds tests for the new service class as well as the syspanel services view. Fixes bug 956552. Change-Id: I9407578bb27f3fe0765397793f2de03ed084637b --- horizon/api/keystone.py | 25 +++++++++++++ .../dashboards/syspanel/services/tables.py | 13 ++----- horizon/dashboards/syspanel/services/tests.py | 36 +++++++++++++++++++ horizon/dashboards/syspanel/services/views.py | 15 ++------ horizon/tests/api_tests/keystone_tests.py | 14 ++++++++ 5 files changed, 80 insertions(+), 23 deletions(-) diff --git a/horizon/api/keystone.py b/horizon/api/keystone.py index 9bf872c8d..45f23f632 100644 --- a/horizon/api/keystone.py +++ b/horizon/api/keystone.py @@ -20,8 +20,10 @@ # under the License. import logging +import urlparse from django.conf import settings +from django.utils.translation import ugettext_lazy as _ from keystoneclient import service_catalog from keystoneclient.v2_0 import client as keystone_client @@ -35,6 +37,29 @@ LOG = logging.getLogger(__name__) DEFAULT_ROLE = None +class Service(base.APIDictWrapper): + """ Wrapper for a dict based on the service data from keystone. """ + _attrs = ['id', 'type', 'name'] + + def __init__(self, service, *args, **kwargs): + super(Service, self).__init__(service, *args, **kwargs) + self.url = service['endpoints'][0]['internalURL'] + self.host = urlparse.urlparse(self.url).hostname + self.region = service['endpoints'][0]['region'] + self.disabled = None + + def __unicode__(self): + if(self.type == "identity"): + return _("%(type)s (%(backend)s backend)") \ + % {"type": self.type, + "backend": keystone_backend_name()} + else: + return self.type + + def __repr__(self): + return "" % unicode(self) + + def _get_endpoint_url(request, endpoint_type, catalog=None): if getattr(request.user, "service_catalog", None): return base.url_for(request, diff --git a/horizon/dashboards/syspanel/services/tables.py b/horizon/dashboards/syspanel/services/tables.py index 3f4e59c88..71f61be66 100644 --- a/horizon/dashboards/syspanel/services/tables.py +++ b/horizon/dashboards/syspanel/services/tables.py @@ -4,7 +4,6 @@ from django import template from django.utils.translation import ugettext_lazy as _ from horizon import tables -from horizon import api LOG = logging.getLogger(__name__) @@ -34,18 +33,10 @@ def get_enabled(service, reverse=False): return options[0] if not service.disabled else options[1] -def get_service_name(service): - if(service.type == "identity"): - return _("%(type)s (%(backend)s backend)") \ - % {"type": service.type, - "backend": api.keystone_backend_name()} - else: - return service.type - - class ServicesTable(tables.DataTable): id = tables.Column('id', verbose_name=_('Id'), hidden=True) - service = tables.Column(get_service_name, verbose_name=_('Service')) + name = tables.Column("name", verbose_name=_('Name')) + service_type = tables.Column('__unicode__', verbose_name=_('Service')) host = tables.Column('host', verbose_name=_('Host')) enabled = tables.Column(get_enabled, verbose_name=_('Enabled'), diff --git a/horizon/dashboards/syspanel/services/tests.py b/horizon/dashboards/syspanel/services/tests.py index e69de29bb..dbbedce9a 100644 --- a/horizon/dashboards/syspanel/services/tests.py +++ b/horizon/dashboards/syspanel/services/tests.py @@ -0,0 +1,36 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 Nebula, Inc. +# +# 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 django.core.urlresolvers import reverse + +from horizon import test + + +INDEX_URL = reverse('horizon:syspanel:services:index') + + +class ServicessViewTests(test.BaseAdminViewTests): + def test_index(self): + res = self.client.get(INDEX_URL) + self.assertTemplateUsed(res, 'syspanel/services/index.html') + self.assertQuerysetEqual(res.context['table'].data, + ['', + '', + '', + '', + '', + '', + '']) diff --git a/horizon/dashboards/syspanel/services/views.py b/horizon/dashboards/syspanel/services/views.py index 9b73c8bfc..480c865ec 100644 --- a/horizon/dashboards/syspanel/services/views.py +++ b/horizon/dashboards/syspanel/services/views.py @@ -19,7 +19,6 @@ # under the License. import logging -import urlparse from horizon import api from horizon import tables @@ -35,15 +34,7 @@ class IndexView(tables.DataTableView): def get_data(self): services = [] - for i, service in enumerate(self.request.session['serviceCatalog']): - url = service['endpoints'][0]['internalURL'] - hostname = urlparse.urlparse(url).hostname - row = {'id': i, # id is required for table to render properly - 'type': service['type'], - 'internalURL': url, - 'host': hostname, - 'region': service['endpoints'][0]['region'], - 'disabled': None} - services.append(api.base.APIDictWrapper(row)) - + for i, service in enumerate(self.request.user.service_catalog): + service['id'] = i + services.append(api.keystone.Service(service)) return services diff --git a/horizon/tests/api_tests/keystone_tests.py b/horizon/tests/api_tests/keystone_tests.py index e29029ccd..c3730e040 100644 --- a/horizon/tests/api_tests/keystone_tests.py +++ b/horizon/tests/api_tests/keystone_tests.py @@ -173,3 +173,17 @@ class RoleAPITests(test.APITestCase): # Verify that a second call doesn't hit the API again, # (it would show up in mox as an unexpected method call) role = api.keystone.get_default_role(self.request) + + +class ServiceAPITests(test.APITestCase): + def test_service_wrapper(self): + catalog = self.service_catalog + identity_data = api.base.get_service_from_catalog(catalog, "identity") + identity_data['id'] = 1 + service = api.keystone.Service(identity_data) + self.assertEqual(unicode(service), u"identity (native backend)") + self.assertEqual(service.region, + identity_data["endpoints"][0]["region"]) + self.assertEqual(service.url, + "http://int.keystone.example.com:5000/v2.0") + self.assertEqual(service.host, "int.keystone.example.com")