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, has_many, #initialize, #reload

Constructor Details

This class inherits a constructor from Profitbricks::Model

Class Method Details

+ (LoadBalancer) create(options = {})

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:



100
101
102
103
104
# File 'lib/profitbricks/load_balancer.rb', line 100

def create(options = {})
  options[:server_ids] = options.delete(:servers).collect(&:id) if options[:servers]
  response = Profitbricks.request :create_load_balancer, options
  self.find(:id => response[:load_balancer_id])
end

+ (LoadBalancer) find(options = {})

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:



111
112
113
114
115
# File 'lib/profitbricks/load_balancer.rb', line 111

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

Instance Method Details

- (Boolean) activate_servers(servers)

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



57
58
59
60
61
62
# File 'lib/profitbricks/load_balancer.rb', line 57

def activate_servers(servers)
  raise "You have to provide at least one server" unless servers
  options = {load_balancer_id: self.id, server_ids: servers.collect(&:id)}
  response = Profitbricks.request :activate_load_balancing_on_servers, options
  return true
end

- (Boolean) deactivate_servers(servers)

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



68
69
70
71
72
73
# File 'lib/profitbricks/load_balancer.rb', line 68

def deactivate_servers(servers)
  raise "You have to provide at least one server" unless servers
  options = {load_balancer_id: self.id, server_ids: servers.collect(&:id)}
  response = Profitbricks.request :deactivate_load_balancing_on_servers, options
  return true
end

- (Boolean) delete

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



80
81
82
83
# File 'lib/profitbricks/load_balancer.rb', line 80

def delete
  Profitbricks.request :delete_load_balancer, load_balancer_id: self.id
  return true
end

- (Boolean) deregister_servers(servers)

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



46
47
48
49
50
51
# File 'lib/profitbricks/load_balancer.rb', line 46

def deregister_servers(servers)
  raise "You have to provide at least one server" unless servers
  options = {load_balancer_id: self.id, server_ids: servers.collect(&:id)}
  response = Profitbricks.request :deregister_servers_on_load_balancer, options
  return true
end

- (Boolean) register_servers(servers)

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



30
31
32
33
34
35
36
# File 'lib/profitbricks/load_balancer.rb', line 30

def register_servers(servers)
  raise "You have to provide at least one server" unless servers
  options = {load_balancer_id: self.id, server_ids: servers.collect(&:id)}
  response = Profitbricks.request :register_servers_on_load_balancer, options
  self.reload
  return true
end

- (Boolean) update(options = {})

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
# File 'lib/profitbricks/load_balancer.rb', line 13

def update(options = {})
  options.merge!(:load_balancer_id=> self.id)
  options[:load_balancer_name] = options.delete :name
  response = Profitbricks.request :update_load_balancer, options
  self.reload
  return true
end