From 9e31053c7ceb2bf182a5b23a11f16e89568d10ba Mon Sep 17 00:00:00 2001 From: Rakesh H S Date: Mon, 14 Sep 2015 17:42:16 +0530 Subject: [PATCH] Convergence: Fix FnGetRefId to fetch value from cache_data Fix FnGetRefId of WaitConditionHandle, ElasticIp and NeutronResource to fetch value from cache_data, if present. Change-Id: Icd92f6514c7590c90da2fcd2123d33862d613486 --- heat/engine/resource.py | 12 ++++--- .../aws/cfn/wait_condition_handle.py | 5 +-- heat/engine/resources/aws/ec2/eip.py | 2 +- .../resources/openstack/neutron/neutron.py | 6 +--- heat/tests/aws/test_eip.py | 32 +++++++++++++++++++ heat/tests/aws/test_waitcondition.py | 30 +++++++++++++++++ .../tests/neutron/test_neutron_floating_ip.py | 25 +++++++++++++++ 7 files changed, 97 insertions(+), 15 deletions(-) diff --git a/heat/engine/resource.py b/heat/engine/resource.py index 2bf2ee715..da2293fdb 100644 --- a/heat/engine/resource.py +++ b/heat/engine/resource.py @@ -1459,6 +1459,12 @@ class Resource(object): '''Returns state, tuple of action, status.''' return (self.action, self.status) + def get_reference_id(self): + if self.resource_id is not None: + return six.text_type(self.resource_id) + else: + return six.text_type(self.name) + def FnGetRefId(self): ''' For the intrinsic function Ref. @@ -1467,11 +1473,7 @@ class Resource(object): ''' if self.stack.has_cache_data(self.name): return self.stack.cache_data_reference_id(self.name) - - if self.resource_id is not None: - return six.text_type(self.resource_id) - else: - return six.text_type(self.name) + return self.get_reference_id() def physical_resource_name_or_FnGetRefId(self): res_name = self.physical_resource_name() diff --git a/heat/engine/resources/aws/cfn/wait_condition_handle.py b/heat/engine/resources/aws/cfn/wait_condition_handle.py index 814313c63..f5c09b27f 100644 --- a/heat/engine/resources/aws/cfn/wait_condition_handle.py +++ b/heat/engine/resources/aws/cfn/wait_condition_handle.py @@ -35,10 +35,7 @@ class WaitConditionHandle(wc_base.BaseWaitConditionHandle): 'Data', 'Reason', 'Status', 'UniqueId' ) - def FnGetRefId(self): - ''' - Override the default resource FnGetRefId so we return the signed URL - ''' + def get_reference_id(self): if self.resource_id: wc = signal_responder.WAITCONDITION return six.text_type(self._get_ec2_signed_url(signal_type=wc)) diff --git a/heat/engine/resources/aws/ec2/eip.py b/heat/engine/resources/aws/ec2/eip.py index e10a3b676..95e6cb973 100644 --- a/heat/engine/resources/aws/ec2/eip.py +++ b/heat/engine/resources/aws/ec2/eip.py @@ -177,7 +177,7 @@ class ElasticIp(resource.Resource): server = self.client().servers.get(instance_id_old) server.remove_floating_ip(self._ipaddress()) - def FnGetRefId(self): + def get_reference_id(self): eip = self._ipaddress() if eip: return six.text_type(eip) diff --git a/heat/engine/resources/openstack/neutron/neutron.py b/heat/engine/resources/openstack/neutron/neutron.py index b1b67ce5f..3ef271dc2 100644 --- a/heat/engine/resources/openstack/neutron/neutron.py +++ b/heat/engine/resources/openstack/neutron/neutron.py @@ -122,11 +122,7 @@ class NeutronResource(resource.Resource): attributes = self._show_resource() return attributes[name] - def FnGetRefId(self): - # convergence: try some luck with cached data first - if self.stack.has_cache_data(self.name): - return self.stack.cache_data_reference_id(self.name) - + def get_reference_id(self): return six.text_type(self.resource_id) @staticmethod diff --git a/heat/tests/aws/test_eip.py b/heat/tests/aws/test_eip.py index 74b739bf3..2c7d4eee4 100644 --- a/heat/tests/aws/test_eip.py +++ b/heat/tests/aws/test_eip.py @@ -13,6 +13,7 @@ import copy +import mock import mox from neutronclient.v2_0 import client as neutronclient from novaclient import exceptions as nova_exceptions @@ -372,6 +373,37 @@ class EIPTest(common.HeatTestCase): self.assertEqual((rsrc.DELETE, rsrc.COMPLETE), rsrc.state) self.m.VerifyAll() + @mock.patch.object(eip.ElasticIp, '_ipaddress') + def test_FnGetRefId_resource_name(self, mock_ipaddr): + t = template_format.parse(ipassoc_template_validate) + stack = utils.parse_stack(t) + rsrc = stack['eip'] + mock_ipaddr.return_value = None + self.assertEqual('eip', rsrc.FnGetRefId()) + + @mock.patch.object(eip.ElasticIp, '_ipaddress') + def test_FnGetRefId_resource_ip(self, mock_ipaddr): + t = template_format.parse(ipassoc_template_validate) + stack = utils.parse_stack(t) + rsrc = stack['eip'] + mock_ipaddr.return_value = 'x.x.x.x' + self.assertEqual('x.x.x.x', rsrc.FnGetRefId()) + + def test_FnGetRefId_convergence_cache_data(self): + t = template_format.parse(ipassoc_template_validate) + template = tmpl.Template(t) + stack = parser.Stack(utils.dummy_context(), 'test', template, + cache_data={ + 'eip': { + 'uuid': mock.ANY, + 'id': mock.ANY, + 'action': 'CREATE', + 'status': 'COMPLETE', + 'reference_id': '1.1.1.1'}}) + + rsrc = stack['eip'] + self.assertEqual('1.1.1.1', rsrc.FnGetRefId()) + class AllocTest(common.HeatTestCase): diff --git a/heat/tests/aws/test_waitcondition.py b/heat/tests/aws/test_waitcondition.py index e9cbbd082..3fe856e64 100644 --- a/heat/tests/aws/test_waitcondition.py +++ b/heat/tests/aws/test_waitcondition.py @@ -16,6 +16,7 @@ import datetime import json import uuid +import mock from oslo_utils import timeutils import six from six.moves.urllib import parse @@ -255,6 +256,35 @@ class WaitConditionTest(common.HeatTestCase): self.assertEqual('status:SUCCESS reason:cat', ret) self.m.VerifyAll() + def test_FnGetRefId_resource_name(self): + self.stack = self.create_stack() + rsrc = self.stack['WaitHandle'] + self.assertEqual('WaitHandle', rsrc.FnGetRefId()) + + @mock.patch.object(aws_wch.WaitConditionHandle, '_get_ec2_signed_url') + def test_FnGetRefId_signed_url(self, mock_get_signed_url): + self.stack = self.create_stack() + rsrc = self.stack['WaitHandle'] + rsrc.resource_id = '123' + mock_get_signed_url.return_value = 'http://signed_url' + self.assertEqual('http://signed_url', rsrc.FnGetRefId()) + + def test_FnGetRefId_convergence_cache_data(self): + t = template_format.parse(test_template_waitcondition) + template = tmpl.Template(t) + stack = parser.Stack(utils.dummy_context(), 'test', template, + cache_data={ + 'WaitHandle': { + 'uuid': mock.ANY, + 'id': mock.ANY, + 'action': 'CREATE', + 'status': 'COMPLETE', + 'reference_id': 'http://convg_signed_url' + }}) + + rsrc = stack['WaitHandle'] + self.assertEqual('http://convg_signed_url', rsrc.FnGetRefId()) + def test_validate_handle_url_bad_stackid(self): self.m.ReplayAll() diff --git a/heat/tests/neutron/test_neutron_floating_ip.py b/heat/tests/neutron/test_neutron_floating_ip.py index 78985e3b4..10074410b 100644 --- a/heat/tests/neutron/test_neutron_floating_ip.py +++ b/heat/tests/neutron/test_neutron_floating_ip.py @@ -13,6 +13,7 @@ import copy +import mock import mox from neutronclient.common import exceptions as qe from neutronclient.neutron import v2_0 as neutronV20 @@ -23,6 +24,8 @@ from heat.common import template_format from heat.engine.cfn import functions as cfn_funcs from heat.engine import rsrc_defn from heat.engine import scheduler +from heat.engine import stack as parser +from heat.engine import template as tmpl from heat.tests import common from heat.tests import utils @@ -237,6 +240,28 @@ class NeutronFloatingIPTest(common.HeatTestCase): self.m.VerifyAll() + def test_FnGetRefId(self): + t = template_format.parse(neutron_floating_template) + stack = utils.parse_stack(t) + rsrc = stack['floating_ip'] + rsrc.resource_id = 'xyz' + self.assertEqual('xyz', rsrc.FnGetRefId()) + + def test_FnGetRefId_convergence_cache_data(self): + t = template_format.parse(neutron_floating_template) + template = tmpl.Template(t) + stack = parser.Stack(utils.dummy_context(), 'test', template, + cache_data={ + 'floating_ip': { + 'uuid': mock.ANY, + 'id': mock.ANY, + 'action': 'CREATE', + 'status': 'COMPLETE', + 'reference_id': 'abc'}}) + + rsrc = stack['floating_ip'] + self.assertEqual('abc', rsrc.FnGetRefId()) + def test_port(self): self.stub_NetworkConstraint_validate() self.stub_SubnetConstraint_validate()