Class: Aws::Resources::Resource

Inherits:
Object
  • Object
show all
Extended by:
OperationMethods
Defined in:
lib/aws-sdk-resources/resource.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OperationMethods

add_batch_operation, add_operation, batch_operation, batch_operation_names, batch_operations, inherited, operation, operation_names, operations

Constructor Details

#initialize(*identifiers, options = {}) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • options (Hash) (defaults to: {})

    Options except ‘:data` and identifier options are used to construct a Client unless `:client` is given.

Options Hash (options):

  • :client (Client)


11
12
13
14
15
16
# File 'lib/aws-sdk-resources/resource.rb', line 11

def initialize(*args)
  options = args.last.is_a?(Hash) ? args.pop.dup : {}
  @identifiers = extract_identifiers(args, options)
  @data = options.delete(:data)
  @client = extract_client(options)
end

Class Attribute Details

.client_classClass<Client>?

Returns When constructing a resource, the client will default to an instance of the this class.

Returns:

  • (Class<Client>, nil)

    When constructing a resource, the client will default to an instance of the this class.



197
198
199
# File 'lib/aws-sdk-resources/resource.rb', line 197

def client_class
  @client_class
end

.load_operationOperations::LoadOperation?

Returns:



200
201
202
# File 'lib/aws-sdk-resources/resource.rb', line 200

def load_operation
  @load_operation
end

.resource_nameString?

Returns The resource name.

Returns:

  • (String, nil)

    The resource name.



192
193
194
# File 'lib/aws-sdk-resources/resource.rb', line 192

def resource_name
  @resource_name
end

Instance Attribute Details

#clientClient (readonly)

Marked private to prevent double documentation

Returns:

  • (Client)


20
21
22
# File 'lib/aws-sdk-resources/resource.rb', line 20

def client
  @client
end

#identifiersHash<Symbol,String> (readonly)

Marked private to prevent double documentation

Returns:

  • (Hash<Symbol,String>)


24
25
26
# File 'lib/aws-sdk-resources/resource.rb', line 24

def identifiers
  @identifiers
end

Class Method Details

.add_data_attribute(name) ⇒ void

This method returns an undefined value.

Registers a data attribute. This defines a simple getter for the attribute which will access #data, loading the resource if necessary.

Parameters:

  • name (Symbol)


222
223
224
225
# File 'lib/aws-sdk-resources/resource.rb', line 222

def add_data_attribute(name)
  safe_define_method(name) { data[name] }
  @data_attributes << name
end

.add_identifier(name) ⇒ void

This method returns an undefined value.

Parameters:

  • name (Symbol)


211
212
213
214
215
# File 'lib/aws-sdk-resources/resource.rb', line 211

def add_identifier(name)
  name = name.to_sym
  safe_define_method(name) { @identifiers[name] }
  @identifiers << name
end

.data_attributesArray<Symbol>

Returns an array of symbolized data attribute names.

Returns:

  • (Array<Symbol>)

    Returns an array of symbolized data attribute names.



229
230
231
# File 'lib/aws-sdk-resources/resource.rb', line 229

def data_attributes
  @data_attributes.dup
end

.identifiersArray<Symbol>

Returns:

  • (Array<Symbol>)

See Also:



205
206
207
# File 'lib/aws-sdk-resources/resource.rb', line 205

def identifiers
  @identifiers.dup
end

.inherited(subclass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



234
235
236
237
238
# File 'lib/aws-sdk-resources/resource.rb', line 234

def inherited(subclass)
  subclass.send(:instance_variable_set, "@identifiers", [])
  subclass.send(:instance_variable_set, "@data_attributes", [])
  super
end

Instance Method Details

#dataStruct

Returns:

  • (Struct)


119
120
121
122
# File 'lib/aws-sdk-resources/resource.rb', line 119

def data
  load unless @data
  @data
end

#data_loaded?Boolean

Returns ‘true` if #data has been loaded.

