2012-05-14 14:10:36 -04:00
|
|
|
require 'fog/core'
|
2013-01-11 16:06:58 -05:00
|
|
|
require 'fog/rackspace/mock_data'
|
2010-12-16 14:24:52 -05:00
|
|
|
|
2009-10-10 22:05:17 -04:00
|
|
|
module Fog
|
|
|
|
module Rackspace
|
2010-09-07 15:21:16 -04:00
|
|
|
extend Fog::Provider
|
|
|
|
|
2011-08-16 22:33:15 -04:00
|
|
|
module Errors
|
|
|
|
class ServiceError < Fog::Errors::Error
|
2012-12-13 15:17:12 -05:00
|
|
|
attr_reader :response_data, :status_code
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
status_code ? "[HTTP #{status_code}] #{super}" : super
|
|
|
|
end
|
2011-08-16 22:33:15 -04:00
|
|
|
|
|
|
|
def self.slurp(error)
|
2012-12-13 15:17:12 -05:00
|
|
|
data = nil
|
|
|
|
message = nil
|
|
|
|
status_code = nil
|
2013-01-11 16:06:58 -05:00
|
|
|
|
2012-12-13 15:17:12 -05:00
|
|
|
if error.response
|
2013-01-11 16:06:58 -05:00
|
|
|
status_code = error.response.status
|
2012-12-13 15:17:12 -05:00
|
|
|
unless error.response.body.empty?
|
|
|
|
data = Fog::JSON.decode(error.response.body)
|
|
|
|
message = data.values.first ? data.values.first['message'] : data['message']
|
2012-12-05 13:53:11 -05:00
|
|
|
end
|
2011-08-16 22:33:15 -04:00
|
|
|
end
|
2013-01-11 16:06:58 -05:00
|
|
|
|
2011-08-16 22:33:15 -04:00
|
|
|
new_error = super(error, message)
|
|
|
|
new_error.instance_variable_set(:@response_data, data)
|
2012-12-13 15:17:12 -05:00
|
|
|
new_error.instance_variable_set(:@status_code, status_code)
|
2011-08-16 22:33:15 -04:00
|
|
|
new_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class InternalServerError < ServiceError; end
|
2011-08-27 18:46:15 -04:00
|
|
|
class Conflict < ServiceError; end
|
|
|
|
class NotFound < ServiceError; end
|
2011-08-27 22:06:46 -04:00
|
|
|
class ServiceUnavailable < ServiceError; end
|
2011-08-16 22:33:15 -04:00
|
|
|
|
|
|
|
class BadRequest < ServiceError
|
2012-12-05 13:53:11 -05:00
|
|
|
#TODO - Need to find a better way to print out these validation errors when they are thrown
|
2011-08-16 22:33:15 -04:00
|
|
|
attr_reader :validation_errors
|
|
|
|
|
|
|
|
def self.slurp(error)
|
|
|
|
new_error = super(error)
|
2012-12-05 13:53:11 -05:00
|
|
|
unless new_error.response_data.nil? or new_error.response_data['badRequest'].nil?
|
|
|
|
new_error.instance_variable_set(:@validation_errors, new_error.response_data['badRequest']['validationErrors'])
|
2011-08-16 22:33:15 -04:00
|
|
|
end
|
|
|
|
new_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-23 15:03:28 -04:00
|
|
|
service(:block_storage, 'rackspace/block_storage', 'BlockStorage')
|
|
|
|
service(:cdn, 'rackspace/cdn', 'CDN')
|
|
|
|
service(:compute, 'rackspace/compute', 'Compute')
|
|
|
|
service(:compute_v2, 'rackspace/compute_v2', 'Compute v2')
|
|
|
|
service(:dns, 'rackspace/dns', 'DNS')
|
|
|
|
service(:storage, 'rackspace/storage', 'Storage')
|
|
|
|
service(:load_balancers, 'rackspace/load_balancers', 'LoadBalancers')
|
|
|
|
service(:identity, 'rackspace/identity', 'Identity')
|
|
|
|
service(:databases, 'rackspace/databases', 'Databases')
|
2010-09-07 15:21:16 -04:00
|
|
|
|
2011-09-12 11:01:48 -04:00
|
|
|
def self.authenticate(options, connection_options = {})
|
2010-08-19 15:28:10 -04:00
|
|
|
rackspace_auth_url = options[:rackspace_auth_url] || "auth.api.rackspacecloud.com"
|
2011-06-15 16:19:58 -04:00
|
|
|
url = rackspace_auth_url.match(/^https?:/) ? \
|
|
|
|
rackspace_auth_url : 'https://' + rackspace_auth_url
|
2011-07-01 16:04:42 -04:00
|
|
|
uri = URI.parse(url)
|
2011-09-12 11:01:48 -04:00
|
|
|
connection = Fog::Connection.new(url, false, connection_options)
|
2010-06-12 16:21:32 -04:00
|
|
|
@rackspace_api_key = options[:rackspace_api_key]
|
|
|
|
@rackspace_username = options[:rackspace_username]
|
2010-03-19 21:29:42 -04:00
|
|
|
response = connection.request({
|
2011-07-01 16:04:42 -04:00
|
|
|
:expects => [200, 204],
|
2010-03-19 21:29:42 -04:00
|
|
|
:headers => {
|
|
|
|
'X-Auth-Key' => @rackspace_api_key,
|
|
|
|
'X-Auth-User' => @rackspace_username
|
|
|
|
},
|
2011-07-01 16:04:42 -04:00
|
|
|
:host => uri.host,
|
2010-03-19 21:29:42 -04:00
|
|
|
:method => 'GET',
|
2011-07-01 16:04:42 -04:00
|
|
|
:path => (uri.path and not uri.path.empty?) ? uri.path : 'v1.0'
|
2010-03-19 21:29:42 -04:00
|
|
|
})
|
|
|
|
response.headers.reject do |key, value|
|
|
|
|
!['X-Server-Management-Url', 'X-Storage-Url', 'X-CDN-Management-Url', 'X-Auth-Token'].include?(key)
|
2009-11-08 01:29:25 -05:00
|
|
|
end
|
2009-10-10 22:05:17 -04:00
|
|
|
end
|
2011-09-16 23:46:22 -04:00
|
|
|
|
|
|
|
# CGI.escape, but without special treatment on spaces
|
|
|
|
def self.escape(str,extra_exclude_chars = '')
|
|
|
|
str.gsub(/([^a-zA-Z0-9_.-#{extra_exclude_chars}]+)/) do
|
|
|
|
'%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
|
|
|
|
end
|
|
|
|
end
|
2009-10-10 22:05:17 -04:00
|
|
|
end
|
|
|
|
end
|