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

Requests for nodes

This commit is contained in:
Brian Hartsock 2011-07-13 13:27:55 -07:00
parent 2252f2d4ae
commit c1724c28f3
9 changed files with 282 additions and 0 deletions

View file

@ -51,6 +51,12 @@ module Fog
request :list_load_balancers
request :update_load_balancer
request :delete_load_balancer
request :create_node
request :list_nodes
request :get_node
request :update_node
request :delete_node
request :delete_nodes
class Real
def initialize(options={})

View file

@ -0,0 +1,32 @@
module Fog
module Rackspace
class LoadBalancer
class Real
def create_node(load_balancer_id, address, port, condition, options = {})
data = {
'nodes' => [
{
'address' => address,
'port' => port,
'condition' => condition
}
]}
if options.has_key?(:weight)
data['nodes'][0]['weight'] = options[:weight]
end
request(
:body => data.to_json,
:expects => [200, 202],
:method => 'POST',
:path => "loadbalancers/#{load_balancer_id}/nodes.json"
)
end
end
class Mock
def create_node(options = {})
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Rackspace
class LoadBalancer
class Real
def delete_node(load_balancer_id, node_id)
request(
:expects => [200, 202],
:path => "loadbalancers/#{load_balancer_id}/nodes/#{node_id}",
:method => 'DELETE'
)
end
end
class Mock
def delete_node(load_balancer_id, node_id)
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,22 @@
module Fog
module Rackspace
class LoadBalancer
class Real
def delete_nodes(load_balancer_id, *node_ids)
query_string = node_ids.collect { |node_id| "id=#{node_id}" }.join('&')
puts query_string
request(
:expects => [200, 202],
:path => "loadbalancers/#{load_balancer_id}/nodes?#{node_ids}",
:method => 'DELETE'
)
end
end
class Mock
def delete_nodes(load_balancer_id, *node_ids)
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Rackspace
class LoadBalancer
class Real
def get_node(load_balancer_id, node_id)
request(
:expects => 200,
:path => "loadbalancers/#{load_balancer_id}/nodes/#{node_id}.json",
:method => 'GET'
)
end
end
class Mock
def get_node(options = {})
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Rackspace
class LoadBalancer
class Real
def list_nodes(load_balancer_id)
request(
:expects => 200,
:method => 'GET',
:path => "loadbalancers/#{load_balancer_id}/nodes.json"
)
end
end
class Mock
def list_nodes(load_balancer_id)
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,31 @@
module Fog
module Rackspace
class LoadBalancer
class Real
def update_node(load_balancer_id, node_id, options = {})
data = {
'node' => {}
}
if options.has_key? :weight
data['node']['weight'] = options[:weight]
end
if options.has_key? :condition
data['node']['condition'] = options[:condition]
end
#TODO - Do anything if no valid options are passed in?
request(
:body => data.to_json,
:expects => [200, 202],
:method => 'PUT',
:path => "loadbalancers/#{load_balancer_id}/nodes/#{node_id}.json"
)
end
end
class Mock
def update_node(options = {})
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,39 @@
SINGLE_NODE_FORMAT = {'address' => String, 'id' => Integer, 'status' => String, 'weight' => Fog::Nullable::Integer, 'port' => Integer, 'condition' => String}
NODE_FORMAT = {'node' => SINGLE_NODE_FORMAT}
NODES_FORMAT = {'nodes' => [SINGLE_NODE_FORMAT]}
VIRTUAL_IP_FORMAT = {'type' => String, 'id' => Integer, 'type' => String, 'ipVersion' => String, 'address' => String}
STATUS_ACTIVE = 'ACTIVE'
LOAD_BALANCERS_FORMAT = {
'loadBalancers' => [
{
'name' => String,
'id' => Integer,
'port' => Integer,
'protocol' => String,
'algorithm' => String,
'status' => String,
'virtualIps' => [VIRTUAL_IP_FORMAT],
'nodes' => NODES_FORMAT,
'created' => { 'time' => String },
'updated' => { 'time' => String }
}]
}
LOAD_BALANCER_FORMAT = {
'loadBalancer' => {
'name' => String,
'id' => Integer,
'port' => Integer,
'protocol' => String,
'algorithm' => String,
'status' => String,
'cluster' => { 'name' => String },
'virtualIps' => [VIRTUAL_IP_FORMAT],
'nodes' => [NODE_FORMAT],
'created' => { 'time' => String },
'updated' => { 'time' => String },
'connectionLogging' => { 'enabled' => Fog::Boolean }
}}

