Class: Profitbricks::LoadBalancer

Inherits:
Model
  • Object
show all
Defined in:
lib/profitbricks/load_balancer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#attributes, belongs_to, get_xml_and_update_attributes, #get_xml_and_update_attributes, has_many, #initialize, #reload

Constructor Details

This class inherits a constructor from Profitbricks::Model

Class Method Details

.create(options = {}) ⇒ LoadBalancer

Creates a virtual Load Balancer within an existing virtual data center.

A Load Balancer connected to a LAN will not distribute traffic to any server, until it is specified to do so. In the current version, a Load Balancer cannot distribute traffic across multiple data centers or LANs. Load Balancer and servers must always be in the same LAN.

Parameters:

  • options (Hash) (defaults to: {})

    parameters for the new NIC

Options Hash (options):

  • :data_center_id (String)

    data center ID wherein the load balancer is to be created (required)

  • :name (String)

    Name of the load balancer to be created

  • :algorithm (String)

    load balancer algorithm. ‘ROUND_ROBIN’ is default and the only supported algorithm at the moment

  • :ip (String)

    A DHCP IP adress is being assigned to the load balancer automatically by ProfitBricks. A private IP can be defined by the user. Additional, public IPs can be reserved and assigned to the load balancer manually.

  • :lan_id (Fixnum)

    Identifier of the target LAN ID > 0 If the specified LAN ID does not exist or if LAN ID is not specified, a new LAN with the given ID / with a next available ID starting from 1 will be created respectively

  • :servers (Array<Server>)

    Array of servers to connect to the LoadBalancer

Returns:



106
107
108
109
110
111
112
113
# File 'lib/profitbricks/load_balancer.rb', line 106

def create(options = {})
  options[:server_ids] = options[:servers].collect { |s| s.id }.join(',')
  xml = "<arg0>"
  xml += get_xml_and_update_attributes options, [:data_center_id, :lan_id, :ip, :name, :algorithm, :server_ids]
  xml += "</arg0>"
  response = Profitbricks.request :create_load_balancer, xml
  self.find(:id => response.to_hash[:create_load_balancer_response][:return][:load_balancer_id])
end

.find(options = {}) ⇒ LoadBalancer

Returns information about a virtual load balancer.

Parameters:

  • options (Hash) (defaults to: {})

    currently just :id is supported

Options Hash (options):

  • :id (String)

    The id of the load balancer to locate

Returns:



120
121
122
123
124
# File 'lib/profitbricks/load_balancer.rb', line 120

def find(options = {})
  raise "Unable to locate the LoadBalancer named '#{options[:name]}'" unless options[:id]
  response = Profitbricks.request :get_load_balancer, "<loadBalancerId>#{options[:id]}</loadBalancerId>"
  PB::LoadBalancer.new(response.to_hash[:get_load_balancer_response][:return])
end

Instance Method Details

#activate_servers(servers) ⇒ Boolean

Enables the load balancer to distribute traffic to the specified servers.

Parameters:

  • [Array<Server>] (Hash)

    a customizable set of options

Returns:

  • (Boolean)

    true on success, false otherwise



61
62
63
64
65
66
67
# File 'lib/profitbricks/load_balancer.rb', line 61

def activate_servers(servers)
  raise "You have to provide at least one server" unless servers
  options = {:server_ids => servers.collect { |s| s.id }.join(','), :load_balancer_id=> self.id}
  xml = get_xml_and_update_attributes options, [:server_ids, :load_balancer_id]
  response = Profitbricks.request :activate_load_balancing_on_servers, xml
  return true if response.to_hash[:activate_load_balancing_on_servers_response][:return]
end

#deactivate_servers(servers) ⇒ Boolean

Disables the load balancer to distribute traffic to the specified servers.

Parameters:

  • [Array<Server>] (Hash)

    a customizable set of options

Returns:

  • (Boolean)

    true on success, false otherwise



73
74
75
76
77
78
79
# File 'lib/profitbricks/load_balancer.rb', line 73

def deactivate_servers(servers)
  raise "You have to provide at least one server" unless servers
  options = {:server_ids => servers.collect { |s| s.id }.join(','), :load_balancer_id=> self.id}
  xml = get_xml_and_update_attributes options, [:server_ids, :load_balancer_id]
  response = Profitbricks.request :deactivate_load_balancing_on_servers, xml
  return true if response.to_hash[:deactivate_load_balancing_on_servers_response][:return]
