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:
parent
a31c1902f1
commit
868ace8347
16 changed files with 105 additions and 60 deletions
|
@ -4,6 +4,7 @@ module Fog
|
||||||
extend Fog::Provider
|
extend Fog::Provider
|
||||||
|
|
||||||
service_path 'fog/rackspace'
|
service_path 'fog/rackspace'
|
||||||
|
service 'cdn'
|
||||||
service 'compute'
|
service 'compute'
|
||||||
service 'files'
|
service 'files'
|
||||||
service 'servers'
|
service 'servers'
|
||||||
|
|
81
lib/fog/rackspace/cdn.rb
Normal file
81
lib/fog/rackspace/cdn.rb
Normal 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
|
|
@ -16,7 +16,7 @@ module Fog
|
||||||
# * body<~Array>:
|
# * body<~Array>:
|
||||||
# * container<~String>: Name of container
|
# * container<~String>: Name of container
|
||||||
def get_cdn_containers(options = {})
|
def get_cdn_containers(options = {})
|
||||||
response = cdn_request(
|
response = request(
|
||||||
:expects => [200, 204],
|
:expects => [200, 204],
|
||||||
:method => 'GET',
|
:method => 'GET',
|
||||||
:path => '',
|
:path => '',
|
|
@ -18,7 +18,7 @@ module Fog
|
||||||
# * 'X-User-Agent-ACL'<~String> - ?
|
# * 'X-User-Agent-ACL'<~String> - ?
|
||||||
# * 'X-Referrer-ACL'<~String> - ?
|
# * 'X-Referrer-ACL'<~String> - ?
|
||||||
def head_cdn_container(container)
|
def head_cdn_container(container)
|
||||||
response = cdn_request(
|
response = request(
|
||||||
:expects => 204,
|
:expects => 204,
|
||||||
:method => 'HEAD',
|
:method => 'HEAD',
|
||||||
:path => container,
|
:path => container,
|
|
@ -15,7 +15,7 @@ module Fog
|
||||||
# * 'X-User-Agent-ACL'<~String> - ?
|
# * 'X-User-Agent-ACL'<~String> - ?
|
||||||
# * 'X-Referrer-ACL'<~String> - ?
|
# * 'X-Referrer-ACL'<~String> - ?
|
||||||
def put_cdn_container(name, options = {})
|
def put_cdn_container(name, options = {})
|
||||||
response = cdn_request(
|
response = request(
|
||||||
:expects => [201, 202],
|
:expects => [201, 202],
|
||||||
:headers => options,
|
:headers => options,
|
||||||
:method => 'PUT',
|
:method => 'PUT',
|
|
@ -9,7 +9,7 @@ module Fog
|
||||||
# * name<~String> - Name of container to delete
|
# * name<~String> - Name of container to delete
|
||||||
#
|
#
|
||||||
def delete_container(name)
|
def delete_container(name)
|
||||||
response = storage_request(
|
response = request(
|
||||||
:expects => 204,
|
:expects => 204,
|
||||||
:method => 'DELETE',
|
:method => 'DELETE',
|
||||||
:path => CGI.escape(name)
|
:path => CGI.escape(name)
|
||||||
|
|
|
@ -10,7 +10,7 @@ module Fog
|
||||||
# * object<~String> - Name of object to delete
|
# * object<~String> - Name of object to delete
|
||||||
#
|
#
|
||||||
def delete_object(container, object)
|
def delete_object(container, object)
|
||||||
response = storage_request(
|
response = request(
|
||||||
:expects => 204,
|
:expects => 204,
|
||||||
:method => 'DELETE',
|
:method => 'DELETE',
|
||||||
:path => "#{CGI.escape(container)}/#{CGI.escape(object)}"
|
:path => "#{CGI.escape(container)}/#{CGI.escape(object)}"
|
||||||
|
|
|
@ -29,7 +29,7 @@ module Fog
|
||||||
# * 'last_modified'<~String> - Last modified timestamp
|
# * 'last_modified'<~String> - Last modified timestamp
|
||||||
# * 'name'<~String> - Name of object
|
# * 'name'<~String> - Name of object
|
||||||
def get_container(container, options = {})
|
def get_container(container, options = {})
|
||||||
response = storage_request(
|
response = request(
|
||||||
:expects => 200,
|
:expects => 200,
|
||||||
:method => 'GET',
|
:method => 'GET',
|
||||||
:path => container,
|
:path => container,
|
||||||
|
|
|
@ -18,7 +18,7 @@ module Fog
|
||||||
# * 'count'<~Integer>: - Number of items in container
|
# * 'count'<~Integer>: - Number of items in container
|
||||||
# * 'name'<~String>: - Name of container
|
# * 'name'<~String>: - Name of container
|
||||||
def get_containers(options = {})
|
def get_containers(options = {})
|
||||||
response = storage_request(
|
response = request(
|
||||||
:expects => [200, 204],
|
:expects => [200, 204],
|
||||||
:method => 'GET',
|
:method => 'GET',
|
||||||
:path => '',
|
:path => '',
|
||||||
|
|
|
@ -10,7 +10,7 @@ module Fog
|
||||||
# * object<~String> - Name of object to look for
|
# * object<~String> - Name of object to look for
|
||||||
#
|
#
|
||||||
def get_object(container, object, &block)
|
def get_object(container, object, &block)
|
||||||
response = storage_request({
|
response = request({
|
||||||
:block => block,
|
:block => block,
|
||||||
:expects => 200,
|
:expects => 200,
|
||||||
:method => 'GET',
|
:method => 'GET',
|
||||||
|
|
|
@ -14,7 +14,7 @@ module Fog
|
||||||
# * 'X-Container-Object-Count'<~String> - Count of containers
|
# * 'X-Container-Object-Count'<~String> - Count of containers
|
||||||
# * 'X-Container-Bytes-Used'<~String> - Bytes used
|
# * 'X-Container-Bytes-Used'<~String> - Bytes used
|
||||||
def head_container(container)
|
def head_container(container)
|
||||||
response = storage_request(
|
response = request(
|
||||||
:expects => 204,
|
:expects => 204,
|
||||||
:method => 'HEAD',
|
:method => 'HEAD',
|
||||||
:path => container,
|
:path => container,
|
||||||
|
|
|
@ -11,7 +11,7 @@ module Fog
|
||||||
# * 'X-Account-Container-Count'<~String> - Count of containers
|
# * 'X-Account-Container-Count'<~String> - Count of containers
|
||||||
# * 'X-Account-Bytes-Used'<~String> - Bytes used
|
# * 'X-Account-Bytes-Used'<~String> - Bytes used
|
||||||
def head_containers
|
def head_containers
|
||||||
response = storage_request(
|
response = request(
|
||||||
:expects => 204,
|
:expects => 204,
|
||||||
:method => 'HEAD',
|
:method => 'HEAD',
|
||||||
:path => '',
|
:path => '',
|
||||||
|
|
|
@ -10,7 +10,7 @@ module Fog
|
||||||
# * object<~String> - Name of object to look for
|
# * object<~String> - Name of object to look for
|
||||||
#
|
#
|
||||||
def head_object(container, object)
|
def head_object(container, object)
|
||||||
response = storage_request({
|
response = request({
|
||||||
:expects => 200,
|
:expects => 200,
|
||||||
:method => 'GET',
|
:method => 'GET',
|
||||||
:path => "#{CGI.escape(container)}/#{CGI.escape(object)}"
|
:path => "#{CGI.escape(container)}/#{CGI.escape(object)}"
|
||||||
|
|
|
@ -9,7 +9,7 @@ module Fog
|
||||||
# * name<~String> - Name for container, should be < 256 bytes and must not contain '/'
|
# * name<~String> - Name for container, should be < 256 bytes and must not contain '/'
|
||||||
#
|
#
|
||||||
def put_container(name)
|
def put_container(name)
|
||||||
response = storage_request(
|
response = request(
|
||||||
:expects => [201, 202],
|
:expects => [201, 202],
|
||||||
:method => 'PUT',
|
:method => 'PUT',
|
||||||
:path => CGI.escape(name)
|
:path => CGI.escape(name)
|
||||||
|
|
|
@ -11,7 +11,7 @@ module Fog
|
||||||
def put_object(container, object, data, options = {})
|
def put_object(container, object, data, options = {})
|
||||||
data = parse_data(data)
|
data = parse_data(data)
|
||||||
headers = data[:headers].merge!(options)
|
headers = data[:headers].merge!(options)
|
||||||
response = storage_request(
|
response = request(
|
||||||
:body => data[:body],
|
:body => data[:body],
|
||||||
:expects => 201,
|
:expects => 201,
|
||||||
:headers => headers,
|
:headers => headers,
|
||||||
|
|
|
@ -14,14 +14,11 @@ module Fog
|
||||||
request :delete_container
|
request :delete_container
|
||||||
request :delete_object
|
request :delete_object
|
||||||
request :get_container
|
request :get_container
|
||||||
request :get_cdn_containers
|
|
||||||
request :get_containers
|
request :get_containers
|
||||||
request :get_object
|
request :get_object
|
||||||
request :head_cdn_container
|
|
||||||
request :head_container
|
request :head_container
|
||||||
request :head_containers
|
request :head_containers
|
||||||
request :head_object
|
request :head_object
|
||||||
request :put_cdn_container
|
|
||||||
request :put_container
|
request :put_container
|
||||||
request :put_object
|
request :put_object
|
||||||
|
|
||||||
|
@ -82,61 +79,27 @@ module Fog
|
||||||
credentials = Fog::Rackspace.authenticate(options)
|
credentials = Fog::Rackspace.authenticate(options)
|
||||||
@auth_token = credentials['X-Auth-Token']
|
@auth_token = credentials['X-Auth-Token']
|
||||||
|
|
||||||
if(credentials['X-CDN-Management-Url'])
|
uri = URI.parse(credentials['X-Storage-Url'])
|
||||||
cdn_uri = URI.parse(credentials['X-CDN-Management-Url'])
|
@host = uri.host
|
||||||
@cdn_host = cdn_uri.host
|
@path = uri.path
|
||||||
@cdn_path = cdn_uri.path
|
@port = uri.port
|
||||||
@cdn_port = cdn_uri.port
|
@scheme = uri.scheme
|
||||||
@cdn_scheme = cdn_uri.scheme
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
|
||||||
@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])
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def reload
|
def reload
|
||||||
@cdn_connection.reset
|
|
||||||
@storage_connection.reset
|
@storage_connection.reset
|
||||||
end
|
end
|
||||||
|
|
||||||
def cdn_request(params, parse_json = true)
|
def request(params, parse_json = true, &block)
|
||||||
begin
|
begin
|
||||||
response = @cdn_connection.request(params.merge!({
|
response = @connection.request(params.merge!({
|
||||||
:headers => {
|
:headers => {
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
'X-Auth-Token' => @auth_token
|
'X-Auth-Token' => @auth_token
|
||||||
}.merge!(params[:headers] || {}),
|
}.merge!(params[:headers] || {}),
|
||||||
:host => @cdn_host,
|
:host => @host,
|
||||||
:path => "#{@cdn_path}/#{params[:path]}",
|
: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
|
|
||||||
|
|
||||||
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]}",
|
|
||||||
}), &block)
|
}), &block)
|
||||||
rescue Excon::Errors::Error => error
|
rescue Excon::Errors::Error => error
|
||||||
raise case error
|
raise case error
|
||||||
|
|
Loading…
Add table
Reference in a new issue