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
|
2013-04-15 10:31:13 -04:00
|
|
|
include Fog::Rackspace::Errors
|
2010-09-08 16:50:23 -04:00
|
|
|
|
2013-04-15 14:21:37 -04:00
|
|
|
class ServiceError < Fog::Rackspace::Errors::ServiceError; end
|
|
|
|
class InternalServerError < Fog::Rackspace::Errors::InternalServerError; end
|
|
|
|
class BadRequest < Fog::Rackspace::Errors::BadRequest; end
|
|
|
|
|
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
|
2013-07-02 18:21:37 -04:00
|
|
|
request :delete_static_large_object
|
2013-07-02 18:20:57 -04:00
|
|
|
request :delete_multiple_objects
|
2010-09-08 16:50:23 -04:00
|
|
|
request :get_container
|
|
|
|
request :get_containers
|
|
|
|
request :get_object
|
2012-06-12 04:28:17 -04:00
|
|
|
request :get_object_https_url
|
2013-10-28 16:05:28 -04:00
|
|
|
request :get_object_http_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
|
2013-07-02 18:21:40 -04:00
|
|
|
request :put_dynamic_obj_manifest
|
2013-07-02 18:21:37 -04:00
|
|
|
request :put_static_obj_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-07-02 18:21:37 -04: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-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]
|
2013-08-19 19:05:07 -04:00
|
|
|
@rackspace_cdn_ssl = options[:rackspace_cdn_ssl]
|
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
|
|
|
|
|
2013-08-19 19:05:07 -04:00
|
|
|
def ssl?
|
|
|
|
!!@rackspace_cdn_ssl
|
|
|
|
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-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-07-02 18:21:37 -04: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-03-01 15:48:26 -05:00
|
|
|
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?
|
2013-03-01 15:46:14 -05:00
|
|
|
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
|
2013-07-02 18:21:37 -04:00
|
|
|
end
|
|
|
|
|
2013-03-07 17:01:42 -05:00
|
|
|
# Return Account Details
|
|
|
|
# @return [Fog::Storage::Rackspace::Account] account details object
|
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-03-07 17:01:42 -05:00
|
|
|
# Using SSL?
|
|
|
|
# @return [Boolean] return true if service is returning SSL-Secured URLs in public_url methods
|
|
|
|
# @see Directory#public_url
|
2013-02-25 10:19:19 -05:00
|
|
|
def ssl?
|
2013-08-19 19:05:07 -04:00
|
|
|
!!rackspace_cdn_ssl
|
2013-02-25 10:19:19 -05:00
|
|
|
end
|
2010-09-08 16:50:23 -04:00
|
|
|
|
2013-03-07 17:01:42 -05:00
|
|
|
# Resets presistent service connections
|
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
|
|
|
|
|
2013-09-24 13:02:10 -04:00
|
|
|
def request(params, parse_json = true)
|
|
|
|
super
|
2013-06-21 16:11:17 -04:00
|
|
|
rescue Excon::Errors::NotFound => error
|
2013-08-01 15:31:51 -04:00
|
|
|
raise NotFound.slurp(error, self)
|
2013-06-21 16:11:17 -04:00
|
|
|
rescue Excon::Errors::BadRequest => error
|
2013-08-01 15:31:51 -04:00
|
|
|
raise BadRequest.slurp(error, self)
|
2013-06-21 16:11:17 -04:00
|
|
|
rescue Excon::Errors::InternalServerError => error
|
2013-08-01 15:31:51 -04:00
|
|
|
raise InternalServerError.slurp(error, self)
|
2013-06-21 16:11:17 -04:00
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
2013-08-01 15:31:51 -04:00
|
|
|
raise ServiceError.slurp(error, self)
|
2010-09-08 16:50:23 -04:00
|
|
|
end
|
|
|
|
|
2013-02-20 10:04:56 -05:00
|
|
|
def service_net?
|
|
|
|
@rackspace_servicenet == true
|
2013-07-02 18:21:37 -04:00
|
|
|
end
|
|
|
|
|
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,
|
2013-05-30 13:52:37 -04:00
|
|
|
:rackspace_auth_url => @rackspace_auth_url,
|
|
|
|
:connection_options => @connection_options
|
2013-07-02 18:21:37 -04:00
|
|
|
}
|
2013-03-01 15:48:26 -05:00
|
|
|
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-07-02 18:21:37 -04:00
|
|
|
|
2013-02-28 15:57:09 -05:00
|
|
|
def service_name
|
|
|
|
:cloudFiles
|
|
|
|
end
|
2013-02-28 12:35:25 -05:00
|
|
|
|
2013-08-01 15:31:51 -04:00
|
|
|
def request_id_header
|
|
|
|
"X-Trans-Id"
|
|
|
|
end
|
|
|
|
|
2013-02-28 15:57:09 -05:00
|
|
|
def region
|
|
|
|
@rackspace_region
|
|
|
|
end
|
|
|
|
|
|
|
|
def endpoint_uri(service_endpoint_url=nil)
|
2013-03-13 17:01:32 -04:00
|
|
|
return @uri if @uri
|
|
|
|
|
2013-02-28 15:57:09 -05:00
|
|
|
@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-07-02 18:21:37 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-02-20 14:02:32 -05:00
|
|
|
def authenticate_v1(options)
|
|
|
|
credentials = Fog::Rackspace.authenticate(options, @connection_options)
|
|
|
|
endpoint_uri credentials['X-Storage-Url']
|
2013-03-01 15:48:26 -05:00
|
|
|
@auth_token = credentials['X-Auth-Token']
|
2013-02-20 14:02:32 -05:00
|
|
|
end
|
2013-07-02 18:21:37 -04:00
|
|
|
|
2010-09-08 16:50:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|