deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [2/4] git commit: Client: Added support for load balancers (DTACLOUD-543)
Date Tue, 16 Apr 2013 21:01:06 GMT
Client: Added support for load balancers (DTACLOUD-543)


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/7e756893
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/7e756893
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/7e756893

Branch: refs/heads/master
Commit: 7e756893e5a841ff89d6195c4af1f60e624599e2
Parents: d358f63
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Tue Apr 16 11:21:09 2013 +0200
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Tue Apr 16 11:21:31 2013 +0200

----------------------------------------------------------------------
 client/lib/deltacloud/client/connection.rb         |    1 +
 client/lib/deltacloud/client/methods.rb            |    1 +
 .../lib/deltacloud/client/methods/load_balancer.rb |   96 +++++++++++++++
 client/lib/deltacloud/client/models.rb             |    1 +
 client/lib/deltacloud/client/models/instance.rb    |    9 ++
 .../lib/deltacloud/client/models/load_balancer.rb  |   55 ++++++++
 6 files changed, 163 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/7e756893/client/lib/deltacloud/client/connection.rb
----------------------------------------------------------------------
diff --git a/client/lib/deltacloud/client/connection.rb b/client/lib/deltacloud/client/connection.rb
index a60b10a..5c98afd 100644
--- a/client/lib/deltacloud/client/connection.rb
+++ b/client/lib/deltacloud/client/connection.rb
@@ -39,6 +39,7 @@ module Deltacloud::Client
     include Deltacloud::Client::Methods::Realm
     include Deltacloud::Client::Methods::StorageSnapshot
     include Deltacloud::Client::Methods::StorageVolume
+    include Deltacloud::Client::Methods::LoadBalancer
 
     def initialize(opts={})
       @request_driver = opts[:driver]

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/7e756893/client/lib/deltacloud/client/methods.rb
----------------------------------------------------------------------
diff --git a/client/lib/deltacloud/client/methods.rb b/client/lib/deltacloud/client/methods.rb
index 9c81ae3..21df6dc 100644
--- a/client/lib/deltacloud/client/methods.rb
+++ b/client/lib/deltacloud/client/methods.rb
@@ -29,3 +29,4 @@ require_relative './methods/key'
 require_relative './methods/realm'
 require_relative './methods/storage_volume'
 require_relative './methods/storage_snapshot'
+require_relative './methods/load_balancer'

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/7e756893/client/lib/deltacloud/client/methods/load_balancer.rb
----------------------------------------------------------------------
diff --git a/client/lib/deltacloud/client/methods/load_balancer.rb b/client/lib/deltacloud/client/methods/load_balancer.rb
new file mode 100644
index 0000000..d4c64f0
--- /dev/null
+++ b/client/lib/deltacloud/client/methods/load_balancer.rb
@@ -0,0 +1,96 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+module Deltacloud::Client
+  module Methods
+    module LoadBalancer
+
+      # Retrieve list of all load_balancer entities
+      #
+      # Filter options:
+      #
+      # - :id -> Filter entities using 'id' attribute
+      #
+      def load_balancers(filter_opts={})
+        from_collection :load_balancers,
+        connection.get(api_uri('load_balancers'), filter_opts)
+      end
+
+      # Retrieve the single load_balancer entity
+      #
+      # - load_balancer_id -> LoadBalancer entity to retrieve
+      #
+      def load_balancer(load_balancer_id)
+        from_resource :load_balancer,
+          connection.get(api_uri("load_balancers/#{load_balancer_id}"))
+      end
+
+      # Destroy load balancer
+      # Returns 'true' if the response was 204 No Content
+      #
+      # - load_balancer_id -> The 'id' of the LoadBalancer to destroy
+      #
+      def destroy_load_balancer(load_balancer_id)
+        destroy_resource :load_balancer, load_balancer_id
+      end
+
+      # Create a new load_balancer
+      #
+      # - create_opts
+      # :name               - Load Balancer name
+      # :realm_id           - Load Balancer realm id
+      # :listener_protocol  - Protocol to use for LB listener (HTTP or TCP)
+      # :listener_balancer_port - Load Balancer port (like. 80)
+      # :listener_instance_port - Instances port (like, 8080)
+      #
+      # Example:
+      #
+      #  client.create_load_balancer(:name => 'test2', :realm_id => 'us-east-1a', :listener_protocol
=> 'HTTP', :listener_balancer_port => '80', :listener_instance_port => '8080')
+      #
+      def create_load_balancer(create_opts={})
+        must_support! :load_balancers
+        response = connection.post(api_uri('load_balancers')) do |request|
+          request.params = create_opts
+        end
+        model(:load_balancer).convert(self, response.body)
+      end
+
+      # Register an Instance to given Load Balancer
+      #
+      # load_balancer_id - Load Balancer to use
+      # instance_id      - Instance to register to load balancer
+      #
+      def register_instance(load_balancer_id, instance_id)
+        response = connection.post(api_uri("/load_balancers/#{load_balancer_id}/register"))
do |request|
+          request.params = { 'instance_id' => instance_id }
+        end
+        model(:load_balancer).convert(self, response.body)
+      end
+
+      # Unregister an Instance from given Load Balancer
+      #
+      # load_balancer_id - Load Balancer to use
+      # instance_id      - Instance to unregister from load balancer
+      #
+      def unregister_instance(load_balancer_id, instance_id)
+        response = connection.post(api_uri("/load_balancers/#{load_balancer_id}/unregister"))
do |request|
+          request.params = { 'instance_id' => instance_id }
+        end
+        model(:load_balancer).convert(self, response.body)
+      end
+
+    end
+  end
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/7e756893/client/lib/deltacloud/client/models.rb
----------------------------------------------------------------------
diff --git a/client/lib/deltacloud/client/models.rb b/client/lib/deltacloud/client/models.rb
index 0f08ec3..67005e1 100644
--- a/client/lib/deltacloud/client/models.rb
+++ b/client/lib/deltacloud/client/models.rb
@@ -28,3 +28,4 @@ require_relative './models/key'
 require_relative './models/realm'
 require_relative './models/storage_volume'
 require_relative './models/storage_snapshot'