View file

@ -0,0 +1,92 @@
Shindo.tests('Fog::Rackspace::LoadBalancer | load_balancer_tests', ['rackspace']) do
@service = Fog::Rackspace::LoadBalancer.new
@lb = @service.load_balancers.create({
:name => ('fog' + Time.now.to_i.to_s),
:protocol => 'HTTP',
:port => 80,
:virtual_ips => [{ :type => 'PUBLIC'}],
:nodes => [{ :address => '10.0.0.1', :port => 80, :condition => 'ENABLED'}]
})
@nodes_created = []
tests('success') do
@lb.wait_for { ready? }
tests('#create_node').formats(NODES_FORMAT) do
data = @service.create_node(@lb.id, '10.10.10.10', 80, 'ENABLED').body
@nodes_created << data['nodes'][0]['id']
data
end
@lb.wait_for { ready? }
tests('#create_node with weight').formats(NODES_FORMAT) do
data = @service.create_node(@lb.id, '10.10.10.11', 80, 'ENABLED', { :weight => 10 }).body
@nodes_created << data['nodes'][0]['id']
data
end
@lb.wait_for { ready? }
tests("list_nodes").formats(NODES_FORMAT) do
@service.list_nodes(@lb.id).body
end
@lb.wait_for { ready? }
tests("get_node(#{@lb_node_id})").formats(NODE_FORMAT) do
@service.get_node(@lb.id, @nodes_created[0]).body
end
tests("update_node(#{@lb.id}, #{@nodes_created[0]})").succeeds do
@lb.wait_for { ready? }
tests("condition").succeeds do
@service.update_node(@lb.id, @nodes_created[0], { :condition => 'DISABLED' })
end
@lb.wait_for { ready? }
tests("weight").succeeds do
@service.update_node(@lb.id, @nodes_created[0], { :weight => 20 })
end
@lb.wait_for { ready? }
tests("condition and weight").succeeds do
@service.update_node(@lb.id, @nodes_created[0], { :condition => 'DISABLED', :weight => 20 })
end
end
end
tests('failure') do
tests('create_node(invalid ip)').raises(Fog::Rackspace::LoadBalancer::BadRequest) do
@service.create_node(@lb.id, '', 80, 'ENABLED')
end
tests('create_node(invalid condition)').raises(Fog::Rackspace::LoadBalancer::BadRequest) do
@service.create_node(@lb.id, '10.10.10.10', 80, 'EABLED')
end
tests('get_node(0)').raises(Fog::Rackspace::LoadBalancer::NotFound) do
@service.get_node(@lb.id, 0)
end
tests('delete_nodes(0)').raises(Fog::Rackspace::LoadBalancer::ServiceError) do
@service.delete_nodes(@lb.id, 0)
end
tests('update_node(0)').raises(Fog::Rackspace::LoadBalancer::NotFound) do
@service.update_node(@lb.id, 0, { :weight => 20 })
end
end
tests('success') do
@lb.wait_for { ready? }
tests("#delete_nodes(multiple node)").succeeds do
pending
@service.delete_nodes(@lb.id, *@nodes_created)
end
@lb.wait_for { ready? }
tests("#delete_node()").succeeds do
node_id = @service.create_node(@lb.id, '10.10.10.12', 80, 'ENABLED').body['nodes'][0]['id']
@lb.wait_for { ready? }
@service.delete_node(@lb.id, node_id)
end
end
@lb.wait_for { ready? }
@lb.destroy
end