Returns:

  • (Boolean)

    Returns ‘true` if #data has been loaded.



125
126
127
# File 'lib/aws-sdk-resources/resource.rb', line 125

def data_loaded?
  !@data.nil?
end

#exists?(options = {}) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
139
140
# File 'lib/aws-sdk-resources/resource.rb', line 130

def exists?(options = {})
  wait_until_exists(options) { |w| w.max_attempts = 1 }
  true
rescue Waiters::Errors::UnexpectedError => e
  raise e.error
rescue Waiters::Errors::WaiterFailed
  false
rescue NoMethodError
  msg = "#exists? is not implemented for #{self.class.name}"
  raise NotImplementedError, msg
end

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



156
157
158
159
160
161
# File 'lib/aws-sdk-resources/resource.rb', line 156

def inspect
  identifiers = self.identifiers.map do |name, value|
    "#{name}=#{value.inspect}"
  end.join(', ')
  "#<#{[self.class.name, identifiers].join(' ').strip}>"
end

#loadself Also known as: reload

Note:

Calling this method will send a request to AWS.

Loads data for this resource.

Returns:

  • (self)


145
146
147
148
149
150
151
152
# File 'lib/aws-sdk-resources/resource.rb', line 145

def load
  if load_operation = self.class.load_operation
    @data = load_operation.call(resource:self, client:client)
    self
  else
    raise NotImplementedError, "#load not defined for #{self.class.name}"
  end
end

#wait_until(options = {}) {|resource| ... } ⇒ Resource

Note:

The waiting operation is performed on a copy. The original resource remains unchanged

Waiter polls an API operation until a resource enters a desired state.

## Basic Usage

Waiter will polls until it is successful, it fails by entering a terminal state, or until a maximum number of attempts are made.

# polls in a loop until condition is true
resource.wait_until(options) {|resource| condition}

## Example

instance.wait_until(max_attempts:10, delay:5) {|instance| instance.state.name == 'running' }

## Configuration

You can configure the maximum number of polling attempts, and the delay (in seconds) between each polling attempt. The waiting condition is set by passing a block to #wait_until:

# poll for ~25 seconds
resource.wait_until(max_attempts:5,delay:5) {|resource|...}

## Callbacks

You can be notified before each polling attempt and before each delay. If you throw ‘:success` or `:failure` from these callbacks, it will terminate the waiter.

started_at = Time.now
# poll for 1 hour, instead of a number of attempts
proc = Proc.new do |attempts, response|
  throw :failure if Time.now - started_at > 3600
end

  # disable max attempts
instance.wait_until(before_wait:proc, max_attempts:nil) {...}

## Handling Errors

When a waiter is successful, it returns the Resource. When a waiter fails, it raises an error.

begin
  resource.wait_until(...)
rescue Aws::Waiters::Errors::WaiterFailed
  # resource did not enter the desired state in time
end

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :max_attempts (Integer) — default: 10

    Maximum number of attempts

  • :delay (Integer) — default: 10

    Delay between each attempt in seconds

  • :before_attempt (Proc) — default: nil

    Callback invoked before each attempt

  • :before_wait (Proc) — default: nil

    Callback invoked before each wait

Yield Parameters:

  • resource (Resource)

    to be used in the waiting condition

Returns:

  • (Resource)

    if the waiter was successful

Raises:

  • (Aws::Waiters::Errors::FailureStateError)

    Raised when the waiter terminates because the waiter has entered a state that it will not transition out of, preventing success.

  • (Aws::Waiters::Errors::TooManyAttemptsError)

    Raised when the configured maximum number of attempts have been made, and the waiter is not yet successful.

  • (Aws::Waiters::Errors::UnexpectedError)

    Raised when an error is encountered while polling for a resource that is not expected.

  • (NotImplementedError)

    Raised when the resource does not support #reload operation



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/aws-sdk-resources/resource.rb', line 101

def wait_until(options = {}, &block)
  resource_copy = self.dup
  attempts = 0
  options[:max_attempts] = 10 unless options.key?(:max_attempts)
  options[:delay] ||= 10
  options[:poller] = Proc.new do
    attempts += 1
    if block.call(resource_copy)
      [:success, resource_copy]
    else
      resource_copy.reload unless attempts == options[:max_attempts]
      :retry
    end
  end
  Waiters::Waiter.new(options).wait({})
end