incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 5/6] Added initial IP managment for EC2
Date Wed, 25 Aug 2010 14:42:29 GMT
---
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb |   47 ++++++++++++++++++++++-
 server/views/ip_addresses/index.html.haml       |    4 ++
 server/views/ip_addresses/show.html.haml        |    3 +
 3 files changed, 53 insertions(+), 1 deletions(-)

diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index 909eca3..eea0c41 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -36,7 +36,7 @@ module Deltacloud
 class EC2Driver < Deltacloud::BaseDriver
 
   def supported_collections
-    DEFAULT_COLLECTIONS + [ :keys ]
+    DEFAULT_COLLECTIONS + [ :keys, :ip_addresses ]
   end
 
   feature :instances, :user_data
@@ -310,6 +310,41 @@ class EC2Driver < Deltacloud::BaseDriver
     end
   end
 
+  def ip_addresses(credentials, opts=nil)
+    ec2 = new_client( credentials )
+    ips = []
+    safely do
+      address_set = ec2.describe_addresses.addressesSet
+      address_set.item.each do |ip|
+        ips << convert_ip_address(ip)
+      end if address_set
+    end
+    ips = filter_on( ips, :type, opts )
+    ips = filter_on( ips, :state, opts )
+    return ips
+  end
+
+  def ip_address(credentials, opts={})
+    ip_addresses(credentials).select { |ip| ip.id.eql?(opts[:id])}.first
+  end
+
+  def create_ip_address(credentials, opts)
+    ec2 = new_client( credentials )
+    safely do
+      ip = ec2.allocate_address().publicIp
+      return ip_addresses(credentials, :id => ip)
+    end
+  end
+
+  def destroy_ip_address(credentials, opts)
+    ec2 = new_client( credentials )
+    response = false
+    safely do
+      response = ec2.release_address(:public_ip => opts[:id]).return
+    end
+    throw BackendError.new(500, 'Unable to release this IP address', '', '') unless response.to_s.eql?("true")
+  end
+
   private
 
   def new_client(credentials)
@@ -323,6 +358,16 @@ class EC2Driver < Deltacloud::BaseDriver
     end
   end
 
+  def convert_ip_address(ip)
+    IpAddress.new({
+      :id => ip['publicIp'],
+      :address => ip['publicIp'],
+      :type => 'public',
+      :state => ip['instanceId'] ? 'assigned' : 'unassigned',
+      :realm_id => 'us-east-1a' # TODO: Add realm Realm here
+    })
+  end
+
   def convert_key(key)
     Key.new({
       :id => key['keyName'],
diff --git a/server/views/ip_addresses/index.html.haml b/server/views/ip_addresses/index.html.haml
index a0e0fe8..b0a430b 100644
--- a/server/views/ip_addresses/index.html.haml
+++ b/server/views/ip_addresses/index.html.haml
@@ -21,3 +21,7 @@
           =ip.state
         %td
           =link_to ip.realm_id, realm_url(ip.realm_id)
+  %tfoot
+    %tr
+      %td{:colspan => 5, :style => "text-align:right;"}
+        =link_to 'Create &raquo;', create_ip_address_url, :class => 'post'
diff --git a/server/views/ip_addresses/show.html.haml b/server/views/ip_addresses/show.html.haml
index 1a66514..76d3722 100644
--- a/server/views/ip_addresses/show.html.haml
+++ b/server/views/ip_addresses/show.html.haml
@@ -18,3 +18,6 @@
     %dt Realm
     %dd
       = link_to driver.realm(credentials, @ip_address.realm_id).name, realm_url(@ip_address.realm_id)
+    %dt
+    %dd
+      = link_to 'Destroy', destroy_ip_address_url(@ip_address.id), :class => 'delete'
-- 
1.7.2


Mime
View raw message