On 11/11/10 10:01 -0500, Toby Crawley wrote:
I played with load balancer support against ec2, and it worked for me - ACK, 
with one inline comment below.

Thanks!

  -- Michal


On 11/08/2010 06:43 AM, [email protected] wrote:
From: Michal Fojtik<[email protected]>

---
 server/lib/deltacloud/base_driver/features.rb      |    8 ++
 .../lib/deltacloud/helpers/application_helper.rb   |    4 +-
 server/public/javascripts/application.js           |   12 +++
 server/server.rb                                   |   76 ++++++++++++++++++++
 server/views/instances/new.html.haml               |    8 ++
 server/views/load_balancers/index.html.haml        |   33 +++++++++
 server/views/load_balancers/index.xml.haml         |    5 ++
 server/views/load_balancers/new.html.haml          |   38 ++++++++++
 server/views/load_balancers/show.html.haml         |   37 ++++++++++
 server/views/load_balancers/show.xml.haml          |   21 ++++++
 10 files changed, 240 insertions(+), 2 deletions(-)
 create mode 100644 server/views/load_balancers/index.html.haml
 create mode 100644 server/views/load_balancers/index.xml.haml
 create mode 100644 server/views/load_balancers/new.html.haml
 create mode 100644 server/views/load_balancers/show.html.haml
 create mode 100644 server/views/load_balancers/show.xml.haml

diff --git a/server/lib/deltacloud/base_driver/features.rb 
b/server/lib/deltacloud/base_driver/features.rb
index 3b19ca2..2d79f4b 100644
--- a/server/lib/deltacloud/base_driver/features.rb
+++ b/server/lib/deltacloud/base_driver/features.rb
@@ -169,5 +169,13 @@ module Deltacloud
         param :location, :string, :optional
       end
     end
+
+    declare_feature :instances, :register_to_load_balancer do
+      description "Register instance to load balancer"
+      operation :create do
+        param :load_balancer_id, :string, :optional
+      end
+    end
+
   end
 end
diff --git a/server/lib/deltacloud/helpers/application_helper.rb 
b/server/lib/deltacloud/helpers/application_helper.rb
index 189148c..00e8bc9 100644
--- a/server/lib/deltacloud/helpers/application_helper.rb
+++ b/server/lib/deltacloud/helpers/application_helper.rb
@@ -41,8 +41,8 @@ module ApplicationHelper
     collections[:instances].operations[action.to_sym].method
   end

-  def driver_has_feature?(feature_name)
-    not driver.features(:instances).select{ |f| f.name.eql?(feature_name) 
}.empty?
+  def driver_has_feature?(feature_name, collection_name = :instances)
+    not driver.features(collection_name).select{ |f| f.name.eql?(feature_name) 
}.empty?
   end

   def driver_has_auth_features?
diff --git a/server/public/javascripts/application.js 
b/server/public/javascripts/application.js
index 80e1d1c..509768b 100644
--- a/server/public/javascripts/application.js
+++ b/server/public/javascripts/application.js
@@ -29,4 +29,16 @@ $(document).ready(function() {
     return false;
   })

+  if ($('select#list_instances').length) {
+    $('select#list_instances').html("<option>Loading instances...</option>");
+    $.getJSON("/api/instances?state=RUNNING&format=json",
+      function(data){
+        $('select#list_instances').empty();
+        $.each(data.instances, function(i,item){
+          $('select#list_instances').append('<option 
value="'+item.id+'">'+item.id+'</option>');
+        });
+      }
+    );
+  }
+
 })
diff --git a/server/server.rb b/server/server.rb
index b8720a9..7044cd1 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -175,11 +175,87 @@ get "/api/instances/new" do
   @image   = driver.image( credentials, :id =>  params[:image_id] )
   @hardware_profiles = driver.hardware_profiles(credentials, :architecture =>  
@image.architecture )
   @realms = driver.realms(credentials)
+  if driver_has_feature?(:register_to_load_balancer)
+    @load_balancers = driver.load_balancers(credentials)
+  end
   respond_to do |format|
     format.html { haml :"instances/new" }
   end
 end

