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

Merge pull request #1583 from rackspace/auth20

[Rackspace] Updated compute_v2, storage, and cdn services to support auth 2.0
This commit is contained in:
Kyle Rames 2013-03-12 08:07:50 -07:00
commit 1a75f0e4e5
13 changed files with 734 additions and 130 deletions

View file

@ -5,6 +5,9 @@ module Fog
module Rackspace
extend Fog::Provider
US_AUTH_ENDPOINT = 'https://identity.api.rackspacecloud.com/v2.0'
UK_AUTH_ENDPOINT = 'https://lon.identity.api.rackspacecloud.com/v2.0'
module Errors
class ServiceError < Fog::Errors::Error
attr_reader :response_data, :status_code

View file

@ -1,4 +1,5 @@
require 'fog/rackspace'
require 'fog/rackspace/service'
require 'fog/cdn'
module Fog
@ -6,7 +7,7 @@ module Fog
class Rackspace < Fog::Service
requires :rackspace_api_key, :rackspace_username
recognizes :rackspace_auth_url, :persistent, :rackspace_cdn_ssl
recognizes :rackspace_auth_url, :persistent, :rackspace_cdn_ssl, :rackspace_region, :rackspace_cdn_url
request_path 'fog/rackspace/requests/cdn'
request :get_containers
@ -24,6 +25,18 @@ module Fog
"X-Cdn-Ssl-Uri" => :ssl_uri
}.freeze
def service_name
:cloudFilesCDN
end
def region
@rackspace_region
end
def endpoint_uri(service_endpoint_url=nil)
@uri = super(@rackspace_cdn_url || service_endpoint_url, :rackspace_cdn_url)
end
def publish_container(container, publish = true)
enabled = publish ? 'True' : 'False'
response = put_container(container.key, 'X-Cdn-Enabled' => enabled)
@ -53,9 +66,9 @@ module Fog
end
end
class Mock
class Mock < Fog::Rackspace::Service
include Base
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {}
@ -85,27 +98,24 @@ module Fog
end
class Real
class Real < Fog::Rackspace::Service
include Base
def initialize(options={})
@connection_options = options[:connection_options] || {}
credentials = Fog::Rackspace.authenticate(options, @connection_options)
@auth_token = credentials['X-Auth-Token']
@rackspace_auth_url = options[:rackspace_auth_url]
@rackspace_cdn_url = options[:rackspace_cdn_url]
@rackspace_region = options[:rackspace_region] || :dfw
authenticate(options)
@enabled = false
@persistent = options[:persistent] || false
if credentials['X-CDN-Management-Url']
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}", @persistent, @connection_options)
if endpoint_uri
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
@enabled = true
end
end
def enabled?
@enabled
end
@ -116,7 +126,7 @@ module Fog
def purge(file)
unless file.is_a? Fog::Storage::Rackspace::File
raise Fog::Errors::NotImplemented.new("#{object.class} does not support CDN purging")
raise Fog::Errors::NotImplemented.new("#{object.class} does not support CDN purging") if object
end
delete_object file.directory.key, file.key
@ -129,10 +139,10 @@ module Fog
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Auth-Token' => @auth_token
'X-Auth-Token' => auth_token
}.merge!(params[:headers] || {}),
:host => @host,
:path => "#{@path}/#{params[:path]}",
:host => endpoint_uri.host,
:path => "#{endpoint_uri.path}/#{params[:path]}",
}))
rescue Excon::Errors::HTTPStatusError => error
raise case error
@ -147,6 +157,14 @@ module Fog
end
response
end
private
def authenticate_v1(options)
credentials = Fog::Rackspace.authenticate(options, @connection_options)
endpoint_uri credentials['X-CDN-Management-Url']
@auth_token = credentials['X-Auth-Token']
end
end
end

View file

