Module: RightAws::RightAwsBaseInterface

Constant Summary

DEFAULT_SIGNATURE_VERSION =
'2'
BLOCK_DEVICE_KEY_MAPPING =

:nodoc:

{                                                           # :nodoc:
:device_name               => 'DeviceName',
:virtual_name              => 'VirtualName',
:no_device                 => 'NoDevice',
:ebs_snapshot_id           => 'Ebs.SnapshotId',
:ebs_volume_size           => 'Ebs.VolumeSize',
:ebs_delete_on_termination => 'Ebs.DeleteOnTermination' }
@@caching =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aws_access_key_idObject (readonly)

Current aws_access_key_id



259
260
261
# File 'lib/awsbase/right_awsbase.rb', line 259

def aws_access_key_id
  @aws_access_key_id
end

#aws_secret_access_keyObject (readonly)

Current aws_secret_access_key



261
262
263
# File 'lib/awsbase/right_awsbase.rb', line 261

def aws_secret_access_key
  @aws_secret_access_key
end

#cacheObject (readonly)

Cache



277
278
279
# File 'lib/awsbase/right_awsbase.rb', line 277

def cache
  @cache
end

#connectionObject (readonly)

RightHttpConnection instance



275
276
277
# File 'lib/awsbase/right_awsbase.rb', line 275

def connection
  @connection
end

#last_errorsObject

Last AWS errors list (used by AWSErrorHandler)



267
268
269
# File 'lib/awsbase/right_awsbase.rb', line 267

def last_errors
  @last_errors
end

#last_requestObject (readonly)

Last HTTP request object



263
264
265
# File 'lib/awsbase/right_awsbase.rb', line 263

def last_request
  @last_request
end

#last_request_idObject

Returns Amazons request ID for the latest request



269
270
271
# File 'lib/awsbase/right_awsbase.rb', line 269

def last_request_id
  @last_request_id
end

#last_responseObject (readonly)

Last HTTP response object



265
266
267
# File 'lib/awsbase/right_awsbase.rb', line 265

def last_response
  @last_response
end

#loggerObject

Logger object



271
272
273
# File 'lib/awsbase/right_awsbase.rb', line 271

def logger
  @logger
end

#paramsObject

Initial params hash



273
274
275
# File 'lib/awsbase/right_awsbase.rb', line 273

def params
  @params
end

#signature_versionObject (readonly)

Signature version (all services except s3)



279
280
281
# File 'lib/awsbase/right_awsbase.rb', line 279

def signature_version
  @signature_version
end

Class Method Details

.cachingObject



251
252
253
# File 'lib/awsbase/right_awsbase.rb', line 251

def self.caching
  @@caching
end

.caching=(caching) ⇒ Object



254
255
256
# File 'lib/awsbase/right_awsbase.rb', line 254

def self.caching=(caching)
  @@caching = caching
end

Instance Method Details

#amazonize_block_device_mappings(block_device_mappings, key = 'BlockDeviceMapping') ⇒ Object

:nodoc:



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/awsbase/right_awsbase.rb', line 673

def amazonize_block_device_mappings(block_device_mappings, key = 'BlockDeviceMapping') # :nodoc:
  result = {}
  unless block_device_mappings.right_blank?
    block_device_mappings = [block_device_mappings] unless block_device_mappings.is_a?(Array)
    block_device_mappings.each_with_index do |b, idx|
      BLOCK_DEVICE_KEY_MAPPING.each do |local_name, remote_name|
        value = b[local_name]
        case local_name
        when :no_device then value = value ? '' : nil   # allow to pass :no_device as boolean
        end
        result["#{key}.#{idx+1}.#{remote_name}"] = value unless value.nil?
      end
    end
  end
  result
end

#amazonize_hash_with_key_mapping(key, mapping, hash, options = {}) ⇒ Object

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'}


812
813
814
815
816
817
818
819
820
821
822
# 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

#amazonize_list(masks, list, options = {}) ⇒ Object

Format array of items into Amazons handy hash ('?' is a place holder): Options:

 :default => "something"   : Set a value to "something" when it is nil
 :default => :skip_nils    : Skip nil values

amazonize_list('Item', ['a', 'b', 'c']) =>
  { 'Item.1' => 'a', 'Item.2' => 'b', 'Item.3' => 'c' }

amazonize_list('Item.?.instance', ['a', 'c']) #=>
  { 'Item.1.instance' => 'a', 'Item.2.instance' => 'c' }

