You are viewing documentation for version 2 of the AWS SDK for Ruby. Version 3 documentation can be found here.

Class: Aws::Resources::Resource

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

Direct Known Subclasses

AutoScaling::Activity, AutoScaling::AutoScalingGroup, AutoScaling::Instance, AutoScaling::LaunchConfiguration, AutoScaling::LifecycleHook, AutoScaling::LoadBalancer, AutoScaling::NotificationConfiguration, AutoScaling::Resource, AutoScaling::ScalingPolicy, AutoScaling::ScheduledAction, AutoScaling::Tag, CloudFormation::Event, CloudFormation::Resource, CloudFormation::Stack, CloudFormation::StackResource, CloudFormation::StackResourceSummary, CloudWatch::Alarm, CloudWatch::Metric, CloudWatch::Resource, DynamoDB::Resource, DynamoDB::Table, EC2::ClassicAddress, EC2::DhcpOptions, EC2::Image, EC2::Instance, EC2::InternetGateway, EC2::KeyPair, EC2::KeyPairInfo, EC2::NetworkAcl, EC2::NetworkInterface, EC2::NetworkInterfaceAssociation, EC2::PlacementGroup, EC2::Resource, EC2::Route, EC2::RouteTable, EC2::RouteTableAssociation, EC2::SecurityGroup, EC2::Snapshot, EC2::Subnet, EC2::Tag, EC2::Volume, EC2::Vpc, EC2::VpcAddress, EC2::VpcPeeringConnection, Glacier::Account, Glacier::Archive, Glacier::Job, Glacier::MultipartUpload, Glacier::Notification, Glacier::Resource, Glacier::Vault, IAM::AccessKey, IAM::AccessKeyPair, IAM::AccountPasswordPolicy, IAM::AccountSummary, IAM::AssumeRolePolicy, IAM::CurrentUser, IAM::Group, IAM::GroupPolicy, IAM::InstanceProfile, IAM::LoginProfile, IAM::MfaDevice, IAM::Policy, IAM::PolicyVersion, IAM::Resource, IAM::Role, IAM::RolePolicy, IAM::SamlProvider, IAM::ServerCertificate, IAM::SigningCertificate, IAM::User, IAM::UserPolicy, IAM::VirtualMfaDevice, OpsWorks::Layer, OpsWorks::Resource, OpsWorks::Stack, OpsWorks::StackSummary, Aws::RDS::AccountQuota, Aws::RDS::Certificate, Aws::RDS::DBCluster, Aws::RDS::DBClusterParameterGroup, Aws::RDS::DBClusterSnapshot, Aws::RDS::DBEngine, Aws::RDS::DBEngineVersion, Aws::RDS::DBInstance, Aws::RDS::DBLogFile, Aws::RDS::DBParameterGroup, Aws::RDS::DBParameterGroupFamily, Aws::RDS::DBSecurityGroup, Aws::RDS::DBSnapshot, Aws::RDS::DBSnapshotAttribute, Aws::RDS::DBSubnetGroup, Aws::RDS::Event, Aws::RDS::EventCategoryMap, Aws::RDS::EventSubscription, Aws::RDS::OptionGroup, Aws::RDS::OptionGroupOption, Aws::RDS::Parameter, Aws::RDS::PendingMaintenanceAction, Aws::RDS::ReservedDBInstance, Aws::RDS::ReservedDBInstancesOffering, Aws::RDS::Resource, Aws::RDS::ResourcePendingMaintenanceActionList, S3::Bucket, S3::BucketAcl, S3::BucketCors, S3::BucketLifecycle, S3::BucketLifecycleConfiguration, S3::BucketLogging, S3::BucketNotification, S3::BucketPolicy, S3::BucketRequestPayment, S3::BucketTagging, S3::BucketVersioning, S3::BucketWebsite, S3::MultipartUpload, S3::MultipartUploadPart, S3::Object, S3::ObjectAcl, S3::ObjectSummary, S3::ObjectVersion, S3::Resource, SNS::PlatformApplication, SNS::PlatformEndpoint, SNS::Resource, SNS::Subscription, SNS::Topic, SQS::Message, SQS::Queue, SQS::Resource

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, 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):



11
12
13
14
15
16
# File 'aws-sdk-resources/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 'aws-sdk-resources/lib/aws-sdk-resources/resource.rb', line 197

def client_class
  @client_class
end

.load_operationOperations::LoadOperation?

Returns:



200
201
202
# File 'aws-sdk-resources/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 'aws-sdk-resources/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:



20
21
22
# File 'aws-sdk-resources/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 'aws-sdk-resources/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 'aws-sdk-resources/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 'aws-sdk-resources/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 'aws-sdk-resources/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 'aws-sdk-resources/lib/aws-sdk-resources/resource.rb', line 205

def identifiers
  @identifiers.dup
end

Instance Method Details

#dataStruct

Returns:

  • (Struct)


119
120
121
122
# File 'aws-sdk-resources/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 'aws-sdk-resources/lib/aws-sdk-resources/resource.rb', line 125

def data_loaded?
  !@data.nil?
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 'aws-sdk-resources/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:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'aws-sdk-resources/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