Add OS::Glance::Image resource

Add OS::Glance::Image resource.
Implements: blueprint glance-image

Change-Id: If0b5f322da758973d6b47f9ca6888fef40f92f45
This commit is contained in:
huangtianhua 2014-04-26 18:29:49 +08:00
parent 053d77bb09
commit 6b35fb51ce
2 changed files with 321 additions and 0 deletions

View File

@ -0,0 +1,127 @@
#
# 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.common import exceptions as glance_exceptions
from heat.engine import constraints
from heat.engine import properties
from heat.engine import resource
from heat.openstack.common.gettextutils import _
class GlanceImage(resource.Resource):
'''
A resource managing for image in Glance.
'''
PROPERTIES = (
NAME, IMAGE_ID, IS_PUBLIC, MIN_DISK, MIN_RAM, PROTECTED,
DISK_FORMAT, CONTAINER_FORMAT, LOCATION
) = (
'name', 'id', 'is_public', 'min_disk', 'min_ram', 'protected',
'disk_format', 'container_format', 'location'
)
properties_schema = {
NAME: properties.Schema(
properties.Schema.STRING,
_('Name for the image. The name of an image is not '
'unique to a Image Service node.')
),
IMAGE_ID: properties.Schema(
properties.Schema.STRING,
_('The image ID. Glance will generate a UUID if not specified.')
),
IS_PUBLIC: properties.Schema(
properties.Schema.BOOLEAN,
_('Scope of image accessibility. Public or private. '
'Default value is False means private.'),
default=False,
),
MIN_DISK: properties.Schema(
properties.Schema.INTEGER,
_('Amount of disk space (in GB) required to boot image. '
'Default value is 0 if not specified '
'and means no limit on the disk size.'),
constraints=[
constraints.Range(min=0),
]
),
MIN_RAM: properties.Schema(
properties.Schema.INTEGER,
_('Amount of ram (in MB) required to boot image. Default value '
'is 0 if not specified and means no limit on the ram size.'),
constraints=[
constraints.Range(min=0),
]
),
PROTECTED: properties.Schema(
properties.Schema.BOOLEAN,
_('Whether the image can be deleted. If the value is True, '
'the image is protected and cannot be deleted.')
),
DISK_FORMAT: properties.Schema(
properties.Schema.STRING,
_('Disk format of image.'),
required=True,
constraints=[
constraints.AllowedValues(['ami', 'ari', 'aki',
'vhd', 'vmdk', 'raw',
'qcow2', 'vdi', 'iso'])
]
),
CONTAINER_FORMAT: properties.Schema(
properties.Schema.STRING,
_('Container format of image.'),
required=True,
constraints=[
constraints.AllowedValues(['ami', 'ari', 'aki',
'bare', 'ova', 'ovf'])
]
),
LOCATION: properties.Schema(
properties.Schema.STRING,
_('URL where the data for this image already resides. For '
'example, if the image data is stored in swift, you could '
'specify "swift://example.com/container/obj".'),
required=True,
),
}
def handle_create(self):
args = dict((k, v) for k, v in self.properties.items()
if v is not None)
image_id = self.glance().images.create(**args).id
self.resource_id_set(image_id)
return image_id
def check_create_complete(self, image_id):
image = self.glance().images.get(image_id)
return image.status == 'active'
def handle_delete(self):
if self.resource_id is None:
return
try:
self.glance().images.delete(self.resource_id)
except glance_exceptions.NotFound:
pass
self.resource_id_set(None)
def resource_mapping():
return {
'OS::Glance::Image': GlanceImage
}

View File

