~ubuntu-branches/ubuntu/precise/horizon/precise-updates

« back to all changes in this revision

Viewing changes to horizon/api/base.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-03-02 12:11:59 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20120302121159-65b88lcl4slve26i
Tags: 2012.1~e4-0ubuntu1
* New upstream version.
* debian/rules: Update due to upstream build changes.
* debian/control: Update standards-version.
* debian/patches/openstack-config-settings.patch: Dropped
* debian/patches/fix-dashboard-django-wsgi.patch: Refreshed
* debian/patches/fix-dashboard-manage.patch: Refreshed
* debian/openstack-dashboard.install: Update due to upstream build changes.
* debian/dashboard: Update to upstream build changes.
* debian/pydist-overrides: Dont try to install python-django-nose-selenium.
* debian/openstack-dashboard.install: Add missing config files.
* debian/rules: Fix broken settings.py
* debian/patches/pkg-setup.patch: Copy missing templates, shameously
  taken from debian
* debian/patches/fix-broken-tarbll.patch: Add missing files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2012 United States Government as represented by the
 
4
# Administrator of the National Aeronautics and Space Administration.
 
5
# All Rights Reserved.
 
6
#
 
7
# Copyright 2012 Nebula, Inc.
 
8
#
 
9
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
10
#    not use this file except in compliance with the License. You may obtain
 
11
#    a copy of the License at
 
12
#
 
13
#         http://www.apache.org/licenses/LICENSE-2.0
 
14
#
 
15
#    Unless required by applicable law or agreed to in writing, software
 
16
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
17
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
18
#    License for the specific language governing permissions and limitations
 
19
#    under the License.
 
20
 
 
21
import logging
 
22
 
 
23
from horizon import exceptions
 
24
 
 
25
 
 
26
__all__ = ('APIResourceWrapper', 'APIDictWrapper',
 
27
           'get_service_from_catalog', 'url_for',)
 
28
 
 
29
 
 
30
LOG = logging.getLogger(__name__)
 
31
 
 
32
 
 
33
class APIResourceWrapper(object):
 
34
    """ Simple wrapper for api objects
 
35
 
 
36
        Define _attrs on the child class and pass in the
 
37
        api object as the only argument to the constructor
 
38
    """
 
39
    _attrs = []
 
40
 
 
41
    def __init__(self, apiresource):
 
42
        self._apiresource = apiresource
 
43
 
 
44
    def __getattr__(self, attr):
 
45
        if attr in self._attrs:
 
46
            # __getattr__ won't find properties
 
47
            return self._apiresource.__getattribute__(attr)
 
48
        else:
 
49
            LOG.debug('Attempted to access unknown attribute "%s" on'
 
50
                      ' APIResource object of type "%s" wrapping resource of'
 
51
                      ' type "%s"' % (attr, self.__class__,
 
52
                                      self._apiresource.__class__))
 
53
            raise AttributeError(attr)
 
54
 
 
55
 
 
56
class APIDictWrapper(object):
 
57
    """ Simple wrapper for api dictionaries
 
58
 
 
59
        Some api calls return dictionaries.  This class provides identical
 
60
        behavior as APIResourceWrapper, except that it will also behave as a
 
61
        dictionary, in addition to attribute accesses.
 
62
 
 
63
        Attribute access is the preferred method of access, to be
 
64
        consistent with api resource objects from novclient.
 
65
    """
 
66
    def __init__(self, apidict):
 
67
        self._apidict = apidict
 
68
 
 
69
    def __getattr__(self, attr):
 
70
        try:
 
71
            return self._apidict[attr]
 
72
        except KeyError:
 
73
            msg = 'Unknown attribute "%(attr)s" on APIResource object ' \
 
74
                  'of type "%(cls)s"' % {'attr': attr, 'cls': self.__class__}
 
75
            LOG.debug(msg)
 
76
            raise AttributeError(msg)
 
77
 
 
78
    def __getitem__(self, item):
 
79
        try:
 
80
            return self.__getattr__(item)
 
81
        except AttributeError, e:
 
82
            # caller is expecting a KeyError
 
83
            raise KeyError(e)
 
84
 
 
85
    def get(self, item, default=None):
 
86
        try:
 
87
            return self.__getattr__(item)
 
88
        except AttributeError:
 
89
            return default
 
90
 
 
91
 
 
92
def get_service_from_catalog(catalog, service_type):
 
93
    if catalog:
 
94
        for service in catalog:
 
95
            if service['type'] == service_type:
 
96
                return service
 
97
    return None
 
98
 
 
99
 
 
100
def url_for(request, service_type, admin=False, endpoint_type='internalURL'):
 
101
    catalog = request.user.service_catalog
 
102
    service = get_service_from_catalog(catalog, service_type)
 
103
    if service:
 
104
        try:
 
105
            if admin:
 
106
                return service['endpoints'][0]['adminURL']
 
107
            else:
 
108
                return service['endpoints'][0][endpoint_type]
 
109
        except (IndexError, KeyError):
 
110
            raise exceptions.ServiceCatalogException(service_type)
 
111
    else:
 
112
        raise exceptions.ServiceCatalogException(service_type)