@ -1,4 +1,6 @@
require 'fog/compute'
require 'fog/rackspace/service'
module Fog
module Compute
@ -16,6 +18,8 @@ module Fog
recognizes :rackspace_endpoint
recognizes :rackspace_auth_url
recognizes :rackspace_auth_token
recognizes :rackspace_region
recognizes :rackspace_compute_url
model_path 'fog/rackspace/models/compute_v2'
model :server
@ -69,8 +73,8 @@ module Fog
request :create_network
request :delete_network
class Mock
include Fog::Rackspace::MockData
class Mock < Fog::Rackspace::Service
include Fog::Rackspace::MockData
def initialize(options)
@rackspace_api_key = options[:rackspace_api_key]
@ -93,26 +97,23 @@ module Fog
end
end
class Real
class Real < Fog::Rackspace::Service
def initialize(options = {})
@rackspace_api_key = options[:rackspace_api_key]
@rackspace_username = options[:rackspace_username]
@rackspace_auth_url = options[:rackspace_auth_url]
@rackspace_endpoint = options[:rackspace_compute_url] || options[:rackspace_endpoint]
@rackspace_region = options[:rackspace_region] || :dfw
@rackspace_must_reauthenticate = false
@connection_options = options[:connection_options] || {}
endpoint = options[:rackspace_endpoint] || DFW_ENDPOINT
uri = URI.parse(endpoint)
@host = uri.host
@persistent = options[:persistent] || false
@path = uri.path
@port = uri.port
@scheme = uri.scheme
authenticate
@connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options)
deprecation_warnings(options)
@persistent = options[:persistent] || false
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
end
def request(params)
@ -121,10 +122,10 @@ module Fog
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Auth-Token' => @auth_token
'X-Auth-Token' => auth_token
}.merge!(params[:headers] || {}),
:host => @host,
:path => "#{@path}/#{params[:path]}"
:host => @uri.host,
:path => "#{endpoint_uri.path}/#{params[:path]}"
}))
rescue Excon::Errors::NotFound => error
raise NotFound.slurp error
@ -145,19 +146,51 @@ module Fog
end
response
end
private
def authenticate
options = {
:rackspace_api_key => @rackspace_api_key,
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url
}
credentials = Fog::Rackspace.authenticate(options, @connection_options)
@auth_token = credentials['X-Auth-Token']
super(options)
end
def service_name
:cloudServersOpenStack
end
def region
@rackspace_region
end
def endpoint_uri(service_endpoint_url=nil)
@uri = super(@rackspace_endpoint || service_endpoint_url, :rackspace_compute_url)
end
private
def deprecation_warnings(options)
Fog::Logger.deprecation("The :rackspace_endpoint option is deprecated. Please use :rackspace_compute_url for custom endpoints") if options[:rackspace_endpoint]
if [DFW_ENDPOINT, ORD_ENDPOINT, LON_ENDPOINT].include?(@rackspace_endpoint) && v2_authentication?
regions = @identity_service.service_catalog.display_service_regions(:cloudServersOpenStack)
Fog::Logger.deprecation("Please specify region using :rackspace_region rather than :rackspace_endpoint. Valid region for :rackspace_region are #{regions}.")
end
end
def setup_endpoint(credentials)
account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
@path = "#{@path}/#{account_id}"
endpoint = @rackspace_endpoint || credentials['X-Server-Management-Url'] || DFW_ENDPOINT
@uri = URI.parse(endpoint)
@uri.path = "#{@uri.path}/#{account_id}"
end
def authenticate_v1(options)
credentials = Fog::Rackspace.authenticate(options, @connection_options)
setup_endpoint credentials
@auth_token = credentials['X-Auth-Token']
end
end
end

View file