+get '/api/load_balancers/new' do
+  @realms = driver.realms(credentials)
+  @instances = driver.instances(credentials) if 
driver_has_feature?(:register_instance, :load_balancers)
+  respond_to do |format|
+    format.html { haml :"load_balancers/new" }
+  end
+end
+
+
+collection :load_balancers do
+  description "Load balancers"
+
+  operation :index do
+    description "List of all active load balancers"
+    control do
+      filter_all :load_balancers
+    end
+  end
+
+  operation :show do
+    description "Show details about given load balancer"
+    param :id,  :string,  :required
+    control { show :load_balancer }
+  end
+
+  operation :create do
+    description "Create a new load balancer"
+    param :name,  :string,  :required
+    param :realm_id,  :string,  :required
+    param :listener_protocol,  :string,  :required, ['HTTP', 'TCP']
+    param :listener_lbr_port,  :string,  :required
+    param :listener_inst_port,  :string,  :required

I would prefer more descriptive port names - maybe :listener_balancer_port, 
:listener_instance_port?

Sure, I'll rename this params.



+    control do
+      @load_balancer = driver.create_load_balancer(credentials, params)
+      respond_to do |format|
+        format.xml { haml :"load_balancers/show" }
+        format.html { haml :"load_balancers/show" }
+      end
+    end
+  end
+
+  operation :register, :method =>  :post, :member =>  true do
+    description "Add instance to loadbalancer"
+    param :id,  :string,  :required
+    param :instance_id, :string,  :required
+    control do
+      driver.lb_register_instance(credentials, params)
+      redirect(load_balancer_url(params[:id]))
+    end
+  end
+
+  operation :unregister, :method =>  :post, :member =>  true do
+    description "Remove instance from loadbalancer"
+    param :id,  :string,  :required
+    param :instance_id, :string,  :required
+    control do
+      driver.lb_unregister_instance(credentials, params)
+      redirect(load_balancer_url(params[:id]))
+    end
+  end
+
+  operation :destroy do
+    description "Destroy given load balancer"
+    param :id,  :string,  :required
+    control do
+      driver.destroy_load_balancer(credentials, params[:id])
+      redirect(load_balancers_url)
+    end
+  end
+
+end
+
+
 collection :instances do
   description<<END
   An instance is a concrete machine realized from an image.
diff --git a/server/views/instances/new.html.haml 
b/server/views/instances/new.html.haml
index 6d67c2d..558582d 100644
--- a/server/views/instances/new.html.haml
+++ b/server/views/instances/new.html.haml
@@ -9,6 +9,14 @@
     %label
       Instance Name:
       %input{ :name =>  'name', :size =>  30 }/
+  -if driver_has_feature?(:register_to_load_balancer)
+    %p
+      %label
+        Assign to load balancer:
+        %select{:name =>  'load_balancer_id'}
+          %option{:value =>  ""}
+          - @load_balancers.each do |load_balancer|
+            %option{:value =>  load_balancer.id} #{load_balancer.id}
   -if driver_has_feature?(:authentication_key)
     %p
       %label
