mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[rackspace] rename files/servers to storage/compute
This commit is contained in:
parent
5e5946b2ad
commit
a5411d8cf9
44 changed files with 339 additions and 289 deletions
|
@ -4,8 +4,10 @@ module Fog
|
|||
extend Fog::Provider
|
||||
|
||||
service_path 'fog/rackspace'
|
||||
service 'compute'
|
||||
service 'files'
|
||||
service 'servers'
|
||||
service 'storage'
|
||||
|
||||
def self.authenticate(options)
|
||||
rackspace_auth_url = options[:rackspace_auth_url] || "auth.api.rackspacecloud.com"
|
||||
|
|
|
@ -9,31 +9,31 @@ module Rackspace
|
|||
def [](service)
|
||||
@@connections ||= Hash.new do |hash, key|
|
||||
hash[key] = case key
|
||||
when :files
|
||||
Fog::Rackspace::Files.new
|
||||
when :servers
|
||||
Fog::Rackspace::Servers.new
|
||||
when :compute
|
||||
Fog::Rackspace::Compute.new
|
||||
when :storage
|
||||
Fog::Rackspace::Storage.new
|
||||
end
|
||||
end
|
||||
@@connections[service]
|
||||
end
|
||||
|
||||
def services
|
||||
[:files, :servers]
|
||||
[:compute, :storage]
|
||||
end
|
||||
|
||||
for collection in Fog::Rackspace::Files.collections
|
||||
for collection in Fog::Rackspace::Compute.collections
|
||||
module_eval <<-EOS, __FILE__, __LINE__
|
||||
def #{collection}
|
||||
self[:files].#{collection}
|
||||
self[:compute].#{collection}
|
||||
end
|
||||
EOS
|
||||
end
|
||||
|
||||
for collection in Fog::Rackspace::Servers.collections
|
||||
for collection in Fog::Rackspace::Storage.collections
|
||||
module_eval <<-EOS, __FILE__, __LINE__
|
||||
def #{collection}
|
||||
self[:servers].#{collection}
|
||||
self[:storage].#{collection}
|
||||
end
|
||||
EOS
|
||||
end
|
||||
|
@ -46,4 +46,4 @@ module Rackspace
|
|||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
109
lib/fog/rackspace/compute.rb
Normal file
109
lib/fog/rackspace/compute.rb
Normal file
|
@ -0,0 +1,109 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Compute < Fog::Service
|
||||
|
||||
requires :rackspace_api_key, :rackspace_username
|
||||
|
||||
model_path 'fog/rackspace/models/compute'
|
||||
model :flavor
|
||||
collection :flavors
|
||||
model :image
|
||||
collection :images
|
||||
model :server
|
||||
collection :servers
|
||||
|
||||
request_path 'fog/rackspace/requests/compute'
|
||||
request :create_image
|
||||
request :create_server
|
||||
request :delete_image
|
||||
request :delete_server
|
||||
request :get_flavor_details
|
||||
request :get_image_details
|
||||
request :get_server_details
|
||||
request :list_addresses
|
||||
request :list_private_addresses
|
||||
request :list_public_addresses
|
||||
request :list_flavors
|
||||
request :list_flavors_detail
|
||||
request :list_images
|
||||
request :list_images_detail
|
||||
request :list_servers
|
||||
request :list_servers_detail
|
||||
request :reboot_server
|
||||
request :update_server
|
||||
|
||||
class Mock
|
||||
include Collections
|
||||
|
||||
def self.data
|
||||
@data ||= Hash.new do |hash, key|
|
||||
hash[key] = {
|
||||
:last_modified => {
|
||||
:images => {},
|
||||
:servers => {}
|
||||
},
|
||||
:images => {},
|
||||
:servers => {}
|
||||
}
|
||||
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 Collections
|
||||
|
||||
def initialize(options={})
|
||||
credentials = Fog::Rackspace.authenticate(options)
|
||||
@auth_token = credentials['X-Auth-Token']
|
||||
uri = URI.parse(credentials['X-Server-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
|
||||
@connection.reset
|
||||
end
|
||||
|
||||
def request(params)
|
||||
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::Compute::NotFound.slurp(error)
|
||||
else
|
||||
error
|
||||
end
|
||||
end
|
||||
unless response.body.empty?
|
||||
response.body = JSON.parse(response.body)
|
||||
end
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,135 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Files < Fog::Service
|
||||
|
||||
requires :rackspace_api_key, :rackspace_username
|
||||
|
||||
model_path 'fog/rackspace/models/files'
|
||||
model :directory
|
||||
collection :directories
|
||||
model :file
|
||||
collection :files
|
||||
|
||||
request_path 'fog/rackspace/requests/files'
|
||||
request :delete_container
|
||||
request :delete_object
|
||||
request :get_container
|
||||
request :get_containers
|
||||
request :get_object
|
||||
request :head_container
|
||||
request :head_containers
|
||||
request :head_object
|
||||
request :put_container
|
||||
request :put_object
|
||||
|
||||
module Utils
|
||||
|
||||
def parse_data(data)
|
||||
metadata = {
|
||||
:body => nil,
|
||||
:headers => {}
|
||||
}
|
||||
|
||||
if data.is_a?(String)
|
||||
metadata[:body] = data
|
||||
metadata[:headers]['Content-Length'] = metadata[:body].size.to_s
|
||||
else
|
||||
filename = ::File.basename(data.path)
|
||||
unless (mime_types = MIME::Types.of(filename)).empty?
|
||||
metadata[:headers]['Content-Type'] = mime_types.first.content_type
|
||||
end
|
||||
metadata[:body] = data.read
|
||||
metadata[:headers]['Content-Length'] = ::File.size(data.path).to_s
|
||||
end
|
||||
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
|
||||
metadata
|
||||
end
|
||||
class Files
|
||||
|
||||
def self.new(attributes = {})
|
||||
location = caller.first
|
||||
warning = "[yellow][WARN] Fog::Rackspace::Files#new is deprecated, use Fog::Rackspace::Storage#new instead[/]"
|
||||
warning << " [light_black](" << location << ")[/] "
|
||||
Formatador.display_line(warning)
|
||||
Fog::Rackspace::Storage.new(attributes)
|
||||
end
|
||||
|
||||
class Mock
|
||||
include Collections
|
||||
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 Collections
|
||||
include Utils
|
||||
|
||||
def initialize(options={})
|
||||
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])
|
||||
end
|
||||
|
||||
def reload
|
||||
@connection.reset
|
||||
end
|
||||
|
||||
def cdn_request(params)
|
||||
response = @cdn_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]}",
|
||||
}))
|
||||
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)
|
||||
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)
|
||||
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
|
||||
end
|
|
@ -2,7 +2,7 @@ require 'fog/model'
|
|||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
|
||||
class Flavor < Fog::Model
|
||||
|
|
@ -3,11 +3,11 @@ require 'fog/rackspace/models/servers/flavor'
|
|||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
|
||||
class Flavors < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Servers::Flavor
|
||||
model Fog::Rackspace::Compute::Flavor
|
||||
|
||||
def all
|
||||
data = connection.list_flavors_detail.body['flavors']
|
||||
|
@ -17,7 +17,7 @@ module Fog
|
|||
def get(flavor_id)
|
||||
data = connection.get_flavor_details(flavor_id).body['flavor']
|
||||
new(data)
|
||||
rescue Fog::Rackspace::Servers::NotFound
|
||||
rescue Fog::Rackspace::Compute::NotFound
|
||||
nil
|
||||
end
|
||||
|
|
@ -2,7 +2,7 @@ require 'fog/model'
|
|||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
|
||||
class Image < Fog::Model
|
||||
|
|
@ -3,11 +3,11 @@ require 'fog/rackspace/models/servers/image'
|
|||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
|
||||
class Images < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Servers::Image
|
||||
model Fog::Rackspace::Compute::Image
|
||||
|
||||
attribute :server
|
||||
|
||||
|
@ -22,7 +22,7 @@ module Fog
|
|||
def get(image_id)
|
||||
data = connection.get_image_details(image_id).body['image']
|
||||
new(data)
|
||||
rescue Fog::Rackspace::Servers::NotFound
|
||||
rescue Fog::Rackspace::Compute::NotFound
|
||||
nil
|
||||
end
|
||||
|
|
@ -2,7 +2,7 @@ require 'fog/model'
|
|||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
|
||||
class Server < Fog::Model
|
||||
|
|
@ -3,11 +3,11 @@ require 'fog/rackspace/models/servers/server'
|
|||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
|
||||
class Servers < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Servers::Server
|
||||
model Fog::Rackspace::Compute::Server
|
||||
|
||||
def all
|
||||
data = connection.list_servers_detail.body['servers']
|
||||
|
@ -25,7 +25,7 @@ module Fog
|
|||
if server = connection.get_server_details(server_id).body['server']
|
||||
new(server)
|
||||
end
|
||||
rescue Fog::Rackspace::Servers::NotFound
|
||||
rescue Fog::Rackspace::Compute::NotFound
|
||||
nil
|
||||
end
|
||||
|
|
@ -3,11 +3,11 @@ require 'fog/rackspace/models/files/directory'
|
|||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
|
||||
class Directories < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Files::Directory
|
||||
model Fog::Rackspace::Storage::Directory
|
||||
|
||||
def all
|
||||
data = connection.get_containers.body
|
||||
|
@ -23,7 +23,7 @@ module Fog
|
|||
directory.files << directory.files.new(file)
|
||||
end
|
||||
directory
|
||||
rescue Excon::Errors::NotFound
|
||||
rescue Fog::Rackspace::Storage::NotFound
|
||||
nil
|
||||
end
|
||||
|
|
@ -3,7 +3,7 @@ require 'fog/rackspace/models/files/files'
|
|||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
|
||||
class Directory < Fog::Model
|
||||
extend Fog::Deprecation
|
|
@ -2,7 +2,7 @@ require 'fog/model'
|
|||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
|
||||
class File < Fog::Model
|
||||
|
|
@ -3,7 +3,7 @@ require 'fog/rackspace/models/files/file'
|
|||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
|
||||
class Files < Fog::Collection
|
||||
|
||||
|
@ -13,7 +13,7 @@ module Fog
|
|||
attribute :path
|
||||
attribute :prefix
|
||||
|
||||
model Fog::Rackspace::Files::File
|
||||
model Fog::Rackspace::Storage::File
|
||||
|
||||
def all(options = {})
|
||||
requires :directory
|
||||
|
@ -48,7 +48,7 @@ module Fog
|
|||
end
|
||||
end
|
||||
new(file_data)
|
||||
rescue Excon::Errors::NotFound
|
||||
rescue Fog::Rackspace::Storage::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
|
@ -67,7 +67,7 @@ module Fog
|
|||
end
|
||||
end
|
||||
new(file_data)
|
||||
rescue Excon::Errors::NotFound
|
||||
rescue Fog::Rackspace::Storage::NotFound
|
||||
nil
|
||||
end
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# Create an image from a running server
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# Create a new server
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# Delete an image
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# Delete an existing server
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# Get details for flavor by id
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# Get details for image by id
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# Get details about a server
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# List all server addresses
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# List all flavors (IDs and names only)
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# List all flavors
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# List all images (IDs and names only)
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# List all images
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# List private server addresses
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# List public server addresses
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# List all servers (IDs and names only)
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# List all servers details
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# Reboot an existing server
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers
|
||||
class Compute
|
||||
class Real
|
||||
|
||||
# Update an existing server
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
class Real
|
||||
|
||||
# Delete an existing container
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
class Real
|
||||
|
||||
# Delete an existing container
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
class Real
|
||||
|
||||
# Get details for container and total bytes stored
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
class Real
|
||||
|
||||
# List existing storage containers
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
class Real
|
||||
|
||||
# Get details for object
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
class Real
|
||||
|
||||
# List number of objects and total bytes stored
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
class Real
|
||||
|
||||
# List number of containers and total bytes stored
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
class Real
|
||||
|
||||
# Get headers for object
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
class Real
|
||||
|
||||
# Create a new container
|
|
@ -1,6 +1,6 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Files
|
||||
class Storage
|
||||
class Real
|
||||
|
||||
# Create a new object
|
|
@ -1,109 +1,15 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Servers < Fog::Service
|
||||
|
||||
requires :rackspace_api_key, :rackspace_username
|
||||
|
||||
model_path 'fog/rackspace/models/servers'
|
||||
model :flavor
|
||||
collection :flavors
|
||||
model :image
|
||||
collection :images
|
||||
model :server
|
||||
collection :servers
|
||||
|
||||
request_path 'fog/rackspace/requests/servers'
|
||||
request :create_image
|
||||
request :create_server
|
||||
request :delete_image
|
||||
request :delete_server
|
||||
request :get_flavor_details
|
||||
request :get_image_details
|
||||
request :get_server_details
|
||||
request :list_addresses
|
||||
request :list_private_addresses
|
||||
request :list_public_addresses
|
||||
request :list_flavors
|
||||
request :list_flavors_detail
|
||||
request :list_images
|
||||
request :list_images_detail
|
||||
request :list_servers
|
||||
request :list_servers_detail
|
||||
request :reboot_server
|
||||
request :update_server
|
||||
|
||||
class Mock
|
||||
include Collections
|
||||
|
||||
def self.data
|
||||
@data ||= Hash.new do |hash, key|
|
||||
hash[key] = {
|
||||
:last_modified => {
|
||||
:images => {},
|
||||
:servers => {}
|
||||
},
|
||||
:images => {},
|
||||
:servers => {}
|
||||
}
|
||||
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
|
||||
class Servers
|
||||
|
||||
def self.new(attributes = {})
|
||||
location = caller.first
|
||||
warning = "[yellow][WARN] Fog::Rackspace::Servers#new is deprecated, use Fog::Rackspace::Compute#new instead[/]"
|
||||
warning << " [light_black](" << location << ")[/] "
|
||||
Formatador.display_line(warning)
|
||||
Fog::Rackspace::Compute.new(attributes)
|
||||
end
|
||||
|
||||
class Real
|
||||
include Collections
|
||||
|
||||
def initialize(options={})
|
||||
credentials = Fog::Rackspace.authenticate(options)
|
||||
@auth_token = credentials['X-Auth-Token']
|
||||
uri = URI.parse(credentials['X-Server-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
|
||||
@connection.reset
|
||||
end
|
||||
|
||||
def request(params)
|
||||
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::Servers::NotFound.slurp(error)
|
||||
else
|
||||
error
|
||||
end
|
||||
end
|
||||
unless response.body.empty?
|
||||
response.body = JSON.parse(response.body)
|
||||
end
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
153
lib/fog/rackspace/storage.rb
Normal file
153
lib/fog/rackspace/storage.rb
Normal file
|
@ -0,0 +1,153 @@
|
|||
module Fog
|
||||
module Rackspace
|
||||
class Storage < Fog::Service
|
||||
|
||||
requires :rackspace_api_key, :rackspace_username
|
||||
|
||||
model_path 'fog/rackspace/models/storage'
|
||||
model :directory
|
||||
collection :directories
|
||||
model :file
|
||||
collection :files
|
||||
|
||||
request_path 'fog/rackspace/requests/storage'
|
||||
request :delete_container
|
||||
request :delete_object
|
||||
request :get_container
|
||||
request :get_containers
|
||||
request :get_object
|
||||
request :head_container
|
||||
request :head_containers
|
||||
request :head_object
|
||||
request :put_container
|
||||
request :put_object
|
||||
|
||||
module Utils
|
||||
|
||||
def parse_data(data)
|
||||
metadata = {
|
||||
:body => nil,
|
||||
:headers => {}
|
||||
}
|
||||
|
||||
if data.is_a?(String)
|
||||
metadata[:body] = data
|
||||
metadata[:headers]['Content-Length'] = metadata[:body].size.to_s
|
||||
else
|
||||
filename = ::File.basename(data.path)
|
||||
unless (mime_types = MIME::Types.of(filename)).empty?
|
||||
metadata[:headers]['Content-Type'] = mime_types.first.content_type
|
||||
end
|
||||
metadata[:body] = data.read
|
||||
metadata[:headers]['Content-Length'] = ::File.size(data.path).to_s
|
||||
end
|
||||
# metadata[:headers]['Content-MD5'] = Base64.encode64(Digest::MD5.digest(metadata[:body])).strip
|
||||
metadata
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
include Collections
|
||||
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 Collections
|
||||
include Utils
|
||||
|
||||
def initialize(options={})
|
||||
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])
|
||||
end
|
||||
|
||||
def reload
|
||||
@connection.reset
|
||||
end
|
||||
|
||||
def cdn_request(params)
|
||||
begin
|
||||
response = @cdn_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]}",
|
||||
}), &block)
|
||||
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
|
Loading…
Add table
Reference in a new issue