Class: CloudLB::Balancer

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudlb/balancer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#algorithmObject

Returns the value of attribute algorithm.



9
10
11
# File 'lib/cloudlb/balancer.rb', line 9

def algorithm
  @algorithm
end

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/cloudlb/balancer.rb', line 8

def connection
  @connection
end

#connection_loggingObject

Returns the value of attribute connection_logging.



10
11
12
# File 'lib/cloudlb/balancer.rb', line 10

def connection_logging
  @connection_logging
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/cloudlb/balancer.rb', line 4

def id
  @id
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/cloudlb/balancer.rb', line 5

def name
  @name
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/cloudlb/balancer.rb', line 7

def port
  @port
end

#protocolObject

Returns the value of attribute protocol.



6
7
8
# File 'lib/cloudlb/balancer.rb', line 6

def protocol
  @protocol
end

#statusObject (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"}]


192
193
194
195
196
197
198
# File 'lib/cloudlb/balancer.rb', line 192

def add_to_access_list(options={})
  (raise CloudLB::Exception::MissingArgument, "Must supply address and type") unless (options[:address] && options[:type])
  body = {'accessList' => [ { :address => options[:address], :type => options[: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

Returns:

  • (Boolean)


105
106
107
108
109
# File 'lib/cloudlb/balancer.rb', line 105

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(options={})
  (raise CloudLB::Exception::MissingArgument, "Must provide a node IP address") if options[:address].to_s.empty?
  (raise CloudLB::Exception::MissingArgument, "Must provide a node TCP port") if options[:port].to_s.empty?
  options[:condition] ||= "ENABLED"
  body = {:nodes => [options]}.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

#delete_access_listObject

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


205
206
207
208
209
# File 'lib/cloudlb/balancer.rb', line 205

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


215
216
217
218
219
# File 'lib/cloudlb/balancer.rb', line 215

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.



88
89
90
91
92
# File 'lib/cloudlb/balancer.rb', line 88

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

#get_connection_throttleObject Also known as: connection_throttle



226
227
228
# File 'lib/cloudlb/balancer.rb', line 226

def get_connection_throttle
  CloudLB::ConnectionThrottle.new(self)
end

#get_health_monitorObject Also known as: health_monitor



221
222
223
# File 'lib/cloudlb/balancer.rb', line 221

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_listObject Also known as: access_list

Returns the current access control list for the load balancer.



178
179
180
181
182
# File 'lib/cloudlb/balancer.rb', line 178

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_nodesObject 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_ipsObject 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

#populateObject 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.



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/cloudlb/balancer.rb', line 165

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.

Returns:

  • (Boolean)


157
158
159
160
161
# File 'lib/cloudlb/balancer.rb', line 157

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

#usageObject

TODO: Figure out formats for startTime and endTime



95
96
97
98
99
# File 'lib/cloudlb/balancer.rb', line 95

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