2012-04-26 19:04:20 -04:00
require File . expand_path ( File . join ( File . dirname ( __FILE__ ) , '..' , 'rackspace' ) )
module Fog
module Rackspace
class Databases < Fog :: Service
2013-04-15 10:31:13 -04:00
include Fog :: Rackspace :: Errors
2012-06-23 11:36:50 -04:00
class ServiceError < Fog :: Rackspace :: Errors :: ServiceError ; end
class InternalServerError < Fog :: Rackspace :: Errors :: InternalServerError ; end
class BadRequest < Fog :: Rackspace :: Errors :: BadRequest ; end
2013-03-01 11:26:47 -05:00
DFW_ENDPOINT = 'https://dfw.databases.api.rackspacecloud.com/v1.0'
LON_ENDPOINT = 'https://lon.databases.api.rackspacecloud.com/v1.0'
ORD_ENDPOINT = 'https://ord.databases.api.rackspacecloud.com/v1.0'
2012-04-26 19:04:20 -04:00
requires :rackspace_api_key , :rackspace_username
recognizes :rackspace_auth_url
recognizes :rackspace_auth_token
2012-06-21 17:47:52 -04:00
recognizes :rackspace_endpoint
2013-03-13 17:01:32 -04:00
recognizes :rackspace_region
recognizes :rackspace_database_url
2012-04-26 19:04:20 -04:00
model_path 'fog/rackspace/models/databases'
model :flavor
collection :flavors
model :instance
collection :instances
model :database
collection :databases
model :user
collection :users
request_path 'fog/rackspace/requests/databases'
2012-06-23 10:40:29 -04:00
request :list_flavors
2012-04-26 19:04:20 -04:00
request :get_flavor
2012-06-21 17:47:52 -04:00
2012-06-23 10:40:29 -04:00
request :list_instances
2012-04-26 19:04:20 -04:00
request :get_instance
2012-06-21 17:47:52 -04:00
request :create_instance
request :delete_instance
2012-04-26 19:04:20 -04:00
request :check_root_user
2012-06-21 17:47:52 -04:00
request :enable_root_user
request :restart_instance
request :resize_instance
request :resize_instance_volume
2012-04-26 19:04:20 -04:00
request :list_databases
2012-06-21 17:47:52 -04:00
request :create_database
request :delete_database
2012-04-26 19:04:20 -04:00
request :list_users
2012-06-21 17:47:52 -04:00
request :create_user
request :delete_user
2012-04-26 19:04:20 -04:00
2013-03-13 17:01:32 -04:00
class Mock < Fog :: Rackspace :: Service
2012-04-26 19:04:20 -04:00
def request ( params )
Fog :: Mock . not_implemented
end
end
2013-03-13 17:01:32 -04:00
class Real < Fog :: Rackspace :: Service
def service_name
:cloudDatabases
end
def region
@rackspace_region
end
2012-04-26 19:04:20 -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
@connection_options = options [ :connection_options ] || { }
2013-03-15 17:38:33 -04:00
setup_custom_endpoint ( options )
2012-04-26 19:04:20 -04:00
authenticate
2013-03-13 17:01:32 -04:00
deprecation_warnings ( options )
@persistent = options [ :persistent ] || false
@connection = Fog :: Connection . new ( endpoint_uri . to_s , @persistent , @connection_options )
2012-04-26 19:04:20 -04:00
end
2013-06-24 11:13:57 -04:00
def request ( params , parse_json = true , & block )
super ( params , parse_json , & block )
rescue Excon :: Errors :: NotFound = > error
2013-08-14 11:05:25 -04:00
raise NotFound . slurp ( error , self )
2013-06-24 11:13:57 -04:00
rescue Excon :: Errors :: BadRequest = > error
2013-08-14 11:05:25 -04:00
raise BadRequest . slurp ( error , self )
2013-06-24 11:13:57 -04:00
rescue Excon :: Errors :: InternalServerError = > error
2013-08-14 11:05:25 -04:00
raise InternalServerError . slurp ( error , self )
2013-06-24 11:13:57 -04:00
rescue Excon :: Errors :: HTTPStatusError = > error
2013-08-14 11:05:25 -04:00
raise ServiceError . slurp ( error , self )
2012-04-26 19:04:20 -04:00
end
2013-03-13 17:01:32 -04:00
def endpoint_uri ( service_endpoint_url = nil )
@uri = super ( @rackspace_endpoint || service_endpoint_url , :rackspace_database_url )
end
2012-04-26 19:04:20 -04:00
2013-06-21 16:11:17 -04:00
def authenticate ( options = { } )
super ( {
2012-04-26 19:04:20 -04:00
:rackspace_api_key = > @rackspace_api_key ,
:rackspace_username = > @rackspace_username ,
2013-05-30 13:52:37 -04:00
:rackspace_auth_url = > @rackspace_auth_url ,
:connection_options = > @connection_options
2013-06-21 16:11:17 -04:00
} )
2013-03-13 17:01:32 -04:00
end
private
2013-03-15 17:38:33 -04:00
def setup_custom_endpoint ( options )
2013-03-20 14:25:38 -04:00
@rackspace_endpoint = Fog :: Rackspace . normalize_url ( options [ :rackspace_database_url ] || options [ :rackspace_endpoint ] )
2013-03-15 17:38:33 -04:00
if v2_authentication?
case @rackspace_endpoint
when DFW_ENDPOINT
@rackspace_endpoint = nil
@rackspace_region = :dfw
when ORD_ENDPOINT
@rackspace_endpoint = nil
@rackspace_region = :ord
when LON_ENDPOINT
@rackspace_endpoint = nil
@rackspace_region = :lon
else
# we are actually using a custom endpoint
@rackspace_region = options [ :rackspace_region ] || :dfw
end
else
#if we are using auth1 and the endpoint is not set, default to DFW_ENDPOINT for historical reasons
@rackspace_endpoint || = DFW_ENDPOINT
end
end
2013-03-13 17:01:32 -04:00
def deprecation_warnings ( options )
Fog :: Logger . deprecation ( " The :rackspace_endpoint option is deprecated. Please use :rackspace_database_url for custom endpoints " ) if options [ :rackspace_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 region for :rackspace_region are #{ regions } . " )
end
end
2013-03-15 17:38:33 -04:00
def append_tenant_v1 ( credentials )
2013-03-13 17:01:32 -04:00
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 )
2012-04-26 19:04:20 -04:00
credentials = Fog :: Rackspace . authenticate ( options , @connection_options )
2013-03-15 17:38:33 -04:00
append_tenant_v1 credentials
2012-04-26 19:04:20 -04:00
@auth_token = credentials [ 'X-Auth-Token' ]
end
end
end
end
end