diff --git a/server/views/load_balancers/index.html.haml 
b/server/views/load_balancers/index.html.haml
new file mode 100644
index 0000000..65b109c
--- /dev/null
+++ b/server/views/load_balancers/index.html.haml
@@ -0,0 +1,33 @@
+%h1 Load Balancers
+
+%table.display
+  %thead
+    %tr
+      %th ID
+      %th Hostname
+      %th Realm
+      %th Balancer port
+      %th Instances port
+      %th Actions
+  %tbody
+    - @elements.each do |balancer|
+      %tr
+        %td
+          = link_to balancer.id, load_balancer_url( balancer.id )
+        %td
+          = balancer.public_addresses.first
+        %td
+          = 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/>"
+        %td
+          - balancer.listeners.each do |listener|
+            ="#{listener.protocol}[#{listener.load_balancer_port}]<br/>"
+        %td
+          =link_to 'Destroy', destroy_load_balancer_url(balancer.id), :class 
=>  'delete'
+  %tfoot
+    %tr
+      %td{:colspan =>  6, :style =>  "text-align:right;"}
+        =link_to 'Create&raquo;', "#{url_for('/api/load_balancers/new')}", :class 
=>  'button'
+
diff --git a/server/views/load_balancers/index.xml.haml 
b/server/views/load_balancers/index.xml.haml
new file mode 100644
index 0000000..22c6911
--- /dev/null
+++ b/server/views/load_balancers/index.xml.haml
@@ -0,0 +1,5 @@
+
+!!!XML
+%load_balancers
+  - @elements.each do |c|
+    = haml :'load_balancers/show', :locals =>  { :@load_balancer =>  c, :partial 
=>  true }
diff --git a/server/views/load_balancers/new.html.haml 
b/server/views/load_balancers/new.html.haml
new file mode 100644
index 0000000..561caa4
--- /dev/null
+++ b/server/views/load_balancers/new.html.haml
@@ -0,0 +1,38 @@
+%h1 New Load Balancer
+
+%form{ :action =>  '/api/load_balancers', :method =>  :post }
+  %p
+    %label
+      Name:
+    %input{ :name =>  'name', :size =>  30 }/
+  -if @instances
+    %p
+      %label
+        Running instance:
+      %select{ :name =>  'instance_id'}
+        - @instances.select{|i| i.state=="RUNNING"}.each do |instance|
+          %option{ :value =>  instance.id } #{instance.id}
+  %p
+    %label
+      Realm:
+    %select{ :name =>  'realm_id'}
+      - @realms.each do |realm|
+        %option{ :value =>  realm.id } #{realm.id} - #{realm.name}
+  %hr
+  %p
+    %label
+      Protocol:
+    %select{ :name =>  'listener_protocol'}
+      %option{ :value =>  'HTTP'}  HTTP
+      %option{ :value =>  'TCP'} TCP
+  %p
+    %label
+      Load balancer port:
+    %input{ :name =>  "listener_lbr_port", :size =>  30}
+  %p
+    %label
+      Instances port:
+    %input{ :name =>  "listener_inst_port", :size =>  30}
+  %p
+    %input{ :type =>  :submit, :name =>  "commit", :value =>  "create" }/
+
diff --git a/server/views/load_balancers/show.html.haml 
b/server/views/load_balancers/show.html.haml
new file mode 100644
index 0000000..7b6f40b
--- /dev/null
+++ b/server/views/load_balancers/show.html.haml
@@ -0,0 +1,37 @@
+%h1
+  = @load_balancer.id
+
+%dl
+  %di
+    %dt Public addresses
+    %dd
+      = @load_balancer.public_addresses.join(',')
+    - if @load_balancer.created_at
+      %dt Created at
+      %dd
+        = @load_balancer.created_at
+    %dt Realms
+    %dd
+      = @load_balancer.realms.collect { |r| "#{r.id} - #{r.name}" }.join(',')
+    %dt Listeners
+    %dd
+      - @load_balancer.listeners.each do |listener|
+        ="Load balancer port: #{listener.load_balancer_port}"
+        %br
+        ="Instance port: #{listener.instance_port}"
+        %br
+    - if @load_balancer.instances.class.eql?(Array)
+      %dt Instances
+      - @load_balancer.instances.each do |inst|
+        %dd
+          =inst.id
+          %a{:class =>  :post, :href =>  
unregister_load_balancer_url(@load_balancer.id, :instance_id =>  inst.id)} Delete
+
+%form{:action =>  url_for("/api/load_balancers/#...@load_balancer.id}/register"), 
:method =>  :post}
+  %p
+    %strong Add instances to load balancer
+  %p
+    %label Instance
+    %select{:name =>  :instance_id, :id =>  "list_instances"}
+    %input{:type =>  :submit, :value =>  "Assign"}
+
diff --git a/server/views/load_balancers/show.xml.haml 
b/server/views/load_balancers/show.xml.haml
new file mode 100644
index 0000000..e36f79f
--- /dev/null
+++ b/server/views/load_balancers/show.xml.haml
@@ -0,0 +1,21 @@
+- unless defined?(partial)
+  !!! XML
+%load_balancer{ :href =>  key_url(@load_balancer.id), :id =>  
@load_balancer.id}
+  %actions
+    %link{ :rel =>  "destroy", :method =>  "delete", :href =>  
destroy_load_balancer_url(@load_balancer.id)}
+  %public_addresses
+    - @load_balancer.public_addresses.each do |address|
+      %address #{address}
+  %created_at<
+    = @load_balancer.created_at
+  %realm{ :href =>  realm_url(@load_balancer.realms.first.id), :id =>  
@load_balancer.realms.first.id}
+  %listeners
+    - @load_balancer.listeners.each do |listener|
+      %listener{ :protocol =>  listener.protocol}
+        %load_balancer_port #{listener.load_balancer_port}
+        %instance_port #{listener.instance_port}
+  %instances
+    - @load_balancer.instances.each do |instance|
+      %instance{:href =>  instance_url(instance.id), :id =>  instance.id}
+        %link{:rel =>  "unregister", :href =>  
unregister_load_balancer_url(@load_balancer.id, { :instance_id =>  instance.id})}
+


--
--------------------------------------------------------
Michal Fojtik, [email protected]
Deltacloud API: http://deltacloud.org
--------------------------------------------------------

Reply via email to