2010-11-01 11:50:30 -07:00
|
|
|
module Fog
|
2011-06-15 17:32:15 -07:00
|
|
|
module CDN
|
|
|
|
class Rackspace < Fog::Service
|
2010-11-01 11:50:30 -07:00
|
|
|
|
2010-12-16 15:31:24 -08:00
|
|
|
requires :rackspace_api_key, :rackspace_username
|
|
|
|
recognizes :rackspace_auth_url, :persistent
|
2010-11-01 11:50:30 -07:00
|
|
|
|
2011-01-07 17:02:41 -08:00
|
|
|
model_path 'fog/cdn/models/rackspace'
|
2010-11-01 12:31:39 -07:00
|
|
|
|
2011-01-07 17:02:41 -08:00
|
|
|
request_path 'fog/cdn/requests/rackspace'
|
2010-11-05 11:37:12 -07:00
|
|
|
request :get_containers
|
|
|
|
request :head_container
|
2011-03-04 12:27:52 -08:00
|
|
|
request :post_container
|
2010-11-05 11:37:12 -07:00
|
|
|
request :put_container
|
2010-11-01 11:50:30 -07:00
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
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-11-01 11:50:30 -07:00
|
|
|
def initialize(options={})
|
|
|
|
@rackspace_username = options[:rackspace_username]
|
2011-05-19 15:35:33 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data[@rackspace_username]
|
2011-03-10 11:16:55 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data.delete(@rackspace_username)
|
2010-11-01 11:50:30 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(options={})
|
2011-07-16 04:34:16 +02:00
|
|
|
require 'multi_json'
|
2010-11-01 11:50:30 -07:00
|
|
|
credentials = Fog::Rackspace.authenticate(options)
|
|
|
|
@auth_token = credentials['X-Auth-Token']
|
2011-07-01 15:04:42 -05:00
|
|
|
@enabled = false
|
|
|
|
|
|
|
|
if credentials['X-CDN-Management-Url']
|
2011-07-11 17:12:15 -05:00
|
|
|
uri = URI.parse(credentials['X-CDN-Management-Url'])
|
|
|
|
@host = uri.host
|
|
|
|
@path = uri.path
|
|
|
|
@port = uri.port
|
|
|
|
@scheme = uri.scheme
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
|
|
|
|
@enabled = true
|
2011-07-01 15:04:42 -05:00
|
|
|
end
|
|
|
|
end
|
2010-11-01 11:50:30 -07:00
|
|
|
|
2011-07-01 15:04:42 -05:00
|
|
|
def enabled?
|
2011-07-11 17:12:15 -05:00
|
|
|
@enabled
|
2010-11-01 11:50:30 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@cdn_connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(params, parse_json = true)
|
|
|
|
begin
|
|
|
|
response = @connection.request(params.merge!({
|
|
|
|
:headers => {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'X-Auth-Token' => @auth_token
|
|
|
|
}.merge!(params[:headers] || {}),
|
|
|
|
:host => @host,
|
|
|
|
:path => "#{@path}/#{params[:path]}",
|
|
|
|
}))
|
2010-11-30 12:07:47 +08:00
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
2010-11-01 11:50:30 -07:00
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
|
|
|
Fog::Rackspace::Storage::NotFound.slurp(error)
|
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
|
2011-07-20 11:08:11 -05:00
|
|
|
response.body = MultiJson.decode(response.body)
|
2010-11-01 11:50:30 -07:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|