mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
dxsdk-27 make generic credentials
This commit is contained in:
parent
aacc5115c5
commit
ddb7f11bc5
5 changed files with 104 additions and 45 deletions
|
@ -110,9 +110,26 @@ module Fog
|
||||||
expires = false if expire > DateTime.now
|
expires = false if expire > DateTime.now
|
||||||
rescue
|
rescue
|
||||||
end
|
end
|
||||||
return options[:credentials] unless expires
|
if expires
|
||||||
options = options.clone
|
options = options.clone
|
||||||
options.delete(:credentials)
|
options.delete(:credentials)
|
||||||
|
else
|
||||||
|
endpoints = options[:credentials][:endpoints]
|
||||||
|
type = options[:hp_service_type]
|
||||||
|
zone = options[:hp_avl_zone]
|
||||||
|
begin
|
||||||
|
creds = options[:credentials].clone
|
||||||
|
creds[:endpoint_url] = endpoints_get(endpoints, type, zone)
|
||||||
|
begin
|
||||||
|
creds[:cdn_endpoint_url] = endpoints_get(endpoints, "CDN", zone)
|
||||||
|
rescue
|
||||||
|
end
|
||||||
|
return creds
|
||||||
|
rescue
|
||||||
|
end
|
||||||
|
options = options.clone
|
||||||
|
options.delete(:credentials)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
hp_auth_uri = options[:hp_auth_uri] || "https://region-a.geo-1.identity.hpcloudsvc.com:35357/v2.0/tokens"
|
hp_auth_uri = options[:hp_auth_uri] || "https://region-a.geo-1.identity.hpcloudsvc.com:35357/v2.0/tokens"
|
||||||
# append /tokens if missing from auth uri
|
# append /tokens if missing from auth uri
|
||||||
|
@ -184,19 +201,21 @@ module Fog
|
||||||
### fish out auth_token and endpoint for the service
|
### fish out auth_token and endpoint for the service
|
||||||
auth_token = body['access']['token']['id']
|
auth_token = body['access']['token']['id']
|
||||||
expires = body['access']['token']['expires']
|
expires = body['access']['token']['expires']
|
||||||
endpoint_url = get_endpoint_from_catalog(body['access']['serviceCatalog'], @hp_service_type, @hp_avl_zone)
|
endpoints = service_catalog_endpoints(body['access']['serviceCatalog'])
|
||||||
# If service is Storage, then get the CDN endpoint as well. 'Name' is unique instead of 'Type'
|
endpoint_url = endpoints_get(endpoints, @hp_service_type, @hp_avl_zone)
|
||||||
if @hp_service_type == "Object Storage"
|
begin
|
||||||
cdn_endpoint_url = get_endpoint_from_catalog(body['access']['serviceCatalog'], "CDN", @hp_avl_zone)
|
cdn_endpoint_url = endpoints_get(endpoints, "CDN", @hp_avl_zone)
|
||||||
|
rescue
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
creds = {
|
||||||
:auth_token => auth_token,
|
:auth_token => auth_token,
|
||||||
:expires => expires,
|
:expires => expires,
|
||||||
|
:endpoints => endpoints,
|
||||||
:endpoint_url => endpoint_url,
|
:endpoint_url => endpoint_url,
|
||||||
:cdn_endpoint_url => cdn_endpoint_url
|
:cdn_endpoint_url => cdn_endpoint_url
|
||||||
}
|
}
|
||||||
|
return creds
|
||||||
end
|
end
|
||||||
|
|
||||||
# CGI.escape, but without special treatment on spaces
|
# CGI.escape, but without special treatment on spaces
|
||||||
|
@ -208,20 +227,34 @@ module Fog
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def self.get_endpoint_from_catalog(service_catalog, service_type, avl_zone)
|
def self.service_catalog_endpoints(service_catalog)
|
||||||
raise "Unable to parse service catalog." unless service_catalog
|
raise "Unable to parse service catalog." unless service_catalog
|
||||||
service_item = service_catalog.detect do |s|
|
endpoints = {}
|
||||||
# 'Name' is unique instead of 'Type'
|
service_catalog.each do |s|
|
||||||
s["name"] == service_type
|
name = s["name"].to_sym
|
||||||
end
|
next if name.nil?
|
||||||
if service_item and service_item['endpoints']
|
name = name.to_sym
|
||||||
endpoint = service_item['endpoints'].detect do |ep|
|
next if s['endpoints'].nil?
|
||||||
ep['region'] == avl_zone
|
endpoints[name] = {}
|
||||||
|
s['endpoints'].each do |ep|
|
||||||
|
next if ep['region'].nil?
|
||||||
|
next if ep['publicURL'].nil?
|
||||||
|
next if ep['publicURL'].empty?
|
||||||
|
endpoints[name][ep['region'].to_sym] = ep['publicURL']
|
||||||
end
|
end
|
||||||
endpoint_url = endpoint['publicURL'] if endpoint
|
|
||||||
raise "Unable to retrieve endpoint service url for availability zone '#{avl_zone}' from service catalog. " if endpoint_url.nil?
|
|
||||||
return endpoint_url
|
|
||||||
end
|
end
|
||||||
|
return endpoints
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.endpoints_get(endpoints, service_type, avl_zone)
|
||||||
|
service_type = service_type.to_sym
|
||||||
|
avl_zone = avl_zone.to_sym
|
||||||
|
unless endpoints[service_type].nil?
|
||||||
|
unless endpoints[service_type][avl_zone].nil?
|
||||||
|
return endpoints[service_type][avl_zone]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
raise "Unable to retrieve endpoint service url for availability zone '#{avl_zone}' from service catalog. "
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.set_user_agent_header(conn_opts, base_str, client_str)
|
def self.set_user_agent_header(conn_opts, base_str, client_str)
|
||||||
|
|
|
@ -3,15 +3,18 @@ require 'date'
|
||||||
Shindo.tests('Fog::HP::BlockStorage', ['hp', 'blockstorage']) do
|
Shindo.tests('Fog::HP::BlockStorage', ['hp', 'blockstorage']) do
|
||||||
credentials = {
|
credentials = {
|
||||||
:auth_token => 'auth_token',
|
:auth_token => 'auth_token',
|
||||||
:endpoint_url => 'http://127.0.0.1:0/path/',
|
:endpoint_url => 'http://127.0.0.1/bpath/',
|
||||||
|
:endpoints => {
|
||||||
|
:"Block Storage" => {
|
||||||
|
:zone => 'http://127.0.0.1/bpath/'}},
|
||||||
:expires => (DateTime.now + 1).to_s
|
:expires => (DateTime.now + 1).to_s
|
||||||
}
|
}
|
||||||
options = {
|
options = {
|
||||||
:hp_access_key => 'hp_account_id',
|
:hp_access_key => 'key',
|
||||||
:hp_secret_key => 'hp_secret_key',
|
:hp_secret_key => 'secret',
|
||||||
:hp_tenant_id => 'hp_tenant_id',
|
:hp_tenant_id => 'tenant',
|
||||||
:hp_avl_zone => 'hp_avl_zone',
|
:hp_avl_zone => 'zone',
|
||||||
:hp_auth_uri => 'hp_auth_uri',
|
:hp_auth_uri => 'https://region-a.geo-1.identity.hpcloudsvc.com:35357/v2.0/tokens',
|
||||||
:credentials => credentials
|
:credentials => credentials
|
||||||
}
|
}
|
||||||
tests('Test good credentials').returns(credentials) do
|
tests('Test good credentials').returns(credentials) do
|
||||||
|
@ -20,14 +23,25 @@ Shindo.tests('Fog::HP::BlockStorage', ['hp', 'blockstorage']) do
|
||||||
end
|
end
|
||||||
tests('Test expired credentials') do
|
tests('Test expired credentials') do
|
||||||
credentials[:expires] = (DateTime.now - 1).to_s
|
credentials[:expires] = (DateTime.now - 1).to_s
|
||||||
raises(Excon::Errors::SocketError) { Fog::HP::BlockStorage::Real.new(options) }
|
raises(Excon::Errors::Unauthorized) { Fog::HP::BlockStorage::Real.new(options) }
|
||||||
end
|
end
|
||||||
tests('Test no expires') do
|
tests('Test no expires') do
|
||||||
credentials[:expires] = nil
|
credentials[:expires] = nil
|
||||||
raises(Excon::Errors::SocketError) { Fog::HP::BlockStorage::Real.new(options) }
|
raises(Excon::Errors::Unauthorized) { Fog::HP::BlockStorage::Real.new(options) }
|
||||||
end
|
end
|
||||||
tests('Test no creds') do
|
tests('Test no creds') do
|
||||||
options[:credentials] = nil
|
options[:credentials] = nil
|
||||||
raises(Excon::Errors::SocketError) { Fog::HP::BlockStorage::Real.new(options) }
|
raises(Excon::Errors::Unauthorized) { Fog::HP::BlockStorage::Real.new(options) }
|
||||||
|
end
|
||||||
|
tests('Test no service') do
|
||||||
|
options[:credentials] = credentials
|
||||||
|
options[:credentials][:endpoints] = {
|
||||||
|
:"CDN" => {
|
||||||
|
:zone => 'http://127.0.0.1/bpath/'}},
|
||||||
|
raises(Excon::Errors::Unauthorized) { Fog::HP::BlockStorage::Real.new(options) }
|
||||||
|
end
|
||||||
|
tests('Test no creds') do
|
||||||
|
options[:credentials][:endpoints] = nil
|
||||||
|
raises(Excon::Errors::Unauthorized) { Fog::HP::BlockStorage::Real.new(options) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,14 +2,18 @@ Shindo.tests('Fog::CDN::HP', ['hp', 'cdn']) do
|
||||||
credentials = {
|
credentials = {
|
||||||
:auth_token => 'auth_token',
|
:auth_token => 'auth_token',
|
||||||
:endpoint_url => 'http://127.0.0.1/cdnpath/',
|
:endpoint_url => 'http://127.0.0.1/cdnpath/',
|
||||||
|
:cdn_endpoint_url => "http://127.0.0.1/cdnpath/",
|
||||||
|
:endpoints => {
|
||||||
|
:"CDN" => {
|
||||||
|
:zone => 'http://127.0.0.1/cdnpath/'}},
|
||||||
:expires => (DateTime.now + 1).to_s
|
:expires => (DateTime.now + 1).to_s
|
||||||
}
|
}
|
||||||
options = {
|
options = {
|
||||||
:hp_access_key => 'hp_account_id',
|
:hp_access_key => 'key',
|
||||||
:hp_secret_key => 'hp_secret_key',
|
:hp_secret_key => 'secret',
|
||||||
:hp_tenant_id => 'hp_tenant_id',
|
:hp_tenant_id => 'tenant',
|
||||||
:hp_avl_zone => 'hp_avl_zone',
|
:hp_avl_zone => 'zone',
|
||||||
:hp_auth_uri => 'hp_auth_uri',
|
:hp_auth_uri => 'https://region-a.geo-1.identity.hpcloudsvc.com:35357/v2.0/tokens',
|
||||||
:credentials => credentials
|
:credentials => credentials
|
||||||
}
|
}
|
||||||
tests('Test cached CDN credentials').returns(credentials) do
|
tests('Test cached CDN credentials').returns(credentials) do
|
||||||
|
|
|
@ -2,14 +2,17 @@ Shindo.tests('Fog::Compute::HP', ['hp', 'compute']) do
|
||||||
credentials = {
|
credentials = {
|
||||||
:auth_token => 'auth_token',
|
:auth_token => 'auth_token',
|
||||||
:endpoint_url => 'http://127.0.0.1/computepath/',
|
:endpoint_url => 'http://127.0.0.1/computepath/',
|
||||||
|
:endpoints => {
|
||||||
|
:"Compute" => {
|
||||||
|
:zone => 'http://127.0.0.1/computepath/'}},
|
||||||
:expires => (DateTime.now + 1).to_s
|
:expires => (DateTime.now + 1).to_s
|
||||||
}
|
}
|
||||||
options = {
|
options = {
|
||||||
:hp_access_key => 'hp_account_id',
|
:hp_access_key => 'key',
|
||||||
:hp_secret_key => 'hp_secret_key',
|
:hp_secret_key => 'secret',
|
||||||
:hp_tenant_id => 'hp_tenant_id',
|
:hp_tenant_id => 'tenant',
|
||||||
:hp_avl_zone => 'hp_avl_zone',
|
:hp_avl_zone => 'zone',
|
||||||
:hp_auth_uri => 'hp_auth_uri',
|
:hp_auth_uri => 'https://region-a.geo-1.identity.hpcloudsvc.com:35357/v2.0/tokens',
|
||||||
:credentials => credentials
|
:credentials => credentials
|
||||||
}
|
}
|
||||||
tests('Test cached Compute credentials').returns(credentials) do
|
tests('Test cached Compute credentials').returns(credentials) do
|
||||||
|
|
|
@ -2,15 +2,20 @@ Shindo.tests('Fog::Storage::HP', ['hp', 'storage']) do
|
||||||
credentials = {
|
credentials = {
|
||||||
:auth_token => 'auth_token',
|
:auth_token => 'auth_token',
|
||||||
:endpoint_url => 'http://127.0.0.1/path/',
|
:endpoint_url => 'http://127.0.0.1/path/',
|
||||||
:cdn_endpoint_url => 'hp_cdn_uri',
|
:cdn_endpoint_url => 'http://127.0.0.1/cdnpath/',
|
||||||
|
:endpoints => {
|
||||||
|
:"Object Storage" => {
|
||||||
|
:zone => 'http://127.0.0.1/path/'},
|
||||||
|
:"CDN" => {
|
||||||
|
:zone => 'http://127.0.0.1/cdnpath/'}},
|
||||||
:expires => (DateTime.now + 1).to_s
|
:expires => (DateTime.now + 1).to_s
|
||||||
}
|
}
|
||||||
options = {
|
options = {
|
||||||
:hp_access_key => 'hp_account_id',
|
:hp_access_key => 'key',
|
||||||
:hp_secret_key => 'hp_secret_key',
|
:hp_secret_key => 'secret',
|
||||||
:hp_tenant_id => 'hp_tenant_id',
|
:hp_tenant_id => 'tenant',
|
||||||
:hp_avl_zone => 'hp_avl_zone',
|
:hp_avl_zone => 'zone',
|
||||||
:hp_auth_uri => 'hp_auth_uri',
|
:hp_auth_uri => 'https://region-a.geo-1.identity.hpcloudsvc.com:35357/v2.0/tokens',
|
||||||
:credentials => credentials
|
:credentials => credentials
|
||||||
}
|
}
|
||||||
tests('Test cached Storage credentials').returns(credentials) do
|
tests('Test cached Storage credentials').returns(credentials) do
|
||||||
|
|
Loading…
Add table
Reference in a new issue