From fc8dd31ff79ba2c0c610e7a6fad450eb10acae93 Mon Sep 17 00:00:00 2001 From: Unmesh Gurjar Date: Thu, 21 Aug 2014 14:28:49 +0530 Subject: [PATCH] Fixed adopt failure for stack with resource group Added support to adopt a resource group and corresponding unit test. Change-Id: Icfeff0e30a9c4bd7fa0279e156709e078b81fcab Closes-bug: #1353942 --- heat/engine/resources/resource_group.py | 18 +++++++++ heat/tests/test_resource_group.py | 54 +++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/heat/engine/resources/resource_group.py b/heat/engine/resources/resource_group.py index 7a1683a3a..1a6e9b464 100644 --- a/heat/engine/resources/resource_group.py +++ b/heat/engine/resources/resource_group.py @@ -253,6 +253,24 @@ class ResourceGroup(stack_resource.StackResource): def child_params(self): return {} + def handle_adopt(self, resource_data): + names = self._resource_names() + if names: + return self.create_with_template(self._assemble_nested(names), + {}, + adopt_data=resource_data) + + def check_adopt_complete(self, adopter): + if adopter is None: + return True + done = adopter.step() + if done: + if self._nested.state != (self._nested.ADOPT, + self._nested.COMPLETE): + raise exception.Error(self._nested.status_reason) + + return done + def resource_mapping(): return { diff --git a/heat/tests/test_resource_group.py b/heat/tests/test_resource_group.py index 448c36202..10454cfa5 100644 --- a/heat/tests/test_resource_group.py +++ b/heat/tests/test_resource_group.py @@ -14,12 +14,15 @@ import copy import mock import six +import uuid from heat.common import exception from heat.engine import properties from heat.engine import resource from heat.engine.resources import resource_group from heat.engine import scheduler +from heat.engine import stack as stackm +from heat.engine import template as templatem from heat.tests import common from heat.tests import generic_resource from heat.tests import utils @@ -480,3 +483,54 @@ class ResourceGroupTest(common.HeatTestCase): snip = stack.t.resource_definitions(stack)['group1'] resgrp = resource_group.ResourceGroup('test', snip, stack) self.assertEqual({}, resgrp.child_params()) + + def test_adopt(self): + tmpl = templatem.Template(template) + stack = stackm.Stack(utils.dummy_context(), + 'test_stack', + tmpl, + stack_id=str(uuid.uuid4())) + + resg = stack['group1'] + + adopt_data = { + "status": "COMPLETE", + "name": "group1", + "resource_data": {}, + "metadata": {}, + "resource_id": "test-group1-id", + "action": "CREATE", + "type": "OS::Heat::ResourceGroup", + "resources": { + "0": { + "status": "COMPLETE", + "name": "0", + "resource_data": {}, + "resource_id": "ID-0", + "action": "CREATE", + "type": "dummy.resource", + "metadata": {} + }, + "1": { + "status": "COMPLETE", + "name": "1", + "resource_data": {}, + "resource_id": "ID-1", + "action": "CREATE", + "type": "dummy.resource", + "metadata": {} + } + } + } + scheduler.TaskRunner(resg.adopt, adopt_data)() + self.assertEqual((resg.ADOPT, resg.COMPLETE), resg.state) + self.assertEqual(adopt_data['name'], resg.name) + # a new nested stack should be created + self.assertIsNotNone(resg.resource_id) + # verify all the resources in resource group are adopted. + self.assertEqual(adopt_data['resources']['0']['resource_id'], + resg.FnGetAtt('resource.0')) + self.assertEqual(adopt_data['resources']['1']['resource_id'], + resg.FnGetAtt('resource.1')) + self.assertRaises(exception.InvalidTemplateAttribute, resg.FnGetAtt, + 'resource.2')