@ -7,7 +7,7 @@ module Fog
UK_ENDPOINT = 'https://lon.identity.api.rackspacecloud.com/v2.0'
requires :rackspace_username, :rackspace_api_key
recognizes :rackspace_auth_url
recognizes :rackspace_auth_url, :rackspace_region
model_path 'fog/rackspace/models/identity'
model :user
@ -18,6 +18,7 @@ module Fog
collection :credentials
model :tenant
collection :tenants
model :service_catalog
request_path 'fog/rackspace/requests/identity'
request :create_token
@ -33,15 +34,20 @@ module Fog
request :delete_user
class Mock
attr_reader :service_catalog
def request
Fog::Mock.not_implemented
end
end
class Real
attr_reader :service_catalog, :auth_token
def initialize(options={})
@rackspace_username = options[:rackspace_username]
@rackspace_api_key = options[:rackspace_api_key]
@rackspace_region = options[:rackspace_region]
@rackspace_auth_url = options[:rackspace_auth_url] || US_ENDPOINT
uri = URI.parse(@rackspace_auth_url)
@ -55,7 +61,7 @@ module Fog
authenticate
end
def request(params)
begin
parameters = params.merge!({
@ -72,9 +78,10 @@ module Fog
response
end
end
def authenticate
data = self.create_token(@rackspace_username, @rackspace_api_key).body
@service_catalog = ServiceCatalog.from_response(self, data)
@auth_token = data['access']['token']['id']
end
end

View file

@ -0,0 +1,83 @@
require 'fog/core/model'
module Fog
module Rackspace
class Identity
class ServiceCatalog < Fog::Model
attr_reader :catalog
def initialize(attributes)
@service = attributes.delete(:service)
@catalog = {}
end
def services
catalog.keys
end
def get_endpoints(service_type)
service_type = service_type.is_a?(String) ? service_type.to_sym : service_type
catalog[service_type]
end
def display_service_regions(service_type)
endpoints = get_endpoints(service_type)
endpoints.collect { |k,v| ":#{k}" }.join(", ")
end
def get_endpoint(service_type, region=nil)
endpoint = get_endpoints(service_type)
raise "Unable to locate endpoint for service #{service_type}" unless endpoint
return endpoint if endpoint.is_a?(String) #There is only one endpoint for service
unless region
raise "There are multiple endpoints avaliable for #{service_type}. Please specify one of the following regions: #{display_service_regions(service_type)}."
end
region = region.is_a?(String) ? region.to_sym : region
endpoint = get_endpoints(service_type)[region]
raise "Unknown region :#{region} for service #{service_type}. Please use one of the following regions: #{display_service_regions(service_type)}" unless endpoint
endpoint
end
def reload
@service.authenticate
@catalog = @service.service_catalog.catalog
self
end
def self.from_response(service, hash)
service_catalog = ServiceCatalog.new :service => service
services = hash["access"]["serviceCatalog"]
services.each do |serv|
name = serv["name"].to_sym
service_catalog.send(:add_endpoints, name, serv)
end
service_catalog
end
private
def add_endpoints(service_name, hash)
endpoints = hash["endpoints"]
if endpoints.size == 1
catalog[service_name] = endpoints[0]["publicURL"].freeze
else
catalog[service_name] = endpoints_from_array(endpoints)
end
end
def endpoints_from_array(endpoints)
hash = {}
endpoints.each do |endpoint|
region = endpoint["region"].downcase.to_sym
url = endpoint["publicURL"].freeze
hash[region] = url
end
hash
end
end
end
end
end

View file

@ -24,14 +24,14 @@ module Fog
method = 'GET'
expires = expires.to_i
object_path_escaped = "#{@path}/#{Fog::Rackspace.escape(container)}/#{Fog::Rackspace.escape(object,"/")}"
object_path_unescaped = "#{@path}/#{Fog::Rackspace.escape(container)}/#{object}"
object_path_escaped = "#{@uri.path}/#{Fog::Rackspace.escape(container)}/#{Fog::Rackspace.escape(object,"/")}"
object_path_unescaped = "#{@uri.path}/#{Fog::Rackspace.escape(container)}/#{object}"
string_to_sign = "#{method}\n#{expires}\n#{object_path_unescaped}"
hmac = Fog::HMAC.new('sha1', @rackspace_temp_url_key)
sig = sig_to_hex(hmac.sign(string_to_sign))
"https://#{@host}#{object_path_escaped}?temp_url_sig=#{sig}&temp_url_expires=#{expires}"
"https://#{@uri.host}#{object_path_escaped}?temp_url_sig=#{sig}&temp_url_expires=#{expires}"
end
private

View file

@ -0,0 +1,79 @@
module Fog
module Rackspace
class Service
def service_name
raise Fog::Errors::NotImplemented.new("Please implement the #service_name method")
end
def region
raise Fog::Errors::NotImplemented.new("Please implement the #region method")
end
def endpoint_uri(service_endpoint=nil, endpoint_name=nil)
return @uri if @uri
url = service_endpoint
unless url
if v1_authentication?
raise "Service Endpoint must be specified via #{endpoint_name} parameter"
else
url = endpoint_uri_v2
end
end
@uri = URI.parse url
end
def authenticate(options)
self.send authentication_method, options
end
private
def authentication_method
return :authenticate_v2 unless @rackspace_auth_url
if @rackspace_auth_url =~ /v2(\.\d)?\w*$/
:authenticate_v2
else
Fog::Logger.deprecation "Authentication using a v1.0/v1.1 endpoint is deprecated. Please specify a v2.0 endpoint using :rackpace_auth_url.\
For a list of v2.0 endpoints refer to http://docs.rackspace.com/auth/api/v2.0/auth-client-devguide/content/Endpoints-d1e180.html"
:authenticate_v1
end
end
def v1_authentication?
@identity_service.nil?
end
def v2_authentication?
@identity_service != nil
end
def authenticate_v2(identity_options)
hash = {
:rackspace_api_key => identity_options[:rackspace_api_key],
:rackspace_username => identity_options[:rackspace_username],
:rackspace_auth_url => identity_options[:rackspace_auth_url]
}
@identity_service = Fog::Rackspace::Identity.new(hash)
@auth_token = @identity_service.auth_token
end
def authenticate_v1(options)
raise Fog::Errors::NotImplemented.new("Authentication of legacy endpoints is not implemented for this service.")
end
def endpoint_uri_v2
@uri = @identity_service.service_catalog.get_endpoint(service_name, region)
end
def auth_token
@auth_token || @identity_service.auth_token
end
end
end
end

View file

@ -1,4 +1,5 @@
require 'fog/rackspace'
require 'fog/rackspace/service'
require 'fog/storage'
module Fog
@ -6,8 +7,8 @@ module Fog
class Rackspace < Fog::Service
requires :rackspace_api_key, :rackspace_username
recognizes :rackspace_auth_url, :rackspace_servicenet, :rackspace_cdn_ssl, :persistent
recognizes :rackspace_temp_url_key
recognizes :rackspace_auth_url, :rackspace_servicenet, :rackspace_cdn_ssl, :persistent, :rackspace_region
recognizes :rackspace_temp_url_key, :rackspace_storage_url, :rackspace_cdn_url
model_path 'fog/rackspace/models/storage'
model :directory
@ -39,7 +40,9 @@ module Fog
:provider => 'Rackspace',
:rackspace_api_key => @rackspace_api_key,
:rackspace_auth_url => @rackspace_auth_url,
:rackspace_cdn_url => @rackspace_cdn_url,
:rackspace_username => @rackspace_username,
:rackspace_region => @rackspace_region,
:rackspace_cdn_ssl => @rackspace_cdn_ssl
)
if @cdn.enabled?
@ -47,9 +50,9 @@ module Fog
end
end
end
end
class Mock
class Mock < Fog::Rackspace::Service
include Utils
def self.data
@ -76,10 +79,19 @@ module Fog
self.class.data.delete(@rackspace_username)
end
def service_name
:cloudFiles
end
def region
@rackspace_region
end
end
class Real
class Real < Fog::Rackspace::Service
include Utils
attr_reader :rackspace_cdn_ssl
def initialize(options={})
@ -91,14 +103,17 @@ module Fog
@rackspace_servicenet = options[:rackspace_servicenet]
@rackspace_auth_token = options[:rackspace_auth_token]
@rackspace_storage_url = options[:rackspace_storage_url]
@rackspace_cdn_url = options[:rackspace_cdn_url]
@rackspace_region = options[:rackspace_region] || :dfw
@rackspace_temp_url_key = options[:rackspace_temp_url_key]
@rackspace_must_reauthenticate = false
@connection_options = options[:connection_options] || {}
authenticate
@persistent = options[:persistent] || false
Excon.defaults[:ssl_verify_peer] = false if options[:rackspace_servicenet] == true
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end
Excon.defaults[:ssl_verify_peer] = false if service_net?
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
end
# Return Account Details
# @return [Fog::Storage::Rackspace::Account] account details object
@ -125,10 +140,10 @@ module Fog
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Auth-Token' => @auth_token
'X-Auth-Token' => auth_token
}.merge!(params[:headers] || {}),
:host => @host,
:path => "#{@path}/#{params[:path]}",
:host => endpoint_uri.host,
:path => "#{endpoint_uri.path}/#{params[:path]}",
}), &block)
rescue Excon::Errors::Unauthorized => error
if error.response.body != 'Bad username or password' # token expiration
@ -152,28 +167,46 @@ module Fog
response
end
private
def service_net?
@rackspace_servicenet == true
end
def authenticate
if @rackspace_must_reauthenticate || @rackspace_auth_token.nil?
options = {
:rackspace_api_key => @rackspace_api_key,
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url
}
credentials = Fog::Rackspace.authenticate(options, @connection_options)
@auth_token = credentials['X-Auth-Token']
uri = URI.parse(credentials['X-Storage-Url'])
}
super(options)
else
@auth_token = @rackspace_auth_token
uri = URI.parse(@rackspace_storage_url)
@uri = URI.parse(@rackspace_storage_url)
end
@host = @rackspace_servicenet == true ? "snet-#{uri.host}" : uri.host
@path = uri.path
@port = uri.port
@scheme = uri.scheme
end
def service_name
:cloudFiles
end
def region
@rackspace_region
end
def endpoint_uri(service_endpoint_url=nil)
@uri = super(@rackspace_storage_url || service_endpoint_url, :rackspace_storage_url)
@uri.host = "snet-#{@uri.host}" if service_net?
@uri
end
private
def authenticate_v1(options)
credentials = Fog::Rackspace.authenticate(options, @connection_options)
endpoint_uri credentials['X-Storage-Url']
@auth_token = credentials['X-Auth-Token']
end
end
end
end

