~ubuntu-branches/ubuntu/trusty/heat/trusty

« back to all changes in this revision

Viewing changes to .pc/rename-quantumclient.patch/heat/engine/clients.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short
  • Date: 2013-08-08 01:08:42 UTC
  • Revision ID: package-import@ubuntu.com-20130808010842-77cni2v4vlib7rus
Tags: 2013.2~b2-0ubuntu4
[ Chuck Short ]
* debian/rules: Enable testsuite during builds.
* debian/patches/fix-sqlalchemy-0.8.patch: Build against sqlalchemy 0.8.
* debian/patches/rename-quantumclient.patch: quantumclient -> neutronclient.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
#
 
4
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
5
#    not use this file except in compliance with the License. You may obtain
 
6
#    a copy of the License at
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
10
#    Unless required by applicable law or agreed to in writing, software
 
11
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
12
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
13
#    License for the specific language governing permissions and limitations
 
14
#    under the License.
 
15
 
 
16
from oslo.config import cfg
 
17
 
 
18
from heat.openstack.common import importutils
 
19
from heat.openstack.common import log as logging
 
20
 
 
21
logger = logging.getLogger(__name__)
 
22
 
 
23
 
 
24
from heat.common import heat_keystoneclient as hkc
 
25
from novaclient import client as novaclient
 
26
try:
 
27
    from swiftclient import client as swiftclient
 
28
except ImportError:
 
29
    swiftclient = None
 
30
    logger.info('swiftclient not available')
 
31
try:
 
32
    from quantumclient.v2_0 import client as quantumclient
 
33
except ImportError:
 
34
    quantumclient = None
 
35
    logger.info('quantumclient not available')
 
36
try:
 
37
    from cinderclient import client as cinderclient
 
38
except ImportError:
 
39
    cinderclient = None
 
40
    logger.info('cinderclient not available')
 
41
 
 
42
 
 
43
cloud_opts = [
 
44
    cfg.StrOpt('cloud_backend',
 
45
               default=None,
 
46
               help="Cloud module to use as a backend. Defaults to OpenStack.")
 
47
]
 
48
cfg.CONF.register_opts(cloud_opts)
 
49
 
 
50
 
 
51
class OpenStackClients(object):
 
52
    '''
 
53
    Convenience class to create and cache client instances.
 
54
    '''
 
55
 
 
56
    def __init__(self, context):
 
57
        self.context = context
 
58
        self._nova = {}
 
59
        self._keystone = None
 
60
        self._swift = None
 
61
        self._quantum = None
 
62
        self._cinder = None
 
63
 
 
64
    def keystone(self):
 
65
        if self._keystone:
 
66
            return self._keystone
 
67
 
 
68
        self._keystone = hkc.KeystoneClient(self.context)
 
69
        return self._keystone
 
70
 
 
71
    def url_for(self, **kwargs):
 
72
        return self.keystone().client.service_catalog.url_for(**kwargs)
 
73
 
 
74
    def nova(self, service_type='compute'):
 
75
        if service_type in self._nova:
 
76
            return self._nova[service_type]
 
77
 
 
78
        con = self.context
 
79
        args = {
 
80
            'project_id': con.tenant,
 
81
            'auth_url': con.auth_url,
 
82
            'service_type': service_type,
 
83
        }
 
84
 
 
85
        if con.password is not None:
 
86
            args['username'] = con.username
 
87
            args['api_key'] = con.password
 
88
        elif con.auth_token is not None:
 
89
            args['username'] = None
 
90
            args['api_key'] = None
 
91
        else:
 
92
            logger.error("Nova connection failed, no password or auth_token!")
 
93
            return None
 
94
 
 
95
        client = novaclient.Client(1.1, **args)
 
96
 
 
97
        if con.password is None and con.auth_token is not None:
 
98
            management_url = self.url_for(service_type=service_type)
 
99
            client.client.auth_token = con.auth_token
 
100
            client.client.management_url = management_url
 
101
 
 
102
        self._nova[service_type] = client
 
103
        return client
 
104
 
 
105
    def swift(self):
 
106
        if swiftclient is None:
 
107
            return None
 
108
        if self._swift:
 
109
            return self._swift
 
110
 
 
111
        con = self.context
 
112
        args = {
 
113
            'auth_version': '2.0',
 
114
            'tenant_name': con.tenant,
 
115
            'user': con.username
 
116
        }
 
117
 
 
118
        if con.password is not None:
 
119
            args['key'] = con.password
 
120
            args['authurl'] = con.auth_url
 
121
        elif con.auth_token is not None:
 
122
            args['key'] = None
 
123
            args['authurl'] = None
 
124
            args['preauthtoken'] = con.auth_token
 
125
            args['preauthurl'] = self.url_for(service_type='object-store')
 
126
        else:
 
127
            logger.error("Swift connection failed, no password or " +
 
128
                         "auth_token!")
 
129
            return None
 
130
        self._swift = swiftclient.Connection(**args)
 
131
        return self._swift
 
132
 
 
133
    def quantum(self):
 
134
        if quantumclient is None:
 
135
            return None
 
136
        if self._quantum:
 
137
            logger.debug('using existing _quantum')
 
138
            return self._quantum
 
139
 
 
140
        con = self.context
 
141
        args = {
 
142
            'auth_url': con.auth_url,
 
143
            'service_type': 'network',
 
144
        }
 
145
 
 
146
        if con.password is not None:
 
147
            args['username'] = con.username
 
148
            args['password'] = con.password
 
149
            args['tenant_name'] = con.tenant
 
150
        elif con.auth_token is not None:
 
151
            args['token'] = con.auth_token
 
152
            args['endpoint_url'] = self.url_for(service_type='network')
 
153
        else:
 
154
            logger.error("Quantum connection failed, "
 
155
                         "no password or auth_token!")
 
156
            return None
 
157
        logger.debug('quantum args %s', args)
 
158
 
 
159
        self._quantum = quantumclient.Client(**args)
 
160
 
 
161
        return self._quantum
 
162
 
 
163
    def cinder(self):
 
164
        if cinderclient is None:
 
165
            return self.nova('volume')
 
166
        if self._cinder:
 
167
            return self._cinder
 
168
 
 
169
        con = self.context
 
170
        args = {
 
171
            'service_type': 'volume',
 
172
            'auth_url': con.auth_url,
 
173
            'project_id': con.tenant
 
174
        }
 
175
 
 
176
        if con.password is not None:
 
177
            args['username'] = con.username
 
178
            args['api_key'] = con.password
 
179
        elif con.auth_token is not None:
 
180
            args['username'] = None
 
181
            args['api_key'] = None
 
182
        else:
 
183
            logger.error("Cinder connection failed, "
 
184
                         "no password or auth_token!")
 
185
            return None
 
186
        logger.debug('cinder args %s', args)
 
187
 
 
188
        self._cinder = cinderclient.Client('1', **args)
 
189
        if con.password is None and con.auth_token is not None:
 
190
            management_url = self.url_for(service_type='volume')
 
191
            self._cinder.client.auth_token = con.auth_token
 
192
            self._cinder.client.management_url = management_url
 
193
 
 
194
        return self._cinder
 
195
 
 
196
if cfg.CONF.cloud_backend:
 
197
    cloud_backend_module = importutils.import_module(cfg.CONF.cloud_backend)
 
198
    Clients = cloud_backend_module.Clients
 
199
else:
 
200
    Clients = OpenStackClients
 
201
 
 
202
logger.debug('Using backend %s' % Clients)