1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/rackspace/models/load_balancer.rb

117 lines
3.1 KiB
Ruby
Raw Normal View History

require 'fog/core/model'
module Fog
module Rackspace
class LoadBalancer
class LoadBalancer < Fog::Model
identity :id
attribute :cluster
attribute :connection_logging, :aliases => 'connectionLogging', :squash => 'enabled'
attribute :port
attribute :protocol
attribute :algorithm
attribute :virtual_ips, :aliases => 'virtualIps'
attribute :created
attribute :updated
attribute :name
attribute :state, :aliases => 'status'
2011-07-18 22:45:47 -04:00
attribute :nodes
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
def nodes
@nodes ||= begin
Fog::Rackspace::LoadBalancer::Nodes.new({
:connection => connection,
:load_balancer => self})
end
end
2011-07-18 22:45:47 -04:00
def nodes=(new_nodes=[])
nodes.load(new_nodes)
end
2011-07-18 22:45:47 -04:00
def virtual_ips
@virtual_ips ||= begin
Fog::Rackspace::LoadBalancer::VirtualIps.new({
:connection => connection,
:load_balancer => self})
end
2011-07-18 22:45:47 -04:00
end
def virtual_ips=(new_virtual_ips=[])
virtual_ips.load(new_virtual_ips)
2011-07-18 22:45:47 -04:00
end
def enable_connection_logging
connection.set_connection_logging identity, true
connection_logging = true
end
def disable_connection_logging
connection.set_connection_logging identity, false
connection_logging = false
end
def destroy
requires :identity
connection.delete_load_balancer(identity)
true
end
def ready?
state == 'ACTIVE'
end
def save
if identity
update
else
create
end
true
end
private
def create
requires :name, :protocol, :port, :virtual_ips, :nodes
data = connection.create_load_balancer(name, protocol, port, virtual_ips_hash, nodes_hash)
merge_attributes(data.body['loadBalancer'])
end
def update
requires :name, :protocol, :port, :algorithm
options = {
:name => name,
:algorithm => algorithm,
:protocol => protocol,
:port => port}
connection.update_load_balancer(identity, options)
#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
end
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
end
end
end
end