Module: Aws::AwsBaseInterface

Included in:
AcfInterface, Ec2, Elb, Mon, Rds, S3Interface, SdbInterface, SqsInterface
Defined in:
lib/awsbase/right_awsbase.rb

Constant Summary collapse

DEFAULT_SIGNATURE_VERSION =
'2'
@@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



298
299
300
# File 'lib/awsbase/right_awsbase.rb', line 298

def aws_access_key_id
  @aws_access_key_id
end

#cacheObject (readonly)

Cache



314
315
316
# File 'lib/awsbase/right_awsbase.rb', line 314

def cache
  @cache
end

#connectionObject (readonly)

RightHttpConnection instance



312
313
314
# File 'lib/awsbase/right_awsbase.rb', line 312

def connection
  @connection
end

#last_errorsObject

Last AWS errors list (used by AWSErrorHandler)



304
305
306
# File 'lib/awsbase/right_awsbase.rb', line 304

def last_errors
  @last_errors
end

#last_requestObject (readonly)

Last HTTP request object



300
301
302
# File 'lib/awsbase/right_awsbase.rb', line 300

def last_request
  @last_request
end

#last_request_idObject

Returns Amazons request ID for the latest request



306
307
308
# File 'lib/awsbase/right_awsbase.rb', line 306

def last_request_id
  @last_request_id
end

#last_responseObject (readonly)

Last HTTP response object



302
303
304
# File 'lib/awsbase/right_awsbase.rb', line 302

def last_response
  @last_response
end

#loggerObject

Logger object



308
309
310
# File 'lib/awsbase/right_awsbase.rb', line 308

def logger
  @logger
end

#paramsObject

Initial params hash



310
311
312
# File 'lib/awsbase/right_awsbase.rb', line 310

def params
  @params
end

#signature_versionObject (readonly)

Signature version (all services except s3)



316
317
318
# File 'lib/awsbase/right_awsbase.rb', line 316

def signature_version
  @signature_version
end

Class Method Details

.cachingObject



289
290
291
# File 'lib/awsbase/right_awsbase.rb', line 289

def self.caching
    @@caching
end

.caching=(caching) ⇒ Object



293
294
295
# File 'lib/awsbase/right_awsbase.rb', line 293

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

Instance Method Details

#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.

