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
2013-03-13 17:01:32 -04:00
recognizes :rackspace_load_balancers_url
recognizes :rackspace_region
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'
2012-05-05 12:38:08 -04:00
request :get_ssl_termination
request :set_ssl_termination
2012-06-26 00:35:07 -04:00
request :remove_ssl_termination
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
2013-01-02 14:52:26 -05:00
request :get_content_caching
request :set_content_caching
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
2013-03-13 17:01:32 -04:00
class Mock < Fog :: Rackspace :: Service
2011-08-16 12:56:44 -04:00
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
2013-03-13 17:01:32 -04:00
class Real < Fog :: Rackspace :: Service
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 ] || { }
2013-03-13 17:01:32 -04:00
@rackspace_endpoint = options [ :rackspace_load_balancers_url ] || options [ :rackspace_lb_endpoint ]
@rackspace_region = options [ :rackspace_region ] || :dfw
2011-07-07 18:17:31 -04:00
authenticate
2013-03-13 17:01:32 -04:00
@persistent = options [ :persistent ] || false
@connection = Fog :: Connection . new ( endpoint_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' ,
2013-03-01 17:59:59 -05:00
'Accept' = > 'application/json' ,
2011-07-07 18:17:31 -04:00
'X-Auth-Token' = > @auth_token
} . merge! ( params [ :headers ] || { } ) ,
2013-03-13 17:01:32 -04:00
:host = > endpoint_uri . host ,
:path = > " #{ endpoint_uri . path } / #{ params [ :path ] } "
2011-07-07 18:17:31 -04:00
} ) )
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 = {
2013-03-13 17:01:32 -04:00
:rackspace_api_key = > @rackspace_api_key ,
2011-07-07 18:17:31 -04:00
:rackspace_username = > @rackspace_username ,
:rackspace_auth_url = > @rackspace_auth_url
}
2013-03-13 17:01:32 -04:00
super ( options )
end
def service_name
:cloudLoadBalancers
end
def region
@rackspace_region
end
def endpoint_uri ( service_endpoint_url = nil )
@uri = super ( @rackspace_endpoint || service_endpoint_url , :rackspace_load_balancers_url )
end
private
def deprecation_warnings ( options )
Fog :: Logger . deprecation ( " The :rackspace_lb_endpoint option is deprecated. Please use :rackspace_load_balancers_url for custom endpoints " ) if options [ :rackspace_lb_endpoint ]
if [ DFW_ENDPOINT , ORD_ENDPOINT , LON_ENDPOINT ] . include? ( @rackspace_endpoint ) && v2_authentication?
regions = @identity_service . service_catalog . display_service_regions ( service_name )
Fog :: Logger . deprecation ( " Please specify region using :rackspace_region rather than :rackspace_endpoint. Valid regions for :rackspace_region are #{ regions } . " )
end
end
def setup_endpoint ( credentials )
account_id = credentials [ 'X-Server-Management-Url' ] . match ( / .* \/ ([ \ d]+)$ / ) [ 1 ]
endpoint = @rackspace_endpoint || credentials [ 'X-Server-Management-Url' ] || DFW_ENDPOINT
@uri = URI . parse ( endpoint )
@uri . path = " #{ @uri . path } / #{ account_id } "
end
def authenticate_v1 ( options )
2011-09-12 11:01:48 -04:00
credentials = Fog :: Rackspace . authenticate ( options , @connection_options )
2013-03-13 17:01:32 -04:00
setup_endpoint credentials
2011-07-07 18:17:31 -04:00
@auth_token = credentials [ 'X-Auth-Token' ]
end
2013-03-13 17:01:32 -04:00
2011-07-07 18:17:31 -04:00
end
end
end
end