end

#deleteBoolean

Deletes an existing load balancer. Primary IP addresses of the server’s previously balanced NICs are reset and replaced with ProfitBricks assigned IP address. If a load balancer has been deleted, all servers will still be connected to the same LAN though.

Returns:

  • (Boolean)

    true on success, false otherwise



86
87
88
89
# File 'lib/profitbricks/load_balancer.rb', line 86

def delete
  response = Profitbricks.request :delete_load_balancer, "<loadBalancerId>#{self.id}</loadBalancerId>"
  return true if response.to_hash[:delete_load_balancer_response][:return]
end

#deregister_servers(servers) ⇒ Boolean

Removes servers from the load balancer

By deregistering a server, the server is being removed from the load balancer but still remains connected to the same LAN. The primary IP address of the NIC, through which the load balancer distributed traffic to the server before, is reset and replaced by a ProfitBricks assigned IP address.

Parameters:

  • [Array<Server>] (Hash)

    a customizable set of options

Returns:

  • (Boolean)

    true on success, false otherwise



49
50
51
52
53
54
55
# File 'lib/profitbricks/load_balancer.rb', line 49

def deregister_servers(servers)
  raise "You have to provide at least one server" unless servers
  options = {:server_ids => servers.collect { |s| s.id }.join(','), :load_balancer_id=> self.id}
  xml = get_xml_and_update_attributes options, [:server_ids, :load_balancer_id]
  response = Profitbricks.request :deregister_servers_on_load_balancer, xml
  return true if response.to_hash[:deregister_servers_on_load_balancer_response][:return]
end

#register_servers(servers) ⇒ Boolean

Adds new servers to the Load Balancer within the respective LAN.

If the server is not yet a member of the LAN, a new NIC is created, connected to the LAN and registered with the Load Balancer. The load balancer will distribute traffic to the server through this balanced NIC. If the server is already a member of the LAN, the appropriate NIC is used as balanced NIC. A server can be registered to more than one Load Balancer.

Parameters:

  • [Array<Server>] (Hash)

    a customizable set of options

Returns:

  • (Boolean)

    true on success, false otherwise



32
33
34
35
36
37
38
39
# File 'lib/profitbricks/load_balancer.rb', line 32

def register_servers(servers)
  raise "You have to provide at least one server" unless servers
  options = {:server_ids => servers.collect { |s| s.id }.join(','), :load_balancer_id=> self.id}
  xml = get_xml_and_update_attributes options, [:server_ids, :load_balancer_id]
  response = Profitbricks.request :register_servers_on_load_balancer, xml
  update_attributes(response.to_hash[:register_servers_on_load_balancer_response][:return])
  return true if response.to_hash[:register_servers_on_load_balancer_response][:return]
end

#update(options = {}) ⇒ Boolean

Changes the settings of an existing virtual load balancer.

Parameters:

  • options (Hash) (defaults to: {})

    parameters for the new NIC

Options Hash (options):

  • :name (String)

    Name of the load balancer to be created

  • :algorithm (String)

    load balancer algorithm. ‘ROUND_ROBIN’ is default and the only supported algorithm at the moment

  • :ip (String)

    Updates the IP address of the load balancer with the speficied IP. All servers connected to the load balancer will have their primary IP address updated with the same IP address of the load balancer implicitly Additional customer reserved IP addresses, which have been added to the server’s NIC, remain unchanged Set ip to empty, to reset the IP of load balancer with a Profitbricks assigned IP address.

Returns:

  • (Boolean)

    true on success, false otherwise



13
14
15
16
17
18
19
20
21
# File 'lib/profitbricks/load_balancer.rb', line 13

def update(options = {})
  options.merge!(:load_balancer_id=> self.id)
  xml = "<arg0>"
  xml += get_xml_and_update_attributes options, [:load_balancer_id, :ip, :name, :algorithm]
  xml += "</arg0>"
  response = Profitbricks.request :update_load_balancer, xml
  self.reload
  return true if response.to_hash[:update_load_balancer_response][:return]
end