Add a client wrapper class

This is a slimmed down version of heat's client wrapper.

Change-Id: Ibe22b4150c083533fb1d63efeefcee0b8026799a
This commit is contained in:
Angus Salkeld
2014-03-14 14:40:09 +10:00
parent 2fe2cb28c0
commit a4c9b7f07e
4 changed files with 167 additions and 0 deletions

View File

@@ -246,3 +246,30 @@
#pool_timeout=<None>
[heat_client]
#
# Options defined in solum.common.clients
#
# Type of endpoint in Identity service catalog to use for
# communication with the OpenStack service. (string value)
#endpoint_type=publicURL
# Optional CA cert file to use in SSL connections. (string
# value)
#ca_file=<None>
# Optional PEM-formatted certificate chain file. (string
# value)
#cert_file=<None>
# Optional PEM-formatted file that contains the private key.
# (string value)
#key_file=<None>
# If set, then the server's certificate will not be verified.
# (boolean value)
#insecure=false

View File

@@ -5,6 +5,7 @@ netaddr>=0.7.6
oslo.config>=1.2.0
pbr>=0.6,<1.0
pecan>=0.4.5
python-heatclient>=0.2.3
python-keystoneclient>=0.6.0
six>=1.5.2
SQLAlchemy>=0.7.8,<=0.8.99

100
solum/common/clients.py Normal file
View File

@@ -0,0 +1,100 @@
# Copyright 2014 - Rackspace Hosting.
#
# 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 heatclient
from keystoneclient.v2_0 import client as ksclient
from oslo.config import cfg
from solum.openstack.common.gettextutils import _
from solum.openstack.common import log as logging
LOG = logging.getLogger(__name__)
# Note: this config is duplicated in many projects that use OpenStack
# clients. This should really be in the client.
# There is a place holder bug here:
# https://bugs.launchpad.net/solum/+bug/1292334
# that we use to track this.
heat_client_opts = [
cfg.StrOpt('endpoint_type',
default='publicURL',
help=_(
'Type of endpoint in Identity service catalog to use '
'for communication with the OpenStack service.')),
cfg.StrOpt('ca_file',
help=_('Optional CA cert file to use in SSL connections.')),
cfg.StrOpt('cert_file',
help=_('Optional PEM-formatted certificate chain file.')),
cfg.StrOpt('key_file',
help=_('Optional PEM-formatted file that contains the '
'private key.')),
cfg.BoolOpt('insecure',
default=False,
help=_("If set, then the server's certificate will not "
"be verified."))]
cfg.CONF.register_opts(heat_client_opts, group='heat_client')
class OpenStackClients(object):
"""Convenience class to create and cache client instances."""
def __init__(self, context):
self.context = context
self._keystone = None
self._heat = None
@property
def auth_token(self):
return self.context.auth_token or self.keystone().auth_token
def keystone(self):
if self._keystone:
return self._keystone
args = {
'auth_url': self.context.auth_url,
'token': self.context.auth_token,
'username': None,
'password': None
}
self._keystone = ksclient.Client(**args)
return self._keystone
def _get_client_option(self, client, option):
return getattr(getattr(cfg.CONF, '%s_client' % client), option)
def heat(self):
if self._heat:
return self._heat
endpoint_type = self._get_client_option('heat', 'endpoint_type')
args = {
'auth_url': self.context.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.context.get_url_for(service_type='orchestration',
endpoint_type=endpoint_type)
self._heat = heatclient.Client('1', endpoint, **args)
return self._heat

View File

@@ -0,0 +1,39 @@
# 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 mock
from heatclient import client as heatclient
from solum.common import clients
from solum.tests import base
class ClientsTest(base.BaseTestCase):
@mock.patch.object(heatclient, 'Client')
def test_clients_heat(self, mock_call):
con = mock.MagicMock()
con.tenant = "b363706f891f48019483f8bd6503c54b"
con.auth_token = "3bcc3d3a03f44e3d8377f9247b0ad155"
auth_url = mock.PropertyMock(name="auth_url",
return_value="keystone_url")
type(con).auth_url = auth_url
con.get_url_for = mock.Mock(name="get_url_for")
con.get_url_for.return_value = "url_from_keystone"
obj = clients.OpenStackClients(con)
obj._heat = None
obj.heat()
mock_call.assert_called_once_with(
'1', 'url_from_keystone', username=None,
cert_file=None, token='3bcc3d3a03f44e3d8377f9247b0ad155',
auth_url='keystone_url', ca_file=None, key_file=None,
password=None, insecure=False)