From 2e9b9b90316e1459da81d182b1ddad0edf7f1f63 Mon Sep 17 00:00:00 2001 From: Jeff Peeler Date: Mon, 17 Jun 2013 12:19:49 -0400 Subject: [PATCH] Add InstanceId property to EIP resource This also changes the InstanceId property for an EIPAssociation to not be required, which was necessary for testing and is inline with AWS documentation. Change-Id: I49dd506309fe6a38df6727bca7f6bbebd45fba3f Fixes: bug #1164864 --- heat/engine/resources/eip.py | 32 +++++++++++++++++++++++--------- heat/tests/test_eip.py | 23 ++++++++++++++++++++++- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/heat/engine/resources/eip.py b/heat/engine/resources/eip.py index 461f8d86..dc568565 100644 --- a/heat/engine/resources/eip.py +++ b/heat/engine/resources/eip.py @@ -49,7 +49,19 @@ class ElasticIp(resource.Resource): self.ipaddress = ips.ip self.resource_id_set(ips.id) + if self.properties['InstanceId']: + server = self.nova().servers.get(self.properties['InstanceId']) + res = server.add_floating_ip(self._ipaddress()) + def handle_delete(self): + if self.properties['InstanceId']: + try: + server = self.nova().servers.get(self.properties['InstanceId']) + if server: + server.remove_floating_ip(self._ipaddress()) + except clients.novaclient.exceptions.NotFound as ex: + pass + """De-allocate a floating IP.""" if self.resource_id is not None: self.nova().floating_ips.delete(self.resource_id) @@ -67,7 +79,7 @@ class ElasticIp(resource.Resource): class ElasticIpAssociation(resource.Resource): properties_schema = {'InstanceId': {'Type': 'String', - 'Required': True}, + 'Required': False}, 'EIP': {'Type': 'String'}, 'AllocationId': {'Type': 'String', 'Implemented': False}} @@ -81,18 +93,20 @@ class ElasticIpAssociation(resource.Resource): (self.properties['InstanceId'], self.properties['EIP'])) - server = self.nova().servers.get(self.properties['InstanceId']) - server.add_floating_ip(self.properties['EIP']) + if self.properties['InstanceId']: + server = self.nova().servers.get(self.properties['InstanceId']) + server.add_floating_ip(self.properties['EIP']) self.resource_id_set(self.properties['EIP']) def handle_delete(self): """Remove a floating IP address from a server.""" - try: - server = self.nova().servers.get(self.properties['InstanceId']) - if server: - server.remove_floating_ip(self.properties['EIP']) - except clients.novaclient.exceptions.NotFound as ex: - pass + if self.properties['InstanceId']: + try: + server = self.nova().servers.get(self.properties['InstanceId']) + if server: + server.remove_floating_ip(self.properties['EIP']) + except clients.novaclient.exceptions.NotFound as ex: + pass def resource_mapping(): diff --git a/heat/tests/test_eip.py b/heat/tests/test_eip.py index 6a810d8b..1781056d 100644 --- a/heat/tests/test_eip.py +++ b/heat/tests/test_eip.py @@ -24,6 +24,25 @@ from heat.tests.utils import parse_stack eip_template = ''' +{ + "AWSTemplateFormatVersion" : "2010-09-09", + "Description" : "EIP Test", + "Parameters" : {}, + "Resources" : { + "IPAddress" : { + "Type" : "AWS::EC2::EIP", + "Properties" : { + "InstanceId" : { "Ref" : "WebServer" } + } + }, + "WebServer": { + "Type": "AWS::EC2::Instance", + } + } +} +''' + +eip_template_ipassoc = ''' { "AWSTemplateFormatVersion" : "2010-09-09", "Description" : "EIP Test", @@ -78,6 +97,8 @@ class EIPTest(HeatTestCase): def test_eip(self): eip.ElasticIp.nova().MultipleTimes().AndReturn(self.fc) + self.fc.servers.get('WebServer').AndReturn(self.fc.servers.list()[0]) + self.fc.servers.get('WebServer') self.m.ReplayAll() @@ -114,7 +135,7 @@ class EIPTest(HeatTestCase): self.m.ReplayAll() - t = template_format.parse(eip_template) + t = template_format.parse(eip_template_ipassoc) stack = parse_stack(t) rsrc = self.create_eip(t, stack, 'IPAddress') -- 2.45.2