Returns:

  • (Boolean)


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
# File 'lib/awsbase/right_awsbase.rb', line 524

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)
        response = response.sub(%r{<requestId>.+?</requestId>}, '')
        response_md5 =Digest::MD5.hexdigest(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

Returns:

  • (Boolean)


515
516
517
# File 'lib/awsbase/right_awsbase.rb', line 515

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

#generate_request2(aws_access_key, aws_secret_key, action, api_version, lib_params, user_params = {}) ⇒ Object

FROM SDB



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/awsbase/right_awsbase.rb', line 367

def generate_request2(aws_access_key, aws_secret_key, action, api_version, lib_params, user_params={}) #:nodoc:
    # remove empty params from request
    user_params.delete_if {|key, value| value.nil? }
#            user_params.each_pair do |k,v|
#                user_params[k] = v.force_encoding("UTF-8")
#            end
    #params_string  = params.to_a.collect{|key,val| key + "=#{CGI::escape(val.to_s)}" }.join("&")
    # prepare service data
    service = lib_params[:service]
#      puts 'service=' + service.to_s
    service_hash = {"Action"         => action,
                    "AWSAccessKeyId" => aws_access_key }
    service_hash.update("Version" => api_version) if api_version
    service_hash.update(user_params)
    service_params = signed_service_params(aws_secret_key, service_hash, :get, lib_params[:server], lib_params[:service])
    #
    # use POST method if the length of the query string is too large
    # see http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/MakingRESTRequests.html
    if service_params.size > 2000
        if signature_version == '2'
            # resign the request because HTTP verb is included into signature
            service_params = signed_service_params(aws_secret_key, service_hash, :post, lib_params[:server], service)
        end
        request      = Net::HTTP::Post.new(service)
        request.body = service_params
        request['Content-Type'] = 'application/x-www-form-urlencoded'
    else
        request = Net::HTTP::Get.new("#{service}?#{service_params}")
    end

    #puts "\n\n --------------- QUERY REQUEST TO AWS -------------- \n\n"
    #puts "#{@params[:service]}?#{service_params}\n\n"

    # prepare output hash
    { :request  => request,
      :server   => lib_params[:server],
      :port     => lib_params[:port],
      :protocol => lib_params[:protocol] }
end

#get_conn(connection_name, lib_params, logger) ⇒ Object



407
408
409
410
411
412
# File 'lib/awsbase/right_awsbase.rb', line 407

def get_conn(connection_name, lib_params, logger)
    thread = lib_params[:multi_thread] ? Thread.current : Thread.main
    thread[connection_name] ||= Rightscale::HttpConnection.new(:exception => Aws::AwsError, :logger => logger)
    conn = thread[connection_name]
    return conn
end

#hash_params(prefix, list) ⇒ Object

:nodoc:



660
661
662
663
664
# File 'lib/awsbase/right_awsbase.rb', line 660

def hash_params(prefix, list) #:nodoc:
    groups = {}
    list.each_index{|i| groups.update("#{prefix}.#{i+1}"=>list[i])} if list
    return groups
end

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

:nodoc:

Raises:



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/awsbase/right_awsbase.rb', line 318

def init(service_info, aws_access_key_id, aws_secret_access_key, params={}) #:nodoc:
    @params = params
    raise AwsError.new("AWS access keys are required to operate on #{service_info[:name]}") \
 if aws_access_key_id.blank? || aws_secret_access_key.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]
        @params[:server] = URI.parse(@params[:endpoint_url]).host
        @params[:port] = URI.parse(@params[:endpoint_url]).port
        @params[:service] = URI.parse(@params[:endpoint_url]).path
        @params[:protocol] = URI.parse(@params[:endpoint_url]).scheme
        @params[:region] = nil
    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]
    end
    if !@params[:multi_thread].nil? && @params[:connection_mode].nil? # user defined this
        @params[:connection_mode] = @params[:multi_thread] ? :per_thread : :single
    end
#      @params[:multi_thread] ||= defined?(AWS_DAEMON)
    @params[:connection_mode] ||= :default
    @params[:connection_mode] = :per_request if @params[:connection_mode] == :default
    @logger = @params[: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[:connection_mode].to_s}-connection mode"
    @error_handler = nil
    @cache = {}
    @signature_version = (params[:signature_version] || DEFAULT_SIGNATURE_VERSION).to_s
end

#multi_threadObject

Return true if this instance works in multi_thread mode and false otherwise.



563
564
565
# File 'lib/awsbase/right_awsbase.rb', line 563

def multi_thread
    @params[:multi_thread]
end

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

:nodoc:



557
558
559
560
# File 'lib/awsbase/right_awsbase.rb', line 557

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) ⇒ Object

:nodoc:



639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/awsbase/right_awsbase.rb', line 639

def request_cache_or_info(method, link, parser_class, benchblock, use_cache=true) #: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_given? ? yield(parser) : parser.result
    # update parsed data
    update_cache(method.to_sym, :parsed => result) if use_cache
    result
end

#request_info2(request, parser, lib_params, connection_name, logger, bench) ⇒ Object



414
415
416
417
# File 'lib/awsbase/right_awsbase.rb', line 414

def request_info2(request, parser, lib_params, connection_name, logger, bench)
    t = get_conn(connection_name, lib_params, logger)
    request_info_impl(t, bench, request, parser)
end

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

:nodoc:



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/awsbase/right_awsbase.rb', line 567

