mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[rackspace|storage] refactored storage/cdn.
This commit is contained in:
parent
4dbdd58387
commit
d812df525a
3 changed files with 47 additions and 37 deletions
|
@ -39,11 +39,40 @@ module Fog
|
||||||
self.class.data.delete(@rackspace_username)
|
self.class.data.delete(@rackspace_username)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def ssl?
|
||||||
|
!@rackspace_cdn_ssl.nil?
|
||||||
|
end
|
||||||
|
|
||||||
def purge(object)
|
def purge(object)
|
||||||
return true if object.is_a? Fog::Storage::Rackspace::File
|
return true if object.is_a? Fog::Storage::Rackspace::File
|
||||||
raise Fog::Errors::NotImplemented.new("#{object.class} does not support CDN purging") if object
|
raise Fog::Errors::NotImplemented.new("#{object.class} does not support CDN purging") if object
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class Real
|
class Real
|
||||||
|
|
|
@ -12,11 +12,13 @@ module Fog
|
||||||
attribute :bytes, :aliases => 'X-Container-Bytes-Used'
|
attribute :bytes, :aliases => 'X-Container-Bytes-Used'
|
||||||
attribute :count, :aliases => 'X-Container-Object-Count'
|
attribute :count, :aliases => 'X-Container-Object-Count'
|
||||||
attribute :cdn_cname
|
attribute :cdn_cname
|
||||||
|
|
||||||
|
attr_writer :public
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
requires :key
|
requires :key
|
||||||
service.delete_container(key)
|
service.delete_container(key)
|
||||||
service.cdn.post_container(key, 'X-CDN-Enabled' => 'False')
|
service.cdn.publish_container(self, false) if service.cdn
|
||||||
true
|
true
|
||||||
rescue Excon::Errors::NotFound
|
rescue Excon::Errors::NotFound
|
||||||
false
|
false
|
||||||
|
@ -31,52 +33,26 @@ module Fog
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def public=(new_public)
|
|
||||||
@public = new_public
|
|
||||||
end
|
|
||||||
|
|
||||||
def public?
|
def public?
|
||||||
@public ||= !public_url.nil?
|
@public ||= !public_url.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
def public_url
|
def public_url
|
||||||
requires :key
|
requires :key
|
||||||
@public_url ||= begin
|
public_url ||= service.cdn.public_url(self) if service.cdn
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def save
|
def save
|
||||||
requires :key
|
requires :key
|
||||||
service.put_container(key)
|
create_container(key)
|
||||||
|
|
||||||
if service.cdn && public?
|
raise Fog::Storage::Rackspace::Error.new("Directory can not be set as :public without a CDN provided") if public? && !service.cdn
|
||||||
# if public and CDN connection then update cdn to public
|
public_url = service.cdn.publish_container(self, 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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def create_container(key)
|
||||||
|
service.put_container(key)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,7 +38,8 @@ module Fog
|
||||||
:provider => 'Rackspace',
|
:provider => 'Rackspace',
|
||||||
:rackspace_api_key => @rackspace_api_key,
|
:rackspace_api_key => @rackspace_api_key,
|
||||||
:rackspace_auth_url => @rackspace_auth_url,
|
:rackspace_auth_url => @rackspace_auth_url,
|
||||||
:rackspace_username => @rackspace_username
|
:rackspace_username => @rackspace_username,
|
||||||
|
:rackspace_cdn_ssl => @rackspace_cdn_ssl
|
||||||
)
|
)
|
||||||
if @cdn.enabled?
|
if @cdn.enabled?
|
||||||
@cdn
|
@cdn
|
||||||
|
@ -97,6 +98,10 @@ module Fog
|
||||||
Excon.defaults[:ssl_verify_peer] = false if options[:rackspace_servicenet] == true
|
Excon.defaults[:ssl_verify_peer] = false if options[:rackspace_servicenet] == true
|
||||||
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def ssl?
|
||||||
|
!rackspace_cdn_ssl.nil?
|
||||||
|
end
|
||||||
|
|
||||||
def reload
|
def reload
|
||||||
@connection.reset
|
@connection.reset
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue