2011-07-10 16:35:30 -04:00
|
|
|
require 'fog/core/model'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Rackspace
|
2011-08-04 17:21:22 -04:00
|
|
|
class LoadBalancers
|
2011-07-10 16:35:30 -04:00
|
|
|
class LoadBalancer < Fog::Model
|
|
|
|
|
2011-07-28 14:37:21 -04:00
|
|
|
#States
|
|
|
|
ACTIVE = 'ACTIVE'
|
|
|
|
ERROR = 'ERROR'
|
|
|
|
PENDING_UPDATE = 'PENDING_UPDATE'
|
|
|
|
PENDING_DELTE = 'PENDING_DELETE'
|
|
|
|
SUSPENDED = 'SUSPENDED'
|
|
|
|
DELETED = 'DELETED'
|
|
|
|
BUILD = 'BUILD'
|
|
|
|
|
2011-07-10 16:35:30 -04:00
|
|
|
identity :id
|
|
|
|
|
|
|
|
attribute :cluster
|
2011-07-27 18:00:18 -04:00
|
|
|
attribute :connection_logging, :aliases => 'connectionLogging'
|
2011-07-10 16:35:30 -04:00
|
|
|
attribute :port
|
|
|
|
attribute :protocol
|
|
|
|
attribute :algorithm
|
2011-07-10 16:58:27 -04:00
|
|
|
attribute :virtual_ips, :aliases => 'virtualIps'
|
2011-07-10 16:35:30 -04:00
|
|
|
attribute :created
|
|
|
|
attribute :updated
|
|
|
|
attribute :name
|
2011-07-10 16:58:27 -04:00
|
|
|
attribute :state, :aliases => 'status'
|
2011-07-18 22:45:47 -04:00
|
|
|
attribute :nodes
|
2012-05-05 12:38:08 -04:00
|
|
|
attribute :ssl_termination, :aliases => 'ssltermination'
|
2011-07-18 22:45:47 -04:00
|
|
|
|
|
|
|
def initialize(attributes)
|
|
|
|
#HACK - Since we are hacking how sub-collections work, we have to make sure the connection is valid first.
|
|
|
|
@connection = attributes[:connection]
|
|
|
|
super
|
|
|
|
end
|
2011-07-10 16:35:30 -04:00
|
|
|
|
2011-07-28 13:21:26 -04:00
|
|
|
def access_rules
|
|
|
|
@access_rules ||= begin
|
2011-08-04 17:21:22 -04:00
|
|
|
Fog::Rackspace::LoadBalancers::AccessRules.new({
|
2011-07-28 13:21:26 -04:00
|
|
|
:connection => connection,
|
|
|
|
:load_balancer => self})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def access_rules=(new_access_rules=[])
|
|
|
|
access_rules.load(new_access_rules)
|
|
|
|
end
|
|
|
|
|
2011-07-14 15:20:53 -04:00
|
|
|
def nodes
|
2011-07-19 18:48:21 -04:00
|
|
|
@nodes ||= begin
|
2011-08-04 17:21:22 -04:00
|
|
|
Fog::Rackspace::LoadBalancers::Nodes.new({
|
2011-07-19 18:48:21 -04:00
|
|
|
:connection => connection,
|
|
|
|
:load_balancer => self})
|
|
|
|
end
|
2011-07-14 15:20:53 -04:00
|
|
|
end
|
|
|
|
|
2011-07-18 22:45:47 -04:00
|
|
|
def nodes=(new_nodes=[])
|
2011-07-19 18:48:21 -04:00
|
|
|
nodes.load(new_nodes)
|
2011-07-10 16:35:30 -04:00
|
|
|
end
|
|
|
|
|
2012-05-05 12:38:08 -04:00
|
|
|
def ssl_termination
|
|
|
|
requires :identity
|
|
|
|
ssl_termination = connection.get_ssl_termination(identity)
|
|
|
|
(ssl_termination.status == 404) ? nil : ssl_termination.body["sslTermination"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable_ssl_termination(securePort, privatekey, certificate, enabled=true, secureTrafficOnly=false)
|
|
|
|
requires :identity
|
|
|
|
connection.set_ssl_termination(identity, securePort, privatekey, certificate, enabled, secureTrafficOnly)
|
|
|
|
end
|
|
|
|
|
2011-07-18 22:45:47 -04:00
|
|
|
def virtual_ips
|
2011-07-19 18:48:21 -04:00
|
|
|
@virtual_ips ||= begin
|
2011-08-04 17:21:22 -04:00
|
|
|
Fog::Rackspace::LoadBalancers::VirtualIps.new({
|
2011-07-19 18:48:21 -04:00
|
|
|
:connection => connection,
|
|
|
|
:load_balancer => self})
|
|
|
|
end
|
2011-07-18 22:45:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def virtual_ips=(new_virtual_ips=[])
|
2011-07-19 18:48:21 -04:00
|
|
|
virtual_ips.load(new_virtual_ips)
|
2011-07-18 22:45:47 -04:00
|
|
|
end
|
|
|
|
|
2011-07-21 10:41:40 -04:00
|
|
|
def enable_connection_logging
|
2011-07-28 10:15:51 -04:00
|
|
|
requires :identity
|
2011-07-21 10:41:40 -04:00
|
|
|
connection.set_connection_logging identity, true
|
2011-07-27 18:00:18 -04:00
|
|
|
attributes[:connection_logging] = true
|
2011-07-21 10:41:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def disable_connection_logging
|
2011-07-28 10:15:51 -04:00
|
|
|
requires :identity
|
2011-07-21 10:41:40 -04:00
|
|
|
connection.set_connection_logging identity, false
|
2011-07-27 18:00:18 -04:00
|
|
|
attributes[:connection_logging] = false
|
2011-07-21 10:41:40 -04:00
|
|
|
end
|
|
|
|
|
2011-07-28 10:15:51 -04:00
|
|
|
def health_monitor
|
|
|
|
requires :identity
|
|
|
|
monitor = connection.get_monitor(identity).body['healthMonitor']
|
|
|
|
monitor.count == 0 ? nil : monitor
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable_health_monitor(type, delay, timeout, attempsBeforeDeactivation, options = {})
|
|
|
|
requires :identity
|
2011-12-08 10:46:43 -05:00
|
|
|
connection.set_monitor(identity, type, delay, timeout, attempsBeforeDeactivation, options)
|
2011-07-28 10:15:51 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable_health_monitor
|
|
|
|
requires :identity
|
|
|
|
connection.remove_monitor(identity)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def connection_throttling
|
|
|
|
requires :identity
|
|
|
|
throttle = connection.get_connection_throttling(identity).body['connectionThrottle']
|
|
|
|
throttle.count == 0 ? nil : throttle
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable_connection_throttling(max_connections, min_connections, max_connection_rate, rate_interval)
|
|
|
|
requires :identity
|
|
|
|
connection.set_connection_throttling(identity, max_connections, min_connections, max_connection_rate, rate_interval)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable_connection_throttling
|
|
|
|
requires :identity
|
|
|
|
connection.remove_connection_throttling(identity)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def session_persistence
|
|
|
|
requires :identity
|
|
|
|
persistence = connection.get_session_persistence(identity).body['sessionPersistence']
|
|
|
|
persistence.count == 0 ? nil : persistence
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable_session_persistence(type)
|
|
|
|
requires :identity
|
|
|
|
connection.set_session_persistence(identity, type)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable_session_persistence
|
|
|
|
requires :identity
|
|
|
|
connection.remove_session_persistence(identity)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2011-07-10 16:35:30 -04:00
|
|
|
def destroy
|
2011-07-14 15:20:53 -04:00
|
|
|
requires :identity
|
|
|
|
connection.delete_load_balancer(identity)
|
2011-07-10 16:35:30 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def ready?
|
2011-07-28 14:37:21 -04:00
|
|
|
state == ACTIVE
|
2011-07-10 16:35:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
if identity
|
|
|
|
update
|
|
|
|
else
|
|
|
|
create
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2011-07-28 10:15:51 -04:00
|
|
|
def usage(options = {})
|
|
|
|
requires :identity
|
|
|
|
connection.get_load_balancer_usage(identity, options).body
|
|
|
|
end
|
|
|
|
|
2011-12-16 11:40:44 -05:00
|
|
|
def error_page
|
|
|
|
requires :identity
|
|
|
|
connection.get_error_page(identity).body['errorpage']['content']
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_page=(content)
|
|
|
|
requires :identity
|
|
|
|
connection.set_error_page identity, content
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_error_page
|
|
|
|
requires :identity
|
|
|
|
connection.remove_error_page identity
|
|
|
|
end
|
|
|
|
|
2011-07-10 16:35:30 -04:00
|
|
|
private
|
|
|
|
def create
|
|
|
|
requires :name, :protocol, :port, :virtual_ips, :nodes
|
2012-03-31 16:07:27 -04:00
|
|
|
|
|
|
|
if algorithm
|
|
|
|
options = { :algorithm => algorithm }
|
|
|
|
else
|
|
|
|
options = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
data = connection.create_load_balancer(name, protocol, port, virtual_ips_hash, nodes_hash, options)
|
2011-07-10 16:35:30 -04:00
|
|
|
merge_attributes(data.body['loadBalancer'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2011-07-14 15:20:53 -04:00
|
|
|
requires :name, :protocol, :port, :algorithm
|
2011-07-10 16:35:30 -04:00
|
|
|
options = {
|
2011-07-14 15:20:53 -04:00
|
|
|
:name => name,
|
|
|
|
:algorithm => algorithm,
|
|
|
|
:protocol => protocol,
|
|
|
|
:port => port}
|
2011-07-10 16:35:30 -04:00
|
|
|
connection.update_load_balancer(identity, options)
|
2011-07-14 15:20:53 -04:00
|
|
|
|
|
|
|
#TODO - Should this bubble down to nodes? Without tracking changes this would be very inefficient.
|
|
|
|
# For now, individual nodes will have to be saved individually after saving an LB
|
2011-07-10 16:35:30 -04:00
|
|
|
end
|
2011-07-19 18:48:21 -04:00
|
|
|
|
|
|
|
def virtual_ips_hash
|
|
|
|
virtual_ips.collect do |virtual_ip|
|
|
|
|
{ :type => virtual_ip.type }
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def nodes_hash
|
|
|
|
nodes.collect do |node|
|
|
|
|
{ :address => node.address, :port => node.port, :condition => node.condition, :weight => node.weight }
|
|
|
|
end
|
|
|
|
end
|
2011-07-27 18:00:18 -04:00
|
|
|
|
|
|
|
def connection_logging=(new_value)
|
|
|
|
if !new_value.nil? and new_value.is_a?(Hash)
|
|
|
|
attributes[:connection_logging] = case new_value['enabled']
|
2011-12-13 19:19:28 -05:00
|
|
|
when true,'true'
|
2011-07-27 18:00:18 -04:00
|
|
|
true
|
2011-12-13 19:19:28 -05:00
|
|
|
when false,'false'
|
2011-07-27 18:00:18 -04:00
|
|
|
false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
attributes[:connection_logging] = new_value
|
|
|
|
end
|
|
|
|
end
|
2011-07-10 16:35:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|