You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by mf...@apache.org on 2011/01/06 15:12:56 UTC

svn commit: r1055886 - in /incubator/deltacloud/trunk/server: lib/deltacloud/drivers/ec2/ec2_driver.rb views/load_balancers/index.html.haml views/load_balancers/new.html.haml views/load_balancers/show.html.haml

Author: mfojtik
Date: Thu Jan  6 14:12:56 2011
New Revision: 1055886

URL: http://svn.apache.org/viewvc?rev=1055886&view=rev
Log:
Moved Load Balancers from old EC2 driver to new one.

Modified:
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
    incubator/deltacloud/trunk/server/views/load_balancers/index.html.haml
    incubator/deltacloud/trunk/server/views/load_balancers/new.html.haml
    incubator/deltacloud/trunk/server/views/load_balancers/show.html.haml

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb?rev=1055886&r1=1055885&r2=1055886&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb Thu Jan  6 14:12:56 2011
@@ -35,7 +35,7 @@ module Deltacloud
       class EC2Driver < Deltacloud::BaseDriver
 
         def supported_collections
-          DEFAULT_COLLECTIONS + [ :keys, :buckets ]
+          DEFAULT_COLLECTIONS + [ :keys, :buckets, :load_balancers ]
         end
 
         feature :instances, :user_data
@@ -43,6 +43,7 @@ module Deltacloud
         feature :instances, :security_group
         feature :images, :owner_id
         feature :buckets, :bucket_location
+        feature :instances, :register_to_load_balancer
 
         DEFAULT_REGION = 'us-east-1'
 
@@ -185,9 +186,17 @@ module Deltacloud
           instance_options.merge!(:group_ids => opts[:security_group]) if opts[:security_group]
           safely do
             new_instance = convert_instance(ec2.launch_instances(image_id, instance_options).first)
+            # TODO: Rework this to use client_id for name instead of tag
+            #       Tags should be keept as an optional feature
             if opts[:name]
               tag_instance(credentials, new_instance, opts[:name])
             end
+            # Register Instance to Load Balancer if load_balancer_id is set.
+            # This parameter is a feature parameter
+            if opts[:load_balancer_id] and opts[:load_balancer_id].length>0
+              elb = new_client(credentials, :elb)
+              elb.register_instances_with_load_balancer(opts[:load_balancer_id], [new_instance.id])
+            end
             new_instance
           end
         end
@@ -242,6 +251,60 @@ module Deltacloud
           original_key
         end
 
+        def load_balancer(credentials, opts={})
+          load_balancers(credentials, {
+            :names => [opts[:id]]
+          }).first
+        end
+
+        def load_balancers(credentials, opts=nil)
+          ec2 = new_client( credentials, :elb )
+          result = []
+          safely do
+            loadbalancers = ec2.describe_load_balancers(opts || {})
+            loadbalancers.each do |loadbalancer|
+              result << convert_load_balancer(credentials, loadbalancer)
+            end
+          end
+          return result
+        end
+
+        def create_load_balancer(credentials, opts={})
+          ec2 = new_client( credentials, :elb )
+          safely do
+            ec2.create_load_balancer(opts['name'], [opts['realm_id']],
+              [{:load_balancer_port => opts['listener_balancer_port'],
+                :instance_port => opts['listener_instance_port'],
+                :protocol => opts['listener_protocol']}]
+            )
+            return load_balancer(credentials, opts['name'])
+          end
+        end
+
+        def destroy_load_balancer(credentials, id)
+          ec2 = new_client( credentials, :elb )
+          safely do
+            ec2.delete_load_balancer(id)
+          end
+        end
+
+        def lb_register_instance(credentials, opts={})
+          ec2 = new_client( credentials, :elb)
+          safely do
+            ec2.register_instances_with_load_balancer(opts['id'], [opts['instance_id']])
+            load_balancer(credentials, :id => opts[:id])
+          end
+        end
+
+        def lb_unregister_instance(credentials, opts={})
+          ec2 = new_client( credentials, :elb)
+          safely do
+            ec2.deregister_instances_from_load_balancer(opts['id'], [opts['instance_id']])
+            load_balancer(credentials, :id => opts['id'])
+          end
+        end
+
+
         def buckets(credentials, opts)
           buckets = []
           safely do
@@ -581,6 +644,29 @@ module Deltacloud
           )
         end
 
+        def convert_load_balancer(credentials, loadbalancer)
+          puts loadbalancer.inspect
+          realms = []
+          balancer_realms = loadbalancer[:availability_zones].each do |zone|
+            realms << realm(credentials, zone)
+          end
+          balancer = LoadBalancer.new({
+            :id => loadbalancer[:name],
+            :created_at => loadbalancer[:created_time],
+            :public_addresses => [loadbalancer[:dns_name]],
+            :realms => realms
+          })
+          balancer.listeners = []
+          balancer.instances = []
+          loadbalancer[:listeners].each do |listener|
+            balancer.add_listener(listener)
+          end
+          loadbalancer[:instances].each do |instance|
+            balancer.instances << instance(credentials, :id => instance[:id])
+          end
+          balancer
+        end
+
         def convert_state(ec2_state)
           case ec2_state
             when "terminated"

Modified: incubator/deltacloud/trunk/server/views/load_balancers/index.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/load_balancers/index.html.haml?rev=1055886&r1=1055885&r2=1055886&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/load_balancers/index.html.haml (original)
+++ incubator/deltacloud/trunk/server/views/load_balancers/index.html.haml Thu Jan  6 14:12:56 2011
@@ -17,7 +17,7 @@
         %td
           = balancer.public_addresses.first
         %td
-          = link_to balancer.realms.first.id, realm_url( balancer.realms.first.id )
+          =link_to balancer.realms.first.id, realm_url( balancer.realms.first.id )
         %td
           - balancer.listeners.each do |listener|
             ="#{listener.protocol}[#{listener.load_balancer_port}]<br/>"

Modified: incubator/deltacloud/trunk/server/views/load_balancers/new.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/load_balancers/new.html.haml?rev=1055886&r1=1055885&r2=1055886&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/load_balancers/new.html.haml (original)
+++ incubator/deltacloud/trunk/server/views/load_balancers/new.html.haml Thu Jan  6 14:12:56 2011
@@ -28,11 +28,11 @@
   %p
     %label
       Load balancer port:
-    %input{ :name => "listener_lbr_port", :size => 30}
+    %input{ :name => "listener_balancer_port", :size => 30}
   %p
     %label
       Instances port:
-    %input{ :name => "listener_inst_port", :size => 30}
+    %input{ :name => "listener_instance_port", :size => 30}
   %p
     %input{ :type => :submit, :name => "commit", :value => "create" }/
 

Modified: incubator/deltacloud/trunk/server/views/load_balancers/show.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/load_balancers/show.html.haml?rev=1055886&r1=1055885&r2=1055886&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/load_balancers/show.html.haml (original)
+++ incubator/deltacloud/trunk/server/views/load_balancers/show.html.haml Thu Jan  6 14:12:56 2011
@@ -25,7 +25,7 @@
       - @load_balancer.instances.each do |inst|
         %dd
           =inst.id
-          =link_to_action, 'Delete', unregister_load_balancer_url(@load_balancer.id, :instance_id => inst.id), :post
+          =link_to_action 'Delete', unregister_load_balancer_url(@load_balancer.id, :instance_id => inst.id), :post
 
 %form{:action => url_for("/api/load_balancers/#{@load_balancer.id}/register"), :method => :post}
   %p