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

Merge pull request #2 from SDK-CLI-Docs/feature/31_user_agent

Feature/31 user agent
This commit is contained in:
Rupak Ganguly 2012-08-13 19:44:30 +02:00
commit b7d4c9c571
8 changed files with 61 additions and 5 deletions

View file

@ -2,6 +2,8 @@ module Fog
class Connection
def initialize(url, persistent=false, params={})
# Set a User-Agent
set_user_agent_header("fog/#{Fog::VERSION}", params)
@excon = Excon.new(url, params)
@persistent = persistent
end
@ -31,5 +33,15 @@ module Fog
@excon.reset
end
private
def set_user_agent_header(str, params)
user_agent = {'User-Agent' => str}
if params[:headers]
params[:headers] = user_agent.merge!(params[:headers])
else
params[:headers] = user_agent
end
end
end
end

View file

@ -3,6 +3,12 @@ require 'multi_json'
module Fog
module HP
# define a specific version for the HP Provider
unless const_defined?(:VERSION)
VERSION = '0.0.15'
end
extend Fog::Provider
module Errors
@ -60,6 +66,9 @@ module Fog
@auth_path = "auth/v1.0"
end
service_url = "#{@scheme}://#{@host}:#{@port}"
# Set the User-Agent
@user_agent = options[:user_agent]
set_user_agent_header(connection_options, "hpfog v1/#{Fog::HP::VERSION} (#{@user_agent})")
connection = Fog::Connection.new(service_url, false, connection_options)
@hp_account_id = options[:hp_account_id]
@hp_secret_key = options[:hp_secret_key]
@ -100,6 +109,9 @@ module Fog
@auth_path = "v2.0/tokens"
end
service_url = "#{@scheme}://#{@host}:#{@port}"
# Set the User-Agent. If the caller sets a user_agent, use it.
@user_agent = options[:user_agent]
set_user_agent_header(connection_options, "hpfog/#{Fog::HP::VERSION}", @user_agent)
connection = Fog::Connection.new(service_url, false, connection_options)
### Implement HP Control Services Authentication services ###
@ -134,7 +146,7 @@ module Fog
}
end
# add tenant_id if specified
request_body['auth']['tenantId'] = "#{@hp_tenant_id}" if @hp_tenant_id
request_body['auth']['tenantId'] = @hp_tenant_id if @hp_tenant_id
### Make the call to CS to get auth token and service catalog
response = connection.request(
@ -194,6 +206,19 @@ module Fog
end
end
def self.set_user_agent_header(conn_opts, base_str, client_str)
if client_str
user_agent = {'User-Agent' => base_str + " (#{client_str})"}
else
user_agent = {'User-Agent' => base_str}
end
if conn_opts[:headers]
conn_opts[:headers] = user_agent.merge!(conn_opts[:headers])
else
conn_opts[:headers] = user_agent
end
end
class Mock
def self.etag
Fog::Mock.random_hex(32)

View file

@ -6,7 +6,7 @@ module Fog
class HP < Fog::Service
requires :hp_secret_key, :hp_account_id, :hp_tenant_id, :hp_avl_zone
recognizes :hp_auth_uri, :persistent, :connection_options, :hp_use_upass_auth_style, :hp_auth_version
recognizes :hp_auth_uri, :persistent, :connection_options, :hp_use_upass_auth_style, :hp_auth_version, :user_agent
model_path 'fog/hp/models/block_storage'
model :volume

View file

@ -6,7 +6,7 @@ module Fog
class HP < Fog::Service
requires :hp_secret_key, :hp_account_id, :hp_tenant_id, :hp_avl_zone
recognizes :hp_auth_uri, :hp_cdn_uri, :persistent, :connection_options, :hp_use_upass_auth_style, :hp_auth_version
recognizes :hp_auth_uri, :hp_cdn_uri, :persistent, :connection_options, :hp_use_upass_auth_style, :hp_auth_version, :user_agent
model_path 'fog/hp/models/cdn'

View file

@ -6,7 +6,7 @@ module Fog
class HP < Fog::Service
requires :hp_secret_key, :hp_account_id, :hp_tenant_id, :hp_avl_zone
recognizes :hp_auth_uri, :hp_servicenet, :persistent, :connection_options, :hp_use_upass_auth_style, :hp_auth_version
recognizes :hp_auth_uri, :hp_servicenet, :persistent, :connection_options, :hp_use_upass_auth_style, :hp_auth_version, :user_agent
model_path 'fog/hp/models/compute'
model :address

View file

@ -6,7 +6,7 @@ module Fog
class HP < Fog::Service
requires :hp_secret_key, :hp_account_id, :hp_tenant_id, :hp_avl_zone
recognizes :hp_auth_uri, :hp_servicenet, :hp_cdn_ssl, :hp_cdn_uri, :persistent, :connection_options, :hp_use_upass_auth_style, :hp_auth_version
recognizes :hp_auth_uri, :hp_servicenet, :hp_cdn_ssl, :hp_cdn_uri, :persistent, :connection_options, :hp_use_upass_auth_style, :hp_auth_version, :user_agent
model_path 'fog/hp/models/storage'
model :directory

View file

@ -0,0 +1,6 @@
Shindo.tests('Fog::Connection', 'core') do
tests('user_agent').returns("fog/#{Fog::VERSION}") do
conn = Fog::Connection.new("http://www.testserviceurl.com", false, {})
conn.instance_variable_get(:@excon).connection[:headers]['User-Agent']
end
end

View file

@ -0,0 +1,13 @@
Shindo.tests('Fog::Compute[:hp] | user agent', ['hp', 'user_agent']) do
tests('default for HP providers').returns("hpfog/#{Fog::HP::VERSION}") do
pending if Fog.mocking?
conn = Fog::Compute[:hp]
conn.instance_variable_get(:@connection_options)[:headers]['User-Agent']
end
tests('overriden by clients').returns("hpfog/#{Fog::HP::VERSION} (TesterClient/1.0.0)") do
pending if Fog.mocking?
conn = Fog::Compute::HP.new(:user_agent => "TesterClient/1.0.0")
conn.instance_variable_get(:@connection_options)[:headers]['User-Agent']
end
end