]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Add InstanceId property to EIP resource
authorJeff Peeler <jpeeler@redhat.com>
Mon, 17 Jun 2013 16:19:49 +0000 (12:19 -0400)
committerJeff Peeler <jpeeler@redhat.com>
Mon, 17 Jun 2013 16:24:46 +0000 (12:24 -0400)
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
heat/tests/test_eip.py

index 461f8d86dbd940bcfc6fedc3f4f0b319afada86b..dc5685653ec2ed9c2c2f2fc295575f6436e0f64c 100644 (file)
@@ -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():
index 6a810d8bb565f4763b51411bea9f56c05bba9446..1781056dc6e45ff7f983f5a04826eafed2499677 100644 (file)
@@ -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')