@ -0,0 +1,194 @@
#
# 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.common.exceptions import HTTPNotFound
import mock
import six
from heat.common import exception
from heat.common import template_format
from heat.engine import parser
from heat.engine import resource
from heat.engine.resources import glance_image as gi
from heat.tests.common import HeatTestCase
from heat.tests import utils
image_template = '''
heat_template_version: 2013-05-23
description: This template to define a glance image.
resources:
my_image:
type: OS::Glance::Image
properties:
name: cirros_image
id: 41f0e60c-ebb4-4375-a2b4-845ae8b9c995
disk_format: qcow2
container_format: bare
is_public: True
min_disk: 10
min_ram: 512
protected: False
location: https://launchpad.net/cirros/cirros-0.3.0-x86_64-disk.img
'''
image_template_validate = '''
heat_template_version: 2013-05-23
description: This template to define a glance image.
resources:
image:
type: OS::Glance::Image
properties:
name: image_validate
disk_format: qcow2
container_format: bare
location: https://launchpad.net/cirros/cirros-0.3.0-x86_64-disk.img
'''
class GlanceImageTest(HeatTestCase):
def setUp(self):
super(GlanceImageTest, self).setUp()
utils.setup_dummy_db()
self.ctx = utils.dummy_context()
# For unit testing purpose. Register resource provider
# explicitly.
resource._register_class("OS::Glance::Image", gi.GlanceImage)
tpl = template_format.parse(image_template)
self.stack = parser.Stack(
self.ctx, 'glance_image_test_stack',
parser.Template(tpl)
)
self.my_image = self.stack['my_image']
glance = mock.MagicMock()
self.glanceclient = mock.MagicMock()
self.my_image.glance = glance
glance.return_value = self.glanceclient
self.images = self.glanceclient.images
def _test_validate(self, resource, error_msg):
exc = self.assertRaises(exception.StackValidationFailed,
resource.validate)
self.assertIn(error_msg, six.text_type(exc))
def test_resource_mapping(self):
mapping = gi.resource_mapping()
self.assertEqual(1, len(mapping))
self.assertEqual(gi.GlanceImage, mapping['OS::Glance::Image'])
self.assertIsInstance(self.my_image, gi.GlanceImage)
def test_invalid_min_disk(self):
# invalid 'min_disk'
tpl = template_format.parse(image_template_validate)
stack = parser.Stack(
self.ctx, 'glance_image_stack_validate',
parser.Template(tpl)
)
image = stack['image']
image.t['Properties']['min_disk'] = -1
error_msg = 'min_disk -1 is out of range (min: 0, max: None)'
self._test_validate(image, error_msg)
def test_invalid_min_ram(self):
# invalid 'min_ram'
tpl = template_format.parse(image_template_validate)
stack = parser.Stack(
self.ctx, 'glance_image_stack_validate',
parser.Template(tpl)
)
image = stack['image']
image.t['Properties']['min_ram'] = -1
error_msg = 'min_ram -1 is out of range (min: 0, max: None)'
self._test_validate(image, error_msg)
def test_miss_disk_format(self):
# miss disk_format
tpl = template_format.parse(image_template_validate)
stack = parser.Stack(
self.ctx, 'glance_image_stack_validate',
parser.Template(tpl)
)
image = stack['image']
image.t['Properties'].pop('disk_format')
error_msg = 'Property disk_format not assigned'
self._test_validate(image, error_msg)
def test_invalid_disk_format(self):
# invalid disk_format
tpl = template_format.parse(image_template_validate)
stack = parser.Stack(
self.ctx, 'glance_image_stack_validate',
parser.Template(tpl)
)
image = stack['image']
image.t['Properties']['disk_format'] = 'incorrect_format'
error_msg = ('disk_format "incorrect_format" is not an allowed value '
'[ami, ari, aki, vhd, vmdk, raw, qcow2, vdi, iso]')
self._test_validate(image, error_msg)
def test_miss_container_format(self):
# miss container_format
tpl = template_format.parse(image_template_validate)
stack = parser.Stack(
self.ctx, 'glance_image_stack_validate',
parser.Template(tpl)
)
image = stack['image']
image.t['Properties'].pop('container_format')
error_msg = 'Property container_format not assigned'
self._test_validate(image, error_msg)
def test_invalid_container_format(self):
# invalid container_format
tpl = template_format.parse(image_template_validate)
stack = parser.Stack(
self.ctx, 'glance_image_stack_validate',
parser.Template(tpl)
)
image = stack['image']
image.t['Properties']['container_format'] = 'incorrect_format'
error_msg = ('container_format "incorrect_format" is not an '
'allowed value [ami, ari, aki, bare, ova, ovf]')
self._test_validate(image, error_msg)
def test_miss_location(self):
# miss location
tpl = template_format.parse(image_template_validate)
stack = parser.Stack(
self.ctx, 'glance_image_stack_validate',
parser.Template(tpl)
)
image = stack['image']
image.t['Properties'].pop('location')
error_msg = 'Property location not assigned'
self._test_validate(image, error_msg)
def test_image_handle_create(self):
value = mock.MagicMock()
image_id = '41f0e60c-ebb4-4375-a2b4-845ae8b9c995'
value.id = image_id
self.images.create.return_value = value
self.my_image.handle_create()
self.assertEqual(image_id, self.my_image.resource_id)
def test_image_handle_delete(self):
self.resource_id = None
self.assertIsNone(self.my_image.handle_delete())
image_id = '41f0e60c-ebb4-4375-a2b4-845ae8b9c995'
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.assertIsNone(self.my_image.handle_delete())