Class: Aws::Resources::Resource
- Inherits:
-
Object
- Object
- Aws::Resources::Resource
- Extended by:
- OperationMethods
- Defined in:
- lib/aws-sdk-resources/resource.rb
Class Attribute Summary collapse
-
.client_class ⇒ Class<Client>?
When constructing a resource, the client will default to an instance of the this class.
- .load_operation ⇒ Operations::LoadOperation?
-
.resource_name ⇒ String?
The resource name.
Instance Attribute Summary collapse
-
#client ⇒ Client
readonly
Marked private to prevent double documentation.
-
#identifiers ⇒ Hash<Symbol,String>
readonly
Marked private to prevent double documentation.
Class Method Summary collapse
-
.add_data_attribute(name) ⇒ void
Registers a data attribute.
- .add_identifier(name) ⇒ void
-
.data_attributes ⇒ Array<Symbol>
Returns an array of symbolized data attribute names.
- .identifiers ⇒ Array<Symbol>
- .inherited(subclass) ⇒ Object private
Instance Method Summary collapse
- #data ⇒ Struct
-
#data_loaded? ⇒ Boolean
Returns ‘true` if #data has been loaded.
- #exists?(options = {}) ⇒ Boolean private
-
#initialize(*identifiers, options = {}) ⇒ Resource
constructor
A new instance of Resource.
- #inspect ⇒ Object private
-
#load ⇒ self
(also: #reload)
Loads data for this resource.
-
#wait_until(options = {}) {|resource| ... } ⇒ Resource
Waiter polls an API operation until a resource enters a desired state.
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.
11 12 13 14 15 16 |
# File 'lib/aws-sdk-resources/resource.rb', line 11 def initialize(*args) = args.last.is_a?(Hash) ? args.pop.dup : {} @identifiers = extract_identifiers(args, ) @data = .delete(:data) @client = extract_client() end |
Class Attribute Details
.client_class ⇒ Class<Client>?
Returns 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_operation ⇒ Operations::LoadOperation?
200 201 202 |
# File 'lib/aws-sdk-resources/resource.rb', line 200 def load_operation @load_operation end |
.resource_name ⇒ String?
Returns The resource name.
192 193 194 |
# File 'lib/aws-sdk-resources/resource.rb', line 192 def resource_name @resource_name end |
Instance Attribute Details
#client ⇒ Client (readonly)
Marked private to prevent double documentation
20 21 22 |
# File 'lib/aws-sdk-resources/resource.rb', line 20 def client @client end |
#identifiers ⇒ Hash<Symbol,String> (readonly)
Marked private to prevent double documentation
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.
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.
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_attributes ⇒ 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 |
.identifiers ⇒ Array<Symbol>
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
#data ⇒ 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.
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.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/aws-sdk-resources/resource.rb', line 130 def exists?( = {}) wait_until_exists() { |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 |
#inspect ⇒ 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.
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 |
#load ⇒ self Also known as: reload
Calling this method will send a request to AWS.
Loads data for this resource.
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
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() {|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
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( = {}, &block) resource_copy = self.dup attempts = 0 [:max_attempts] = 10 unless .key?(:max_attempts) [:delay] ||= 10 [:poller] = Proc.new do attempts += 1 if block.call(resource_copy) [:success, resource_copy] else resource_copy.reload unless attempts == [:max_attempts] :retry end end Waiters::Waiter.new().wait({}) end |