2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/rackspace'
|
2011-08-31 16:52:53 -04:00
|
|
|
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
|
2013-02-05 11:41:59 -05:00
|
|
|
recognizes :rackspace_auth_url, :persistent, :rackspace_cdn_ssl
|
2010-11-01 14:50:30 -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
|
2013-02-01 17:16:37 -05:00
|
|
|
request :delete_object
|
2013-02-05 11:41:59 -05:00
|
|
|
|
|
|
|
|
|
|
|
module Base
|
2013-02-04 10:02:30 -05:00
|
|
|
def ssl?
|
|
|
|
!@rackspace_cdn_ssl.nil?
|
|
|
|
end
|
|
|
|
|
2013-02-11 15:39:48 -05:00
|
|
|
def purge(file)
|
|
|
|
unless file.is_a? Fog::Storage::Rackspace::File
|
|
|
|
raise Fog::Errors::NotImplemented.new("#{object.class} does not support CDN purging")
|
|
|
|
end
|
|
|
|
|
|
|
|
delete_object file.directory.key, file.key
|
|
|
|
true
|
2013-02-01 17:16:37 -05:00
|
|
|
end
|
2013-02-11 15:39:48 -05:00
|
|
|
|
2013-02-04 10:02:30 -05:00
|
|
|
def publish_container(container, publish = true)
|
|
|
|
enabled = publish ? 'True' : 'False'
|
2013-02-11 15:39:48 -05:00
|
|
|
response = put_container(container.key, 'X-CDN-Enabled' => enabled)
|
2013-02-04 10:02:30 -05:00
|
|
|
url_from_headers(response.headers, container.cdn_cname)
|
|
|
|
end
|
|
|
|
|
2013-02-11 15:39:48 -05:00
|
|
|
def public_url(container)
|
2013-02-04 10:02:30 -05:00
|
|
|
begin
|
2013-02-11 15:39:48 -05:00
|
|
|
response = head_container(container.key)
|
2013-02-04 10:02:30 -05:00
|
|
|
if response.headers['X-Cdn-Enabled'] == 'True'
|
2013-02-11 15:39:48 -05:00
|
|
|
url_from_headers(response.headers, container.cdn_cname)
|
2013-02-04 10:02:30 -05:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
rescue Fog::Service::NotFound
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def url_from_headers(headers, cdn_cname)
|
2013-02-12 08:41:27 -05:00
|
|
|
return nil unless headers['X-Cdn-Enabled']
|
|
|
|
return headers['X-Cdn-Ssl-Uri'] if ssl?
|
|
|
|
cdn_cname || headers['X-Cdn-Uri']
|
2013-02-04 10:02:30 -05:00
|
|
|
end
|
2010-11-01 14:50:30 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 11:41:59 -05:00
|
|
|
class Mock
|
|
|
|
include Base
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
2010-11-01 14:50:30 -04:00
|
|
|
|
2013-02-05 11:41:59 -05:00
|
|
|
def initialize(options={})
|
|
|
|
@rackspace_username = options[:rackspace_username]
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data[@rackspace_username]
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data.delete(@rackspace_username)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
include Base
|
|
|
|
|
2010-11-01 14:50:30 -04:00
|
|
|
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
|
2013-02-01 17:16:37 -05: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
|