diff --git a/lib/fog/rackspace.rb b/lib/fog/rackspace.rb index bf307e9f3..55cb752b0 100644 --- a/lib/fog/rackspace.rb +++ b/lib/fog/rackspace.rb @@ -4,6 +4,7 @@ module Fog extend Fog::Provider service_path 'fog/rackspace' + service 'cdn' service 'compute' service 'files' service 'servers' diff --git a/lib/fog/rackspace/cdn.rb b/lib/fog/rackspace/cdn.rb new file mode 100644 index 000000000..bbef47ff4 --- /dev/null +++ b/lib/fog/rackspace/cdn.rb @@ -0,0 +1,81 @@ +module Fog + module Rackspace + class Storage < Fog::Service + + requires :rackspace_api_key, :rackspace_username + + request_path 'fog/rackspace/requests/cdn' + request :get_cdn_containers + request :head_cdn_container + request :put_cdn_container + + class Mock + include Utils + + def self.data + @data ||= Hash.new do |hash, key| + hash[key] = {} + end + end + + def self.reset_data(keys=data.keys) + for key in [*keys] + data.delete(key) + end + end + + def initialize(options={}) + @rackspace_username = options[:rackspace_username] + @data = self.class.data[@rackspace_username] + end + + end + + class Real + include Utils + + def initialize(options={}) + require 'json' + credentials = Fog::Rackspace.authenticate(options) + @auth_token = credentials['X-Auth-Token'] + + 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}", options[:persistent]) + end + + def reload + @cdn_connection.reset + end + + def request(params, parse_json = true) + begin + response = @connection.request(params.merge!({ + :headers => { + 'Content-Type' => 'application/json', + 'X-Auth-Token' => @auth_token + }.merge!(params[:headers] || {}), + :host => @host, + :path => "#{@path}/#{params[:path]}", + })) + rescue Excon::Errors::Error => error + raise case error + when Excon::Errors::NotFound + Fog::Rackspace::Storage::NotFound.slurp(error) + else + error + end + end + if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json} + response.body = JSON.parse(response.body) + end + response + end + + end + end + end +end diff --git a/lib/fog/rackspace/requests/storage/get_cdn_containers.rb b/lib/fog/rackspace/requests/cdn/get_cdn_containers.rb similarity index 96% rename from lib/fog/rackspace/requests/storage/get_cdn_containers.rb rename to lib/fog/rackspace/requests/cdn/get_cdn_containers.rb index b37df5b75..1ebe1de9e 100644 --- a/lib/fog/rackspace/requests/storage/get_cdn_containers.rb +++ b/lib/fog/rackspace/requests/cdn/get_cdn_containers.rb @@ -16,7 +16,7 @@ module Fog # * body<~Array>: # * container<~String>: Name of container def get_cdn_containers(options = {}) - response = cdn_request( + response = request( :expects => [200, 204], :method => 'GET', :path => '', diff --git a/lib/fog/rackspace/requests/storage/head_cdn_container.rb b/lib/fog/rackspace/requests/cdn/head_cdn_container.rb similarity index 96% rename from lib/fog/rackspace/requests/storage/head_cdn_container.rb rename to lib/fog/rackspace/requests/cdn/head_cdn_container.rb index 2d41cd909..807721c88 100644 --- a/lib/fog/rackspace/requests/storage/head_cdn_container.rb +++ b/lib/fog/rackspace/requests/cdn/head_cdn_container.rb @@ -18,7 +18,7 @@ module Fog # * 'X-User-Agent-ACL'<~String> - ? # * 'X-Referrer-ACL'<~String> - ? def head_cdn_container(container) - response = cdn_request( + response = request( :expects => 204, :method => 'HEAD', :path => container, diff --git a/lib/fog/rackspace/requests/storage/put_cdn_container.rb b/lib/fog/rackspace/requests/cdn/put_cdn_container.rb similarity index 96% rename from lib/fog/rackspace/requests/storage/put_cdn_container.rb rename to lib/fog/rackspace/requests/cdn/put_cdn_container.rb index 5a295abcd..5e90aa4a6 100644 --- a/lib/fog/rackspace/requests/storage/put_cdn_container.rb +++ b/lib/fog/rackspace/requests/cdn/put_cdn_container.rb @@ -15,7 +15,7 @@ module Fog # * 'X-User-Agent-ACL'<~String> - ? # * 'X-Referrer-ACL'<~String> - ? def put_cdn_container(name, options = {}) - response = cdn_request( + response = request( :expects => [201, 202], :headers => options, :method => 'PUT', diff --git a/lib/fog/rackspace/requests/storage/delete_container.rb b/lib/fog/rackspace/requests/storage/delete_container.rb index 90e6a3076..02c2832bb 100644 --- a/lib/fog/rackspace/requests/storage/delete_container.rb +++ b/lib/fog/rackspace/requests/storage/delete_container.rb @@ -9,7 +9,7 @@ module Fog # * name<~String> - Name of container to delete # def delete_container(name) - response = storage_request( + response = request( :expects => 204, :method => 'DELETE', :path => CGI.escape(name) diff --git a/lib/fog/rackspace/requests/storage/delete_object.rb b/lib/fog/rackspace/requests/storage/delete_object.rb index 81da44d91..abca74a05 100644 --- a/lib/fog/rackspace/requests/storage/delete_object.rb +++ b/lib/fog/rackspace/requests/storage/delete_object.rb @@ -10,7 +10,7 @@ module Fog # * object<~String> - Name of object to delete # def delete_object(container, object) - response = storage_request( + response = request( :expects => 204, :method => 'DELETE', :path => "#{CGI.escape(container)}/#{CGI.escape(object)}" diff --git a/lib/fog/rackspace/requests/storage/get_container.rb b/lib/fog/rackspace/requests/storage/get_container.rb index 320563d90..ba720350e 100644 --- a/lib/fog/rackspace/requests/storage/get_container.rb +++ b/lib/fog/rackspace/requests/storage/get_container.rb @@ -29,7 +29,7 @@ module Fog # * 'last_modified'<~String> - Last modified timestamp # * 'name'<~String> - Name of object def get_container(container, options = {}) - response = storage_request( + response = request( :expects => 200, :method => 'GET', :path => container, diff --git a/lib/fog/rackspace/requests/storage/get_containers.rb b/lib/fog/rackspace/requests/storage/get_containers.rb index 479feed5e..fd6edb26b 100644 --- a/lib/fog/rackspace/requests/storage/get_containers.rb +++ b/lib/fog/rackspace/requests/storage/get_containers.rb @@ -18,7 +18,7 @@ module Fog # * 'count'<~Integer>: - Number of items in container # * 'name'<~String>: - Name of container def get_containers(options = {}) - response = storage_request( + response = request( :expects => [200, 204], :method => 'GET', :path => '', diff --git a/lib/fog/rackspace/requests/storage/get_object.rb b/lib/fog/rackspace/requests/storage/get_object.rb index 603254283..7b3eb405b 100644 --- a/lib/fog/rackspace/requests/storage/get_object.rb +++ b/lib/fog/rackspace/requests/storage/get_object.rb @@ -10,7 +10,7 @@ module Fog # * object<~String> - Name of object to look for # def get_object(container, object, &block) - response = storage_request({ + response = request({ :block => block, :expects => 200, :method => 'GET', diff --git a/lib/fog/rackspace/requests/storage/head_container.rb b/lib/fog/rackspace/requests/storage/head_container.rb index 271449fd8..8d828358d 100644 --- a/lib/fog/rackspace/requests/storage/head_container.rb +++ b/lib/fog/rackspace/requests/storage/head_container.rb @@ -14,7 +14,7 @@ module Fog # * 'X-Container-Object-Count'<~String> - Count of containers # * 'X-Container-Bytes-Used'<~String> - Bytes used def head_container(container) - response = storage_request( + response = request( :expects => 204, :method => 'HEAD', :path => container, diff --git a/lib/fog/rackspace/requests/storage/head_containers.rb b/lib/fog/rackspace/requests/storage/head_containers.rb index 1a593033c..ee8c11735 100644 --- a/lib/fog/rackspace/requests/storage/head_containers.rb +++ b/lib/fog/rackspace/requests/storage/head_containers.rb @@ -11,7 +11,7 @@ module Fog # * 'X-Account-Container-Count'<~String> - Count of containers # * 'X-Account-Bytes-Used'<~String> - Bytes used def head_containers - response = storage_request( + response = request( :expects => 204, :method => 'HEAD', :path => '', diff --git a/lib/fog/rackspace/requests/storage/head_object.rb b/lib/fog/rackspace/requests/storage/head_object.rb index b0fec4f1f..55a5d3960 100644 --- a/lib/fog/rackspace/requests/storage/head_object.rb +++ b/lib/fog/rackspace/requests/storage/head_object.rb @@ -10,7 +10,7 @@ module Fog # * object<~String> - Name of object to look for # def head_object(container, object) - response = storage_request({ + response = request({ :expects => 200, :method => 'GET', :path => "#{CGI.escape(container)}/#{CGI.escape(object)}" diff --git a/lib/fog/rackspace/requests/storage/put_container.rb b/lib/fog/rackspace/requests/storage/put_container.rb index 340abcb08..8becdca3d 100644 --- a/lib/fog/rackspace/requests/storage/put_container.rb +++ b/lib/fog/rackspace/requests/storage/put_container.rb @@ -9,7 +9,7 @@ module Fog # * name<~String> - Name for container, should be < 256 bytes and must not contain '/' # def put_container(name) - response = storage_request( + response = request( :expects => [201, 202], :method => 'PUT', :path => CGI.escape(name) diff --git a/lib/fog/rackspace/requests/storage/put_object.rb b/lib/fog/rackspace/requests/storage/put_object.rb index a7f4687c1..61a712aa5 100644 --- a/lib/fog/rackspace/requests/storage/put_object.rb +++ b/lib/fog/rackspace/requests/storage/put_object.rb @@ -11,7 +11,7 @@ module Fog def put_object(container, object, data, options = {}) data = parse_data(data) headers = data[:headers].merge!(options) - response = storage_request( + response = request( :body => data[:body], :expects => 201, :headers => headers, diff --git a/lib/fog/rackspace/storage.rb b/lib/fog/rackspace/storage.rb index 84c0bc57f..175db465c 100644 --- a/lib/fog/rackspace/storage.rb +++ b/lib/fog/rackspace/storage.rb @@ -14,14 +14,11 @@ module Fog request :delete_container request :delete_object request :get_container - request :get_cdn_containers request :get_containers request :get_object - request :head_cdn_container request :head_container request :head_containers request :head_object - request :put_cdn_container request :put_container request :put_object @@ -82,61 +79,27 @@ module Fog credentials = Fog::Rackspace.authenticate(options) @auth_token = credentials['X-Auth-Token'] - if(credentials['X-CDN-Management-Url']) - cdn_uri = URI.parse(credentials['X-CDN-Management-Url']) - @cdn_host = cdn_uri.host - @cdn_path = cdn_uri.path - @cdn_port = cdn_uri.port - @cdn_scheme = cdn_uri.scheme - @cdn_connection = Fog::Connection.new("#{@cdn_scheme}://#{@cdn_host}:#{@cdn_port}", options[:persistent]) - end - - storage_uri = URI.parse(credentials['X-Storage-Url']) - @storage_host = storage_uri.host - @storage_path = storage_uri.path - @storage_port = storage_uri.port - @storage_scheme = storage_uri.scheme - @storage_connection = Fog::Connection.new("#{@storage_scheme}://#{@storage_host}:#{@storage_port}", options[:persistent]) + uri = URI.parse(credentials['X-Storage-Url']) + @host = uri.host + @path = uri.path + @port = uri.port + @scheme = uri.scheme + @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent]) end def reload - @cdn_connection.reset @storage_connection.reset end - def cdn_request(params, parse_json = true) + def request(params, parse_json = true, &block) begin - response = @cdn_connection.request(params.merge!({ + response = @connection.request(params.merge!({ :headers => { 'Content-Type' => 'application/json', 'X-Auth-Token' => @auth_token }.merge!(params[:headers] || {}), - :host => @cdn_host, - :path => "#{@cdn_path}/#{params[:path]}", - })) - rescue Excon::Errors::Error => error - raise case error - when Excon::Errors::NotFound - Fog::Rackspace::Storage::NotFound.slurp(error) - else - error - end - end - if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json} - response.body = JSON.parse(response.body) - end - response - end - - def storage_request(params, parse_json = true, &block) - begin - response = @storage_connection.request(params.merge!({ - :headers => { - 'Content-Type' => 'application/json', - 'X-Auth-Token' => @auth_token - }.merge!(params[:headers] || {}), - :host => @storage_host, - :path => "#{@storage_path}/#{params[:path]}", + :host => @host, + :path => "#{@path}/#{params[:path]}", }), &block) rescue Excon::Errors::Error => error raise case error