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

Merge pull request #3110 from smashwilson/load-balancer-params

[rackspace] More optional parameters for Cloud Load Balancers
This commit is contained in:
Wesley Beary 2014-08-21 09:56:16 -05:00
commit ceee958656
5 changed files with 136 additions and 40 deletions

View file

@ -226,8 +226,9 @@ module Fog
end
private
def create
requires :name, :protocol, :port, :virtual_ips, :nodes
requires :name, :protocol, :virtual_ips
options = {}
options[:algorithm] = algorithm if algorithm
@ -244,10 +245,12 @@ module Fog
:algorithm => algorithm,
:protocol => protocol,
:port => port,
:timeout => timeout }
:timeout => timeout
}
service.update_load_balancer(identity, options)
#TODO - Should this bubble down to nodes? Without tracking changes this would be very inefficient.
# 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

View file

@ -2,19 +2,36 @@ module Fog
module Rackspace
class LoadBalancers
class Real
def create_load_balancer(name, protocol, port, virtual_ips, nodes, options = {})
data = {
'loadBalancer' => {
'name' => name,
'port' => port,
'protocol' => protocol,
'virtualIps' => virtual_ips,
}
# Issue an asynchronous request to create a new Load Balancer.
#
# @param name [String] human-friendly identifier for the balancer that will be shown in
# the web UI.
# @param protocol [String] well-known protocol describing the traffic to be load balanced.
# @param port [Integer] port for the balancer to listen on and balance to.
# @param virtual_ips [Array<Hash>] description of the kind of IP address to bind to, or id
# of the existing virtual IP from another balancer. Examples: `{ 'type' => 'PUBLIC' }`,
# `{ 'type' => 'PRIVATE' }`, `{ 'id' => 1234 }`
# @param nodes [Array<Hash>] collection of
# @option options [String] :algorithm balancing algorithm for the balancer to use.
# See http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/Algorithms-d1e4367.html
# @option options [String] :timeout amount of time the load balancer will wait for a response
# from a back-end node before terminating the connection. Defaults to 30 seconds, may be
# increased to a maximum of 120 seconds.
#
def create_load_balancer(name, protocol, port = nil, virtual_ips = [{'type' => 'PUBLIC'}], nodes = nil, options = {})
lb_data = {
'name' => name,
'protocol' => protocol,
'virtualIps' => virtual_ips
}
data['loadBalancer']['nodes'] = nodes if nodes && !nodes.empty?
data['loadBalancer']['algorithm'] = options[:algorithm] if options.key? :algorithm
data['loadBalancer']['timeout'] = options[:timeout] if options.key? :timeout
lb_data['nodes'] = nodes if nodes && !nodes.empty?
lb_data['port'] = port if port
lb_data['algorithm'] = options[:algorithm] if options.key? :algorithm
lb_data['timeout'] = options[:timeout] if options.key? :timeout
data = { 'loadBalancer' => lb_data }
request(
:body => Fog::JSON.encode(data),
@ -23,15 +40,64 @@ module Fog
:path => 'loadbalancers.json'
)
end
end
class Mock
def create_load_balancer(name, protocol, port, virtual_ips, nodes, options = {})
data = {"loadBalancer"=>{"name"=>name, "id"=>Fog::Mock.random_numbers(6), "protocol"=>protocol, "port"=>port, "algorithm"=>"RANDOM", "status"=>"BUILD",
"cluster"=>{"name"=>"my-cluster.rackspace.net"}, "timeout"=>30, "created"=>{"time"=> MockData.zulu_time},
"updated"=>{"time"=>MockData.zulu_time }, "halfClosed"=>false, "connectionLogging"=>{"enabled"=>false}, "contentCaching"=>{"enabled"=>false}}}
data["virtual_ips"] = virtual_ips.map {|n| {"virtualIps"=>[{"address"=> MockData.ipv4_address, "id"=>Fog::Mock.random_numbers(6), "type"=> n.type, "ipVersion"=>"IPV4"}, {"address"=> MockData.ipv6_address, "id"=> Fog::Mock.random_numbers(4), "type"=>"PUBLIC", "ipVersion"=>"IPV6"}], "sourceAddresses"=>{"ipv6Public"=> MockData.ipv6_address, "ipv4Servicenet"=>MockData.ipv4_address, "ipv4Public"=>MockData.ipv4_address}}
data["nodes"] = nodes.map {|n| {"address"=>n.address, "id"=>Fog::Mock.random_numbers(6), "type"=>"PRIMARY", "port"=>n.port, "status"=>"ONLINE", "condition"=>"ENABLED", "weight"=>1}}}
data = {
"loadBalancer" => {
"name" => name,
"id" => Fog::Mock.random_numbers(6),
"protocol" => protocol,
"port" => port,
"algorithm" => "RANDOM",
"status" => "BUILD",
"cluster" => { "name" => "my-cluster.rackspace.net" },
"timeout" => 30,
"created" => {"time" => MockData.zulu_time },
"updated" => {"time" => MockData.zulu_time },
"halfClosed" => false,
"connectionLogging" => { "enabled" => false },
"contentCaching" => { "enabled" => false }
}
}
data["virtual_ips"] = virtual_ips.map do |n|
{
"virtualIps" => [
{
"address" => MockData.ipv4_address,
"id" => Fog::Mock.random_numbers(6),
"type" => n.type,
"ipVersion" => "IPV4"
},
{
"address" => MockData.ipv6_address,
"id" => Fog::Mock.random_numbers(4),
"type" => "PUBLIC",
"ipVersion" => "IPV6"
}
],
"sourceAddresses" => {
"ipv6Public" => MockData.ipv6_address,
"ipv4Servicenet" => MockData.ipv4_address,
"ipv4Public" => MockData.ipv4_address
}
}
end
data["nodes"] = nodes.map do |n|
{
"address" => n.address,
"id" => Fog::Mock.random_numbers(6),
"type" => "PRIMARY",
"port" => n.port,
"status" => "ONLINE",
"condition" => "ENABLED",
"weight" => 1
}
end
Excon::Response.new(:body => data, :status => 202)
end

View file

@ -2,16 +2,24 @@ Shindo.tests('Fog::Rackspace::LoadBalancers | load_balancer', ['rackspace']) do
pending if Fog.mocking?
LOAD_BALANCER_ATTRIBUTES = {
:name => 'fog' + Time.now.to_i.to_s,
:protocol => 'HTTP',
:port => 80,
:virtual_ips => [{ :type => 'PUBLIC'}],
:nodes => [{ :address => '1.1.1.1', :port => 80, :condition => 'ENABLED'}]
}
MINIMAL_LB_ATTRIBUTES = {
:name => "fog#{Time.now.to_i}",
:protocol => 'HTTP',
:virtual_ips => [{ :type => 'PUBLIC' }],
}
NORMAL_LB_ATTRIBUTES = MINIMAL_LB_ATTRIBUTES.merge({
:port => 8080,
:nodes => [{ :address => '1.1.1.1', :port => 80, :condition => 'ENABLED' }]
})
FULL_LB_ATTRIBUTES = NORMAL_LB_ATTRIBUTES.merge({
:algorithm => 'LEAST_CONNECTIONS',
:timeout => 60
})
given_a_load_balancer_service do
model_tests(@service.load_balancers, LOAD_BALANCER_ATTRIBUTES, false) do
model_tests(@service.load_balancers, NORMAL_LB_ATTRIBUTES, false) do
@instance.wait_for { ready? }
tests('#save => saving existing with port = 88').succeeds do
@ -41,7 +49,7 @@ Shindo.tests('Fog::Rackspace::LoadBalancers | load_balancer', ['rackspace']) do
returns(false) { @instance.connection_logging }
end
@instance.wait_for { ready? }
@instance.wait_for { ready? }
tests('#enable_content_caching').succeeds do
@instance.enable_content_caching
returns(true) { @instance.content_caching }
@ -169,11 +177,20 @@ Shindo.tests('Fog::Rackspace::LoadBalancers | load_balancer', ['rackspace']) do
@instance.wait_for { ready? }
end
tests('create(...with algorithm...)') do
attributes = LOAD_BALANCER_ATTRIBUTES.clone
attributes[:algorithm] = 'LEAST_CONNECTIONS'
attributes[:timeout] = 60
@lb = @service.load_balancers.create attributes
tests('create with minimal attributes') do
@lb = @service.load_balancers.create MINIMAL_LB_ATTRIBUTES
returns(MINIMAL_LB_ATTRIBUTES[:name]) { @lb.name }
returns('HTTP') { @lb.protocol }
returns(80) { @lb.port }
@lb.wait_for { ready? }
@lb.destroy
end
tests('create with full attributes') do
@lb = @service.load_balancers.create FULL_LB_ATTRIBUTES
returns('LEAST_CONNECTIONS') { @lb.algorithm }
returns(60) { @lb.timeout }
@ -183,7 +200,7 @@ Shindo.tests('Fog::Rackspace::LoadBalancers | load_balancer', ['rackspace']) do
end
tests('failure') do
@lb = @service.load_balancers.new LOAD_BALANCER_ATTRIBUTES
@lb = @service.load_balancers.new NORMAL_LB_ATTRIBUTES
tests('#usage => Requires ID').raises(ArgumentError) do
@lb.usage
end

View file

@ -160,7 +160,7 @@ LOAD_BALANCER_FORMAT = {
'timeout' => Integer,
'cluster' => { 'name' => String },
'virtualIps' => [VIRTUAL_IP_FORMAT],
'nodes' => [SINGLE_NODE_FORMAT],
'nodes' => Fog::Nullable::Array,
'created' => { 'time' => String },
'updated' => { 'time' => String },
'contentCaching' => { 'enabled' => Fog::Boolean }

View file

@ -6,11 +6,19 @@ Shindo.tests('Fog::Rackspace::LoadBalancers | load_balancer_tests', ['rackspace'
tests('success') do
@lb_id = nil
@lb_ids = []
@lb_name = 'fog' + Time.now.to_i.to_s
tests("#create_load_balancer(#{@lb_name}, 'HTTP', 80,...)").formats(LOAD_BALANCER_FORMAT) do
data = @service.create_load_balancer(@lb_name, 'HTTP', 80, [{ :type => 'PUBLIC'}], [{ :address => '1.1.1.1', :port => 80, :condition => 'ENABLED'}]).body
tests("#create_load_balancer(#{@lb_name}, 'HTTP')").formats(LOAD_BALANCER_FORMAT) do
data = @service.create_load_balancer(@lb_name, 'HTTP').body
@lb_id = data['loadBalancer']['id']
@lb_ids << @lb_id
data
end
tests("#create_load_balancer(#{@lb_name}, 'HTTP', 80,...)").formats(LOAD_BALANCER_FORMAT) do
data = @service.create_load_balancer(@lb_name, 'HTTP', 80, [{ :type => 'PUBLIC' }], [{ :address => '1.1.1.1', :port => 80, :condition => 'ENABLED' }]).body
@lb_ids << data['loadBalancer']['id']
data
end
@ -18,7 +26,7 @@ Shindo.tests('Fog::Rackspace::LoadBalancers | load_balancer_tests', ['rackspace'
data = @service.create_load_balancer(@lb_name, 'HTTP', 80, [{ :type => 'PUBLIC'}],
[{ :address => '1.1.1.1', :port => 80, :condition => 'ENABLED'}],
{ :algorithm => 'LEAST_CONNECTIONS', :timeout => 30 }).body
@lb_id = data['loadBalancer']['id']
@lb_ids << data['loadBalancer']['id']
returns('LEAST_CONNECTIONS') { data['loadBalancer']['algorithm'] }
returns(30) { data['loadBalancer']['timeout'] }
data
@ -52,8 +60,10 @@ Shindo.tests('Fog::Rackspace::LoadBalancers | load_balancer_tests', ['rackspace'
sleep 10
end
tests("#delete_load_balancer(#{@ld_id})").succeeds do
@service.delete_load_balancer(@lb_id).body
@lb_ids.each do |id|
tests("#delete_load_balancer(#{id})").succeeds do
@service.delete_load_balancer(id).body
end
end
end