View file

@ -1,4 +1,91 @@
Shindo.tests('Fog::CDN::Rackspace', ['rackspace']) do
def assert_method(url, method)
@service.instance_variable_set "@rackspace_auth_url", url
returns(method) { @service.send :authentication_method }
end
tests('#authentication_method') do
@service = Fog::Storage::Rackspace.new
assert_method nil, :authenticate_v2
assert_method 'https://identity.api.rackspacecloud.com', :authenticate_v1
assert_method 'https://identity.api.rackspacecloud.com/v1', :authenticate_v1
assert_method 'https://identity.api.rackspacecloud.com/v1.1', :authenticate_v1
assert_method 'https://identity.api.rackspacecloud.com/v2.0', :authenticate_v2
assert_method 'https://lon.identity.api.rackspacecloud.com', :authenticate_v1
assert_method 'https://lon.identity.api.rackspacecloud.com/v1', :authenticate_v1
assert_method 'https://lon.identity.api.rackspacecloud.com/v1.1', :authenticate_v1
assert_method 'https://lon.identity.api.rackspacecloud.com/v2.0', :authenticate_v2
end
tests('authentication v1') do
pending if Fog.mocking?
@service = Fog::CDN::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0'
tests('variables populated') do
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(false, "path populated") { @service.instance_variable_get("@uri").path.nil? }
returns(true, "identity_service was not used") { @service.instance_variable_get("@identity_service").nil? }
end
tests('custom endpoint') do
@service = Fog::CDN::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0',
:rackspace_cdn_url => 'https://my-custom-cdn-endpoint.com'
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-cdn-endpoint\.com/) != nil }
end
end
tests('authentation v2') do
pending if Fog.mocking?
@service = Fog::CDN::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0'
tests('variables populated') do
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(false, "path populated") { @service.instance_variable_get("@uri").path.nil? }
returns(false, "identity service was used") { @service.instance_variable_get("@identity_service").nil? }
end
tests('dfw region') do
@service = Fog::CDN::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0', :rackspace_region => :dfw
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true) { (@service.instance_variable_get("@uri").host =~ /cdn1/) != nil }
end
tests('ord region') do
@service = Fog::CDN::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0', :rackspace_region => :ord
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true) { (@service.instance_variable_get("@uri").host =~ /cdn2/) != nil }
end
tests('custom endpoint') do
@service = Fog::CDN::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0',
:rackspace_cdn_url => 'https://my-custom-cdn-endpoint.com'
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-cdn-endpoint\.com/) != nil }
end
end
tests('default auth') do
pending if Fog.mocking?
tests('no params') do
@service = Fog::CDN::Rackspace.new
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses DFW") { (@service.instance_variable_get("@uri").host =~ /cdn1/) != nil }
end
tests('specify region') do
@service = Fog::CDN::Rackspace.new :rackspace_region => :ord
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true) { (@service.instance_variable_get("@uri").host =~ /cdn2/) != nil }
end
tests('custom endpoint') do
@service = Fog::CDN::Rackspace.new :rackspace_cdn_url => 'https://my-custom-cdn-endpoint.com'
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-cdn-endpoint\.com/) != nil }
end
end
pending if Fog.mocking?
@ -20,59 +107,58 @@ Shindo.tests('Fog::CDN::Rackspace', ['rackspace']) do
@directory = Fog::Storage[:rackspace].directories.create(directory_attributes)
@cdn = @directory.service.cdn
begin
tests('publish_container').succeeds do
returns(nil, "CDN is not enabled") { container_meta_attributes['X-CDN-Enabled'] }
urls = @cdn.publish_container @directory
returns(true, "hash contains expected urls") { Fog::CDN::Rackspace::Base::URI_HEADERS.values.all? { |url_type| urls[url_type] } }
returns("True", "CDN is enabled") { container_meta_attributes['X-Cdn-Enabled'] }
end
tests('urls') do
tests('CDN enabled container').returns(false) do
@cdn.publish_container @directory
@cdn.urls(@directory).empty?
end
tests('Non-CDN enabled container').returns(true) do
@cdn.publish_container @directory, false
@cdn.urls(@directory).empty?
end
tests('Non-existent container').returns(true) do
non_existent_container = Fog::Storage::Rackspace::Directory.new :key => "non-existent"
@cdn.urls(non_existent_container).empty?
end
end
tests('urls_from_headers') do
headers = {
"X-Cdn-Streaming-Uri"=>"http://168e307d41afe64f1a62-d1e9259b2132e81da48ed3e1e802ef22.r2.stream.cf1.rackcdn.com",
"X-Cdn-Uri"=>"http://6e8f4bf5125c9c2e4e3a-d1e9259b2132e81da48ed3e1e802ef22.r2.cf1.rackcdn.com",
"Date"=>"Fri, 15 Feb 2013 18:36:41 GMT",
"Content-Length"=>"0",
"X-Trans-Id"=>"tx424df53b79bc43fe994d3cec0c4d2d8a",
"X-Ttl"=>"3600",
"X-Cdn-Ssl-Uri"=>"https://f83cb7d39e0b9ff9581b-d1e9259b2132e81da48ed3e1e802ef22.ssl.cf1.rackcdn.com",
"X-Cdn-Ios-Uri"=>"http://a590286a323fec6aed22-d1e9259b2132e81da48ed3e1e802ef22.iosr.cf1.rackcdn.com",
"X-Cdn-Enabled"=>"True",
"Content-Type"=>"text/html; charset=UTF-8",
"X-Log-Retention"=>"False"
}
urls = @cdn.send(:urls_from_headers, headers)
returns(4) { urls.size }
returns("http://168e307d41afe64f1a62-d1e9259b2132e81da48ed3e1e802ef22.r2.stream.cf1.rackcdn.com") { urls[:streaming_uri] }
returns("http://6e8f4bf5125c9c2e4e3a-d1e9259b2132e81da48ed3e1e802ef22.r2.cf1.rackcdn.com") { urls[:uri] }
returns("https://f83cb7d39e0b9ff9581b-d1e9259b2132e81da48ed3e1e802ef22.ssl.cf1.rackcdn.com") { urls[:ssl_uri] }
returns("http://a590286a323fec6aed22-d1e9259b2132e81da48ed3e1e802ef22.iosr.cf1.rackcdn.com") { urls[:ios_uri] }
end
tests('purge') do
pending
end
ensure
@directory.destroy if @directory
begin
tests('publish_container').succeeds do
returns(nil, "CDN is not enabled") { container_meta_attributes['X-CDN-Enabled'] }
urls = @cdn.publish_container @directory
returns(true, "hash contains expected urls") { Fog::CDN::Rackspace::Base::URI_HEADERS.values.all? { |url_type| urls[url_type] } }
returns("True", "CDN is enabled") { container_meta_attributes['X-Cdn-Enabled'] }
end
end
tests('urls') do
tests('CDN enabled container').returns(false) do
@cdn.publish_container @directory
@cdn.urls(@directory).empty?
end
tests('Non-CDN enabled container').returns(true) do
@cdn.publish_container @directory, false
@cdn.urls(@directory).empty?
end
tests('Non-existent container').returns(true) do
non_existent_container = Fog::Storage::Rackspace::Directory.new :key => "non-existent"
@cdn.urls(non_existent_container).empty?
end
end
tests('urls_from_headers') do
headers = {
"X-Cdn-Streaming-Uri"=>"http://168e307d41afe64f1a62-d1e9259b2132e81da48ed3e1e802ef22.r2.stream.cf1.rackcdn.com",
"X-Cdn-Uri"=>"http://6e8f4bf5125c9c2e4e3a-d1e9259b2132e81da48ed3e1e802ef22.r2.cf1.rackcdn.com",
"Date"=>"Fri, 15 Feb 2013 18:36:41 GMT",
"Content-Length"=>"0",
"X-Trans-Id"=>"tx424df53b79bc43fe994d3cec0c4d2d8a",
"X-Ttl"=>"3600",
"X-Cdn-Ssl-Uri"=>"https://f83cb7d39e0b9ff9581b-d1e9259b2132e81da48ed3e1e802ef22.ssl.cf1.rackcdn.com",
"X-Cdn-Ios-Uri"=>"http://a590286a323fec6aed22-d1e9259b2132e81da48ed3e1e802ef22.iosr.cf1.rackcdn.com",
"X-Cdn-Enabled"=>"True",
"Content-Type"=>"text/html; charset=UTF-8",
"X-Log-Retention"=>"False"
}
urls = @cdn.send(:urls_from_headers, headers)
returns(4) { urls.size }
returns("http://168e307d41afe64f1a62-d1e9259b2132e81da48ed3e1e802ef22.r2.stream.cf1.rackcdn.com") { urls[:streaming_uri] }
returns("http://6e8f4bf5125c9c2e4e3a-d1e9259b2132e81da48ed3e1e802ef22.r2.cf1.rackcdn.com") { urls[:uri] }
returns("https://f83cb7d39e0b9ff9581b-d1e9259b2132e81da48ed3e1e802ef22.ssl.cf1.rackcdn.com") { urls[:ssl_uri] }
returns("http://a590286a323fec6aed22-d1e9259b2132e81da48ed3e1e802ef22.iosr.cf1.rackcdn.com") { urls[:ios_uri] }
end
tests('purge') do
pending
end
ensure
@directory.destroy if @directory
end
end

