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-25 10:19:19 -05:00
|
|
|
recognizes :rackspace_auth_url, :persistent, :rackspace_cdn_ssl, :rackspace_region, :rackspace_cdn_url
|
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-25 10:19:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
module Base
|
|
|
|
URI_HEADERS = {
|
|
|
|
"X-Cdn-Ios-Uri" => :ios_uri,
|
|
|
|
"X-Cdn-Uri" => :uri,
|
|
|
|
"X-Cdn-Streaming-Uri" => :streaming_uri,
|
|
|
|
"X-Cdn-Ssl-Uri" => :ssl_uri
|
|
|
|
}.freeze
|
|
|
|
|
2013-02-28 15:57:09 -05:00
|
|
|
def service_name
|
|
|
|
:cloudFilesCDN
|
|
|
|
end
|
|
|
|
|
|
|
|
def region
|
|
|
|
@rackspace_region
|
|
|
|
end
|
|
|
|
|
|
|
|
def endpoint_uri(service_endpoint_url=nil)
|
|
|
|
@uri = super(@rackspace_cdn_url || service_endpoint_url, :rackspace_cdn_url)
|
|
|
|
end
|
|
|
|
|
2013-03-27 10:50:30 -04:00
|
|
|
# Publish container to CDN
|
|
|
|
# @param [Fog::Storage::Rackspace::Directory] directory to publish
|
|
|
|
# @param [Boolean] publish If true directory is published. If false directory is unpublished.
|
|
|
|
# @return [Hash] hash containing urls for published container
|
|
|
|
# @raise [Fog::Rackspace::Errors::NotFound] - HTTP 404
|
|
|
|
# @raise [Fog::Rackspace::Errors::BadRequest] - HTTP 400
|
|
|
|
# @raise [Fog::Rackspace::Errors::InternalServerError] - HTTP 500
|
|
|
|
# @raise [Fog::Rackspace::Errors::ServiceError]
|
2013-02-25 10:19:19 -05:00
|
|
|
def publish_container(container, publish = true)
|
|
|
|
enabled = publish ? 'True' : 'False'
|
|
|
|
response = put_container(container.key, 'X-Cdn-Enabled' => enabled)
|
|
|
|
return {} unless publish
|
|
|
|
urls_from_headers(response.headers)
|
|
|
|
end
|
|
|
|
|
2013-03-27 10:50:30 -04:00
|
|
|
# Returns hash of urls for container
|
|
|
|
# @param [Fog::Storage::Rackspace::Directory] container to retrieve urls for
|
|
|
|
# @return [Hash] hash containing urls for published container
|
|
|
|
# @raise [Fog::Rackspace::Errors::BadRequest] - HTTP 400
|
|
|
|
# @raise [Fog::Rackspace::Errors::InternalServerError] - HTTP 500
|
2013-03-27 13:22:22 -04:00
|
|
|
# @raise [Fog::Rackspace::Errors::ServiceError]
|
2013-03-27 10:50:30 -04:00
|
|
|
# @note If unable to find container or container is not published this method will return an empty hash.
|
2013-02-25 10:19:19 -05:00
|
|
|
def urls(container)
|
|
|
|
begin
|
|
|
|
response = head_container(container.key)
|
|
|
|
return {} unless response.headers['X-Cdn-Enabled'] == 'True'
|
|
|
|
urls_from_headers response.headers
|
|
|
|
rescue Fog::Service::NotFound
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def urls_from_headers(headers)
|
|
|
|
h = {}
|
|
|
|
URI_HEADERS.keys.each do | header |
|
|
|
|
key = URI_HEADERS[header]
|
|
|
|
h[key] = headers[header]
|
|
|
|
end
|
|
|
|
h
|
|
|
|
end
|
|
|
|
end
|
2010-11-01 14:50:30 -04:00
|
|
|
|
2013-02-28 15:57:09 -05:00
|
|
|
class Mock < Fog::Rackspace::Service
|
2013-02-25 10:19:19 -05:00
|
|
|
include Base
|
2010-11-01 14:50:30 -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-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
|
2013-02-25 10:19:19 -05:00
|
|
|
|
|
|
|
def purge(object)
|
|
|
|
return true if object.is_a? Fog::Storage::Rackspace::File
|
|
|
|
raise Fog::Errors::NotImplemented.new("#{object.class} does not support CDN purging") if object
|
|
|
|
end
|
2011-03-10 14:16:55 -05:00
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data.delete(@rackspace_username)
|
2010-11-01 14:50:30 -04:00
|
|
|
end
|
2013-02-01 17:16:37 -05:00
|
|
|
|
2010-11-01 14:50:30 -04:00
|
|
|
end
|
|
|
|
|
2013-02-28 15:57:09 -05:00
|
|
|
class Real < Fog::Rackspace::Service
|
2013-02-25 10:19:19 -05:00
|
|
|
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] || {}
|
2013-02-19 16:53:54 -05:00
|
|
|
@rackspace_auth_url = options[:rackspace_auth_url]
|
2013-02-20 14:02:32 -05:00
|
|
|
@rackspace_cdn_url = options[:rackspace_cdn_url]
|
|
|
|
@rackspace_region = options[:rackspace_region] || :dfw
|
2013-03-01 15:48:26 -05:00
|
|
|
authenticate(options)
|
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
|
|
|
|
2013-02-20 14:02:32 -05:00
|
|
|
if endpoint_uri
|
2013-03-01 15:46:14 -05:00
|
|
|
@connection = Fog::Connection.new(endpoint_uri.to_s, @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-25 10:32:08 -05:00
|
|
|
|
2013-03-27 10:50:30 -04:00
|
|
|
# Returns true if CDN service is enabled
|
|
|
|
# @return [Boolean]
|
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
|
|
|
|
|
2013-03-27 10:50:30 -04:00
|
|
|
# Resets CDN connection
|
2010-11-01 14:50:30 -04:00
|
|
|
def reload
|
|
|
|
@cdn_connection.reset
|
|
|
|
end
|
2013-02-25 10:19:19 -05:00
|
|
|
|
2013-03-27 10:50:30 -04:00
|
|
|
# Purges File
|
|
|
|
# @param [Fog::Storage::Rackspace::File] file to be purged from the CDN
|
|
|
|
# @raise [Fog::Errors::NotImplemented] returned when non file parameters are specified
|
2013-02-25 10:19:19 -05:00
|
|
|
def purge(file)
|
|
|
|
unless file.is_a? Fog::Storage::Rackspace::File
|
2013-02-25 10:32:08 -05:00
|
|
|
raise Fog::Errors::NotImplemented.new("#{object.class} does not support CDN purging") if object
|
2013-02-25 10:19:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
delete_object file.directory.key, file.key
|
|
|
|
true
|
|
|
|
end
|
2010-11-01 14:50:30 -04:00
|
|
|
|
|
|
|
def request(params, parse_json = true)
|
|
|
|
begin
|
|
|
|
response = @connection.request(params.merge!({
|
|
|
|
:headers => {
|
|
|
|
'Content-Type' => 'application/json',
|
2013-03-01 17:59:59 -05:00
|
|
|
'Accept' => 'application/json',
|
2013-03-01 15:48:26 -05:00
|
|
|
'X-Auth-Token' => auth_token
|
2010-11-01 14:50:30 -04:00
|
|
|
}.merge!(params[:headers] || {}),
|
2013-02-20 14:02:32 -05:00
|
|
|
:host => endpoint_uri.host,
|
|
|
|
:path => "#{endpoint_uri.path}/#{params[:path]}",
|
2010-11-01 14:50:30 -04:00
|
|
|
}))
|
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
|
2013-02-20 14:02:32 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def authenticate_v1(options)
|
|
|
|
credentials = Fog::Rackspace.authenticate(options, @connection_options)
|
2013-02-28 15:57:09 -05:00
|
|
|
endpoint_uri credentials['X-CDN-Management-Url']
|
2013-03-01 15:48:26 -05:00
|
|
|
@auth_token = credentials['X-Auth-Token']
|
2013-02-20 14:02:32 -05:00
|
|
|
end
|
2010-11-01 14:50:30 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|