Add OS::Senlin::Profile resource

Add OS::Senlin::Profile resource to heat.

blueprint senlin-resources
Change-Id: I50b5854edd449eca144bd7f42d4d9a37c2241c2c
This commit is contained in:
Ethan Lynn 2015-11-19 18:40:52 +08:00
parent afb7b73f5d
commit dcac282df7
2 changed files with 236 additions and 0 deletions

View File

@ -0,0 +1,108 @@
#
# 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.
#
# Copyright 2015 IBM Corp.
from heat.common import exception
from heat.common.i18n import _
from heat.common import template_format
from heat.engine import properties
from heat.engine import resource
from heat.engine import support
class Profile(resource.Resource):
"""A resource that creates a Senlin Profile.
Profile resource in senlin is a template describing how to create nodes in
cluster.
"""
support_status = support.SupportStatus(version='6.0.0')
default_client_name = 'senlin'
PROPERTIES = (
NAME, SPEC, METADATA,
) = (
'name', 'spec', 'metadata',
)
properties_schema = {
NAME: properties.Schema(
properties.Schema.STRING,
_('Name of the senlin profile. By default, physical resource name '
'is used.'),
update_allowed=True,
),
SPEC: properties.Schema(
properties.Schema.STRING,
_('The spec template content for Senlin profile, should be '
'either in YAML or JSON format.'),
required=True
),
METADATA: properties.Schema(
properties.Schema.MAP,
_('Metadata key-values defined for profile.'),
update_allowed=True,
)
}
def __init__(self, name, definition, stack):
super(Profile, self).__init__(name, definition, stack)
self._spec = None
def _parse_spec(self, spec):
if self._spec is None:
self._spec = template_format.simple_parse(spec)
return self._spec
def handle_create(self):
params = {
'name': (self.properties[self.NAME] or
self.physical_resource_name()),
'spec': self._parse_spec(self.properties[self.SPEC]),
'metadata': self.properties[self.METADATA],
}
profile = self.client().create_profile(**params)
self.resource_id_set(profile.id)
def handle_delete(self):
if self.resource_id is not None:
with self.client_plugin().ignore_not_found:
self.client().delete_profile(self.resource_id)
def validate(self):
try:
self._parse_spec(self.properties[self.SPEC])
except ValueError as ex:
msg = _("Failed to parse %(spec)s: %(ex)s") % {
'spec': self.SPEC,
'ex': ex
}
raise exception.StackValidationFailed(message=msg)
def handle_update(self, json_snippet, tmpl_diff, prop_diff):
if prop_diff:
self.client().update_profile(self.resource_id, **prop_diff)
def _show_resource(self):
profile = self.client().get_profile(self.resource_id)
return profile.to_dict()
def resource_mapping():
return {
'OS::Senlin::Profile': Profile
}

View File

@ -0,0 +1,128 @@
# Copyright 2015 IBM Corp.
#
# 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.
import mock
import six
from heat.common import exception
from heat.common import template_format
from heat.engine.resources.openstack.senlin import profile as sp
from heat.engine import scheduler
from heat.tests import common
from heat.tests import utils
profile_stack_template = """
heat_template_version: 2016-04-08
description: Senlin Profile Template
parameters:
spec:
type: string
resources:
senlin-profile:
type: OS::Senlin::Profile
properties:
name: SenlinProfile
spec:
get_param: spec
"""
profile_spec = """
type: os.heat.stack
version: 1.0
properties:
template:
heat_template_version: 2014-10-16
resources:
random:
type: OS::Heat::RandomString
"""
class FakeProfile(object):
def __init__(self, id='some_id', spec=None):
self.id = id
self.name = "SenlinProfile"
self.metadata = {}
self.spec = spec or template_format.simple_parse(profile_spec)
class SenlinProfileTest(common.HeatTestCase):
def setUp(self):
super(SenlinProfileTest, self).setUp()
self.senlin_mock = mock.MagicMock()
self.patchobject(sp.Profile, 'client', return_value=self.senlin_mock)
self.fake_p = FakeProfile()
self.t = template_format.parse(profile_stack_template)
def _init_profile(self, template, params):
self.stack = utils.parse_stack(template, params)
profile = self.stack['senlin-profile']
return profile
def _create_profile(self, template):
params = {
'spec': profile_spec
}
profile = self._init_profile(template, params)
self.senlin_mock.create_profile.return_value = self.fake_p
scheduler.TaskRunner(profile.create)()
self.assertEqual((profile.CREATE, profile.COMPLETE),
profile.state)
self.assertEqual(self.fake_p.id, profile.resource_id)
return profile
def test_profile_create(self):
self._create_profile(self.t)
expect_kwargs = {
'name': 'SenlinProfile',
'metadata': None,
'spec': template_format.simple_parse(profile_spec)
}
self.senlin_mock.create_profile.assert_called_once_with(
**expect_kwargs)
def test_profile_delete(self):
self.senlin_mock.delete_profile.return_value = None
profile = self._create_profile(self.t)
scheduler.TaskRunner(profile.delete)()
self.senlin_mock.delete_profile.assert_called_once_with(
profile.resource_id)
def test_profile_update(self):
profile = self._create_profile(self.t)
prop_diff = {'metadata': {'foo': 'bar'}}
profile.handle_update(json_snippet=None,
tmpl_diff=None,
prop_diff=prop_diff)
self.senlin_mock.update_profile.assert_called_once_with(
profile.resource_id, **prop_diff)
def test_validate_fail(self):
params = {
'spec': 'foo'
}
stack = utils.parse_stack(self.t, params)
ex = self.assertRaises(exception.StackValidationFailed,
stack['senlin-profile'].validate)
expected = ('Failed to parse spec: The template is not a '
'JSON object or YAML mapping.')
self.assertEqual(expected, six.text_type(ex))
def test_resource_mapping(self):
mapping = sp.resource_mapping()
self.assertEqual(1, len(mapping))
self.assertEqual(sp.Profile,
mapping['OS::Senlin::Profile'])