amazonize_list(['Item.?.Name', 'Item.?.Value'], {'A' => 'a', 'B' => 'b'}) #=>
  { 'Item.1.Name' => 'A', 'Item.1.Value' => 'a',
    'Item.2.Name' => 'B', 'Item.2.Value' => 'b'  }

amazonize_list(['Item.?.Name', 'Item.?.Value'], [['A','a'], ['B','b']]) #=>
  { 'Item.1.Name' => 'A', 'Item.1.Value' => 'a',
    'Item.2.Name' => 'B', 'Item.2.Value' => 'b'  }

amazonize_list(['Filter.?.Key', 'Filter.?.Value.?'], {'A' => ['aa','ab'], 'B' => ['ba','bb']}) #=>
amazonize_list(['Filter.?.Key', 'Filter.?.Value.?'], [['A',['aa','ab']], ['B',['ba','bb']]])   #=>
  {"Filter.1.Key"=>"A",
   "Filter.1.Value.1"=>"aa",
   "Filter.1.Value.2"=>"ab",
   "Filter.2.Key"=>"B",
   "Filter.2.Value.1"=>"ba",
   "Filter.2.Value.2"=>"bb"}


638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/awsbase/right_awsbase.rb', line 638

def amazonize_list(masks, list, options={}) #:nodoc:
  groups = {}
  list_idx = options[:index] || 1
  Array(list).each do |list_item|
    Array(masks).each_with_index do |mask, mask_idx|
      key = mask[/\?/] ? mask.dup : mask.dup + '.?'
      key.sub!('?', list_idx.to_s)
      value = Array(list_item)[mask_idx]
      if value.is_a?(Array)
        groups.merge!(amazonize_list(key, value, options))
      else
        if value.nil?
          next if options[:default] == :skip_nils
          value = options[:default]
        end
        # Hack to avoid having unhandled '?' in keys : do replace them all with '1':
        #  bad:  ec2.amazonize_list(['Filter.?.Key', 'Filter.?.Value.?'], { a: => :b }) => {"Filter.1.Key"=>:a, "Filter.1.Value.?"=>1}
        #  good: ec2.amazonize_list(['Filter.?.Key', 'Filter.?.Value.?'], { a: => :b }) => {"Filter.1.Key"=>:a, "Filter.1.Value.1"=>1}
        key.gsub!('?', '1')
        groups[key] = value
      end
    end
    list_idx += 1
  end
  groups
end

#amazonize_list_with_key_mapping(key, mapping, list, options = {}) ⇒ Object

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'}


833
834
835
836
837
838
839
840
841
842
843
844
# 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

#cache_hits?(function, response, do_raise = :raise) ⇒ Boolean

Check if the aws function response hits the cache or not. If the cache hits:

  • raises an AwsNoChange exception if do_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.



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# 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

#caching?Boolean

Returns true if the describe_xxx responses are being cached



339
340
341
# File 'lib/awsbase/right_awsbase.rb', line 339

def caching?
  @params.key?(:cache) ? @params[:cache] : @@caching
end

#destroy_connection(request, reason) ⇒ Object

:nodoc:



403
404
405
406
407
408
409
410
# File 'lib/awsbase/right_awsbase.rb', line 403

def destroy_connection(request, reason) # :nodoc:
  connections = get_connections_storage(request[:aws_service])
  server_url  = get_server_url(request)
  if connections[server_url]
    connections[server_url][:connection].finish(reason)
    connections.delete(server_url)
  end
end

#generate_request_impl(verb, action, options = {}, custom_options = {}) ⇒ Object

ACF, AMS, EC2, LBS and SDB uses this guy SQS and S3 use their own methods



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/awsbase/right_awsbase.rb', line 441

