Merge "Adds tag support for glance image resource plugin"
This commit is contained in:
commit
69251cdcbd
heat
engine
tests
@ -28,8 +28,11 @@ class GlanceClientPlugin(client_plugin.ClientPlugin):
|
||||
|
||||
service_types = [IMAGE] = ['image']
|
||||
|
||||
def _create(self):
|
||||
supported_versions = [V1, V2] = ['1', '2']
|
||||
|
||||
default_version = V1
|
||||
|
||||
def _create(self, version=None):
|
||||
con = self.context
|
||||
endpoint_type = self._get_client_option(CLIENT_NAME, 'endpoint_type')
|
||||
endpoint = self.url_for(service_type=self.IMAGE,
|
||||
@ -46,7 +49,7 @@ class GlanceClientPlugin(client_plugin.ClientPlugin):
|
||||
'insecure': self._get_client_option(CLIENT_NAME, 'insecure')
|
||||
}
|
||||
|
||||
return gc.Client('1', endpoint, **args)
|
||||
return gc.Client(version, endpoint, **args)
|
||||
|
||||
def _find_with_attr(self, entity, **kwargs):
|
||||
"""Find a item for entity with attributes matching ``**kwargs``."""
|
||||
|
@ -30,10 +30,10 @@ class GlanceImage(resource.Resource):
|
||||
|
||||
PROPERTIES = (
|
||||
NAME, IMAGE_ID, IS_PUBLIC, MIN_DISK, MIN_RAM, PROTECTED,
|
||||
DISK_FORMAT, CONTAINER_FORMAT, LOCATION
|
||||
DISK_FORMAT, CONTAINER_FORMAT, LOCATION, TAGS
|
||||
) = (
|
||||
'name', 'id', 'is_public', 'min_disk', 'min_ram', 'protected',
|
||||
'disk_format', 'container_format', 'location'
|
||||
'disk_format', 'container_format', 'location', 'tags'
|
||||
)
|
||||
|
||||
properties_schema = {
|
||||
@ -103,6 +103,12 @@ class GlanceImage(resource.Resource):
|
||||
'specify "swift://example.com/container/obj".'),
|
||||
required=True,
|
||||
),
|
||||
TAGS: properties.Schema(
|
||||
properties.Schema.LIST,
|
||||
_('List of image tags.'),
|
||||
update_allowed=True,
|
||||
support_status=support.SupportStatus(version='7.0.0')
|
||||
)
|
||||
}
|
||||
|
||||
default_client_name = 'glance'
|
||||
@ -114,12 +120,38 @@ class GlanceImage(resource.Resource):
|
||||
if v is not None)
|
||||
image_id = self.client().images.create(**args).id
|
||||
self.resource_id_set(image_id)
|
||||
|
||||
if self.TAGS in args:
|
||||
for tag in args[self.TAGS]:
|
||||
self.client(
|
||||
version=self.client_plugin().V2).image_tags.update(
|
||||
image_id,
|
||||
tag)
|
||||
|
||||
return image_id
|
||||
|
||||
def check_create_complete(self, image_id):
|
||||
image = self.client().images.get(image_id)
|
||||
return image.status == 'active'
|
||||
|
||||
def handle_update(self, json_snippet=None, tmpl_diff=None, prop_diff=None):
|
||||
if prop_diff and self.TAGS in prop_diff:
|
||||
existing_tags = self.properties.get(self.TAGS, [])
|
||||
|
||||
new_tags = set(prop_diff[self.TAGS]) - set(existing_tags)
|
||||
for tag in new_tags:
|
||||
self.client(
|
||||
version=self.client_plugin().V2).image_tags.update(
|
||||
self.resource_id,
|
||||
tag)
|
||||
|
||||
removed_tags = set(existing_tags) - set(prop_diff[self.TAGS])
|
||||
for tag in removed_tags:
|
||||
self.client(
|
||||
version=self.client_plugin().V2).image_tags.delete(
|
||||
self.resource_id,
|
||||
tag)
|
||||
|
||||
def _show_resource(self):
|
||||
if self.glance().version == 1.0:
|
||||
return super(GlanceImage, self)._show_resource()
|
||||
|
@ -72,6 +72,7 @@ class GlanceImageTest(common.HeatTestCase):
|
||||
self.my_image.client = glance
|
||||
glance.return_value = self.glanceclient
|
||||
self.images = self.glanceclient.images
|
||||
self.image_tags = self.glanceclient.image_tags
|
||||
|
||||
def _test_validate(self, resource, error_msg):
|
||||
exc = self.assertRaises(exception.StackValidationFailed,
|
||||
@ -214,8 +215,33 @@ class GlanceImageTest(common.HeatTestCase):
|
||||
image_id = '41f0e60c-ebb4-4375-a2b4-845ae8b9c995'
|
||||
value.id = image_id
|
||||
self.images.create.return_value = value
|
||||
self.image_tags.update.return_value = None
|
||||
self.my_image.t['Properties']['tags'] = ['tag1']
|
||||
self.my_image.handle_create()
|
||||
|
||||
self.assertEqual(image_id, self.my_image.resource_id)
|
||||
self.image_tags.update.assert_called_once_with(
|
||||
self.my_image.resource_id,
|
||||
'tag1')
|
||||
|
||||
def test_image_handle_update(self):
|
||||
self.my_image.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
|
||||
|
||||
self.my_image.t['Properties']['tags'] = ['tag1']
|
||||
prop_diff = {'tags': ['tag2']}
|
||||
|
||||
self.my_image.handle_update(json_snippet=None,
|
||||
tmpl_diff=None,
|
||||
prop_diff=prop_diff)
|
||||
|
||||
self.image_tags.update.assert_called_once_with(
|
||||
self.my_image.resource_id,
|
||||
'tag2'
|
||||
)
|
||||
self.image_tags.delete.assert_called_once_with(
|
||||
self.my_image.resource_id,
|
||||
'tag1'
|
||||
)
|
||||
|
||||
def test_image_show_resource_v1(self):
|
||||
self.glanceclient.version = 1.0
|
||||
@ -257,7 +283,8 @@ class GlanceImageTest(common.HeatTestCase):
|
||||
'is_public': False,
|
||||
'min_disk': 0,
|
||||
'min_ram': 0,
|
||||
'id': '41f0e60c-ebb4-4375-a2b4-845ae8b9c995'
|
||||
'id': '41f0e60c-ebb4-4375-a2b4-845ae8b9c995',
|
||||
'tags': []
|
||||
}
|
||||
if version == 1.0:
|
||||
image = mock.MagicMock()
|
||||
@ -279,7 +306,8 @@ class GlanceImageTest(common.HeatTestCase):
|
||||
'is_public': False,
|
||||
'min_disk': 0,
|
||||
'min_ram': 0,
|
||||
'id': '41f0e60c-ebb4-4375-a2b4-845ae8b9c995'
|
||||
'id': '41f0e60c-ebb4-4375-a2b4-845ae8b9c995',
|
||||
'tags': []
|
||||
}
|
||||
if version == 1.0:
|
||||
expected.update({'location': 'stub'})
|
||||
|
@ -1427,8 +1427,8 @@ class ValidateTest(common.HeatTestCase):
|
||||
stack = parser.Stack(self.ctx, 'test_stack', template)
|
||||
self.m.StubOutWithMock(self.gc.images, 'get')
|
||||
self.gc.images.get('image_name').AndRaise(glance.exc.HTTPNotFound())
|
||||
self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create')
|
||||
glance.GlanceClientPlugin._create().AndReturn(self.gc)
|
||||
self.m.StubOutWithMock(glance.GlanceClientPlugin, 'client')
|
||||
glance.GlanceClientPlugin.client().AndReturn(self.gc)
|
||||
self.stub_FlavorConstraint_validate()
|
||||
self.m.ReplayAll()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user