1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
This commit is contained in:
Kyle Rames 2013-03-04 13:48:21 -06:00
commit 4fe5391da0
10 changed files with 197 additions and 139 deletions

View file

@ -3,7 +3,7 @@ module Fog
def initialize(url, persistent=false, params={})
Excon.defaults[:headers]['User-Agent'] ||= "fog/#{Fog::VERSION}"
@excon = Excon.new(url.to_s, params)
@excon = Excon.new(url, params)
@persistent = persistent
end

View file

@ -1,37 +0,0 @@
module Fog
module Rackspace
module Authentication
private
def authentication_method
return :authenticate_v2 unless @rackspace_auth_url
if @rackspace_auth_url =~ /v2(\.\d)?\w*$/
:authenticate_v2
else
:authenticate_v1
end
end
def v1_authentication?
@identity_service.nil?
end
def v2_authentication?
@identity_service != nil
end
def authenticate_v2(identity_options)
h = {
: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(h)
@auth_token = @identity_service.auth_token
endpoint_uri
end
end
end
end

View file

@ -1,5 +1,5 @@
require 'fog/rackspace'
require 'fog/rackspace/authentication'
require 'fog/rackspace/service'
require 'fog/cdn'
module Fog
@ -25,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)
@ -54,8 +66,7 @@ module Fog
end
end
class Mock
include Fog::Rackspace::Authentication
class Mock < Fog::Rackspace::Service
include Base
def self.data
@ -87,8 +98,7 @@ module Fog
end
class Real
include Fog::Rackspace::Authentication
class Real < Fog::Rackspace::Service
include Base
def initialize(options={})
@ -101,30 +111,11 @@ module Fog
@persistent = options[:persistent] || false
if endpoint_uri
@connection = Fog::Connection.new(endpoint_uri, @persistent, @connection_options)
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
@enabled = true
end
end
def authenticate(options)
self.send authentication_method, options
end
def endpoint_uri(service_endpoint_url=nil)
return @uri if @uri
url = @rackspace_cdn_url || service_endpoint_url
unless url
if v1_authentication?
raise "Service Endpoint must be specified via :rackspace_cdn_url parameter"
else
url = @identity_service.service_catalog.get_endpoint(:cloudFilesCDN, @rackspace_region)
end
end
@uri = URI.parse url
end
def enabled?
@enabled
end
@ -147,7 +138,7 @@ module Fog
response = @connection.request(params.merge!({
:headers => {
'Content-Type' => 'application/json',
'X-Auth-Token' => @auth_token
'X-Auth-Token' => auth_token
}.merge!(params[:headers] || {}),
:host => endpoint_uri.host,
:path => "#{endpoint_uri.path}/#{params[:path]}",
@ -170,8 +161,8 @@ module Fog
def authenticate_v1(options)
credentials = Fog::Rackspace.authenticate(options, @connection_options)
endpoint_uri credentials['X-CDN-Management-Url']
@auth_token = credentials['X-Auth-Token']
endpoint_uri credentials['X-CDN-Management-Url']
end
end

View file

@ -1,4 +1,6 @@
require 'fog/compute'
require 'fog/rackspace/service'
module Fog
module Compute
@ -71,8 +73,7 @@ module Fog
request :create_network
request :delete_network
class Mock
include Fog::Rackspace::Authentication
class Mock < Fog::Rackspace::Service
include Fog::Rackspace::MockData
def initialize(options)
@ -96,8 +97,7 @@ module Fog
end
end
class Real
include Fog::Rackspace::Authentication
class Real < Fog::Rackspace::Service
def initialize(options = {})
@rackspace_api_key = options[:rackspace_api_key]
@ -113,16 +113,7 @@ module Fog
deprecation_warnings(options)
@persistent = options[:persistent] || false
@connection = Fog::Connection.new(endpoint_uri, @persistent, @connection_options)
end
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
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
end
def request(params)
@ -130,7 +121,7 @@ module Fog
response = @connection.request(params.merge!({
:headers => {
'Content-Type' => 'application/json',
'X-Auth-Token' => @auth_token
'X-Auth-Token' => auth_token
}.merge!(params[:headers] || {}),
:host => @uri.host,
:path => "#{endpoint_uri.path}/#{params[:path]}"
@ -155,41 +146,50 @@ module Fog
response
end
def endpoint_uri(service_endpoint_url=nil)
return @uri if @uri
url = @rackspace_endpoint || service_endpoint_url
unless url
if v1_authentication?
raise "Service Endpoint must be specified via :rackspace_compute_url parameter"
else
url = @identity_service.service_catalog.get_endpoint(:cloudServersOpenStack, @rackspace_region)
end
end
@uri = URI.parse url
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
}
self.send authentication_method, options
end
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]
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']
account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1]
endpoint = @rackspace_endpoint || DFW_ENDPOINT
@uri = URI.parse(endpoint)
@uri.path = "#{@uri.path}/#{account_id}"
@uri
end
end
end

View file

@ -51,10 +51,12 @@ module Fog
services = hash["access"]["serviceCatalog"]
services.each do |serv|
name = serv["name"].to_sym
service_catalog.add_endpoints(name, serv)
service_catalog.send(:add_endpoints, name, serv)
end
service_catalog
end
end
private
def add_endpoints(service_name, hash)
endpoints = hash["endpoints"]
@ -65,7 +67,6 @@ module Fog
end
end
private
def endpoints_from_array(endpoints)
hash = {}
endpoints.each do |endpoint|

View file

@ -0,0 +1,77 @@
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
: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,5 +1,5 @@
require 'fog/rackspace'
require 'fog/rackspace/authentication'
require 'fog/rackspace/service'
require 'fog/storage'
module Fog
@ -52,10 +52,9 @@ module Fog
end
class Mock
class Mock < Fog::Rackspace::Service
include Utils
include Fog::Rackspace::Authentication
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {}
@ -80,12 +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
include Fog::Rackspace::Authentication
attr_reader :rackspace_cdn_ssl
def initialize(options={})
@ -106,7 +112,7 @@ module Fog
authenticate
@persistent = options[:persistent] || false
Excon.defaults[:ssl_verify_peer] = false if service_net?
@connection = Fog::Connection.new(endpoint_uri, @persistent, @connection_options)
@connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options)
end
def account
@ -127,7 +133,7 @@ module Fog
response = @connection.request(params.merge({
:headers => {
'Content-Type' => 'application/json',
'X-Auth-Token' => @auth_token
'X-Auth-Token' => auth_token
}.merge!(params[:headers] || {}),
:host => endpoint_uri.host,
:path => "#{endpoint_uri.path}/#{params[:path]}",
@ -165,26 +171,23 @@ module Fog
:rackspace_username => @rackspace_username,
:rackspace_auth_url => @rackspace_auth_url
}
@uri = self.send authentication_method, options
super(options)
else
@auth_token = @rackspace_auth_token
@uri = URI.parse(@rackspace_storage_url)
end
end
def service_name
:cloudFiles
end
def region
@rackspace_region
end
def endpoint_uri(service_endpoint_url=nil)
return @uri if @uri
url = @rackspace_storage_url || service_endpoint_url
unless url
if v1_authentication?
raise "Service Endpoint must be specified via :rackspace_storage_url parameter"
else
url = @identity_service.service_catalog.get_endpoint(:cloudFiles, @rackspace_region)
end
end
@uri = URI.parse url
@uri = super(@rackspace_storage_url || service_endpoint_url, :rackspace_storage_url)
@uri.host = "snet-#{@uri.host}" if service_net?
@uri
end
@ -193,8 +196,8 @@ module Fog
def authenticate_v1(options)
credentials = Fog::Rackspace.authenticate(options, @connection_options)
@auth_token = credentials['X-Auth-Token']
endpoint_uri credentials['X-Storage-Url']
@auth_token = credentials['X-Auth-Token']
end
end

View file

@ -26,13 +26,14 @@ Shindo.tests('Fog::CDN::Rackspace', ['rackspace']) do
@service = Fog::CDN::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0'
tests('variables populated') do
returns(false, "auth token populated") { @service.instance_variable_get("@auth_token").nil? }
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
@ -42,21 +43,24 @@ Shindo.tests('Fog::CDN::Rackspace', ['rackspace']) do
@service = Fog::CDN::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0'
tests('variables populated') do
returns(false, "auth token populated") { @service.instance_variable_get("@auth_token").nil? }
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
@ -66,16 +70,19 @@ Shindo.tests('Fog::CDN::Rackspace', ['rackspace']) do
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

View file

@ -28,7 +28,7 @@ Shindo.tests('Fog::Compute::RackspaceV2', ['rackspace']) do
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0'
tests('variables populated') do
returns(false, "auth token populated") { @service.instance_variable_get("@auth_token").nil? }
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
@ -36,6 +36,7 @@ Shindo.tests('Fog::Compute::RackspaceV2', ['rackspace']) do
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
@ -45,21 +46,24 @@ Shindo.tests('Fog::Compute::RackspaceV2', ['rackspace']) do
@service = Fog::Compute::RackspaceV2.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0'
tests('variables populated') do
returns(false, "auth token populated") { @service.instance_variable_get("@auth_token").nil? }
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
@ -69,14 +73,17 @@ Shindo.tests('Fog::Compute::RackspaceV2', ['rackspace']) do
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

View file

@ -26,13 +26,14 @@ Shindo.tests('Rackspace | Storage', ['rackspace']) do
@service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v1.0'
tests('variables populated') do
returns(false, "auth token populated") { @service.instance_variable_get("@auth_token").nil? }
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
@ -42,21 +43,24 @@ Shindo.tests('Rackspace | Storage', ['rackspace']) do
@service = Fog::Storage::Rackspace.new :rackspace_auth_url => 'https://identity.api.rackspacecloud.com/v2.0'
tests('variables populated') do
returns(false, "auth token populated") { @service.instance_variable_get("@auth_token").nil? }
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
@ -66,24 +70,29 @@ Shindo.tests('Rackspace | Storage', ['rackspace']) do
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
Fog::Storage[:rackspace].account
end
end