Merge "Create Glance driver for Kingbird."

This commit is contained in:
Jenkins 2017-06-08 11:18:32 +00:00 committed by Gerrit Code Review
commit afe776b0f9
3 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,78 @@
# Copyright 2017 Ericsson AB.
# 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 oslo_log import log
from kingbird.common import exceptions
from kingbird.drivers.openstack.keystone_v3 import KeystoneClient
from glanceclient import Client
LOG = log.getLogger(__name__)
API_VERSION = '2'
class GlanceClient(object):
"""Glance V2 driver."""
def __init__(self, region, context):
"""Create Glance Object.
:param region: Region to which glance object
has to be created.
:param context: context object.
"""
try:
LOG.info('Creating Glance Object')
self.keystone_client = KeystoneClient()
service_id = [service.id for service in
self.keystone_client.services_list
if service.type == 'image'][0]
glance_endpoint = [endpoint.url for endpoint in
self.keystone_client.endpoints_list
if endpoint.service_id == service_id
and endpoint.region == region and
endpoint.interface == 'public'][0]
params = dict()
params['identity_headers'] = self.generate_identity_headers(
context)
self.glance_client = Client(str(API_VERSION), glance_endpoint,
**params)
except exceptions.ServiceUnavailable:
raise
def generate_identity_headers(self, context, status='Confirmed'):
"""Extract headers from context object to create glance client.
:param context: context object.
"""
return {
'X-Auth-Token': getattr(context, 'auth_token', None),
'X-User-Id': getattr(context, 'user', None),
'X-Tenant-Id': getattr(context, 'project', None),
'X-Roles': getattr(context, 'roles', []),
'X-Identity-Status': status,
}
def get_image(self, resource_identifier):
"""Get the image details for the specified resource_identifier.
:param resource_identifier: resource_id for which the details
have to be retrieved.
"""
return self.glance_client.images.get(resource_identifier)

View File

@ -33,6 +33,7 @@ class KeystoneClient(base.DriverBase):
self.session = self.endpoint_cache.admin_session
self.keystone_client = self.endpoint_cache.keystone_client
self.services_list = self.keystone_client.services.list()
self.endpoints_list = self.keystone_client.endpoints.list()
except exceptions.ServiceUnavailable:
raise

View File

@ -0,0 +1,67 @@
# 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 mock import patch
from kingbird.drivers.openstack.glance_v2 import GlanceClient
from kingbird.tests import base
from kingbird.tests import utils
class FakeService(object):
"""Fake service class used to test service enable testcase."""
def __init__(self, type_service, name, id):
self.type = type_service
self.name = name
self.id = id
class FakeEndpoint(object):
"""Fake Endpoints class used to test service enable testcase."""
def __init__(self, url, service_id, region, interface):
self.url = url
self.service_id = service_id
self.region = region
self.interface = interface
class TestGlanceClient(base.KingbirdTestCase):
def setUp(self):
super(TestGlanceClient, self).setUp()
self.ctx = utils.dummy_context()
@patch('kingbird.drivers.openstack.glance_v2.KeystoneClient')
@patch('kingbird.drivers.openstack.glance_v2.Client')
def test_init(self, mock_glance_client, mock_keystone_client):
"""Mock init method of glance."""
fake_service = FakeService('image', 'fake_type', 'fake_id')
fake_endpoint = FakeEndpoint('fake_url', fake_service.id,
'fake_region', 'public')
mock_keystone_client().services_list = [fake_service]
mock_keystone_client().endpoints_list = [fake_endpoint]
GlanceClient('fake_region', self.ctx)
self.assertEqual(1, mock_glance_client.call_count)
@patch('kingbird.drivers.openstack.glance_v2.KeystoneClient')
@patch('kingbird.drivers.openstack.glance_v2.Client')
def test_get_image(self, mock_glance_client, mock_keystone_client):
"""Mock get_image method of glance."""
fake_service = FakeService('image', 'fake_type', 'fake_id')
fake_endpoint = FakeEndpoint('fake_url', fake_service.id,
'fake_region', 'public')
mock_keystone_client().services_list = [fake_service]
mock_keystone_client().endpoints_list = [fake_endpoint]
GlanceClient('fake_region', self.ctx).get_image('fake_resource')
mock_glance_client().images.get.\
assert_called_once_with('fake_resource')