052aa55d34
There are no longer two separate projects living inside the horizon repository. There is a single project now with a single setup.py, single README, etc. The openstack-dashboard/dashboard django project is now named "openstack_dashboard" and lives as an example project in the topmost horizon directory. The "horizon/horizon" directory has been bumped up a level and now is directly on the path when the root horizon directory is on your python path. Javascript media which the horizon module directly relies upon now ships in the horizon/static dir rather than openstack-dashboard/dashboard/static. All the corresponding setup, installation, build, and env scripts have been updated accordingly. Implements blueprint unified-packaging. Change-Id: Ieed8e3c777432cd046c3e0298869a9428756ab62
113 lines
3.6 KiB
Python
113 lines
3.6 KiB
Python
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
# Copyright 2012 United States Government as represented by the
|
|
# Administrator of the National Aeronautics and Space Administration.
|
|
# All Rights Reserved.
|
|
#
|
|
# 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.
|
|
|
|
import logging
|
|
|
|
from horizon import exceptions
|
|
|
|
|
|
__all__ = ('APIResourceWrapper', 'APIDictWrapper',
|
|
'get_service_from_catalog', 'url_for',)
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
class APIResourceWrapper(object):
|
|
""" Simple wrapper for api objects
|
|
|
|
Define _attrs on the child class and pass in the
|
|
api object as the only argument to the constructor
|
|
"""
|
|
_attrs = []
|
|
|
|
def __init__(self, apiresource):
|
|
self._apiresource = apiresource
|
|
|
|
def __getattr__(self, attr):
|
|
if attr in self._attrs:
|
|
# __getattr__ won't find properties
|
|
return self._apiresource.__getattribute__(attr)
|
|
else:
|
|
LOG.debug('Attempted to access unknown attribute "%s" on'
|
|
' APIResource object of type "%s" wrapping resource of'
|
|
' type "%s"' % (attr, self.__class__,
|
|
self._apiresource.__class__))
|
|
raise AttributeError(attr)
|
|
|
|
|
|
class APIDictWrapper(object):
|
|
""" Simple wrapper for api dictionaries
|
|
|
|
Some api calls return dictionaries. This class provides identical
|
|
behavior as APIResourceWrapper, except that it will also behave as a
|
|
dictionary, in addition to attribute accesses.
|
|
|
|
Attribute access is the preferred method of access, to be
|
|
consistent with api resource objects from novclient.
|
|
"""
|
|
def __init__(self, apidict):
|
|
self._apidict = apidict
|
|
|
|
def __getattr__(self, attr):
|
|
try:
|
|
return self._apidict[attr]
|
|
except KeyError:
|
|
msg = 'Unknown attribute "%(attr)s" on APIResource object ' \
|
|
'of type "%(cls)s"' % {'attr': attr, 'cls': self.__class__}
|
|
LOG.debug(msg)
|
|
raise AttributeError(msg)
|
|
|
|
def __getitem__(self, item):
|
|
try:
|
|
return self.__getattr__(item)
|
|
except AttributeError, e:
|
|
# caller is expecting a KeyError
|
|
raise KeyError(e)
|
|
|
|
def get(self, item, default=None):
|
|
try:
|
|
return self.__getattr__(item)
|
|
except AttributeError:
|
|
return default
|
|
|
|
|
|
def get_service_from_catalog(catalog, service_type):
|
|
if catalog:
|
|
for service in catalog:
|
|
if service['type'] == service_type:
|
|
return service
|
|
return None
|
|
|
|
|
|
def url_for(request, service_type, admin=False, endpoint_type='internalURL'):
|
|
catalog = request.user.service_catalog
|
|
service = get_service_from_catalog(catalog, service_type)
|
|
if service:
|
|
try:
|
|
if admin:
|
|
return service['endpoints'][0]['adminURL']
|
|
else:
|
|
return service['endpoints'][0][endpoint_type]
|
|
except (IndexError, KeyError):
|
|
raise exceptions.ServiceCatalogException(service_type)
|
|
else:
|
|
raise exceptions.ServiceCatalogException(service_type)
|