1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[rackspace|cdn&storage] split cdn into it's own service

This commit is contained in:
geemus 2010-11-01 11:50:30 -07:00
parent a31c1902f1
commit 868ace8347
16 changed files with 105 additions and 60 deletions

View file

@ -4,6 +4,7 @@ module Fog
extend Fog::Provider
service_path 'fog/rackspace'
service 'cdn'
service 'compute'
service 'files'
service 'servers'

81
lib/fog/rackspace/cdn.rb Normal file
View file

@ -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

View file

@ -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 => '',

View file

@ -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,

View file

@ -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',

View file

@ -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)

View file

@ -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)}"

View file

@ -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,

View file

@ -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 => '',

View file

@ -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',

View file

@ -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,

View file

@ -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 => '',

View file

@ -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)}"

View file

@ -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)

View file

@ -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,

View file

@ -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