+require_relative './models/load_balancer'

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/7e756893/client/lib/deltacloud/client/models/instance.rb
----------------------------------------------------------------------
diff --git a/client/lib/deltacloud/client/models/instance.rb b/client/lib/deltacloud/client/models/instance.rb
index 77d3498..678fd78 100644
--- a/client/lib/deltacloud/client/models/instance.rb
+++ b/client/lib/deltacloud/client/models/instance.rb
@@ -21,6 +21,7 @@ module Deltacloud::Client
     include Deltacloud::Client::Methods::Realm
     include Deltacloud::Client::Methods::HardwareProfile
     include Deltacloud::Client::Methods::Image
+    include Deltacloud::Client::Methods::LoadBalancer
 
     attr_reader :realm_id
     attr_reader :owner_id
@@ -84,6 +85,14 @@ module Deltacloud::Client
       super(_id, create_opts)
     end
 
+    def register_instance(load_balancer_id)
+      super(load_balancer_id, _id)
+    end
+
+    def unregister_instance(load_balancer_id)
+      super(load_balancer_id, _id)
+    end
+
     # Helper for is_STATE?
     #
     # is_running?

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/7e756893/client/lib/deltacloud/client/models/load_balancer.rb
----------------------------------------------------------------------
diff --git a/client/lib/deltacloud/client/models/load_balancer.rb b/client/lib/deltacloud/client/models/load_balancer.rb
new file mode 100644
index 0000000..f4b5a15
--- /dev/null
+++ b/client/lib/deltacloud/client/models/load_balancer.rb
@@ -0,0 +1,55 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+module Deltacloud::Client
+  class LoadBalancer < Base
+
+    include Deltacloud::Client::Methods::LoadBalancer
+
+    attr_reader :created_at
+    attr_reader :realm_id
+    attr_reader :public_addresses
+    attr_reader :actions
+    attr_reader :listeners
+    attr_reader :instances
+
+    # LoadBalancer model methods
+    #
+    # def reboot!
+    #   load_balancer_reboot(_id)
+    # end
+
+    # Parse the LoadBalancer entity from XML body
+    #
+    # - xml_body -> Deltacloud API XML representation of the load_balancer
+    #
+    def self.parse(xml_body)
+      {
+        :created_at => xml_body.text_at(:created_at),
+        :realm_id => xml_body.attr_at('realm', :id),
+        :actions => xml_body.xpath('actions/link').map { |a| a['rel'].to_sym },
+        :public_addresses => xml_body.xpath('public_addresses/address').map { |a| a.text.strip
},
+        :listeners => xml_body.xpath('listeners/listener').map { |l|
+          {
+            :protocol => l[:protocol],
+            :load_balancer_port => l.text_at(:load_balancer_port),
+            :instance_port => l.text_at(:instance_port)
+          }
+        },
+        :instances => xml_body.xpath('instances/instance').map { |i| i[:id] }
+      }
+    end
+  end
+end


Mime
View raw message