lib/fog/rackspace/cdn.rb in fog-1.10.0 vs lib/fog/rackspace/cdn.rb in fog-1.10.1

- old
+ new

@@ -4,11 +4,11 @@ module Fog module CDN class Rackspace < Fog::Service requires :rackspace_api_key, :rackspace_username - recognizes :rackspace_auth_url, :persistent, :rackspace_cdn_ssl + recognizes :rackspace_auth_url, :persistent, :rackspace_cdn_ssl, :rackspace_region, :rackspace_cdn_url request_path 'fog/rackspace/requests/cdn' request :get_containers request :head_container request :post_container @@ -22,17 +22,44 @@ "X-Cdn-Uri" => :uri, "X-Cdn-Streaming-Uri" => :streaming_uri, "X-Cdn-Ssl-Uri" => :ssl_uri }.freeze + 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 + + # 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] 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 + # 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 + # @raise [Fog::Rackspace::Errors::ServiceError] + # @note If unable to find container or container is not published this method will return an empty hash. def urls(container) begin response = head_container(container.key) return {} unless response.headers['X-Cdn-Enabled'] == 'True' urls_from_headers response.headers @@ -51,13 +78,13 @@ end h end end - class Mock + class Mock < Fog::Rackspace::Service include Base - + def self.data @data ||= Hash.new do |hash, key| hash[key] = {} end end @@ -83,42 +110,45 @@ self.class.data.delete(@rackspace_username) end end - class Real + class Real < Fog::Rackspace::Service include Base - + def initialize(options={}) @connection_options = options[:connection_options] || {} - credentials = Fog::Rackspace.authenticate(options, @connection_options) - @auth_token = credentials['X-Auth-Token'] + @rackspace_auth_url = options[:rackspace_auth_url] + @rackspace_cdn_url = options[:rackspace_cdn_url] + @rackspace_region = options[:rackspace_region] || :dfw + authenticate(options) @enabled = false @persistent = options[:persistent] || false - if credentials['X-CDN-Management-Url'] - 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}", @persistent, @connection_options) + if endpoint_uri + @connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options) @enabled = true end end - + + # Returns true if CDN service is enabled + # @return [Boolean] def enabled? @enabled end + # Resets CDN connection def reload @cdn_connection.reset end + # 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 def purge(file) unless file.is_a? Fog::Storage::Rackspace::File - raise Fog::Errors::NotImplemented.new("#{object.class} does not support CDN purging") + raise Fog::Errors::NotImplemented.new("#{object.class} does not support CDN purging") if object end delete_object file.directory.key, file.key true end @@ -127,14 +157,14 @@ begin response = @connection.request(params.merge!({ :headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json', - 'X-Auth-Token' => @auth_token + 'X-Auth-Token' => auth_token }.merge!(params[:headers] || {}), - :host => @host, - :path => "#{@path}/#{params[:path]}", + :host => endpoint_uri.host, + :path => "#{endpoint_uri.path}/#{params[:path]}", })) rescue Excon::Errors::HTTPStatusError => error raise case error when Excon::Errors::NotFound Fog::Storage::Rackspace::NotFound.slurp(error) @@ -144,9 +174,17 @@ end if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json} response.body = Fog::JSON.decode(response.body) end response + end + + private + + def authenticate_v1(options) + credentials = Fog::Rackspace.authenticate(options, @connection_options) + endpoint_uri credentials['X-CDN-Management-Url'] + @auth_token = credentials['X-Auth-Token'] end end end end