View file

@ -0,0 +1,91 @@
Shindo.tests('Fog::Compute::RackspaceV2', ['rackspace']) do
def assert_method(url, method)
@service.instance_variable_set "@rackspace_auth_url", url
returns(method) { @service.send :authentication_method }
end
tests('#authentication_method') do
@service = Fog::Storage::Rackspace.new
assert_method nil, :authenticate_v2
assert_method 'auth.api.rackspacecloud.com', :authenticate_v1 # chef's default auth endpoint
assert_method 'https://identity.api.rackspacecloud.com', :authenticate_v1
assert_method 'https://identity.api.rackspacecloud.com/v1', :authenticate_v1
assert_method 'https://identity.api.rackspacecloud.com/v1.1', :authenticate_v1
assert_method 'https://identity.api.rackspacecloud.com/v2.0', :authenticate_v2
assert_method 'https://lon.identity.api.rackspacecloud.com', :authenticate_v1
assert_method 'https://lon.identity.api.rackspacecloud.com/v1', :authenticate_v1
assert_method 'https://lon.identity.api.rackspacecloud.com/v1.1', :authenticate_v1
assert_method 'https://lon.identity.api.rackspacecloud.com/v2.0', :authenticate_v2
end
tests('legacy authentication') do
pending if Fog.mocking?
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0'
tests('variables populated') do
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(false, "path populated") { @service.instance_variable_get("@uri").path.nil? }
returns(true, "identity_service was not used") { @service.instance_variable_get("@identity_service").nil? }
end
tests('custom endpoint') do
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0',
:rackspace_compute_url => 'https://my-custom-endpoint.com'
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-endpoint\.com/) != nil }
end
end
tests('current authentation') do
pending if Fog.mocking?
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0'
tests('variables populated') do
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(false, "path populated") { @service.instance_variable_get("@uri").host.nil? }
returns(false, "identity service was used") { @service.instance_variable_get("@identity_service").nil? }
end
tests('dfw region') do
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0', :rackspace_region => :dfw
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true) { (@service.instance_variable_get("@uri").host =~ /dfw/) != nil }
end
tests('ord region') do
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0', :rackspace_region => :ord
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true) { (@service.instance_variable_get("@uri").host =~ /ord/) != nil }
end
tests('custom endpoint') do
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0',
:rackspace_compute_url => 'https://my-custom-endpoint.com'
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-endpoint\.com/) != nil }
end
end
tests('default auth') do
pending if Fog.mocking?
tests('no params') do
@service = Fog::Compute::RackspaceV2.new
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true) { (@service.instance_variable_get("@uri").host =~ /dfw/) != nil }
end
tests('specify region') do
@service = Fog::Compute::RackspaceV2.new :rackspace_region => :ord
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true) { (@service.instance_variable_get("@uri").host =~ /ord/ ) != nil }
end
tests('custom endpoint') do
@service = Fog::Compute::RackspaceV2.new :rackspace_compute_url => 'https://my-custom-endpoint.com'
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-endpoint\.com/) != nil }
end
end
end

