2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/rackspace'
|
2011-08-16 22:33:15 -04:00
|
|
|
|
2011-07-07 18:17:31 -04:00
|
|
|
module Fog
|
|
|
|
module Rackspace
|
2011-08-04 17:21:22 -04:00
|
|
|
class LoadBalancers < Fog::Service
|
2011-07-07 18:17:31 -04:00
|
|
|
|
2011-08-16 22:33:15 -04:00
|
|
|
#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
|
|
|
|
2011-07-07 18:17:31 -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/'
|
2011-07-07 18:17:31 -04:00
|
|
|
|
|
|
|
requires :rackspace_api_key, :rackspace_username
|
|
|
|
recognizes :rackspace_auth_url
|
|
|
|
recognizes :rackspace_auth_token
|
2011-11-11 10:25:31 -05:00
|
|
|
recognizes :rackspace_lb_endpoint
|
2011-07-07 18:17:31 -04:00
|
|
|
|
2011-08-04 17:21:22 -04:00
|
|
|
model_path 'fog/rackspace/models/load_balancers'
|
2011-07-10 16:35:30 -04:00
|
|
|
collection :load_balancers
|
|
|
|
model :load_balancer
|
2011-07-14 15:20:53 -04:00
|
|
|
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
|
2011-07-07 18:17:31 -04:00
|
|
|
|
2011-08-04 17:21:22 -04:00
|
|
|
request_path 'fog/rackspace/requests/load_balancers'
|
2011-07-07 18:17:31 -04:00
|
|
|
request :create_load_balancer
|
2011-07-08 16:39:22 -04:00
|
|
|
request :get_load_balancer
|
|
|
|
request :list_load_balancers
|
|
|
|
request :update_load_balancer
|
2011-07-07 18:17:31 -04:00
|
|
|
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
|
2011-07-15 00:07:29 -04:00
|
|
|
request :create_virtual_ip
|
|
|
|
request :list_virtual_ips
|
|
|
|
request :delete_virtual_ip
|
2011-07-21 10:41:40 -04:00
|
|
|
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
|
2011-07-27 17:06:24 -04:00
|
|
|
request :get_session_persistence
|
|
|
|
request :set_session_persistence
|
|
|
|
request :remove_session_persistence
|
2011-07-27 17:19:44 -04:00
|
|
|
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
|
2011-07-27 21:53:33 -04:00
|
|
|
request :get_load_balancer_usage
|
2011-12-15 16:53:06 -05:00
|
|
|
request :get_error_page
|
|
|
|
request :set_error_page
|
|
|
|
request :remove_error_page
|
2011-07-07 18:17:31 -04:00
|
|
|
|
2011-08-16 12:56:44 -04:00
|
|
|
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
|
|
|
|
|
2011-07-07 18:17:31 -04:00
|
|
|
class Real
|
2011-08-16 12:56:44 -04:00
|
|
|
include Shared
|
|
|
|
|
2011-07-07 18:17:31 -04:00
|
|
|
def initialize(options={})
|
|
|
|
@rackspace_api_key = options[:rackspace_api_key]
|
|
|
|
@rackspace_username = options[:rackspace_username]
|
|
|
|
@rackspace_auth_url = options[:rackspace_auth_url]
|
|
|
|
@rackspace_must_reauthenticate = false
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2011-07-07 18:17:31 -04:00
|
|
|
uri = URI.parse(options[:rackspace_lb_endpoint] || DFW_ENDPOINT)
|
2011-09-12 11:01:48 -04:00
|
|
|
@host = uri.host
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@path = uri.path
|
|
|
|
@port = uri.port
|
|
|
|
@scheme = uri.scheme
|
2011-07-07 18:17:31 -04:00
|
|
|
|
|
|
|
authenticate
|
|
|
|
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options)
|
2011-07-07 18:17:31 -04:00
|
|
|
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
|
2011-07-21 10:41:40 -04:00
|
|
|
rescue Excon::Errors::InternalServerError => error
|
|
|
|
raise InternalServerError.slurp error
|
2011-07-07 18:17:31 -04:00
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
2011-07-09 17:46:15 -04:00
|
|
|
raise ServiceError.slurp error
|
2011-07-07 18:17:31 -04:00
|
|
|
end
|
|
|
|
unless response.body.empty?
|
2012-04-25 10:31:28 -04:00
|
|
|
response.body = Fog::JSON.decode(response.body)
|
2011-07-07 18:17:31 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate
|
|
|
|
options = {
|
|
|
|
:rackspace_api_key => @rackspace_api_key,
|
|
|
|
:rackspace_username => @rackspace_username,
|
|
|
|
:rackspace_auth_url => @rackspace_auth_url
|
|
|
|
}
|
2011-09-12 11:01:48 -04:00
|
|
|
credentials = Fog::Rackspace.authenticate(options, @connection_options)
|
2011-07-07 18:17:31 -04:00
|
|
|
@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
|
|
|
|
2011-07-07 18:17:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|