2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/rackspace'
|
2011-08-31 15:52:53 -05:00
|
|
|
require 'fog/cdn'
|
|
|
|
|
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-12-16 15:31:24 -08:00
|
|
|
requires :rackspace_api_key, :rackspace_username
|
2013-02-25 09:19:19 -06:00
|
|
|
recognizes :rackspace_auth_url, :persistent, :rackspace_cdn_ssl, :rackspace_region, :rackspace_cdn_url
|
2010-11-01 11:50:30 -07:00
|
|
|
|
2011-08-24 14:22:49 -05:00
|
|
|
request_path 'fog/rackspace/requests/cdn'
|
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
|
2013-02-01 16:16:37 -06:00
|
|
|
request :delete_object
|
2013-02-25 09:19:19 -06: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 14:57:09 -06:00
|
|
|
def service_name
|
|
|
|
:cloudFilesCDN
|
|
|
|
end
|
|
|
|
|
|
|
|
def region
|
|
|
|
@rackspace_region
|
|
|
|
end
|
|
|
|
|
2013-08-01 14:31:51 -05:00
|
|
|
def request_id_header
|
|
|
|
"X-Trans-Id"
|
|
|
|
end
|
|
|
|
|
2013-02-28 14:57:09 -06:00
|
|
|
def endpoint_uri(service_endpoint_url=nil)
|
|
|
|
@uri = super(@rackspace_cdn_url || service_endpoint_url, :rackspace_cdn_url)
|
|
|
|
end
|
|
|
|
|
2013-03-27 09:50:30 -05:00
|
|
|
# Publish container to CDN
|
2013-05-02 12:22:05 +01:00
|
|
|
# @param [Fog::Storage::Rackspace::Directory] container directory to publish
|
2013-03-27 09:50:30 -05:00
|
|
|
# @param [Boolean] publish If true directory is published. If false directory is unpublished.
|
|
|
|
# @return [Hash] hash containing urls for published container
|
2013-04-15 13:12:14 -05:00
|
|
|
# @raise [Fog::Storage::Rackspace::NotFound] - HTTP 404
|
|
|
|
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
|
|
|
|
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
|
|
|
|
# @raise [Fog::Storage::Rackspace::ServiceError]
|
2013-02-25 09:19:19 -06: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 09:50:30 -05: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
|
2013-04-15 13:12:14 -05:00
|
|
|
# @raise [Fog::Storage::Rackspace::BadRequest] - HTTP 400
|
|
|
|
# @raise [Fog::Storage::Rackspace::InternalServerError] - HTTP 500
|
|
|
|
# @raise [Fog::Storage::Rackspace::ServiceError]
|
2013-03-27 09:50:30 -05:00
|
|
|
# @note If unable to find container or container is not published this method will return an empty hash.
|
2013-02-25 09:19:19 -06: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 11:50:30 -07:00
|
|
|
|
2013-02-28 14:57:09 -06:00
|
|
|
class Mock < Fog::Rackspace::Service
|
2013-02-25 09:19:19 -06:00
|
|
|
include Base
|
2010-11-01 11:50:30 -07: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 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
|
2013-02-25 09:19:19 -06: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 11:16:55 -08:00
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data.delete(@rackspace_username)
|
2010-11-01 11:50:30 -07:00
|
|
|
end
|
2013-02-01 16:16:37 -06:00
|
|
|
|
2010-11-01 11:50:30 -07:00
|
|
|
end
|
|
|
|
|
2013-02-28 14:57:09 -06:00
|
|
|
class Real < Fog::Rackspace::Service
|
2013-02-25 09:19:19 -06:00
|
|
|
include Base
|
2010-11-01 11:50:30 -07:00
|
|
|
|
|
|
|
def initialize(options={})
|
2013-08-07 12:55:26 -06:00
|
|
|
# api_key and username missing from instance variable sets
|
|
|
|
@rackspace_api_key = options[:rackspace_api_key]
|
|
|
|
@rackspace_username = options[:rackspace_username]
|
|
|
|
|
2011-09-12 10:01:48 -05:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2013-02-19 15:53:54 -06:00
|
|
|
@rackspace_auth_url = options[:rackspace_auth_url]
|
2013-02-20 13:02:32 -06:00
|
|
|
@rackspace_cdn_url = options[:rackspace_cdn_url]
|
|
|
|
@rackspace_region = options[:rackspace_region] || :dfw
|
2013-03-01 14:48:26 -06:00
|
|
|
authenticate(options)
|
2011-07-01 15:04:42 -05:00
|
|
|
@enabled = false
|
2011-09-12 10:01:48 -05:00
|
|
|
@persistent = options[:persistent] || false
|
2011-07-01 15:04:42 -05:00
|
|
|
|
2013-02-20 13:02:32 -06:00
|
|
|
if endpoint_uri
|
2013-03-01 14:46:14 -06:00
|
|
|
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
|
2011-07-11 17:12:15 -05:00
|
|
|
@enabled = true
|
2011-07-01 15:04:42 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-25 09:32:08 -06:00
|
|
|
|
2013-03-27 09:50:30 -05:00
|
|
|
# Returns true if CDN service is enabled
|
|
|
|
# @return [Boolean]
|
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
|
|
|
|
|
2013-03-27 09:50:30 -05:00
|
|
|
# Resets CDN connection
|
2010-11-01 11:50:30 -07:00
|
|
|
def reload
|
|
|
|
@cdn_connection.reset
|
|
|
|
end
|
2013-02-25 09:19:19 -06:00
|
|
|
|
2013-03-27 09:50:30 -05: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 09:19:19 -06:00
|
|
|
def purge(file)
|
|
|
|
unless file.is_a? Fog::Storage::Rackspace::File
|
2013-02-25 09:32:08 -06:00
|
|
|
raise Fog::Errors::NotImplemented.new("#{object.class} does not support CDN purging") if object
|
2013-02-25 09:19:19 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
delete_object file.directory.key, file.key
|
|
|
|
true
|
|
|
|
end
|
2010-11-01 11:50:30 -07:00
|
|
|
|
2013-06-24 10:13:57 -05:00
|
|
|
def request(params, parse_json = true, &block)
|
|
|
|
super(params, parse_json, &block)
|
|
|
|
rescue Excon::Errors::NotFound => error
|
2013-08-01 14:31:51 -05:00
|
|
|
raise Fog::Storage::Rackspace::NotFound.slurp(error, self)
|
2013-06-24 10:13:57 -05:00
|
|
|
rescue Excon::Errors::BadRequest => error
|
2013-08-01 14:31:51 -05:00
|
|
|
raise Fog::Storage::Rackspace::BadRequest.slurp(error, self)
|
2013-06-24 10:13:57 -05:00
|
|
|
rescue Excon::Errors::InternalServerError => error
|
2013-08-01 14:31:51 -05:00
|
|
|
raise Fog::Storage::Rackspace::InternalServerError.slurp(error, self)
|
2013-06-24 10:13:57 -05:00
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
2013-08-01 14:31:51 -05:00
|
|
|
raise Fog::Storage::Rackspace::ServiceError.slurp(error, self)
|
2010-11-01 11:50:30 -07:00
|
|
|
end
|
2013-02-20 13:02:32 -06:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def authenticate_v1(options)
|
|
|
|
credentials = Fog::Rackspace.authenticate(options, @connection_options)
|
2013-02-28 14:57:09 -06:00
|
|
|
endpoint_uri credentials['X-CDN-Management-Url']
|
2013-03-01 14:48:26 -06:00
|
|
|
@auth_token = credentials['X-Auth-Token']
|
2013-02-20 13:02:32 -06:00
|
|
|
end
|
2010-11-01 11:50:30 -07:00
|
|
|
|
2013-08-07 12:55:26 -06:00
|
|
|
# Fix for invalid auth_token, likely after 24 hours.
|
|
|
|
def authenticate(options={})
|
|
|
|
super({
|
|
|
|
:rackspace_api_key => @rackspace_api_key,
|
|
|
|
:rackspace_username => @rackspace_username,
|
|
|
|
:rackspace_auth_url => @rackspace_auth_url,
|
|
|
|
:connection_options => @connection_options
|
|
|
|
})
|
|
|
|
end
|
2010-11-01 11:50:30 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|