View file

@ -0,0 +1,81 @@
require 'fog/rackspace/models/identity/service_catalog'
Shindo.tests('Fog::Rackspace::ServiceCatalog | users', ['rackspace']) do
tests('#from_response') do
before_hash = {"access"=>{"token"=>{"expires"=>"2013-02-20T10:31:00.000-06:00", "tenant"=>{"name"=>"777", "id"=>"777"}, "id"=>"6ca10877-7c50-4a5c-b58f-004d835c39c3"},
"serviceCatalog"=>[{"type"=>"volume", "endpoints"=>[{"region"=>"DFW", "tenantId"=>"777", "publicURL"=>"https://dfw.blockstorage.api.rackspacecloud.com/v1/777"},
{"region"=>"ORD", "tenantId"=>"777", "publicURL"=>"https://ord.blockstorage.api.rackspacecloud.com/v1/777"}], "name"=>"cloudBlockStorage"},
{"type"=>"rax:load-balancer", "endpoints"=>[{"region"=>"ORD", "tenantId"=>"777", "publicURL"=>"https://ord.loadbalancers.api.rackspacecloud.com/v1.0/777"},
{"region"=>"DFW", "tenantId"=>"777", "publicURL"=>"https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/777"}], "name"=>"cloudLoadBalancers"},
{"type"=>"object-store", "endpoints"=>[{"internalURL"=>"https://snet-storage101.dfw1.clouddrive.com/v1/Mosso777", "region"=>"DFW",
"tenantId"=>"Mosso777",
"publicURL"=>"https://storage101.dfw1.clouddrive.com/v1/Mosso777"},
{"internalURL"=>"https://snet-storage101.ord1.clouddrive.com/v1/Mosso777", "region"=>"ORD",
"tenantId"=>"Mosso777",
"publicURL"=>"https://storage101.ord1.clouddrive.com/v1/Mosso777"}], "name"=>"cloudFiles"}, {"type"=>"rax:database",
"endpoints"=>[{"region"=>"DFW", "tenantId"=>"777", "publicURL"=>"https://dfw.databases.api.rackspacecloud.com/v1.0/777"}, {"region"=>"ORD", "tenantId"=>"777",
"publicURL"=>"https://ord.databases.api.rackspacecloud.com/v1.0/777"}], "name"=>"cloudDatabases"}, {"type"=>"rax:dns", "endpoints"=>[{"tenantId"=>"777",
"publicURL"=>"https://dns.api.rackspacecloud.com/v1.0/777"}], "name"=>"cloudDNS"}, {"type"=>"compute", "endpoints"=>[{"versionId"=>"1.0", "tenantId"=>"777",
"versionList"=>"https://servers.api.rackspacecloud.com/", "versionInfo"=>"https://servers.api.rackspacecloud.com/v1.0",
"publicURL"=>"https://servers.api.rackspacecloud.com/v1.0/777"}], "name"=>"cloudServers"}, {"type"=>"compute", "endpoints"=>[{"region"=>"DFW", "versionId"=>"2",
"tenantId"=>"777", "versionList"=>"https://dfw.servers.api.rackspacecloud.com/", "versionInfo"=>"https://dfw.servers.api.rackspacecloud.com/v2",
"publicURL"=>"https://dfw.servers.api.rackspacecloud.com/v2/777"}, {"region"=>"ORD", "versionId"=>"2", "tenantId"=>"777",
"versionList"=>"https://ord.servers.api.rackspacecloud.com/", "versionInfo"=>"https://ord.servers.api.rackspacecloud.com/v2",
"publicURL"=>"https://ord.servers.api.rackspacecloud.com/v2/777"}], "name"=>"cloudServersOpenStack"}, {"type"=>"rax:monitor", "endpoints"=>[{"tenantId"=>"777",
"publicURL"=>"https://monitoring.api.rackspacecloud.com/v1.0/777"}], "name"=>"cloudMonitoring"}, {"type"=>"rax:object-cdn", "endpoints"=>[{"region"=>"DFW",
"tenantId"=>"Mosso777", "publicURL"=>"https://cdn1.clouddrive.com/v1/Mosso777"},
{"region"=>"ORD", "tenantId"=>"Mosso777",
"publicURL"=>"https://cdn2.clouddrive.com/v1/Mosso777"}], "name"=>"cloudFilesCDN"}], "user"=>{"roles"=>[{"description"=>"User Admin
Role.", "name"=>"identity:user-admin", "id"=>"3"}], "name"=>"joe-racker", "RAX-AUTH:defaultRegion"=>"", "id"=>"TK421"}}}
after_hash = {:cloudServers=>"https://servers.api.rackspacecloud.com/v1.0/777", :cloudServersOpenStack=>{:dfw=>"https://dfw.servers.api.rackspacecloud.com/v2/777", :ord=>"https://ord.servers.api.rackspacecloud.com/v2/777"}, :cloudFiles=>{:dfw=>"https://storage101.dfw1.clouddrive.com/v1/Mosso777", :ord=>"https://storage101.ord1.clouddrive.com/v1/Mosso777"}, :cloudBlockStorage=>{:dfw=>"https://dfw.blockstorage.api.rackspacecloud.com/v1/777", :ord=>"https://ord.blockstorage.api.rackspacecloud.com/v1/777"}, :cloudMonitoring=>"https://monitoring.api.rackspacecloud.com/v1.0/777", :cloudLoadBalancers=>{:dfw=>"https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/777", :ord=>"https://ord.loadbalancers.api.rackspacecloud.com/v1.0/777"}, :cloudFilesCDN=>{:dfw=>"https://cdn1.clouddrive.com/v1/Mosso777", :ord=>"https://cdn2.clouddrive.com/v1/Mosso777"}, :cloudDatabases=>{:dfw=>"https://dfw.databases.api.rackspacecloud.com/v1.0/777", :ord=>"https://ord.databases.api.rackspacecloud.com/v1.0/777"}, :cloudDNS=>"https://dns.api.rackspacecloud.com/v1.0/777"}
@service_catalog = Fog::Rackspace::Identity::ServiceCatalog.from_response(nil, before_hash)
returns(after_hash) { @service_catalog.catalog }
end
tests('services') do
services = ["cloudBlockStorage", "cloudDNS", "cloudDatabases", "cloudFiles", "cloudFilesCDN", "cloudLoadBalancers", "cloudMonitoring", "cloudServers", "cloudServersOpenStack"]
returns(services) { @service_catalog.services.collect {|s| s.to_s }.sort }
end
tests('get_endpoints') do
endpoints = {:dfw=>"https://dfw.servers.api.rackspacecloud.com/v2/777", :ord=>"https://ord.servers.api.rackspacecloud.com/v2/777"}
returns(endpoints) { @service_catalog.get_endpoints(:cloudServersOpenStack) }
returns(endpoints) { @service_catalog.get_endpoints('cloudServersOpenStack') }
returns(nil) { @service_catalog.get_endpoints('non-existent') }
end
tests('get_endpoint') do
tests('service with mulitple endpoints') do
returns("https://dfw.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint(:cloudServersOpenStack, :dfw) }
returns("https://ord.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint(:cloudServersOpenStack, :ord) }
returns("https://dfw.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint(:cloudServersOpenStack, 'dfw') }
returns("https://dfw.servers.api.rackspacecloud.com/v2/777") { @service_catalog.get_endpoint('cloudServersOpenStack', 'dfw') }
end
tests('with one endpoint') do
returns("https://monitoring.api.rackspacecloud.com/v1.0/777") { @service_catalog.get_endpoint(:cloudMonitoring, 'dfw') }
end
tests('error conditions') do
raises(RuntimeError) { @service_catalog.get_endpoint(:cloudServersOpenStack) }
raises(RuntimeError) { @service_catalog.get_endpoint(:cloudServersOpenStack, :sat) }
raises(RuntimeError) { @service_catalog.get_endpoint('non-existent') }
end
end
tests('reload').succeeds do
pending if Fog.mocking?
service = Fog::Identity[:rackspace]
service_catalog = service.service_catalog
service_catalog.catalog[:fakeService] = "http:///fake-endpoint.com"
returns("http:///fake-endpoint.com") { service_catalog.get_endpoint :fakeService }
returns("http:///fake-endpoint.com") { service.service_catalog.get_endpoint :fakeService }
service_catalog.reload
raises(RuntimeError) { service_catalog.get_endpoint :fakeService }
raises(RuntimeError) { service.service_catalog.get_endpoint :fakeService }
end
end

View file

@ -6,7 +6,7 @@ Shindo.tests('Fog::Storage[:rackspace] | object requests', ["rackspace"]) do
module RackspaceStorageHelpers
def override_path(path)
@path = path
@uri.path = path
end
end
@ -70,7 +70,7 @@ Shindo.tests('Fog::Storage[:rackspace] | object requests', ["rackspace"]) do
storage = Fog::Storage::Rackspace.new(:rackspace_temp_url_key => "super_secret")
storage.extend RackspaceStorageHelpers
storage.override_path('/fake_version/fake_tenant')
object_url = storage.get_object_https_url('fogobjecttests', 'fog-object', expires_at)
object_url = storage.get_object_https_url('fogobjecttests', 'fog-object', expires_at)
object_url =~ /https:\/\/.*clouddrive.com\/[^\/]+\/[^\/]+\/fogobjecttests\/fog%2Dobject\?temp_url_sig=a24dd5fc955a57adce7d1b5bc4ec2c7660ab8396&temp_url_expires=1344149532/
end

View file

@ -1,8 +1,98 @@
Shindo.tests('Fog::Storage::Rackspace', ['rackspace']) do |variable|
Shindo.tests('Rackspace | Storage', ['rackspace']) do
pending if Fog.mocking?
def assert_method(url, method)
@service.instance_variable_set "@rackspace_auth_url", url
returns(method) { @service.send :authentication_method }
end
tests('#authentication_method') do
@service = Fog::Storage::Rackspace.new
assert_method nil, :authenticate_v2
assert_method 'https://identity.api.rackspacecloud.com', :authenticate_v1
assert_method 'https://identity.api.rackspacecloud.com/v1', :authenticate_v1
assert_method 'https://identity.api.rackspacecloud.com/v1.1', :authenticate_v1
assert_method 'https://identity.api.rackspacecloud.com/v2.0', :authenticate_v2
assert_method 'https://lon.identity.api.rackspacecloud.com', :authenticate_v1
assert_method 'https://lon.identity.api.rackspacecloud.com/v1', :authenticate_v1
assert_method 'https://lon.identity.api.rackspacecloud.com/v1.1', :authenticate_v1
assert_method 'https://lon.identity.api.rackspacecloud.com/v2.0', :authenticate_v2
end
tests('authentication v1') do
pending if Fog.mocking?
@service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0'
tests('variables populated') do
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(false, "path populated") { @service.instance_variable_get("@uri").nil? }
returns(true, "identity_service was not used") { @service.instance_variable_get("@identity_service").nil? }
end
tests('custom endpoint') do
@service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0',
:rackspace_storage_url => 'https://my-custom-endpoint.com'
returns(false, "auth token populated") { @service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-endpoint\.com/) != nil }
end
end
tests('authentation v2') do
pending if Fog.mocking?
@service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0'
tests('variables populated') do
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(false, "path populated") { @service.instance_variable_get("@uri").nil? }
returns(false, "identity service was used") { @service.instance_variable_get("@identity_service").nil? }
end
tests('dfw region') do
@service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0', :rackspace_region => :dfw
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true) { (@service.instance_variable_get("@uri").host =~ /dfw\d/) != nil }
end
tests('ord region') do
@service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0', :rackspace_region => :ord
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true) { (@service.instance_variable_get("@uri").host =~ /ord\d/) != nil }
end
tests('custom endpoint') do
@service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0',
:rackspace_storage_url => 'https://my-custom-endpoint.com'
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-endpoint\.com/) != nil }
end
end
tests('default auth') do
pending if Fog.mocking?
tests('no params') do
@service = Fog::Storage::Rackspace.new
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true) { (@service.instance_variable_get("@uri").host =~ /dfw\d/) != nil }
end
tests('specify region') do
@service = Fog::Storage::Rackspace.new :rackspace_region => :ord
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true) { (@service.instance_variable_get("@uri").host =~ /ord\d/ ) != nil }
end
tests('custom endpoint') do
@service = Fog::Storage::Rackspace.new :rackspace_storage_url => 'https://my-custom-endpoint.com'
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /my-custom-endpoint\.com/) != nil }
end
tests('rackspace_servicenet') do
@service = Fog::Storage::Rackspace.new :rackspace_servicenet => true
returns(true, "auth token populated") { !@service.send(:auth_token).nil? }
returns(true, "uses custom endpoint") { (@service.instance_variable_get("@uri").host =~ /snet-/) != nil }
end
end
tests('account').succeeds do
pending if Fog.mocking?
Fog::Storage[:rackspace].account
end
end
end