def generate_request_impl(verb, action, options={}, custom_options={}) #:nodoc:
  # Form a valid http verb: 'GET' or 'POST' (all the other are not supported now)
  http_verb = verb.to_s.upcase
  # remove empty keys from request options
  options.delete_if { |key, value| value.nil? }
  # prepare service data
  service_hash = {"Action"         => action,
                  "AWSAccessKeyId" => @aws_access_key_id,
                  "Version"        => custom_options[:api_version] || @params[:api_version] }
  service_hash.merge!(options)
  service_hash["SecurityToken"] = @params[:token] if @params[:token]
  # Sign request options
  service_params = signed_service_params(@aws_secret_access_key, service_hash, http_verb, @params[:host_to_sign], @params[:service])
  # Use POST if the length of the query string is too large
  # see http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/MakingRESTRequests.html
  if http_verb != 'POST' && service_params.size > 2000
    http_verb = 'POST'
    if signature_version == '2'
      service_params = signed_service_params(@aws_secret_access_key, service_hash, http_verb, @params[:host_to_sign], @params[:service])
    end
  end
  # create a request
  case http_verb
  when 'GET'
    request = Net::HTTP::Get.new("#{@params[:service]}?#{service_params}")
  when 'POST'
    request      = Net::HTTP::Post.new(@params[:service])
    request.body = service_params
    request['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
  else
    raise "Unsupported HTTP verb #{verb.inspect}!"
  end
  # prepare output hash
  request_hash = { :request  => request,
                   :server   => @params[:server],
                   :port     => @params[:port],
                   :protocol => @params[:protocol] }
  request_hash.merge!(@params[:connection_options])
  request_hash.merge!(@with_connection_options)
  
  # If an action is marked as "non-retryable" and there was no :raise_on_timeout option set
  # explicitly then do set that option
  if Array(RightAwsBase::raise_on_timeout_on_actions).include?(action) && !request_hash.has_key?(:raise_on_timeout)
    request_hash.merge!(:raise_on_timeout => true)
  end

  request_hash
end

#get_connection(request) ⇒ Object

Expire the connection if it has expired.



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/awsbase/right_awsbase.rb', line 413

def get_connection(request) # :nodoc:
  server_url         = get_server_url(request)
  connection_storage = get_connections_storage(request[:aws_service])
  life_time_scratch  = Time.now-@params[:connection_lifetime]
  # Delete out-of-dated connections
  connections_in_list = 0
  connection_storage.to_a.sort{|conn1, conn2| conn2[1][:last_used_at] <=> conn1[1][:last_used_at]}.each do |serv_url, conn_opts|
    if    @params[:max_connections] <= connections_in_list
      conn_opts[:connection].finish('out-of-limit')
      connection_storage.delete(server_url)
    elsif conn_opts[:last_used_at] < life_time_scratch
      conn_opts[:connection].finish('out-of-date')
      connection_storage.delete(server_url)
    else
      connections_in_list += 1
    end
  end
  connection = (connection_storage[server_url] ||= {})
  connection[:last_used_at] = Time.now
  connection[:connection] ||= Rightscale::HttpConnection.new(:exception => RightAws::AwsError, :logger => @logger)
end

#get_connections_storage(aws_service) ⇒ Object

:nodoc:



396
397
398
399
400
401
# File 'lib/awsbase/right_awsbase.rb', line 396

def get_connections_storage(aws_service) # :nodoc:
  case @params[:connections].to_s
  when 'dedicated' then @connections_storage        ||= {}
  else                  Thread.current[aws_service] ||= {}
  end
end

#get_server_url(request) ⇒ Object


HTTP Connections handling




392
393
394
# File 'lib/awsbase/right_awsbase.rb', line 392

def get_server_url(request) # :nodoc:
  "#{request[:protocol]}://#{request[:server]}:#{request[:port]}"
end

#incrementally_list_items(action, parser_class, params = {}, &block) ⇒ Object

Incrementally lists something.



598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/awsbase/right_awsbase.rb', line 598

def incrementally_list_items(action, parser_class, params={}, &block) # :nodoc:
  params = params.dup
  params['MaxItems'] = params.delete(:max_items) if params[:max_items]
  params['Marker']   = params.delete(:marker)    if params[:marker]
  last_response = nil
  loop do
    last_response    = request_info( generate_request(action, params), parser_class.new(:logger => @logger))
    params['Marker'] = last_response[:marker]
    break unless block && block.call(last_response) && !last_response[:marker].right_blank?
  end
  last_response
end

#init(service_info, aws_access_key_id, aws_secret_access_key, params = {}) ⇒ Object

:nodoc:

Raises:



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/awsbase/right_awsbase.rb', line 281

def init(service_info, aws_access_key_id, aws_secret_access_key, params={}) #:nodoc:
  @params = params
  # If one defines EC2_URL he may forget to use a single slash as an "empty service" path.
  # Amazon does not like this therefore add this bad boy if he is missing...
  service_info[:default_service] = '/' if service_info[:default_service].right_blank?
  raise AwsError.new("AWS access keys are required to operate on #{service_info[:name]}") \
    if aws_access_key_id.right_blank? || aws_secret_access_key.right_blank?
  @aws_access_key_id     = aws_access_key_id
  @aws_secret_access_key = aws_secret_access_key
  # if the endpoint was explicitly defined - then use it
  if @params[:endpoint_url]
    uri = URI.parse(@params[:endpoint_url])
    @params[:server]   = uri.host
    @params[:port]     = uri.port
    @params[:service]  = uri.path
    @params[:protocol] = uri.scheme
    # make sure the 'service' path is not empty
    @params[:service]  = service_info[:default_service] if @params[:service].right_blank?
    @params[:region]   = nil
    default_port       = uri.default_port
  else
    @params[:server]   ||= service_info[:default_host]
    @params[:server]     = "#{@params[:region]}.#{@params[:server]}" if @params[:region]
    @params[:port]     ||= service_info[:default_port]
    @params[:service]  ||= service_info[:default_service]
    @params[:protocol] ||= service_info[:default_protocol]
    default_port         = @params[:protocol] == 'https' ? 443 : 80
  end
  # build a host name to sign
  @params[:host_to_sign]  = @params[:server].dup
  @params[:host_to_sign] << ":#{@params[:port]}" unless default_port == @params[:port].to_i
  # a set of options to be passed to RightHttpConnection object
  @params[:connection_options] = {} unless @params[:connection_options].is_a?(Hash) 
  @with_connection_options = {}
  @params[:connections] ||= :shared # || :dedicated
  @params[:max_connections] ||= 10
  @params[:connection_lifetime] ||= 20*60
  @params[:api_version]  ||= service_info[:default_api_version]
  @logger = @params[:logger]
  @logger = ::Rails.logger       if !@logger && defined?(::Rails) && ::Rails.respond_to?(:logger)
  @logger = RAILS_DEFAULT_LOGGER if !@logger && defined?(RAILS_DEFAULT_LOGGER)
  @logger = Logger.new(STDOUT)   if !@logger
  @logger.info "New #{self.class.name} using #{@params[:connections]} connections mode"
  @error_handler = nil
  @cache = {}
  @signature_version = (params[:signature_version] || DEFAULT_SIGNATURE_VERSION).to_s
end

#map_api_keys_and_values(options, *mappings) ⇒ Object

Build API request keys set.

Options is a hash, expectations is a set of keys [and rules] how to represent options. Mappings is an Array (may include hashes) or a Hash.

Example:

options = { :valid_from              => Time.now - 10,
            :instance_count          => 3,
            :image_id                => 'ami-08f41161',
            :spot_price              => 0.059,
            :instance_type           => 'c1.medium',
            :instance_count          => 1,
            :key_name                => 'tim',
            :availability_zone       => 'us-east-1a',
            :monitoring_enabled      => true,
            :launch_group            => 'lg1',
            :availability_zone_group => 'azg1',
            :groups                  => ['a', 'b', 'c'],
            :group_ids               => 'sg-1',
            :user_data               => 'konstantin',
            :block_device_mappings   => [ { :device_name     => '/dev/sdk',
                                            :ebs_snapshot_id => 'snap-145cbc7d',
                                            :ebs_delete_on_termination => true,
                                            :ebs_volume_size => 3,
                                            :virtual_name => 'ephemeral2' }]}
mappings = { :spot_price,
             :availability_zone_group,
             :launch_group,
             :type,
             :instance_count,
             :image_id              => 'LaunchSpecification.ImageId',
             :instance_type         => 'LaunchSpecification.InstanceType',
             :key_name              => 'LaunchSpecification.KeyName',
             :addressing_type       => 'LaunchSpecification.AddressingType',
             :kernel_id             => 'LaunchSpecification.KernelId',
             :ramdisk_id            => 'LaunchSpecification.RamdiskId',
             :subnet_id             => 'LaunchSpecification.SubnetId',
             :availability_zone     => 'LaunchSpecification.Placement.AvailabilityZone',
             :monitoring_enabled    => 'LaunchSpecification.Monitoring.Enabled',
             :valid_from            => { :value => Proc.new { !options[:valid_from].right_blank?  && AwsUtils::utc_iso8601(options[:valid_from]) }},
             :valid_until           => { :value => Proc.new { !options[:valid_until].right_blank? && AwsUtils::utc_iso8601(options[:valid_until]) }},
             :user_data             => { :name  => 'LaunchSpecification.UserData',
                                         :value => Proc.new { !options[:user_data].right_blank? && Base64.encode64(options[:user_data]).delete("\n") }},
             :groups                => { :amazonize_list => 'LaunchSpecification.SecurityGroup'},
             :group_ids             => { :amazonize_list => 'LaunchSpecification.SecurityGroupId'},
             :block_device_mappings => { :amazonize_bdm  => 'LaunchSpecification.BlockDeviceMapping'})

map_api_keys_and_values( options, mappings) #=>
  {"LaunchSpecification.BlockDeviceMapping.1.Ebs.DeleteOnTermination" => true,
   "LaunchSpecification.BlockDeviceMapping.1.VirtualName"             => "ephemeral2",
   "LaunchSpecification.BlockDeviceMapping.1.Ebs.VolumeSize"          => 3,
   "LaunchSpecification.BlockDeviceMapping.1.Ebs.SnapshotId"          => "snap-145cbc7d",
   "LaunchSpecification.BlockDeviceMapping.1.DeviceName"              => "/dev/sdk",
   "LaunchSpecification.SecurityGroupId.1"                            => "sg-1",
   "LaunchSpecification.InstanceType"                                 => "c1.medium",
   "LaunchSpecification.KeyName"                                      => "tim",
   "LaunchSpecification.ImageId"                                      => "ami-08f41161",
   "LaunchSpecification.SecurityGroup.1"                              => "a",
   "LaunchSpecification.SecurityGroup.2"                              => "b",
   "LaunchSpecification.SecurityGroup.3"                              => "c",
   "LaunchSpecification.Placement.AvailabilityZone"                   => "us-east-1a",
   "LaunchSpecification.Monitoring.Enabled"                           => true,
   "LaunchGroup"                                                      => "lg1",
   "InstanceCount"                                                    => 1,
   "SpotPrice"                                                        => 0.059,
   "AvailabilityZoneGroup"                                            => "azg1",
   "ValidFrom"                                                        => "2011-06-30T08:06:30.000Z",
   "LaunchSpecification.UserData"                                     => "a29uc3RhbnRpbg=="}


760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
# File 'lib/awsbase/right_awsbase.rb', line 760

def map_api_keys_and_values(options, *mappings) # :nodoc:
  result = {}
  vars   = {}
  # Fix inputs and make them all to be hashes
  mappings.flatten.each do |mapping|
    unless mapping.is_a?(Hash)
      # mapping is just a :key_name
      mapping = { mapping => { :name  => mapping.to_s.right_camelize, :value => options[mapping] }}
    else
      mapping.each do |local_key, api_opts|
        unless api_opts.is_a?(Hash)
          # mapping is a { :key_name => 'ApiKeyName' }
          mapping[local_key] = { :name  => api_opts.to_s, :value => options[local_key]}
        else
          # mapping is a { :key_name => { :name => 'ApiKeyName', :value => 'Value', ... etc}  }
          api_opts[:name]  = local_key.to_s.right_camelize if (api_opts.keys & [:name, :amazonize_list, :amazonize_bdm]).right_blank?
          api_opts[:value] = options[local_key] unless api_opts.has_key?(:value)
        end
      end
    end
    vars.merge! mapping
  end
  # Build API keys set
  #  vars now is a Hash:
  #    { :key1 => { :name           => 'ApiKey1',   :value => 'BlahBlah'},
  #      :key2 => { :amazonize_list => 'ApiKey2.?', :value => [1, ...] },
  #      :key3 => { :amazonize_bdm  => 'BDM',       :value => [{..}, ...] }, ... }
  #
  vars.each do |local_key, api_opts|
    if api_opts[:amazonize_list]
      result.merge!(amazonize_list( api_opts[:amazonize_list], api_opts[:value] )) unless api_opts[:value].right_blank?
    elsif api_opts[:amazonize_bdm]
      result.merge!(amazonize_block_device_mappings( api_opts[:value], api_opts[:amazonize_bdm] )) unless api_opts[:value].right_blank?
    else
      api_key = api_opts[:name]
      value   = api_opts[:value]
      value   = value.call if value.is_a?(Proc)
      next if value.right_blank?
      result[api_key] = value
    end
  end
  #
  result
end

#on_exception(options = {:raise=>true, :log=>true}) ⇒ Object

:nodoc:



383
384
385
386
# File 'lib/awsbase/right_awsbase.rb', line 383

def on_exception(options={:raise=>true, :log=>true}) # :nodoc:
  raise if $!.is_a?(AwsNoChange)
  AwsError::on_aws_exception(self, options)
end

#request_cache_or_info(method, link, parser_class, benchblock, use_cache = true, &block) ⇒ Object

:nodoc:



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/awsbase/right_awsbase.rb', line 576

def request_cache_or_info(method, link, parser_class, benchblock, use_cache=true, &block) #:nodoc:
  # We do not want to break the logic of parsing hence will use a dummy parser to process all the standard
  # steps (errors checking etc). The dummy parser does nothig - just returns back the params it received.
  # If the caching is enabled and hit then throw  AwsNoChange.
  # P.S. caching works for the whole images list only! (when the list param is blank)
  # check cache
  response, params = request_info(link, RightDummyParser.new)
  cache_hits?(method.to_sym, response.body) if use_cache
  parser = parser_class.new(:logger => @logger)
  benchblock.xml.add!{ parser.parse(response, params) }
  result = block ? block.call(parser) : parser.result
  # update parsed data
  update_cache(method.to_sym, :parsed => result) if use_cache
  result
end

#request_info_impl(aws_service, benchblock, request, parser, &block) ⇒ Object

All services uses this guy.



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/awsbase/right_awsbase.rb', line 491

def request_info_impl(aws_service, benchblock, request, parser, &block) #:nodoc:
  request[:aws_service] = aws_service
  @connection    = get_connection(request)
  @last_request  = request[:request]
  @last_response = nil
  response = nil
  blockexception = nil

  if(block != nil)
    # TRB 9/17/07 Careful - because we are passing in blocks, we get a situation where
    # an exception may get thrown in the block body (which is high-level
    # code either here or in the application) but gets caught in the
    # low-level code of HttpConnection.  The solution is not to let any
    # exception escape the block that we pass to HttpConnection::request.
    # Exceptions can originate from code directly in the block, or from user
    # code called in the other block which is passed to response.read_body.
    benchblock.service.add! do
      begin
        responsehdr = @connection.request(request) do |response|
        #########
          begin
            @last_response = response
            if response.is_a?(Net::HTTPSuccess)
              @error_handler = nil
              response.read_body(&block)
            else
              @error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler
              check_result   = @error_handler.check(request)
              if check_result
                @error_handler = nil
                return check_result
              end
              raise AwsError.new(@last_errors, @last_response.code, @last_request_id)
            end
          rescue Exception => e
            blockexception = e
          end
        end
      rescue Exception => e
        # Kill a connection if we run into a low level connection error
        destroy_connection(request, "error: #{e.message}")
        raise e
      end
      #########

      #OK, now we are out of the block passed to the lower level
      if(blockexception)
        raise blockexception
      end
      benchblock.xml.add! do
        parser.parse(responsehdr)
      end
      return parser.result
    end
  else
    benchblock.service.add! do
      begin
        response = @connection.request(request)
      rescue Exception => e
        # Kill a connection if we run into a low level connection error
        destroy_connection(request, "error: #{e.message}")
        raise e
      end
    end
      # check response for errors...
    @last_response = response
    if response.is_a?(Net::HTTPSuccess)
      @error_handler = nil
      benchblock.xml.add! { parser.parse(response) }
      return parser.result
    else
      @error_handler = AWSErrorHandler.new(self, parser, :errors_list => self.class.amazon_problems) unless @error_handler
      check_result   = @error_handler.check(request)
      if check_result
        @error_handler = nil
        return check_result 
      end
      raise AwsError.new(@last_errors, @last_response.code, @last_request_id)
    end
  end
rescue
  @error_handler = nil
  raise
end

#signed_service_params(aws_secret_access_key, service_hash, http_verb = nil, host = nil, service = nil) ⇒ Object



329
330
331
332
333
334
335
336
# 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

#update_cache(function, hash) ⇒ Object



379
380
381
# File 'lib/awsbase/right_awsbase.rb', line 379

def update_cache(function, hash)
  (@cache[function.to_sym] ||= {}).merge!(hash) if caching?
end

#with_connection_options(options, &block) ⇒ Object

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


898
899
900
901
902
903
# 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