mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Model and collection for load balancers
This commit is contained in:
parent
ad3256339c
commit
3c981d29f9
6 changed files with 132 additions and 2 deletions
|
@ -42,6 +42,8 @@ module Fog
|
|||
recognizes :rackspace_auth_token
|
||||
|
||||
model_path 'fog/rackspace/models'
|
||||
collection :load_balancers
|
||||
model :load_balancer
|
||||
|
||||
request_path 'fog/rackspace/requests'
|
||||
request :create_load_balancer
|
||||
|
|
64
lib/fog/rackspace/models/load_balancer.rb
Normal file
64
lib/fog/rackspace/models/load_balancer.rb
Normal file
|
@ -0,0 +1,64 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class LoadBalancer
|
||||
class LoadBalancer < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :cluster
|
||||
attribute :connection_logging
|
||||
attribute :port
|
||||
attribute :protocol
|
||||
attribute :algorithm
|
||||
attribute :virtual_ips
|
||||
attribute :nodes
|
||||
attribute :created
|
||||
attribute :updated
|
||||
attribute :name
|
||||
attribute :state, :aliases => 'status'
|
||||
|
||||
def initialize(attributes={})
|
||||
super
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :id
|
||||
connection.delete_load_balancer(id)
|
||||
true
|
||||
end
|
||||
|
||||
def ready?
|
||||
self.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, nodes)
|
||||
merge_attributes(data.body['loadBalancer'])
|
||||
end
|
||||
|
||||
def update
|
||||
requires :name, :protool, :port, :algorithm
|
||||
options = {
|
||||
'name' => name,
|
||||
'algorithm' => algorithm,
|
||||
'protocol' => protocol,
|
||||
'port' => port}
|
||||
connection.update_load_balancer(identity, options)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
27
lib/fog/rackspace/models/load_balancers.rb
Normal file
27
lib/fog/rackspace/models/load_balancers.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/rackspace/models/load_balancer'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class LoadBalancer
|
||||
|
||||
class LoadBalancers < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::LoadBalancer::LoadBalancer
|
||||
|
||||
def all
|
||||
data = connection.list_load_balancers.body['loadBalancers']
|
||||
load(data)
|
||||
end
|
||||
|
||||
def get(load_balancer_id)
|
||||
if load_balancer = connection.get_load_balancer(load_balancer_id).body['loadBalancer']
|
||||
new(load_balancer)
|
||||
end
|
||||
rescue Fog::Rackspace::LoadBalancer::NotFound
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -2,13 +2,13 @@ module Fog
|
|||
module Rackspace
|
||||
class LoadBalancer
|
||||
class Real
|
||||
def create_load_balancer(name, protocol, port, virtualIps, nodes)
|
||||
def create_load_balancer(name, protocol, port, virtual_ips, nodes)
|
||||
data = {
|
||||
'loadBalancer' => {
|
||||
'name' => name,
|
||||
'port' => port,
|
||||
'protocol' => protocol,
|
||||
'virtualIps' => virtualIps,
|
||||
'virtualIps' => virtual_ips,
|
||||
'nodes' => nodes
|
||||
#Is algorithm allowed on create?
|
||||
}
|
||||
|
|
15
tests/rackspace/models/load_balancer_tests.rb
Normal file
15
tests/rackspace/models/load_balancer_tests.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
Shindo.tests('Fog::Rackspace::LoadBalancer | load_balancer', ['rackspace']) do
|
||||
|
||||
@lb_name = 'fog' + Time.now.to_i.to_s
|
||||
model_tests(Fog::Rackspace::LoadBalancer.new.load_balancers,
|
||||
{
|
||||
:name => @lb_name,
|
||||
:protocol => 'HTTP',
|
||||
:port => 80,
|
||||
:virtual_ips => [{ :type => 'PUBLIC'}],
|
||||
:nodes => [{ :address => '10.0.0.1', :port => 80, :condition => 'ENABLED'}]
|
||||
},
|
||||
false) do
|
||||
@instance.wait_for { ready? }
|
||||
end
|
||||
end
|
22
tests/rackspace/models/load_balancers_tests.rb
Normal file
22
tests/rackspace/models/load_balancers_tests.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
Shindo.tests('Fog::Rackspace::LoadBalancer | load_balancers', ['rackspace']) do
|
||||
|
||||
@lb_name = 'fog' + Time.now.to_i.to_s
|
||||
collection_tests(Fog::Rackspace::LoadBalancer.new.load_balancers,
|
||||
{
|
||||
:name => @lb_name,
|
||||
:protocol => 'HTTP',
|
||||
:port => 80,
|
||||
:virtual_ips => [{ :type => 'PUBLIC'}],
|
||||
:nodes => [{ :address => '10.0.0.1', :port => 80, :condition => 'ENABLED'}]
|
||||
},
|
||||
false) do
|
||||
@instance.wait_for { ready? }
|
||||
|
||||
tests('saving existing load balancer').succeeds do
|
||||
@instance.port = 88
|
||||
@instance.save
|
||||
end
|
||||
|
||||
@instance.wait_for { ready? }
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue