mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
91 lines
2.4 KiB
Ruby
91 lines
2.4 KiB
Ruby
require 'fog/core/model'
|
|
|
|
module Fog
|
|
module Rackspace
|
|
class LoadBalancer
|
|
class LoadBalancer < Fog::Model
|
|
|
|
identity :id
|
|
|
|
attribute :cluster
|
|
attribute :connection_logging, :aliases => 'connectionLogging'
|
|
attribute :port
|
|
attribute :protocol
|
|
attribute :algorithm
|
|
attribute :virtual_ips, :aliases => 'virtualIps'
|
|
attribute :created
|
|
attribute :updated
|
|
attribute :name
|
|
attribute :state, :aliases => 'status'
|
|
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
|
|
end
|
|
|
|
def nodes=(new_nodes=[])
|
|
@nodes = Fog::Rackspace::LoadBalancer::Nodes.new({
|
|
:connection => connection,
|
|
:load_balancer => self})
|
|
@nodes.load(new_nodes)
|
|
end
|
|
|
|
def virtual_ips
|
|
@virtual_ips
|
|
end
|
|
|
|
def virtual_ips=(new_virtual_ips=[])
|
|
@virtual_ips = Fog::Rackspace::LoadBalancer::VirtualIps.new({
|
|
:connection => connection,
|
|
:load_balancer => self})
|
|
@virtual_ips.load(new_virtual_ips)
|
|
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.to_object, nodes.to_object)
|
|
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
|
|
end
|
|
end
|
|
end
|
|
end
|