From 9e5874562791098f71e3248409604e9ebda625be Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Wed, 4 Jun 2014 14:24:30 +1200 Subject: [PATCH] Implement glance client plugin This moves the client creation code out of Clients._glance() into its own client plugin. Glance is a core project, and python-glanceclient is a dependency, so the import is now mandatory. Change-Id: If61f2510344bf67b7e9aded1dc9d9cedf2426228 --- .../tests/test_rackspace_cloud_server.py | 6 +-- heat/engine/clients/__init__.py | 21 ---------- heat/engine/clients/os/glance.py | 39 +++++++++++++++++++ heat/engine/resources/glance_image.py | 2 +- heat/tests/test_autoscaling_update_policy.py | 6 +-- heat/tests/test_engine_service.py | 6 +-- heat/tests/test_glance_image.py | 4 +- heat/tests/test_instance.py | 10 ++--- heat/tests/test_instance_network.py | 6 +-- heat/tests/test_loadbalancer.py | 6 +-- heat/tests/test_nokey.py | 6 +-- heat/tests/test_server.py | 10 ++--- heat/tests/test_server_tags.py | 6 +-- heat/tests/test_sqlalchemy_api.py | 6 +-- heat/tests/test_validate.py | 18 ++++----- heat/tests/test_volume.py | 5 ++- setup.cfg | 1 + 17 files changed, 89 insertions(+), 69 deletions(-) create mode 100644 heat/engine/clients/os/glance.py diff --git a/contrib/rackspace/rackspace/tests/test_rackspace_cloud_server.py b/contrib/rackspace/rackspace/tests/test_rackspace_cloud_server.py index e1a631708..3784edc18 100644 --- a/contrib/rackspace/rackspace/tests/test_rackspace_cloud_server.py +++ b/contrib/rackspace/rackspace/tests/test_rackspace_cloud_server.py @@ -16,7 +16,7 @@ import mox from heat.common import exception from heat.common import template_format -from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine import environment from heat.engine import parser @@ -69,8 +69,8 @@ class CloudServersTest(HeatTestCase): def _mock_get_image_id_success(self, imageId_input, imageId): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, imageId_input).MultipleTimes().\ diff --git a/heat/engine/clients/__init__.py b/heat/engine/clients/__init__.py index b922a0a19..5f0839d41 100644 --- a/heat/engine/clients/__init__.py +++ b/heat/engine/clients/__init__.py @@ -13,7 +13,6 @@ from ceilometerclient import client as ceilometerclient from cinderclient import client as cinderclient -from glanceclient import client as glanceclient from heatclient import client as heatclient from neutronclient.v2_0 import client as neutronclient from oslo.config import cfg @@ -108,26 +107,6 @@ class OpenStackClients(object): 'Replace with calls to client("glance")') return self.client('glance') - def _glance(self): - - con = self.context - endpoint_type = self._get_client_option('glance', 'endpoint_type') - endpoint = self.url_for(service_type='image', - endpoint_type=endpoint_type) - args = { - 'auth_url': con.auth_url, - 'service_type': 'image', - 'project_id': con.tenant, - 'token': self.auth_token, - 'endpoint_type': endpoint_type, - 'ca_file': self._get_client_option('glance', 'ca_file'), - 'cert_file': self._get_client_option('glance', 'cert_file'), - 'key_file': self._get_client_option('glance', 'key_file'), - 'insecure': self._get_client_option('glance', 'insecure') - } - - return glanceclient.Client('1', endpoint, **args) - def neutron(self): warnings.warn('neutron() is deprecated. ' 'Replace with calls to client("neutron")') diff --git a/heat/engine/clients/os/glance.py b/heat/engine/clients/os/glance.py new file mode 100644 index 000000000..c55bacf71 --- /dev/null +++ b/heat/engine/clients/os/glance.py @@ -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. + +from glanceclient import client as gc + +from heat.engine.clients import client_plugin + + +class GlanceClientPlugin(client_plugin.ClientPlugin): + + def _create(self): + + con = self.context + endpoint_type = self._get_client_option('glance', 'endpoint_type') + endpoint = self.url_for(service_type='image', + endpoint_type=endpoint_type) + args = { + 'auth_url': con.auth_url, + 'service_type': 'image', + 'project_id': con.tenant, + 'token': self.auth_token, + 'endpoint_type': endpoint_type, + 'ca_file': self._get_client_option('glance', 'ca_file'), + 'cert_file': self._get_client_option('glance', 'cert_file'), + 'key_file': self._get_client_option('glance', 'key_file'), + 'insecure': self._get_client_option('glance', 'insecure') + } + + return gc.Client('1', endpoint, **args) diff --git a/heat/engine/resources/glance_image.py b/heat/engine/resources/glance_image.py index 859b486e2..458b5c385 100644 --- a/heat/engine/resources/glance_image.py +++ b/heat/engine/resources/glance_image.py @@ -11,7 +11,7 @@ # License for the specific language governing permissions and limitations # under the License. -from glanceclient.common import exceptions as glance_exceptions +from glanceclient import exc as glance_exceptions from heat.engine import constraints from heat.engine import properties diff --git a/heat/tests/test_autoscaling_update_policy.py b/heat/tests/test_autoscaling_update_policy.py index 7a6ff4b0f..ec296b77a 100644 --- a/heat/tests/test_autoscaling_update_policy.py +++ b/heat/tests/test_autoscaling_update_policy.py @@ -20,7 +20,7 @@ from testtools.matchers import MatchesRegex from heat.common import exception from heat.common import template_format -from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine import function from heat.engine.notification import stack as notification @@ -214,8 +214,8 @@ class AutoScalingGroupTest(HeatTestCase): def _mock_get_image_id_success(self, imageId_input, imageId, update_image=None): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') diff --git a/heat/tests/test_engine_service.py b/heat/tests/test_engine_service.py index 77beb200f..963ae5b8b 100644 --- a/heat/tests/test_engine_service.py +++ b/heat/tests/test_engine_service.py @@ -31,7 +31,7 @@ from heat.common import identifier from heat.common import template_format from heat.common import urlfetch from heat.db import api as db_api -from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine import dependencies from heat.engine import environment @@ -204,8 +204,8 @@ def setup_keystone_mocks(mocks, stack): def setup_mock_for_image_constraint(mocks, imageId_input, imageId_output=744): g_cli_mock = mocks.CreateMockAnything() - mocks.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + mocks.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) mocks.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, imageId_input).\ diff --git a/heat/tests/test_glance_image.py b/heat/tests/test_glance_image.py index f066b34bd..bbca28076 100644 --- a/heat/tests/test_glance_image.py +++ b/heat/tests/test_glance_image.py @@ -11,7 +11,7 @@ # License for the specific language governing permissions and limitations # under the License. -from glanceclient.common.exceptions import HTTPNotFound +from glanceclient import exc as glance_exceptions import mock import six @@ -190,5 +190,5 @@ class GlanceImageTest(HeatTestCase): self.my_image.resource_id = image_id self.images.delete.return_value = None self.assertIsNone(self.my_image.handle_delete()) - self.images.delete.side_effect = HTTPNotFound(404) + self.images.delete.side_effect = glance_exceptions.HTTPNotFound(404) self.assertIsNone(self.my_image.handle_delete()) diff --git a/heat/tests/test_instance.py b/heat/tests/test_instance.py index 5ca58392f..7f96ebdf8 100644 --- a/heat/tests/test_instance.py +++ b/heat/tests/test_instance.py @@ -20,7 +20,7 @@ from neutronclient.v2_0 import client as neutronclient from heat.common import exception from heat.common import template_format -from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine import environment from heat.engine import parser @@ -81,8 +81,8 @@ class InstancesTest(HeatTestCase): def _mock_get_image_id_success(self, imageId_input, imageId): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, imageId_input).MultipleTimes().\ @@ -90,8 +90,8 @@ class InstancesTest(HeatTestCase): def _mock_get_image_id_fail(self, image_id, exp): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, image_id).AndRaise(exp) diff --git a/heat/tests/test_instance_network.py b/heat/tests/test_instance_network.py index e53e0c3ad..5bf264bc7 100644 --- a/heat/tests/test_instance_network.py +++ b/heat/tests/test_instance_network.py @@ -14,7 +14,7 @@ import uuid from heat.common import template_format -from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine import environment from heat.engine import parser @@ -153,8 +153,8 @@ class instancesTest(HeatTestCase): def _mock_get_image_id_success(self, imageId_input, imageId): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, imageId_input).MultipleTimes().\ diff --git a/heat/tests/test_loadbalancer.py b/heat/tests/test_loadbalancer.py index fb6d1c97b..13b1597cb 100644 --- a/heat/tests/test_loadbalancer.py +++ b/heat/tests/test_loadbalancer.py @@ -20,7 +20,7 @@ from oslo.config import cfg from heat.common import exception from heat.common import template_format -from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine import resource from heat.engine.resources import glance_utils @@ -125,8 +125,8 @@ class LoadBalancerTest(HeatTestCase): def _mock_get_image_id_success(self, imageId_input, imageId): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, imageId_input).\ diff --git a/heat/tests/test_nokey.py b/heat/tests/test_nokey.py index 558b70043..a5a6691b3 100644 --- a/heat/tests/test_nokey.py +++ b/heat/tests/test_nokey.py @@ -12,7 +12,7 @@ # under the License. from heat.common import template_format -from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine.resources import glance_utils from heat.engine.resources import instance as instances @@ -63,8 +63,8 @@ class nokeyTest(HeatTestCase): self.m.StubOutWithMock(nova.NovaClientPlugin, '_create') nova.NovaClientPlugin._create().AndReturn(self.fc) g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, 'CentOS 5.2').MultipleTimes().\ diff --git a/heat/tests/test_server.py b/heat/tests/test_server.py index ed913863e..f2d3e7a98 100644 --- a/heat/tests/test_server.py +++ b/heat/tests/test_server.py @@ -21,7 +21,7 @@ from novaclient import exceptions as nova_exceptions from heat.common import exception from heat.common import template_format -from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine import environment from heat.engine import parser @@ -164,8 +164,8 @@ class ServersTest(HeatTestCase): def _mock_get_image_id_success(self, imageId_input, imageId, server_rebuild=False): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, imageId_input).MultipleTimes().\ @@ -177,8 +177,8 @@ class ServersTest(HeatTestCase): def _mock_get_image_id_fail(self, image_id, exp): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, image_id).AndRaise(exp) diff --git a/heat/tests/test_server_tags.py b/heat/tests/test_server_tags.py index 82abdebf0..1312bed0f 100644 --- a/heat/tests/test_server_tags.py +++ b/heat/tests/test_server_tags.py @@ -17,7 +17,7 @@ import uuid import mox from heat.common import template_format -from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine import environment from heat.engine import parser @@ -133,8 +133,8 @@ class ServerTagsTest(HeatTestCase): def _mock_get_image_id_success(self, imageId_input, imageId): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, imageId_input).MultipleTimes().\ diff --git a/heat/tests/test_sqlalchemy_api.py b/heat/tests/test_sqlalchemy_api.py index 8c75f744c..521f1d5c7 100644 --- a/heat/tests/test_sqlalchemy_api.py +++ b/heat/tests/test_sqlalchemy_api.py @@ -25,7 +25,7 @@ from heat.common import context from heat.common import exception from heat.common import template_format from heat.db.sqlalchemy import api as db_api -from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine import environment from heat.engine import parser @@ -97,8 +97,8 @@ class SqlAlchemyTest(HeatTestCase): def _mock_get_image_id_success(self, imageId_input, imageId): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, imageId_input).MultipleTimes().\ diff --git a/heat/tests/test_validate.py b/heat/tests/test_validate.py index fb6292408..d2f7a7933 100644 --- a/heat/tests/test_validate.py +++ b/heat/tests/test_validate.py @@ -11,13 +11,13 @@ # License for the specific language governing permissions and limitations # under the License. -from glanceclient import exc as g_exc +from glanceclient import exc as glance_exceptions import mock from testtools import skipIf from heat.common import exception from heat.common import template_format -from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine import environment from heat.engine.hot.template import HOTemplate @@ -820,8 +820,8 @@ class validateTest(HeatTestCase): def _mock_get_image_id_success(self, imageId_input, imageId): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, imageId_input).MultipleTimes().\ @@ -829,8 +829,8 @@ class validateTest(HeatTestCase): def _mock_get_image_id_fail(self, image_id, exp): g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, image_id).AndRaise(exp) @@ -1301,9 +1301,9 @@ class validateTest(HeatTestCase): self.m.StubOutWithMock(self.gc.images, 'list') self.gc.images.list().AndRaise( - g_exc.ClientException(500)) - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn(self.gc) + glance_exceptions.ClientException(500)) + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn(self.gc) self.m.ReplayAll() self.assertRaises(exception.StackValidationFailed, stack.validate) diff --git a/heat/tests/test_volume.py b/heat/tests/test_volume.py index 31b544ca0..0442dcd59 100644 --- a/heat/tests/test_volume.py +++ b/heat/tests/test_volume.py @@ -22,6 +22,7 @@ from testtools import skipIf from heat.common import exception from heat.common import template_format from heat.engine import clients +from heat.engine.clients.os import glance from heat.engine.clients.os import nova from heat.engine.resources import glance_utils from heat.engine.resources import image @@ -893,8 +894,8 @@ class VolumeTest(HeatTestCase): clients.OpenStackClients._cinder().MultipleTimes().AndReturn( self.cinder_fc) g_cli_mock = self.m.CreateMockAnything() - self.m.StubOutWithMock(clients.OpenStackClients, '_glance') - clients.OpenStackClients._glance().MultipleTimes().AndReturn( + self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create') + glance.GlanceClientPlugin._create().MultipleTimes().AndReturn( g_cli_mock) self.m.StubOutWithMock(glance_utils, 'get_image_id') glance_utils.get_image_id(g_cli_mock, image_id).MultipleTimes().\ diff --git a/setup.cfg b/setup.cfg index 9c4cbccb7..e7b832c19 100644 --- a/setup.cfg +++ b/setup.cfg @@ -38,6 +38,7 @@ oslo.config.opts = heat.common.wsgi = heat.common.wsgi:list_opts heat.clients = + glance = heat.engine.clients.os.glance:GlanceClientPlugin nova = heat.engine.clients.os.nova:NovaClientPlugin swift = heat.engine.clients.os.swift:SwiftClientPlugin