def request_info_impl(connection, benchblock, request, parser, &block) #:nodoc:
    @connection = connection
    @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
            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
                        request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}"
                        raise AwsError.new(@last_errors, @last_response.code, @last_request_id, request_text_data)
                    end
                rescue Exception => e
                    blockexception = e
                end
            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!{ response = @connection.request(request) }
        # 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
            request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}"
            raise AwsError.new(@last_errors, @last_response.code, @last_request_id, request_text_data)
        end
    end
rescue
    @error_handler = nil
    raise
end

#request_info_xml_simple(connection_name, lib_params, request, logger, params = {}) ⇒ Object

This is the direction we should head instead of writing our own parsers for everything, much simpler params:

- :group_tags => hash of indirection to eliminate, see: http://xml-simple.rubyforge.org/
- :force_array => true for all or an array of tag names to force
- :pull_out_array => an array of levels to dig into when generating return value (see rds.rb for example)


424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
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
489
490
491
492
493
494
495
# File 'lib/awsbase/right_awsbase.rb', line 424

def request_info_xml_simple(connection_name, lib_params, request, logger, params = {})

    @connection = get_conn(connection_name, lib_params, logger)
    @last_request = request[:request]
    @last_response = nil

    response = @connection.request(request)
#            benchblock.service.add!{ response = @connection.request(request) }
    # 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
        force_array = params[:force_array] || false
        # Force_array and group_tags don't work nice together so going to force array manually
        xml_simple_options = {"KeyToSymbol"=>false, 'ForceArray' => false}
        xml_simple_options["GroupTags"] = params[:group_tags] if params[:group_tags]

#                { 'GroupTags' => { 'searchpath' => 'dir' }
#                'ForceArray' => %r(_list$)
        parsed = XmlSimple.xml_in(response.body, xml_simple_options)
        # todo: we may want to consider stripping off a couple of layers when doing this, for instance:
        # <DescribeDBInstancesResponse xmlns="http://rds.amazonaws.com/admin/2009-10-16/">
        #  <DescribeDBInstancesResult>
        #    <DBInstances>
        # <DBInstance>....
        # Strip it off and only return an array or hash of <DBInstance>'s (hash by identifier).
        # would have to be able to make the RequestId available somehow though, perhaps some special array subclass which included that?
        unless force_array.is_a? Array
            force_array = []
        end
        parsed = symbolize(parsed, force_array)
#                puts 'parsed=' + parsed.inspect
        if params[:pull_out_array]
            ret = Aws::AwsResponseArray.new(parsed[:response_metadata])
            level_hash = parsed
            params[:pull_out_array].each do |x|
                level_hash = level_hash[x]
            end
            if level_hash.is_a? Hash # When there's only one
                ret << level_hash
            else # should be array
                level_hash.each do |x|
                    ret << x
                end
            end
        elsif params[:pull_out_single]
            # returns a single object
            ret = AwsResponseObjectHash.new(parsed[:response_metadata])
            level_hash = parsed
            params[:pull_out_single].each do |x|
                level_hash = level_hash[x]
            end
            ret.merge!(level_hash)
        else
            ret = parsed
        end
        return ret

    else
        @error_handler = AWSErrorHandler.new(self, nil, :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
        request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}"
        raise AwsError2.new(@last_response.code, @last_request_id, request_text_data, @last_response.body)
    end

end

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



353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/awsbase/right_awsbase.rb', line 353

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

#symbolize(hash, force_array) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/awsbase/right_awsbase.rb', line 497

def symbolize(hash, force_array)
    ret = {}
    hash.keys.each do |key|
        val = hash[key]
        if val.is_a? Hash
            val = symbolize(val, force_array)
            if force_array.include? key
                val = [val]
            end
        elsif val.is_a? Array
            val = val.collect { |x| symbolize(x, force_array) }
        end
        ret[key.underscore.to_sym] = val
    end
    ret
end

#update_cache(function, hash) ⇒ Object



553
554
555
# File 'lib/awsbase/right_awsbase.rb', line 553

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