1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
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
8
# http://www.apache.org/licenses/LICENSE-2.0
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
16
from oslo.config import cfg
18
from heat.openstack.common import importutils
19
from heat.openstack.common import log as logging
21
logger = logging.getLogger(__name__)
24
from heat.common import heat_keystoneclient as hkc
25
from novaclient import client as novaclient
27
from swiftclient import client as swiftclient
30
logger.info('swiftclient not available')
32
from quantumclient.v2_0 import client as quantumclient
35
logger.info('quantumclient not available')
37
from cinderclient import client as cinderclient
40
logger.info('cinderclient not available')
44
cfg.StrOpt('cloud_backend',
46
help="Cloud module to use as a backend. Defaults to OpenStack.")
48
cfg.CONF.register_opts(cloud_opts)
51
class OpenStackClients(object):
53
Convenience class to create and cache client instances.
56
def __init__(self, context):
57
self.context = context
68
self._keystone = hkc.KeystoneClient(self.context)
71
def url_for(self, **kwargs):
72
return self.keystone().client.service_catalog.url_for(**kwargs)
74
def nova(self, service_type='compute'):
75
if service_type in self._nova:
76
return self._nova[service_type]
80
'project_id': con.tenant,
81
'auth_url': con.auth_url,
82
'service_type': service_type,
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
92
logger.error("Nova connection failed, no password or auth_token!")
95
client = novaclient.Client(1.1, **args)
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
102
self._nova[service_type] = client
106
if swiftclient is None:
113
'auth_version': '2.0',
114
'tenant_name': con.tenant,
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:
123
args['authurl'] = None
124
args['preauthtoken'] = con.auth_token
125
args['preauthurl'] = self.url_for(service_type='object-store')
127
logger.error("Swift connection failed, no password or " +
130
self._swift = swiftclient.Connection(**args)
134
if quantumclient is None:
137
logger.debug('using existing _quantum')
142
'auth_url': con.auth_url,
143
'service_type': 'network',
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')
154
logger.error("Quantum connection failed, "
155
"no password or auth_token!")
157
logger.debug('quantum args %s', args)
159
self._quantum = quantumclient.Client(**args)
164
if cinderclient is None:
165
return self.nova('volume')
171
'service_type': 'volume',
172
'auth_url': con.auth_url,
173
'project_id': con.tenant
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
183
logger.error("Cinder connection failed, "
184
"no password or auth_token!")
186
logger.debug('cinder args %s', args)
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
196
if cfg.CONF.cloud_backend:
197
cloud_backend_module = importutils.import_module(cfg.CONF.cloud_backend)
198
Clients = cloud_backend_module.Clients
200
Clients = OpenStackClients
202
logger.debug('Using backend %s' % Clients)