2010-11-01 14:50:30 -04:00
|
|
|
module Fog
|
2011-06-15 20:32:15 -04:00
|
|
|
module CDN
|
|
|
|
class Rackspace < Fog::Service
|
2010-11-01 14:50:30 -04:00
|
|
|
|
2010-12-16 18:31:24 -05:00
|
|
|
requires :rackspace_api_key, :rackspace_username
|
|
|
|
recognizes :rackspace_auth_url, :persistent
|
2010-11-01 14:50:30 -04:00
|
|
|
|
2011-01-07 20:02:41 -05:00
|
|
|
model_path 'fog/cdn/models/rackspace'
|
2010-11-01 15:31:39 -04:00
|
|
|
|
2011-01-07 20:02:41 -05:00
|
|
|
request_path 'fog/cdn/requests/rackspace'
|
2010-11-05 14:37:12 -04:00
|
|
|
request :get_containers
|
|
|
|
request :head_container
|
2011-03-04 15:27:52 -05:00
|
|
|
request :post_container
|
2010-11-05 14:37:12 -04:00
|
|
|
request :put_container
|
2010-11-01 14:50:30 -04: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 14:50:30 -04:00
|
|
|
def initialize(options={})
|
|
|
|
@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-11-01 14:50:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(options={})
|
2011-07-15 22:34:16 -04:00
|
|
|
require 'multi_json'
|
2010-11-01 14:50:30 -04:00
|
|
|
credentials = Fog::Rackspace.authenticate(options)
|
|
|
|
@auth_token = credentials['X-Auth-Token']
|
2011-07-01 16:04:42 -04:00
|
|
|
@enabled = false
|
|
|
|
|
|
|
|
if credentials['X-CDN-Management-Url']
|
2011-07-11 18:12:15 -04: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 16:04:42 -04:00
|
|
|
end
|
|
|
|
end
|
2010-11-01 14:50:30 -04:00
|
|
|
|
2011-07-01 16:04:42 -04:00
|
|
|
def enabled?
|
2011-07-11 18:12:15 -04:00
|
|
|
@enabled
|
2010-11-01 14:50:30 -04: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-29 23:07:47 -05:00
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
2010-11-01 14:50:30 -04: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-15 22:34:16 -04:00
|
|
|
response.body = ::MultiJson.decode(response.body)
|
2010-11-01 14:50:30 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|