2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/rackspace'
|
2013-02-28 12:35:25 -05:00
|
|
|
require 'fog/rackspace/service'
|
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
|
2013-02-19 15:43:06 -05:00
|
|
|
recognizes :rackspace_auth_url, :rackspace_servicenet, :rackspace_cdn_ssl, :persistent, :rackspace_region
|
2013-02-21 14:07:29 -05:00
|
|
|
recognizes :rackspace_temp_url_key, :rackspace_storage_url, :rackspace_cdn_url
|
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
|
2013-02-15 15:26:45 -05:00
|
|
|
model :account
|
2010-09-08 16:50:23 -04:00
|
|
|
|
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
|
2012-06-12 04:28:17 -04:00
|
|
|
request :get_object_https_url
|
2010-09-08 16:50:23 -04:00
|
|
|
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
|
2012-06-12 04:42:09 -04:00
|
|
|
request :post_set_meta_temp_url_key
|
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,
|
2013-02-21 14:07:29 -05:00
|
|
|
:rackspace_cdn_url => @rackspace_cdn_url,
|
2013-02-19 16:53:54 -05:00
|
|
|
:rackspace_username => @rackspace_username,
|
2013-02-25 10:19:19 -05:00
|
|
|
:rackspace_region => @rackspace_region,
|
|
|
|
:rackspace_cdn_ssl => @rackspace_cdn_ssl
|
2010-11-05 14:37:12 -04:00
|
|
|
)
|
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
|
|
|
|
|
2013-02-20 14:02:32 -05:00
|
|
|
end
|
2010-09-08 16:50:23 -04:00
|
|
|
|
2013-02-28 15:57:09 -05:00
|
|
|
class Mock < Fog::Rackspace::Service
|
2010-09-08 16:50:23 -04:00
|
|
|
include Utils
|
2013-02-28 12:35:25 -05:00
|
|
|
|
2010-09-08 16:50:23 -04:00
|
|
|
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
|
|
|
|
|
2013-02-28 15:57:09 -05:00
|
|
|
def service_name
|
|
|
|
:cloudFiles
|
|
|
|
end
|
|
|
|
|
|
|
|
def region
|
|
|
|
@rackspace_region
|
|
|
|
end
|
|
|
|
|
2010-09-08 16:50:23 -04:00
|
|
|
end
|
|
|
|
|
2013-02-28 12:35:25 -05:00
|
|
|
class Real < Fog::Rackspace::Service
|
2010-09-08 16:50:23 -04:00
|
|
|
include Utils
|
2013-02-28 12:35:25 -05:00
|
|
|
|
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]
|
2013-02-21 14:07:29 -05:00
|
|
|
@rackspace_cdn_url = options[:rackspace_cdn_url]
|
2013-02-19 15:43:06 -05:00
|
|
|
@rackspace_region = options[:rackspace_region] || :dfw
|
2012-06-12 04:28:17 -04:00
|
|
|
@rackspace_temp_url_key = options[:rackspace_temp_url_key]
|
2012-05-17 12:07:30 -04:00
|
|
|
@rackspace_must_reauthenticate = false
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2013-02-20 16:48:19 -05:00
|
|
|
|
2013-02-28 15:57:09 -05:00
|
|
|
@auth_token = authenticate
|
2011-09-12 11:01:48 -04:00
|
|
|
@persistent = options[:persistent] || false
|
2013-02-20 10:04:56 -05:00
|
|
|
Excon.defaults[:ssl_verify_peer] = false if service_net?
|
|
|
|
@connection = Fog::Connection.new(endpoint_uri, @persistent, @connection_options)
|
2013-02-19 15:43:06 -05:00
|
|
|
end
|
2013-02-25 10:19:19 -05:00
|
|
|
|
2013-02-15 15:26:45 -05:00
|
|
|
def account
|
|
|
|
account = Fog::Storage::Rackspace::Account.new(:service => self)
|
|
|
|
account.reload
|
|
|
|
end
|
2010-09-08 16:50:23 -04:00
|
|
|
|
2013-02-25 10:19:19 -05:00
|
|
|
def ssl?
|
|
|
|
!rackspace_cdn_ssl.nil?
|
|
|
|
end
|
2010-09-08 16:50:23 -04:00
|
|
|
|
|
|
|
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] || {}),
|
2013-02-20 10:04:56 -05:00
|
|
|
:host => endpoint_uri.host,
|
|
|
|
:path => "#{endpoint_uri.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
|
|
|
|
|
2013-02-20 10:04:56 -05:00
|
|
|
def service_net?
|
|
|
|
@rackspace_servicenet == true
|
2013-02-21 14:07:29 -05:00
|
|
|
end
|
2013-02-19 15:43:06 -05:00
|
|
|
|
2012-05-17 12:07:30 -04:00
|
|
|
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
|
2013-02-19 15:43:06 -05:00
|
|
|
}
|
2013-02-28 12:35:25 -05:00
|
|
|
@auth_token = super(options)
|
2012-05-17 12:07:30 -04:00
|
|
|
else
|
|
|
|
@auth_token = @rackspace_auth_token
|
2013-02-20 10:04:56 -05:00
|
|
|
@uri = URI.parse(@rackspace_storage_url)
|
2012-05-17 12:07:30 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-20 10:04:56 -05:00
|
|
|
|
2013-02-28 15:57:09 -05:00
|
|
|
def service_name
|
|
|
|
:cloudFiles
|
|
|
|
end
|
2013-02-28 12:35:25 -05:00
|
|
|
|
2013-02-28 15:57:09 -05:00
|
|
|
def region
|
|
|
|
@rackspace_region
|
|
|
|
end
|
|
|
|
|
|
|
|
def endpoint_uri(service_endpoint_url=nil)
|
|
|
|
@uri = super(@rackspace_storage_url || service_endpoint_url, :rackspace_storage_url)
|
2013-02-20 14:02:32 -05:00
|
|
|
@uri.host = "snet-#{@uri.host}" if service_net?
|
2013-02-20 10:04:56 -05:00
|
|
|
@uri
|
2013-02-19 15:43:06 -05:00
|
|
|
end
|
2013-02-20 14:02:32 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def authenticate_v1(options)
|
|
|
|
credentials = Fog::Rackspace.authenticate(options, @connection_options)
|
|
|
|
endpoint_uri credentials['X-Storage-Url']
|
2013-02-28 12:35:25 -05:00
|
|
|
credentials['X-Auth-Token']
|
2013-02-20 14:02:32 -05:00
|
|
|
end
|
|
|
|
|
2010-09-08 16:50:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|