incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 4/4] Added support for Addresses to EC2 driver
Date Wed, 08 Dec 2010 12:34:25 GMT
From: Michal Fojtik <mfojtik@redhat.com>

---
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb |   74 ++++++++++++++++++++++-
 1 files changed, 73 insertions(+), 1 deletions(-)

diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index efbfcc8..7cd0146 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -28,7 +28,7 @@ module Deltacloud
 class EC2Driver < Deltacloud::BaseDriver
 
   def supported_collections
-    DEFAULT_COLLECTIONS + [ :keys, :buckets, :load_balancers ]
+    DEFAULT_COLLECTIONS + [ :keys, :buckets, :load_balancers, :addresses ]
   end
 
   feature :instances, :user_data
@@ -284,6 +284,71 @@ class EC2Driver < Deltacloud::BaseDriver
     snapshots
   end
 
+  def addresses(credentials, opts={})
+    ec2 = new_client( credentials )
+    addresses = []
+    safely do
+      response = ec2.describe_addresses
+      response.addressesSet.item.each do |address|
+        addresses << convert_address(address)
+      end if response.addressesSet
+    end
+    addresses
+  end
+
+  def address(credentials, opts={})
+    ec2 = new_client( credentials )
+    address = nil
+    safely do
+      address_response = ec2.describe_addresses(:public_ip => [opts[:id]])
+      if address_response.addressesSet
+        address = convert_address(address_response.addressesSet.item.first)
+      end
+    end
+    address
+  end
+
+  def create_address(credentials, opts={})
+    ec2 = new_client( credentials )
+    address = nil
+    safely do
+      address_response = ec2.allocate_address
+      if address_response['publicIp']
+        address = convert_address(address_response['publicIp'], :create)
+      end
+      if opts[:instance_id] and opts[:instance_id].lenght!=0
+        associate_address(credentials, {
+          :instance_id => opts[:instance_id],
+          :id => address.id
+        })
+      end
+    end
+    address
+  end
+
+  def destroy_address(credentials, opts={})
+    ec2 = new_client( credentials )
+    retval = false
+    safely do
+      retval = ec2.release_address(:public_ip => opts[:id])
+    end
+    retval
+  end
+
+  def associate_address(credentials, opts={})
+    ec2 = new_client( credentials )
+    safely do
+      ec2.associate_address(:instance_id => opts[:instance_id], :public_ip => opts[:id])
+    end
+  end
+
+  def release_address(credentials, opts={})
+    ec2 = new_client( credentials )
+    safely do
+      ec2.disassociate_address(:public_ip => opts[:id])
+    end
+  end
+
   def key(credentials, opts=nil)
     keys(credentials, opts).first
   end
@@ -542,6 +607,13 @@ class EC2Driver < Deltacloud::BaseDriver
     balancer
   end
 
+  def convert_address(address, mode=nil)
+    Address.new(
+      :id => address['publicIp'],
+      :instance_id => address['instanceId'],
+      :visibility => :public
+    )
+  end
 
   def convert_key(key)
     Key.new({
-- 
1.7.3.2


Mime
View raw message