2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/rackspace'
|
2011-08-31 16:52:53 -04:00
|
|
|
require 'fog/storage'
|
|
|
|
|
2010-09-08 16:50:23 -04:00
|
|
|
module Fog
|
2011-06-15 17:26:43 -04:00
|
|
|
module Storage
|
|
|
|
class Rackspace < Fog::Service
|
2010-09-08 16:50:23 -04:00
|
|
|
|
2010-12-16 18:31:24 -05:00
|
|
|
requires :rackspace_api_key, :rackspace_username
|
2011-03-23 09:07:33 -04:00
|
|
|
recognizes :rackspace_auth_url, :rackspace_servicenet, :rackspace_cdn_ssl, :persistent
|
2010-09-08 16:50:23 -04:00
|
|
|
|
2011-08-24 15:12:29 -04:00
|
|
|
model_path 'fog/rackspace/models/storage'
|
2010-09-08 16:50:23 -04:00
|
|
|
model :directory
|
|
|
|
collection :directories
|
|
|
|
model :file
|
|
|
|
collection :files
|
|
|
|
|
2011-08-24 15:12:29 -04:00
|
|
|
request_path 'fog/rackspace/requests/storage'
|
2012-02-16 14:39:32 -05:00
|
|
|
request :copy_object
|
2010-09-08 16:50:23 -04:00
|
|
|
request :delete_container
|
|
|
|
request :delete_object
|
|
|
|
request :get_container
|
|
|
|
request :get_containers
|
|
|
|
request :get_object
|
|
|
|
request :head_container
|
|
|
|
request :head_containers
|
|
|
|
request :head_object
|
|
|
|
request :put_container
|
|
|
|
request :put_object
|
2011-06-24 19:00:44 -04:00
|
|
|
request :put_object_manifest
|
2010-09-08 16:50:23 -04:00
|
|
|
|
|
|
|
module Utils
|
|
|
|
|
2010-11-05 14:37:12 -04:00
|
|
|
def cdn
|
2011-01-21 19:10:53 -05:00
|
|
|
@cdn ||= Fog::CDN.new(
|
|
|
|
:provider => 'Rackspace',
|
|
|
|
:rackspace_api_key => @rackspace_api_key,
|
2011-07-11 18:12:15 -04:00
|
|
|
:rackspace_auth_url => @rackspace_auth_url,
|
2010-11-05 14:37:12 -04:00
|
|
|
:rackspace_username => @rackspace_username
|
|
|
|
)
|
2011-07-01 16:04:42 -04:00
|
|
|
if @cdn.enabled?
|
2011-07-11 18:12:15 -04:00
|
|
|
@cdn
|
2011-07-01 16:04:42 -04:00
|
|
|
end
|
2010-11-05 14:37:12 -04:00
|
|
|
end
|
|
|
|
|
2010-09-08 16:50:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
include Utils
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-19 10:05:33 -04:00
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
2010-09-08 16:50:23 -04:00
|
|
|
def initialize(options={})
|
2010-10-29 17:58:28 -04:00
|
|
|
require 'mime/types'
|
2010-11-05 14:37:12 -04:00
|
|
|
@rackspace_api_key = options[:rackspace_api_key]
|
2010-09-08 16:50:23 -04:00
|
|
|
@rackspace_username = options[:rackspace_username]
|
2011-05-19 18:35:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data[@rackspace_username]
|
2011-03-10 14:16:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data.delete(@rackspace_username)
|
2010-09-08 16:50:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
include Utils
|
2011-03-23 09:07:33 -04:00
|
|
|
attr_reader :rackspace_cdn_ssl
|
2010-09-08 16:50:23 -04:00
|
|
|
|
|
|
|
def initialize(options={})
|
2010-10-29 17:58:28 -04:00
|
|
|
require 'mime/types'
|
2010-11-05 14:37:12 -04:00
|
|
|
@rackspace_api_key = options[:rackspace_api_key]
|
|
|
|
@rackspace_username = options[:rackspace_username]
|
2011-03-23 09:07:33 -04:00
|
|
|
@rackspace_cdn_ssl = options[:rackspace_cdn_ssl]
|
2011-07-01 16:04:42 -04:00
|
|
|
@rackspace_auth_url = options[:rackspace_auth_url]
|
2012-05-17 12:07:30 -04:00
|
|
|
@rackspace_servicenet = options[:rackspace_servicenet]
|
|
|
|
@rackspace_auth_token = options[:rackspace_auth_token]
|
|
|
|
@rackspace_storage_url = options[:rackspace_storage_url]
|
|
|
|
@rackspace_must_reauthenticate = false
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2012-05-17 12:07:30 -04:00
|
|
|
authenticate
|
2011-09-12 11:01:48 -04:00
|
|
|
@persistent = options[:persistent] || false
|
2011-03-18 11:17:46 -04:00
|
|
|
Excon.ssl_verify_peer = false if options[:rackspace_servicenet] == true
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
2010-09-08 16:50:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
2011-06-10 13:16:07 -04:00
|
|
|
@connection.reset
|
2010-09-08 16:50:23 -04:00
|
|
|
end
|
|
|
|
|
2010-11-01 14:50:30 -04:00
|
|
|
def request(params, parse_json = true, &block)
|
2010-09-08 16:50:23 -04:00
|
|
|
begin
|
2012-07-08 08:07:02 -04:00
|
|
|
response = @connection.request(params.merge({
|
2010-09-08 16:50:23 -04:00
|
|
|
:headers => {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'X-Auth-Token' => @auth_token
|
|
|
|
}.merge!(params[:headers] || {}),
|
2010-11-01 14:50:30 -04:00
|
|
|
:host => @host,
|
|
|
|
:path => "#{@path}/#{params[:path]}",
|
2010-09-08 16:50:23 -04:00
|
|
|
}), &block)
|
2012-05-17 12:07:30 -04:00
|
|
|
rescue Excon::Errors::Unauthorized => error
|
|
|
|
if error.response.body != 'Bad username or password' # token expiration
|
|
|
|
@rackspace_must_reauthenticate = true
|
|
|
|
authenticate
|
|
|
|
retry
|
|
|
|
else # bad credentials
|
|
|
|
raise error
|
|
|
|
end
|
2010-11-29 23:07:47 -05:00
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
2010-09-08 16:50:23 -04:00
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
2011-06-15 17:26:43 -04:00
|
|
|
Fog::Storage::Rackspace::NotFound.slurp(error)
|
2010-09-08 16:50:23 -04:00
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
|
2012-04-25 10:31:28 -04:00
|
|
|
response.body = Fog::JSON.decode(response.body)
|
2010-09-08 16:50:23 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2012-05-17 12:07:30 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def authenticate
|
|
|
|
if @rackspace_must_reauthenticate || @rackspace_auth_token.nil?
|
|
|
|
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']
|
|
|
|
uri = URI.parse(credentials['X-Storage-Url'])
|
|
|
|
else
|
|
|
|
@auth_token = @rackspace_auth_token
|
|
|
|
uri = URI.parse(@rackspace_storage_url)
|
|
|
|
end
|
|
|
|
@host = @rackspace_servicenet == true ? "snet-#{uri.host}" : uri.host
|
|
|
|
@path = uri.path
|
|
|
|
@port = uri.port
|
|
|
|
@scheme = uri.scheme
|
|
|
|
end
|
|
|
|
|
2010-09-08 16:50:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|