]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Initial ElasticIp work - not quite working
authorAngus Salkeld <asalkeld@redhat.com>
Thu, 12 Apr 2012 10:28:14 +0000 (20:28 +1000)
committerTomas Sedovic <tomas@sedovic.cz>
Thu, 12 Apr 2012 11:23:15 +0000 (13:23 +0200)
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
heat/engine/resources.py
templates/WordPress_Single_Instance.template

index 8b94492bfc6fd1cc1f84624e4e874444619d9cb0..c25b8048b8fbb715e9cfd6bf9500efd7ac8bfb85 100644 (file)
@@ -145,39 +145,51 @@ class ElasticIp(Resource):
     def __init__(self, name, json_snippet, stack):
         super(ElasticIp, self).__init__(name, json_snippet, stack)
         self.instance_id = ''
+        self.ipaddress = ''
 
         if self.t.has_key('Properties') and self.t['Properties'].has_key('Domain'):
             logger.warn('*** can\'t support Domain %s yet' % (self.t['Properties']['Domain']))
 
     def create(self):
+        """Allocate a floating IP for the current tenant."""
         if self.state != None:
             return
         self.state_set(self.CREATE_IN_PROGRESS)
         super(ElasticIp, self).create()
-        self.instance_id = 'eip-000003'
+
+        ips = self.nova().floating_ips.create()
+        print 'ElasticIp create %s' % str(ips)
+        self.ipaddress = ips.ip
+        self.instance_id = ips.id
+
+    def delete(self):
+        """De-allocate a floating IP."""
+        if self.state == self.DELETE_IN_PROGRESS or self.state == self.DELETE_COMPLETE:
+            return
+
+        self.state_set(self.DELETE_IN_PROGRESS)
+        Resource.delete(self)
+
+        if self.instance_id != None:
+            print 'ElasticIp delete %s:%s' % (self.ipaddress, self.instance_id)
+            self.nova().floating_ips.delete(self.instance_id)
+
+        self.state_set(self.DELETE_COMPLETE)
 
     def FnGetRefId(self):
-        return unicode('0.0.0.0')
+        return unicode(self.ipaddress)
 
     def FnGetAtt(self, key):
-        return unicode(self.instance_id)
+        if key == 'AllocationId':
+            return unicode(self.instance_id)
+        else:
+            logger.warn('%s.GetAtt(%s) is not handled' % (self.name, key))
+            return unicode('')
 
 class ElasticIpAssociation(Resource):
     def __init__(self, name, json_snippet, stack):
         super(ElasticIpAssociation, self).__init__(name, json_snippet, stack)
 
-        # note we only support already assigned ipaddress
-        #
-        # Done with:
-        # nova-manage floating create 172.31.0.224/28
-        # euca-allocate-address
-        #
-
-        if not self.t['Properties'].has_key('EIP'):
-            logger.warn('*** can\'t support this yet')
-        if self.t['Properties'].has_key('AllocationId'):
-            logger.warn('*** can\'t support AllocationId %s yet' % (self.t['Properties']['AllocationId']))
-
     def FnGetRefId(self):
         if not self.t['Properties'].has_key('EIP'):
             return unicode('0.0.0.0')
@@ -185,13 +197,31 @@ class ElasticIpAssociation(Resource):
             return unicode(self.t['Properties']['EIP'])
 
     def create(self):
+        """Add a floating IP address to a server."""
 
         if self.state != None:
             return
         self.state_set(self.CREATE_IN_PROGRESS)
         super(ElasticIpAssociation, self).create()
-        logger.info('$ euca-associate-address -i %s %s' % (self.t['Properties']['InstanceId'],
-                                                           self.t['Properties']['EIP']))
+        print 'ElasticIpAssociation %s.add_floating_ip(%s)' % (self.t['Properties']['InstanceId'],
+                                                               self.t['Properties']['EIP'])
+
+        server = self.nova().servers.get(self.t['Properties']['InstanceId'])
+        server.add_floating_ip(self.t['Properties']['EIP'])
+
+    def delete(self):
+        """Remove a floating IP address from a server."""
+        if self.state == self.DELETE_IN_PROGRESS or self.state == self.DELETE_COMPLETE:
+            return
+
+        self.state_set(self.DELETE_IN_PROGRESS)
+        Resource.delete(self)
+
+        server = self.nova().servers.get(self.t['Properties']['InstanceId'])
+        server.remove_floating_ip(self.t['Properties']['EIP'])
+
+        self.state_set(self.DELETE_COMPLETE)
+
 
 class Volume(Resource):
     def __init__(self, name, json_snippet, stack):
index b3d06afecf6e99ee9d8d212e8d9ce94c51584058..1e25a38d186fc5b24112f47841ad6e95ca2c7af9 100644 (file)
 
   "Resources" : {
 
+    "IPAddress" : {
+      "Type" : "AWS::EC2::EIP"
+    },
+
+    "IPAssoc" : {
+      "Type" : "AWS::EC2::EIPAssociation",
+      "Properties" : {
+        "InstanceId" : { "Ref" : "WebServer" },
+        "EIP" : { "Ref" : "IPAddress" }
+      }
+    },
+
     "WebServer": {
       "Type": "AWS::EC2::Instance",
       "Metadata" : {
     "WebsiteURL" : {
       "Value" : { "Fn::Join" : ["", ["http://", { "Fn::GetAtt" : [ "WebServer", "PublicIp" ]}, "/wordpress"]] },
       "Description" : "URL for Wordpress wiki"
+    },
+    "InstanceIPAddress" : {
+      "Value" : { "Ref" : "IPAddress" }
     }
   }
 }