2012-08-21 15:21:25 -04:00
require File . expand_path ( File . join ( File . dirname ( __FILE__ ) , '..' , 'rackspace' ) )
module Fog
module Rackspace
class BlockStorage < Fog :: Service
2013-04-15 10:31:13 -04:00
include Fog :: Rackspace :: Errors
2012-08-21 15:21:25 -04:00
2012-12-20 12:33:43 -05:00
class IdentifierTaken < Fog :: Errors :: Error ; end
2012-08-21 15:21:25 -04:00
class ServiceError < Fog :: Rackspace :: Errors :: ServiceError ; end
class InternalServerError < Fog :: Rackspace :: Errors :: InternalServerError ; end
class BadRequest < Fog :: Rackspace :: Errors :: BadRequest ; end
DFW_ENDPOINT = 'https://dfw.blockstorage.api.rackspacecloud.com/v1'
LON_ENDPOINT = 'https://lon.blockstorage.api.rackspacecloud.com/v1'
ORD_ENDPOINT = 'https://ord.blockstorage.api.rackspacecloud.com/v1'
requires :rackspace_api_key , :rackspace_username
recognizes :rackspace_auth_url
recognizes :rackspace_endpoint
2013-03-13 17:01:32 -04:00
recognizes :rackspace_region
recognizes :rackspace_block_storage_url
2012-08-21 15:21:25 -04:00
model_path 'fog/rackspace/models/block_storage'
model :volume
collection :volumes
2012-08-23 15:03:28 -04:00
2012-08-22 17:51:35 -04:00
model :volume_type
collection :volume_types
2012-08-23 15:03:28 -04:00
model :snapshot
collection :snapshots
model :snapshot
collection :snapshots
2012-08-21 15:21:25 -04:00
request_path 'fog/rackspace/requests/block_storage'
request :create_volume
request :delete_volume
request :get_volume
request :list_volumes
2012-08-23 15:03:28 -04:00
2012-08-22 17:51:35 -04:00
request :get_volume_type
request :list_volume_types
2012-08-23 15:03:28 -04:00
request :create_snapshot
request :delete_snapshot
request :get_snapshot
request :list_snapshots
2012-08-21 15:21:25 -04:00
2013-03-13 17:01:32 -04:00
class Mock < Fog :: Rackspace :: Service
2013-01-11 16:06:58 -05:00
include Fog :: Rackspace :: MockData
def initialize ( options = { } )
@rackspace_api_key = options [ :rackspace_api_key ]
end
2012-08-21 15:21:25 -04:00
def request ( params )
Fog :: Mock . not_implemented
end
2013-01-11 16:06:58 -05:00
def response ( params = { } )
body = params [ :body ] || { }
status = params [ :status ] || 200
headers = params [ :headers ] || { }
response = Excon :: Response . new ( :body = > body , :headers = > headers , :status = > status )
if params . has_key? ( :expects ) && ! [ * params [ :expects ] ] . include? ( response . status )
raise ( Excon :: Errors . status_error ( params , response ) )
else response
end
end
2012-08-21 15:21:25 -04:00
end
2013-03-13 17:01:32 -04:00
class Real < Fog :: Rackspace :: Service
2012-08-21 15:21:25 -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-08-21 15:21:25 -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-08-21 15:21:25 -04:00
end
def request ( params )
begin
response = @connection . request ( params . merge! ( {
:headers = > {
'Content-Type' = > 'application/json' ,
2013-03-01 17:59:59 -05:00
'Accept' = > 'application/json' ,
2013-03-13 17:01:32 -04:00
'X-Auth-Token' = > auth_token
2012-08-21 15:21:25 -04:00
} . merge! ( params [ :headers ] || { } ) ,
2013-03-13 17:01:32 -04:00
:host = > endpoint_uri . host ,
:path = > " #{ endpoint_uri . path } / #{ params [ :path ] } "
2012-08-21 15:21:25 -04:00
} ) )
rescue Excon :: Errors :: NotFound = > error
2013-04-15 10:31:13 -04:00
raise NotFound . slurp ( error , region )
2012-08-21 15:21:25 -04:00
rescue Excon :: Errors :: BadRequest = > error
raise BadRequest . slurp error
rescue Excon :: Errors :: InternalServerError = > error
raise InternalServerError . slurp error
rescue Excon :: Errors :: HTTPStatusError = > error
raise ServiceError . slurp error
end
unless response . body . empty?
response . body = Fog :: JSON . decode ( response . body )
end
response
end
def authenticate
options = {
2013-03-13 17:01:32 -04:00
:rackspace_api_key = > @rackspace_api_key ,
2012-08-21 15:21:25 -04:00
:rackspace_username = > @rackspace_username ,
2013-05-30 13:52:37 -04:00
:rackspace_auth_url = > @rackspace_auth_url ,
:connection_options = > @connection_options
2012-08-21 15:21:25 -04:00
}
2013-03-13 17:01:32 -04:00
super ( options )
end
def service_name
:cloudBlockStorage
end
def region
@rackspace_region
end
def endpoint_uri ( service_endpoint_url = nil )
@uri = super ( @rackspace_endpoint || service_endpoint_url , :rackspace_block_storage_url )
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_block_storage_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
@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_block_storage_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-08-21 15:21:25 -04:00
credentials = Fog :: Rackspace . authenticate ( options , @connection_options )
2013-03-15 17:38:33 -04:00
append_tenant_v1 credentials
2012-08-21 15:21:25 -04:00
@auth_token = credentials [ 'X-Auth-Token' ]
end
2013-03-13 17:01:32 -04:00
2012-08-21 15:21:25 -04:00
end
end
end
end