Module: RightAws::RightAwsBaseInterface

Included in:
Ec2, SqsGen2Interface
Defined in:
lib/awsbase/right_awsbase.rb

Constant Summary collapse

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



107
108
109
# File 'lib/awsbase/right_awsbase.rb', line 107

def aws_access_key_id
  @aws_access_key_id
end

#cacheObject (readonly)

Cache



123
124
125
# File 'lib/awsbase/right_awsbase.rb', line 123

def cache
  @cache
end

#connectionObject (readonly)

RightHttpConnection instance



121
122
123
# File 'lib/awsbase/right_awsbase.rb', line 121

def connection
  @connection
end

#last_errorsObject

Last AWS errors list (used by AWSErrorHandler)



113
114
115
# File 'lib/awsbase/right_awsbase.rb', line 113

def last_errors
  @last_errors
end

#last_requestObject (readonly)

Last HTTP request object



109
110
111
# File 'lib/awsbase/right_awsbase.rb', line 109

def last_request
  @last_request
end

#last_request_idObject

Last AWS request id (used by AWSErrorHandler)



115
116
117
# File 'lib/awsbase/right_awsbase.rb', line 115

def last_request_id
  @last_request_id
end

#last_responseObject (readonly)

Last HTTP response object



111
112
113
# File 'lib/awsbase/right_awsbase.rb', line 111

def last_response
  @last_response
end

#loggerObject

Logger object



117
118
119
# File 'lib/awsbase/right_awsbase.rb', line 117

def logger
  @logger
end

#paramsObject

Initial params hash



119
120
121
# File 'lib/awsbase/right_awsbase.rb', line 119

def params
  @params
end

#signature_versionObject (readonly)

Signature version (all services except s3)



125
126
127
# File 'lib/awsbase/right_awsbase.rb', line 125

def signature_version
  @signature_version
end

Class Method Details

.cachingObject



99
100
101
# File 'lib/awsbase/right_awsbase.rb', line 99

def self.caching
  @@caching
end

.caching=(caching) ⇒ Object



102
103
104
# File 'lib/awsbase/right_awsbase.rb', line 102

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)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/awsbase/right_awsbase.rb', line 157

def cache_hits?(function, response, do_raise=:raise)
  result = false
  if caching?
    function     = function.to_sym
    response_md5 = MD5.md5(response).to_s
    # well, the response is new, reset cache data
    unless @cache[function] && @cache[function][:response_md5] == response_md5
      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)


148
149
150
# File 'lib/awsbase/right_awsbase.rb', line 148

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

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

:nodoc:

Raises:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/awsbase/right_awsbase.rb', line 127

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
  @params[:server]       ||= service_info[:default_host]
  @params[:port]         ||= service_info[:default_port]
  @params[:service]      ||= service_info[:default_service]
  @params[:protocol]     ||= service_info[:default_protocol]
  @params[:multi_thread] ||= defined?(AWS_DAEMON)
  @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[:multi_thread] ? 'multi' : 'single'}-threaded 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.



193
194
195
# File 'lib/awsbase/right_awsbase.rb', line 193

def multi_thread
  @params[:multi_thread]
end

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

:nodoc:



187
188
189
190
# File 'lib/awsbase/right_awsbase.rb', line 187

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

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

:nodoc:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/awsbase/right_awsbase.rb', line 197

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
            raise AwsError.new(@last_errors, @last_response.code, @last_request_id)
          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
      raise AwsError.new(@last_errors, @last_response.code, @last_request_id)
    end
  end
rescue
  @error_handler = nil
  raise
end

#update_cache(function, hash) ⇒ Object



183
184
185
# File 'lib/awsbase/right_awsbase.rb', line 183

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