Class: CloudLB::Balancer
- Inherits:
-
Object
- Object
- CloudLB::Balancer
- Defined in:
- lib/cloudlb/balancer.rb
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
Returns the value of attribute algorithm.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#connection_logging ⇒ Object
Returns the value of attribute connection_logging.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#port ⇒ Object
Returns the value of attribute port.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#add_to_access_list(options = {}) ⇒ Object
Adds an entry to your access list.
-
#connection_logging? ⇒ Boolean
Returns either true or false if connection logging is enabled for this load balancer.
-
#create_node(options = {}) ⇒ Object
Creates a brand new backend node and associates it with the current load balancer.
-
#create_nodes(nodes) ⇒ Object
Add multiple nodes to a load balancer.
-
#delete_access_list ⇒ Object
Deletes the entire access list for this load balancer, removing all entries.
-
#delete_access_list_member(id) ⇒ Object
Deletes one member of the access list by id.
-
#destroy! ⇒ Object
Deletes the current load balancer object.
-
#destroy_nodes(nodes) ⇒ Object
Remove nodes from a load balancer.
- #get_connection_throttle ⇒ Object (also: #connection_throttle)
- #get_health_monitor ⇒ Object (also: #health_monitor)
-
#get_node(id) ⇒ Object
(also: #node)
Returns a CloudLB::Node object for the given node id.
-
#initialize(connection, id) ⇒ Balancer
constructor
Creates a new CloudLB::Balancer object representing a Load Balancer instance.
-
#list_access_list ⇒ Object
(also: #access_list)
Returns the current access control list for the load balancer.
-
#list_nodes ⇒ Object
(also: #nodes)
Lists the backend nodes that this Balancer sends traffic to.
-
#list_virtual_ips ⇒ Object
(also: #virtual_ips)
Lists the virtual IP addresses associated with this Balancer.
-
#populate ⇒ Object
(also: #refresh)
Updates the information about the current Balancer object by making an API call.
-
#session_persistence=(value) ⇒ Object
Allows toggling of HTTP cookie session persistence.
-
#session_persistence? ⇒ Boolean
Checks to see whether or not the load balancer is using HTTP cookie session persistence.
-
#usage ⇒ Object
TODO: Figure out formats for startTime and endTime.
Constructor Details
#initialize(connection, id) ⇒ Balancer
Creates a new CloudLB::Balancer object representing a Load Balancer instance.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cloudlb/balancer.rb', line 14 def initialize(connection,id) @connection = connection @id = id @lbmgmthost = connection.lbmgmthost @lbmgmtpath = connection.lbmgmtpath @lbmgmtport = connection.lbmgmtport @lbmgmtscheme = connection.lbmgmtscheme populate return self end |
Instance Attribute Details
#algorithm ⇒ Object
Returns the value of attribute algorithm.
9 10 11 |
# File 'lib/cloudlb/balancer.rb', line 9 def algorithm @algorithm end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/cloudlb/balancer.rb', line 8 def connection @connection end |
#connection_logging ⇒ Object
Returns the value of attribute connection_logging.
10 11 12 |
# File 'lib/cloudlb/balancer.rb', line 10 def connection_logging @connection_logging end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/cloudlb/balancer.rb', line 4 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/cloudlb/balancer.rb', line 5 def name @name end |
#port ⇒ Object
Returns the value of attribute port.
7 8 9 |
# File 'lib/cloudlb/balancer.rb', line 7 def port @port end |
#protocol ⇒ Object
Returns the value of attribute protocol.
6 7 8 |
# File 'lib/cloudlb/balancer.rb', line 6 def protocol @protocol end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
11 12 13 |
# File 'lib/cloudlb/balancer.rb', line 11 def status @status end |
Instance Method Details
#add_to_access_list(options = {}) ⇒ Object
Adds an entry to your access list. You must supply the :address (an IP address) and :type (either ALLOW or DENY). Items that are configured with the ALLOW type will always take precedence over items with the DENY type.
>> balancer.add_to_access_list(:address => '192.168.5.99', :type => 'DENY')
=> true
>> b.list_access_list
=> [{"address"=>"192.168.5.99", "id"=>96, "type"=>"DENY"}]
230 231 232 233 234 235 236 |
# File 'lib/cloudlb/balancer.rb', line 230 def add_to_access_list(={}) (raise CloudLB::Exception::MissingArgument, "Must supply address and type") unless ([:address] && [:type]) body = {'accessList' => [ { :address => [:address], :type => [:type].upcase}]}.to_json response = @connection.lbreq("POST",@lbmgmthost,"#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/accesslist",@lbmgmtport,@lbmgmtscheme,{},body) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) true end |
#connection_logging? ⇒ Boolean
Returns either true or false if connection logging is enabled for this load balancer.
>> balancer.connection_logging?
=> false
143 144 145 146 147 |
# File 'lib/cloudlb/balancer.rb', line 143 def connection_logging? response = @connection.lbreq("GET",@lbmgmthost,"#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/connectionlogging",@lbmgmtport,@lbmgmtscheme,{}) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) JSON.parse(response.body)["connectionLogging"]["enabled"] end |
#create_node(options = {}) ⇒ Object
Creates a brand new backend node and associates it with the current load balancer. Returns the new Node object.
Options include:
* :address - The IP address of the backend node *required*
* :port - The TCP port that the backend node listens on. *required*
* :condition - Can be "ENABLED" (default), "DISABLED", or "DRAINING"
* :weight - A weighting for the WEIGHTED_ balancing algorithms. Defaults to 1.
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cloudlb/balancer.rb', line 76 def create_node(={}) (raise CloudLB::Exception::MissingArgument, "Must provide a node IP address") if [:address].to_s.empty? (raise CloudLB::Exception::MissingArgument, "Must provide a node TCP port") if [:port].to_s.empty? [:condition] ||= "ENABLED" body = {:nodes => []}.to_json response = @connection.lbreq("POST", @lbmgmthost, "#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/nodes",@lbmgmtport,@lbmgmtscheme,{},body) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) body = JSON.parse(response.body)['nodes'][0] return get_node(body["id"]) end |
#create_nodes(nodes) ⇒ Object
Add multiple nodes to a load balancer. Returns the new Node objects.
Use this if you’re adding multiple nodes to a load balancer, as adding subsequent nodes while the first one is queued may fail.
nodes
is an array of options given to #create_node.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cloudlb/balancer.rb', line 94 def create_nodes(nodes) nodes.each do |node| (raise CloudLB::Exception::MissingArgument, "Must provide :address for all nodes") if node[:address].to_s.empty? (raise CloudLB::Exception::MissingArgument, "Must provide :port for all nodes") if node[:port].to_s.empty? node[:condition] ||= "ENABLED" end body = {:nodes => nodes}.to_json response = @connection.lbreq("POST", @lbmgmthost, "#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/nodes",@lbmgmtport,@lbmgmtscheme,{},body) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) JSON.parse(response.body)['nodes'].map { |node| get_node(node["id"]) } end |
#delete_access_list ⇒ Object
Deletes the entire access list for this load balancer, removing all entries. Returns true if successful, raises an exception otherwise.
>> balancer.delete_access_list
=> true
243 244 245 246 247 |
# File 'lib/cloudlb/balancer.rb', line 243 def delete_access_list response = @connection.lbreq("DELETE",@lbmgmthost,"#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/accesslist",@lbmgmtport,@lbmgmtscheme,{}) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) true end |
#delete_access_list_member(id) ⇒ Object
Deletes one member of the access list by id.
>> balancer.delete_access_list_member(95)
=> true
253 254 255 256 257 |
# File 'lib/cloudlb/balancer.rb', line 253 def delete_access_list_member(id) response = @connection.lbreq("DELETE",@lbmgmthost,"#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/accesslist/#{CloudLB.escape(id.to_s)}",@lbmgmtport,@lbmgmtscheme,{}) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) true end |
#destroy! ⇒ Object
Deletes the current load balancer object. Returns true if successful, raises an exception otherwise.
126 127 128 129 130 |
# File 'lib/cloudlb/balancer.rb', line 126 def destroy! response = @connection.lbreq("DELETE", @lbmgmthost, "#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}",@lbmgmtport,@lbmgmtscheme) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^202$/) true end |
#destroy_nodes(nodes) ⇒ Object
Remove nodes from a load balancer. Returns true if successful, raises an exception otherwise.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/cloudlb/balancer.rb', line 108 def destroy_nodes(nodes) ids = nodes.map do |node| case node when Node node.id when Hash node[:id] || node['id'] else node end end ids_query = ids.map { |id| "id=#{id}" }.join('&') response = @connection.lbreq("DELETE", @lbmgmthost, "#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/nodes?#{ids_query}",@lbmgmtport,@lbmgmtscheme) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^202$/) true end |
#get_connection_throttle ⇒ Object Also known as: connection_throttle
264 265 266 |
# File 'lib/cloudlb/balancer.rb', line 264 def get_connection_throttle CloudLB::ConnectionThrottle.new(self) end |
#get_health_monitor ⇒ Object Also known as: health_monitor
259 260 261 |
# File 'lib/cloudlb/balancer.rb', line 259 def get_health_monitor CloudLB::HealthMonitor.new(self) end |
#get_node(id) ⇒ Object Also known as: node
Returns a CloudLB::Node object for the given node id.
64 65 66 |
# File 'lib/cloudlb/balancer.rb', line 64 def get_node(id) CloudLB::Node.new(self,id) end |
#list_access_list ⇒ Object Also known as: access_list
Returns the current access control list for the load balancer.
216 217 218 219 220 |
# File 'lib/cloudlb/balancer.rb', line 216 def list_access_list response = @connection.lbreq("GET",@lbmgmthost,"#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/accesslist",@lbmgmtport,@lbmgmtscheme,{}) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) JSON.parse(response.body)["accessList"] end |
#list_nodes ⇒ Object Also known as: nodes
Lists the backend nodes that this Balancer sends traffic to.
>> b.list_nodes
=> [{:status=>"ONLINE", :port=>80, :address=>"173.203.218.1", :condition=>"ENABLED", :id=>1046}]
56 57 58 59 60 |
# File 'lib/cloudlb/balancer.rb', line 56 def list_nodes response = @connection.lbreq("GET", @lbmgmthost, "#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/nodes",@lbmgmtport,@lbmgmtscheme) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) CloudLB.symbolize_keys(JSON.parse(response.body)["nodes"]) end |
#list_virtual_ips ⇒ Object Also known as: virtual_ips
Lists the virtual IP addresses associated with this Balancer
>> b.list_virtual_ips
=> [{:type=>"PUBLIC", :address=>"174.143.139.191", :ipVersion=>"IPV4", :id=>38}]
45 46 47 48 49 |
# File 'lib/cloudlb/balancer.rb', line 45 def list_virtual_ips response = @connection.lbreq("GET", @lbmgmthost, "#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/virtualips",@lbmgmtport,@lbmgmtscheme) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) CloudLB.symbolize_keys(JSON.parse(response.body)["virtualIps"]) end |
#populate ⇒ Object Also known as: refresh
Updates the information about the current Balancer object by making an API call.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cloudlb/balancer.rb', line 26 def populate response = @connection.lbreq("GET",@lbmgmthost,"#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}",@lbmgmtport,@lbmgmtscheme) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) data = JSON.parse(response.body)['loadBalancer'] @id = data["id"] @name = data["name"] @protocol = data["protocol"] @port = data["port"] @algorithm = data["algorithm"] @connection_logging = data["connectionLogging"]["enabled"] @status = data["status"] true end |
#session_persistence=(value) ⇒ Object
Allows toggling of HTTP cookie session persistence. Valid values are true and false to enable or disable, respectively. FIXME - Trying to set the persistence to true is currently returning an undocumented 405 error.
203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/cloudlb/balancer.rb', line 203 def session_persistence=(value) (raise CloudLB::Exception::MissingArgument, "value must be true or false") unless [true,false].include?(value) if value == true body = {'sessionPersistence' => {'persistenceType' => 'HTTP_COOKIE'}} response = @connection.lbreq("POST", @lbmgmthost, "#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/sessionpersistence",@lbmgmtport,@lbmgmtscheme,{},body.to_json) elsif value == false response = @connection.lbreq("DELETE", @lbmgmthost, "#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/sessionpersistence",@lbmgmtport,@lbmgmtscheme) end CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) true end |
#session_persistence? ⇒ Boolean
Checks to see whether or not the load balancer is using HTTP cookie session persistence. Returns true if it is, false otherwise.
195 196 197 198 199 |
# File 'lib/cloudlb/balancer.rb', line 195 def session_persistence? response = @connection.lbreq("GET",@lbmgmthost,"#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/sessionpersistence",@lbmgmtport,@lbmgmtscheme,{}) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) JSON.parse(response.body)["sessionPersistence"]["persistenceType"] == "HTTP_COOKIE" ? true : false end |
#usage ⇒ Object
TODO: Figure out formats for startTime and endTime
133 134 135 136 137 |
# File 'lib/cloudlb/balancer.rb', line 133 def usage response = @connection.lbreq("GET",@lbmgmthost,"#{@lbmgmtpath}/loadbalancers/#{CloudLB.escape(@id.to_s)}/usage",@lbmgmtport,@lbmgmtscheme,{}) CloudLB::Exception.raise_exception(response) unless response.code.to_s.match(/^20.$/) CloudLB.symbolize_keys(JSON.parse(response.body)["loadBalancerUsageRecords"]) end |