Merge "Add a method for getting an endpoint"

This commit is contained in:
Jenkins 2015-05-02 13:14:02 +00:00 committed by Gerrit Code Review
commit 77a5f1f756
2 changed files with 51 additions and 21 deletions

View File

@ -375,8 +375,7 @@ class OpenStackCloud(object):
try: try:
self._keystone_client = keystone_client.Client( self._keystone_client = keystone_client.Client(
session=self.keystone_session, session=self.keystone_session,
auth_url=self.keystone_session.get_endpoint( auth_url=self.get_endpoint('identity'),
interface=ksc_auth.AUTH_INTERFACE),
timeout=self.api_timeout) timeout=self.api_timeout)
except Exception as e: except Exception as e:
self.log.debug( self.log.debug(
@ -549,7 +548,7 @@ class OpenStackCloud(object):
return self.api_versions['image'] return self.api_versions['image']
# Yay. We get to guess ... # Yay. We get to guess ...
# Get rid of trailing '/' if present # Get rid of trailing '/' if present
endpoint = self._get_glance_endpoint() endpoint = self.get_endpoint('image')
if endpoint.endswith('/'): if endpoint.endswith('/'):
endpoint = endpoint[:-1] endpoint = endpoint[:-1]
url_bits = endpoint.split('/') url_bits = endpoint.split('/')
@ -557,17 +556,11 @@ class OpenStackCloud(object):
return url_bits[-1][1] return url_bits[-1][1]
return '1' # Who knows? Let's just try 1 ... return '1' # Who knows? Let's just try 1 ...
def _get_glance_endpoint(self):
if self._glance_endpoint is None:
self._glance_endpoint = self.get_endpoint(
service_type=self.get_service_type('image'))
return self._glance_endpoint
@property @property
def glance_client(self): def glance_client(self):
if self._glance_client is None: if self._glance_client is None:
token = self.auth_token token = self.auth_token
endpoint = self._get_glance_endpoint() endpoint = self.get_endpoint('image')
glance_api_version = self._get_glance_api_version() glance_api_version = self._get_glance_api_version()
kwargs = dict() kwargs = dict()
if self.api_timeout is not None: if self.api_timeout is not None:
@ -591,7 +584,7 @@ class OpenStackCloud(object):
if self._swift_client is None: if self._swift_client is None:
token = self.auth_token token = self.auth_token
endpoint = self.get_endpoint( endpoint = self.get_endpoint(
service_type=self.get_service_type('object-store')) service_key='object-store')
self._swift_client = swift_client.Connection( self._swift_client = swift_client.Connection(
preauthurl=endpoint, preauthurl=endpoint,
preauthtoken=token, preauthtoken=token,
@ -633,7 +626,7 @@ class OpenStackCloud(object):
def trove_client(self): def trove_client(self):
if self._trove_client is None: if self._trove_client is None:
endpoint = self.get_endpoint( endpoint = self.get_endpoint(
service_type=self.get_service_type('database')) service_key='database')
trove_api_version = self._get_trove_api_version(endpoint) trove_api_version = self._get_trove_api_version(endpoint)
# Make the connection - can't use keystone session until there # Make the connection - can't use keystone session until there
# is one # is one
@ -694,18 +687,24 @@ class OpenStackCloud(object):
"Could not find a flavor with {ram} and '{include}'".format( "Could not find a flavor with {ram} and '{include}'".format(
ram=ram, include=include)) ram=ram, include=include))
def get_endpoint(self, service_type): def get_endpoint(self, service_key):
if service_type in self.endpoints: if service_key in self.endpoints:
return self.endpoints[service_type] return self.endpoints[service_key]
try: try:
endpoint = self.keystone_session.get_endpoint( # keystone is a special case in keystone, because what?
service_type=service_type, if service_key == 'identity':
interface=self.endpoint_type, endpoint = self.keystone_session.get_endpoint(
region_name=self.region_name) interface=ksc_auth.AUTH_INTERFACE)
else:
endpoint = self.keystone_session.get_endpoint(
service_type=self.get_service_type(service_key),
service_name=self.get_service_name(service_key),
interface=self.endpoint_type,
region_name=self.region_name)
except Exception as e: except Exception as e:
self.log.debug("keystone cannot get endpoint", exc_info=True) self.log.debug("keystone cannot get endpoint", exc_info=True)
raise OpenStackCloudException( raise OpenStackCloudException(
"Error getting %s endpoint: %s" % (service_type, str(e))) "Error getting %s endpoint: %s" % (service_key, str(e)))
return endpoint return endpoint
def list_servers(self): def list_servers(self):
@ -2135,7 +2134,7 @@ class OperatorCloud(OpenStackCloud):
# dict as the endpoint. # dict as the endpoint.
endpoint = self.auth['endpoint'] endpoint = self.auth['endpoint']
else: else:
endpoint = self.get_endpoint(service_type='baremetal') endpoint = self.get_endpoint(service_key='baremetal')
try: try:
self._ironic_client = ironic_client.Client( self._ironic_client = ironic_client.Client(
'1', endpoint, token=token, '1', endpoint, token=token,

View File

@ -12,7 +12,10 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from keystoneclient import auth as ksc_auth
import mock import mock
import testtools
import shade import shade
from shade import exc from shade import exc
@ -674,3 +677,31 @@ class TestShadeOperator(base.TestCase):
glance_mock.images.list.return_value = [fake_image] glance_mock.images.list.return_value = [fake_image]
self.assertEqual('22', self.cloud.get_image_id('22')) self.assertEqual('22', self.cloud.get_image_id('22'))
self.assertEqual('22', self.cloud.get_image_id('22 name')) self.assertEqual('22', self.cloud.get_image_id('22 name'))
def test_get_endpoint_provided(self):
self.cloud.endpoints['image'] = 'http://fake.url'
self.assertEqual('http://fake.url', self.cloud.get_endpoint('image'))
@mock.patch.object(shade.OpenStackCloud, 'keystone_session')
def test_get_endpoint_session(self, session_mock):
session_mock.get_endpoint.return_value = 'http://fake.url'
self.assertEqual('http://fake.url', self.cloud.get_endpoint('image'))
@mock.patch.object(shade.OpenStackCloud, 'keystone_session')
def test_get_endpoint_exception(self, session_mock):
class FakeException(Exception):
pass
def side_effect(*args, **kwargs):
raise FakeException("No service")
session_mock.get_endpoint.side_effect = side_effect
with testtools.ExpectedException(
exc.OpenStackCloudException,
"Error getting image endpoint: No service"):
self.cloud.get_endpoint("image")
@mock.patch.object(shade.OpenStackCloud, 'keystone_session')
def test_get_endpoint_identity(self, session_mock):
self.cloud.get_endpoint('identity')
session_mock.get_endpoint.assert_called_with(
interface=ksc_auth.AUTH_INTERFACE)