Implement heat client plugin

This moves the client creation code out of Clients._heat() into
its own client plugin.

Clients._get_client_option is no longer used, so has been deleted

Change-Id: Ie14906b6cc605d6db49a5153ffa27f82debcc430
This commit is contained in:
Steve Baker 2014-06-04 16:51:32 +12:00
parent bbea94577f
commit cc1ffff771
4 changed files with 63 additions and 55 deletions

View File

@ -11,7 +11,6 @@
# License for the specific language governing permissions and limitations
# under the License.
from heatclient import client as heatclient
from oslo.config import cfg
from stevedore import extension
import warnings
@ -123,49 +122,11 @@ class OpenStackClients(object):
'Replace with calls to client("ceilometer")')
return self.client('ceilometer')
def _get_client_option(self, client, option):
try:
group_name = 'clients_' + client
cfg.CONF.import_opt(option, 'heat.common.config',
group=group_name)
return getattr(getattr(cfg.CONF, group_name), option)
except (cfg.NoSuchGroupError, cfg.NoSuchOptError):
cfg.CONF.import_opt(option, 'heat.common.config', group='clients')
return getattr(cfg.CONF.clients, option)
def _get_heat_url(self):
heat_url = self._get_client_option('heat', 'url')
if heat_url:
tenant_id = self.context.tenant_id
heat_url = heat_url % {'tenant_id': tenant_id}
return heat_url
def heat(self):
warnings.warn('heat() is deprecated. '
'Replace with calls to client("heat")')
return self.client('heat')
def _heat(self):
con = self.context
endpoint_type = self._get_client_option('heat', 'endpoint_type')
args = {
'auth_url': con.auth_url,
'token': self.auth_token,
'username': None,
'password': None,
'ca_file': self._get_client_option('heat', 'ca_file'),
'cert_file': self._get_client_option('heat', 'cert_file'),
'key_file': self._get_client_option('heat', 'key_file'),
'insecure': self._get_client_option('heat', 'insecure')
}
endpoint = self._get_heat_url()
if not endpoint:
endpoint = self.url_for(service_type='orchestration',
endpoint_type=endpoint_type)
return heatclient.Client('1', endpoint, **args)
class ClientBackend(object):
'''Delay choosing the backend client module until the client's class needs

View File

@ -0,0 +1,47 @@
#
# 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 heatclient import client as hc
from heat.engine.clients import client_plugin
class HeatClientPlugin(client_plugin.ClientPlugin):
def _create(self):
con = self.context
endpoint_type = self._get_client_option('heat', 'endpoint_type')
args = {
'auth_url': con.auth_url,
'token': self.auth_token,
'username': None,
'password': None,
'ca_file': self._get_client_option('heat', 'ca_file'),
'cert_file': self._get_client_option('heat', 'cert_file'),
'key_file': self._get_client_option('heat', 'key_file'),
'insecure': self._get_client_option('heat', 'insecure')
}
endpoint = self._get_heat_url()
if not endpoint:
endpoint = self.url_for(service_type='orchestration',
endpoint_type=endpoint_type)
return hc.Client('1', endpoint, **args)
def _get_heat_url(self):
heat_url = self._get_client_option('heat', 'url')
if heat_url:
tenant_id = self.context.tenant_id
heat_url = heat_url % {'tenant_id': tenant_id}
return heat_url

View File

@ -23,14 +23,11 @@ from heat.tests.common import HeatTestCase
class ClientsTest(HeatTestCase):
def test_clients_chosen_at_module_initilization(self):
self.assertFalse(hasattr(clients.Clients, 'nova'))
self.assertTrue(hasattr(clients.Clients('fakecontext'), 'nova'))
def test_clients_get_heat_url(self):
con = mock.Mock()
con.tenant_id = "b363706f891f48019483f8bd6503c54b"
obj = clients.Clients(con)
c = clients.Clients(con)
obj = c.client_plugin('heat')
obj._get_client_option = mock.Mock()
obj._get_client_option.return_value = None
self.assertIsNone(obj._get_heat_url())
@ -49,16 +46,17 @@ class ClientsTest(HeatTestCase):
con.auth_url = "http://auth.example.com:5000/v2.0"
con.tenant_id = "b363706f891f48019483f8bd6503c54b"
con.auth_token = "3bcc3d3a03f44e3d8377f9247b0ad155"
obj = clients.Clients(con)
c = clients.Clients(con)
obj = c.client_plugin('heat')
obj._get_heat_url = mock.Mock(name="_get_heat_url")
obj._get_heat_url.return_value = None
obj.url_for = mock.Mock(name="url_for")
obj.url_for.return_value = "url_from_keystone"
obj.client('heat')
obj.client()
self.assertEqual('url_from_keystone', mock_call.call_args[0][1])
obj._get_heat_url.return_value = "url_from_config"
del(obj._clients['heat'])
obj.client('heat')
obj._client = None
obj.client()
self.assertEqual('url_from_config', mock_call.call_args[0][1])
@mock.patch.object(heatclient, 'Client')
@ -68,13 +66,13 @@ class ClientsTest(HeatTestCase):
con.auth_url = "http://auth.example.com:5000/v2.0"
con.tenant_id = "b363706f891f48019483f8bd6503c54b"
con.auth_token = None
obj = clients.Clients(con)
c = clients.Clients(con)
obj = c.client_plugin('heat')
obj._get_heat_url = mock.Mock(name="_get_heat_url")
obj._get_heat_url.return_value = None
obj.url_for = mock.Mock(name="url_for")
obj.url_for.return_value = "url_from_keystone"
self.assertIsNotNone(obj.client('heat'))
self.assertEqual('anewtoken', obj.client('keystone').auth_token)
self.assertEqual('anewtoken', c.client('keystone').auth_token)
@mock.patch.object(heatclient, 'Client')
def test_clients_heat_cached(self, mock_call):
@ -83,14 +81,15 @@ class ClientsTest(HeatTestCase):
con.auth_url = "http://auth.example.com:5000/v2.0"
con.tenant_id = "b363706f891f48019483f8bd6503c54b"
con.auth_token = "3bcc3d3a03f44e3d8377f9247b0ad155"
obj = clients.Clients(con)
c = clients.Clients(con)
obj = c.client_plugin('heat')
obj._get_heat_url = mock.Mock(name="_get_heat_url")
obj._get_heat_url.return_value = None
obj.url_for = mock.Mock(name="url_for")
obj.url_for.return_value = "url_from_keystone"
obj._heat = None
heat = obj.client('heat')
heat_cached = obj.client('heat')
obj._client = None
heat = obj.client()
heat_cached = obj.client()
self.assertEqual(heat, heat_cached)
def test_clients_auth_token_update(self):

View File

@ -41,6 +41,7 @@ heat.clients =
ceilometer = heat.engine.clients.os.ceilometer:CeilometerClientPlugin
cinder = heat.engine.clients.os.cinder:CinderClientPlugin
glance = heat.engine.clients.os.glance:GlanceClientPlugin
heat = heat.engine.clients.os.heat_plugin:HeatClientPlugin
nova = heat.engine.clients.os.nova:NovaClientPlugin
neutron = heat.engine.clients.os.neutron:NeutronClientPlugin
swift = heat.engine.clients.os.swift:SwiftClientPlugin