2011-08-31 16:52:53 -04:00
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rackspace'))
|
|
|
|
require 'fog/cdn'
|
|
|
|
|
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-08-24 15:22:49 -04:00
|
|
|
model_path 'fog/rackspace/models/cdn'
|
2010-11-01 15:31:39 -04:00
|
|
|
|
2011-08-24 15:22:49 -04:00
|
|
|
request_path 'fog/rackspace/requests/cdn'
|
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-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
|
|
|
credentials = Fog::Rackspace.authenticate(options, @connection_options)
|
2010-11-01 14:50:30 -04:00
|
|
|
@auth_token = credentials['X-Auth-Token']
|
2011-07-01 16:04:42 -04:00
|
|
|
@enabled = false
|
2011-09-12 11:01:48 -04:00
|
|
|
@persistent = options[:persistent] || false
|
2011-07-01 16:04:42 -04:00
|
|
|
|
|
|
|
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
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
2011-07-11 18:12:15 -04:00
|
|
|
@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
|
2011-09-03 09:52:51 -04:00
|
|
|
Fog::Storage::Rackspace::NotFound.slurp(error)
|
2010-11-01 14:50:30 -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-11-01 14:50:30 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|