diff --git a/lib/fog/rackspace/cdn.rb b/lib/fog/rackspace/cdn.rb index 4aa86360c..3c78c0756 100644 --- a/lib/fog/rackspace/cdn.rb +++ b/lib/fog/rackspace/cdn.rb @@ -39,11 +39,40 @@ module Fog self.class.data.delete(@rackspace_username) end + def ssl? + !@rackspace_cdn_ssl.nil? + end + 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 - + + def publish_container(container, publish = true) + enabled = publish ? 'True' : 'False' + response = post_container(container.key, 'X-CDN-Enabled' => enabled) + url_from_headers(response.headers, container.cdn_cname) + end + + def public_url(object) + begin + response = head_container(key) + if response.headers['X-Cdn-Enabled'] == 'True' + url_from_headers(response.headers, object.cdn_name) + else + nil + end + rescue Fog::Service::NotFound + nil + end + end + + private + + def url_from_headers(headers, cdn_cname) + return headers['X-Cdn-Ssl-Uri'] if ssl? + cdn_cname || headers['X-Cdn-Uri'] + end end class Real diff --git a/lib/fog/rackspace/models/storage/directory.rb b/lib/fog/rackspace/models/storage/directory.rb index 4ecdfa7da..d69c511ea 100644 --- a/lib/fog/rackspace/models/storage/directory.rb +++ b/lib/fog/rackspace/models/storage/directory.rb @@ -12,11 +12,13 @@ module Fog attribute :bytes, :aliases => 'X-Container-Bytes-Used' attribute :count, :aliases => 'X-Container-Object-Count' attribute :cdn_cname + + attr_writer :public def destroy requires :key service.delete_container(key) - service.cdn.post_container(key, 'X-CDN-Enabled' => 'False') + service.cdn.publish_container(self, false) if service.cdn true rescue Excon::Errors::NotFound false @@ -31,52 +33,26 @@ module Fog end end - def public=(new_public) - @public = new_public - end - def public? @public ||= !public_url.nil? end - def public_url + def public_url requires :key - @public_url ||= begin - begin response = service.cdn.head_container(key) - if response.headers['X-Cdn-Enabled'] == 'True' - if service.rackspace_cdn_ssl == true - response.headers['X-Cdn-Ssl-Uri'] - else - cdn_cname || response.headers['X-Cdn-Uri'] - end - end - rescue Fog::Service::NotFound - nil - end - end + public_url ||= service.cdn.public_url(self) if service.cdn end def save requires :key - service.put_container(key) + create_container(key) - if service.cdn && public? - # if public and CDN connection then update cdn to public - uri_header = 'X-CDN-URI' - if service.rackspace_cdn_ssl == true - uri_header = 'X-CDN-SSL-URI' - end - @public_url = service.cdn.put_container(key, 'X-CDN-Enabled' => 'True').headers[uri_header] - elsif service.cdn && !public? - service.cdn.put_container(key, 'X-CDN-Enabled' => 'False') - @public_url = nil - elsif !service.cdn && public? - # if public but no CDN connection then error - raise(Fog::Storage::Rackspace::Error.new("Directory can not be set as :public without a CDN provided")) - end - true + raise Fog::Storage::Rackspace::Error.new("Directory can not be set as :public without a CDN provided") if public? && !service.cdn + public_url = service.cdn.publish_container(self, public?) end + private + def create_container(key) + service.put_container(key) end end diff --git a/lib/fog/rackspace/storage.rb b/lib/fog/rackspace/storage.rb index 0141c9630..1f2794b3a 100644 --- a/lib/fog/rackspace/storage.rb +++ b/lib/fog/rackspace/storage.rb @@ -38,7 +38,8 @@ module Fog :provider => 'Rackspace', :rackspace_api_key => @rackspace_api_key, :rackspace_auth_url => @rackspace_auth_url, - :rackspace_username => @rackspace_username + :rackspace_username => @rackspace_username, + :rackspace_cdn_ssl => @rackspace_cdn_ssl ) if @cdn.enabled? @cdn @@ -97,6 +98,10 @@ module Fog Excon.defaults[:ssl_verify_peer] = false if options[:rackspace_servicenet] == true @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options) end + + def ssl? + !rackspace_cdn_ssl.nil? + end def reload @connection.reset