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/load_balancers.rb

157 lines
5 KiB
Ruby
Raw Normal View History

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rackspace'))
module Fog
module Rackspace
class LoadBalancers < Fog::Service
#These references exist for backwards compatibility
class ServiceError < Fog::Rackspace::Errors::ServiceError; end
class InternalServerError < Fog::Rackspace::Errors::InternalServerError; end
class BadRequest < Fog::Rackspace::Errors::BadRequest; end
2011-07-09 17:46:15 -04:00
DFW_ENDPOINT = 'https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/'
ORD_ENDPOINT = 'https://ord.loadbalancers.api.rackspacecloud.com/v1.0/'
2011-07-28 14:37:21 -04:00
LON_ENDPOINT = 'https://lon.loadbalancers.api.rackspacecloud.com/v1.0/'
requires :rackspace_api_key, :rackspace_username
recognizes :rackspace_auth_url
recognizes :rackspace_auth_token
recognizes :rackspace_lb_endpoint
model_path 'fog/rackspace/models/load_balancers'
collection :load_balancers
model :load_balancer
collection :nodes
model :node
2011-07-18 22:45:47 -04:00
collection :virtual_ips
model :virtual_ip
2011-07-28 13:21:26 -04:00
collection :access_rules
model :access_rule
request_path 'fog/rackspace/requests/load_balancers'
request :create_load_balancer
2011-07-08 16:39:22 -04:00
request :get_load_balancer
request :list_load_balancers
request :update_load_balancer
request :delete_load_balancer
2011-07-13 16:27:55 -04:00
request :create_node
request :list_nodes
request :get_node
request :update_node
request :delete_node
request :delete_nodes
request :create_virtual_ip
request :list_virtual_ips
request :delete_virtual_ip
request :list_protocols
request :list_algorithms
request :get_connection_logging
request :set_connection_logging
2011-07-27 16:55:01 -04:00
request :create_access_rule
request :list_access_rules
request :delete_access_rule
request :delete_all_access_rules
request :get_session_persistence
request :set_session_persistence
request :remove_session_persistence
request :get_connection_throttling
request :remove_connection_throttling
request :set_connection_throttling
2011-07-27 21:12:46 -04:00
request :get_monitor
request :set_monitor
request :remove_monitor
2011-07-27 21:44:43 -04:00
request :get_usage
request :get_load_balancer_usage
module Shared
def algorithms
list_algorithms.body['algorithms'].collect { |i| i['name'] }
end
def protocols
list_protocols.body['protocols']
end
def usage(options = {})
get_usage(options).body
end
end
class Mock
include Shared
def initialize(options={})
@rackspace_api_key = options[:rackspace_api_key]
@rackspace_username = options[:rackspace_username]
@rackspace_auth_url = options[:rackspace_auth_url]
end
end
class Real
include Shared
def initialize(options={})
require 'multi_json'
@rackspace_api_key = options[:rackspace_api_key]
@rackspace_username = options[:rackspace_username]
@rackspace_auth_url = options[:rackspace_auth_url]
@rackspace_must_reauthenticate = false
@connection_options = options[:connection_options] || {}
uri = URI.parse(options[:rackspace_lb_endpoint] || DFW_ENDPOINT)
@host = uri.host
@persistent = options[:persistent] || false
@path = uri.path
@port = uri.port
@scheme = uri.scheme
authenticate
@connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options)
end
def request(params)
#TODO - Unify code with other rackspace services
begin
response = @connection.request(params.merge!({
:headers => {
'Content-Type' => 'application/json',
'X-Auth-Token' => @auth_token
}.merge!(params[:headers] || {}),
:host => @host,
:path => "#{@path}/#{params[:path]}"
}))
2011-07-09 17:46:15 -04:00
rescue Excon::Errors::NotFound => error
raise NotFound.slurp error
rescue Excon::Errors::BadRequest => error
raise BadRequest.slurp error
rescue Excon::Errors::InternalServerError => error
raise InternalServerError.slurp error
rescue Excon::Errors::HTTPStatusError => error
2011-07-09 17:46:15 -04:00
raise ServiceError.slurp error
end
unless response.body.empty?
response.body = MultiJson.decode(response.body)
end
response
end
def authenticate
options = {
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url
}
credentials = Fog::Rackspace.authenticate(options, @connection_options)
@auth_token = credentials['X-Auth-Token']
account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
@path = "#{@path}/#{account_id}"
end
2011-07-28 13:23:21 -04:00
end
end
end
end