A cloud-config resource based on SoftwareConfig

This simple resource allows cloud-config to be specified
as a map property, and stores the resulting YAML as a SoftwareConfig
resource.

Because cloud-init is boot only, this resource won't be used with
a SoftwareDeployment resource, also it will not have inputs or outputs.

Instead, a server's user_data can contain this resource via get_resource
when user_data_format is SOFTWARE_CONFIG.

Implements: blueprint cloud-init-resource

Change-Id: I15c8d6a03b8da274531b76cca658e37def4eb460
This commit is contained in:
Steve Baker 2013-12-20 09:57:02 +13:00 committed by JUN JIE NAN
parent bb0db1f722
commit 093fd9af7c
2 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,49 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# 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 heat.common.template_format import yaml
from heat.common.template_format import yaml_dumper
from heat.engine import properties
from heat.engine.resources.software_config import software_config
class CloudConfig(software_config.SoftwareConfig):
PROPERTIES = (
CLOUD_CONFIG
) = (
'cloud_config'
)
properties_schema = {
CLOUD_CONFIG: properties.Schema(
properties.Schema.MAP,
_('Map representing the cloud-config data structure which will '
'be formatted as YAML.')
)
}
def handle_create(self):
props = {self.NAME: self.physical_resource_name()}
cloud_config = yaml.dump(self.properties.get(
self.CLOUD_CONFIG), Dumper=yaml_dumper)
props[self.CONFIG] = '#cloud-config\n%s' % cloud_config
sc = self.heat().software_configs.create(**props)
self.resource_id_set(sc.id)
def resource_mapping():
return {
'OS::Heat::CloudConfig': CloudConfig,
}

View File

@ -0,0 +1,62 @@
#
# 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
from heat.engine import parser
from heat.engine import template
import heat.engine.resources.software_config.cloud_config as cc
from heat.tests.common import HeatTestCase
from heat.tests import utils
class CloudConfigTest(HeatTestCase):
def setUp(self):
super(CloudConfigTest, self).setUp()
utils.setup_dummy_db()
self.ctx = utils.dummy_context()
self.properties = {
'cloud_config': {'foo': 'bar'}
}
self.stack = parser.Stack(
self.ctx, 'software_config_test_stack',
template.Template({
'Resources': {
'config_mysql': {
'Type': 'OS::Heat::CloudConfig',
'Properties': self.properties
}}}))
self.config = self.stack['config_mysql']
heat = mock.MagicMock()
self.config.heat = heat
self.software_configs = heat.return_value.software_configs
def test_resource_mapping(self):
mapping = cc.resource_mapping()
self.assertEqual(1, len(mapping))
self.assertEqual(cc.CloudConfig,
mapping['OS::Heat::CloudConfig'])
self.assertIsInstance(self.config, cc.CloudConfig)
def test_handle_create(self):
sc = mock.MagicMock()
config_id = 'c8a19429-7fde-47ea-a42f-40045488226c'
sc.id = config_id
self.software_configs.create.return_value = sc
self.config.handle_create()
self.assertEqual(config_id, self.config.resource_id)
kwargs = self.software_configs.create.call_args[1]
self.assertEqual('#cloud-config\n{foo: bar}\n', kwargs['config'])