module RightAws::RightAwsBaseInterface
Constants
- DEFAULT_SIGNATURE_VERSION
Attributes
Current #aws_access_key_id
Current #aws_secret_access_key
Cache
RightHttpConnection instance
Last AWS errors list (used by AWSErrorHandler)
Last HTTP request object
Last AWS request id (used by AWSErrorHandler)
Last HTTP response object
Logger object
Initial params hash
Signature version (all services except s3)
Public Class Methods
# File lib/awsbase/right_awsbase.rb, line 251 def self.caching @@caching end
# File lib/awsbase/right_awsbase.rb, line 254 def self.caching=(caching) @@caching = caching end
Public Instance Methods
Transform a hash of parameters into a hash suitable for sending to Amazon using a key mapping.
amazonize_hash_with_key_mapping('Group.Filter', {:some_param => 'SomeParam'}, {:some_param => 'value'}) #=> {'Group.Filter.SomeParam' => 'value'}
# File lib/awsbase/right_awsbase.rb, line 812 def amazonize_hash_with_key_mapping(key, mapping, hash, options={}) result = {} unless hash.right_blank? mapping.each do |local_name, remote_name| value = hash[local_name] next if value.nil? result["#{key}.#{remote_name}"] = value end end result end
Transform a list of hashes of parameters into a hash suitable for sending to Amazon using a key mapping.
amazonize_list_with_key_mapping('Group.Filter', [{:some_param => 'SomeParam'}, {:some_param => 'SomeParam'}], {:some_param => 'value'}) #=> {'Group.Filter.1.SomeParam' => 'value', 'Group.Filter.2.SomeParam' => 'value'}
# File lib/awsbase/right_awsbase.rb, line 833 def amazonize_list_with_key_mapping(key, mapping, list, options={}) result = {} unless list.right_blank? list.each_with_index do |item, index| mapping.each do |local_name, remote_name| value = item[local_name] next if value.nil? result["#{key}.#{index+1}.#{remote_name}"] = value end end end end
Check if the aws function response hits the cache or not. If the cache hits:
-
raises an
AwsNoChange
exception ifdo_raise
==:raise
. -
returnes parsed response from the cache if it exists or
true
otherwise.
If the cache miss or the caching is off then returns false
.
# File lib/awsbase/right_awsbase.rb, line 348 def cache_hits?(function, response, do_raise=:raise) result = false if caching? function = function.to_sym # get rid of requestId (this bad boy was added for API 2008-08-08+ and it is uniq for every response) # feb 04, 2009 (load balancer uses 'RequestId' hence use 'i' modifier to hit it also) response = response.sub(%r{<requestId>.+?</requestId>}i, '') # this should work for both ruby 1.8.x and 1.9.x response_md5 = Digest::MD5::new.update(response).to_s # check for changes unless @cache[function] && @cache[function][:response_md5] == response_md5 # well, the response is new, reset cache data update_cache(function, {:response_md5 => response_md5, :timestamp => Time.now, :hits => 0, :parsed => nil}) else # aha, cache hits, update the data and throw an exception if needed @cache[function][:hits] += 1 if do_raise == :raise raise(AwsNoChange, "Cache hit: #{function} response has not changed since "+ "#{@cache[function][:timestamp].strftime('%Y-%m-%d %H:%M:%S')}, "+ "hits: #{@cache[function][:hits]}.") else result = @cache[function][:parsed] || true end end end result end
Returns true
if the describe_xxx responses are being cached
# File lib/awsbase/right_awsbase.rb, line 339 def caching? @params.key?(:cache) ? @params[:cache] : @@caching end
# File lib/awsbase/right_awsbase.rb, line 329 def signed_service_params(aws_secret_access_key, service_hash, http_verb=nil, host=nil, service=nil ) case signature_version.to_s when '0' then AwsUtils::sign_request_v0(aws_secret_access_key, service_hash) when '1' then AwsUtils::sign_request_v1(aws_secret_access_key, service_hash) when '2' then AwsUtils::sign_request_v2(aws_secret_access_key, service_hash, http_verb, host, service) else raise AwsError.new("Unknown signature version (#{signature_version.to_s}) requested") end end
# File lib/awsbase/right_awsbase.rb, line 379 def update_cache(function, hash) (@cache[function.to_sym] ||= {}).merge!(hash) if caching? end
Execute a block of code with custom set of settings for right_http_connection. Accepts next options (see Rightscale::HttpConnection for explanation):
:raise_on_timeout :http_connection_retry_count :http_connection_open_timeout :http_connection_read_timeout :http_connection_retry_delay :user_agent :exception Example #1: # Try to create a snapshot but stop with exception if timeout is received # to avoid having a duplicate API calls that create duplicate snapshots. ec2 = Rightscale::Ec2::new(aws_access_key_id, aws_secret_access_key) ec2.with_connection_options(:raise_on_timeout => true) do ec2.create_snapshot('vol-898a6fe0', 'KD: WooHoo!!') end Example #2: # Opposite case when the setting is global: @ec2 = Rightscale::Ec2::new(aws_access_key_id, aws_secret_access_key, :connection_options => { :raise_on_timeout => true }) # Create an SSHKey but do tries on timeout ec2.with_connection_options(:raise_on_timeout => false) do new_key = ec2.create_key_pair('my_test_key') end Example #3: # Global settings (HttpConnection level): Rightscale::HttpConnection::params[:http_connection_open_timeout] = 5 Rightscale::HttpConnection::params[:http_connection_read_timeout] = 250 Rightscale::HttpConnection::params[:http_connection_retry_count] = 2 # Local setings (RightAws level) ec2 = Rightscale::Ec2::new(AWS_ID, AWS_KEY, :region => 'us-east-1', :connection_options => { :http_connection_read_timeout => 2, :http_connection_retry_count => 5, :user_agent => 'Mozilla 4.0' }) # Custom settings (API call level) ec2.with_connection_options(:raise_on_timeout => true, :http_connection_read_timeout => 10, :user_agent => '') do pp ec2.describe_images end
# File lib/awsbase/right_awsbase.rb, line 898 def with_connection_options(options, &block) @with_connection_options = options block.call self ensure @with_connection_options = {} end