Class: AWS::EC2::SnapshotCollection

Inherits:
Collection show all
Includes:
TaggedCollection
Defined in:
lib/aws/ec2/snapshot_collection.rb

Overview

Represents a collection of Amazon EBS snapshots. Typically you should get an instance of this class by calling #snapshots.

Examples:

Create a snapshot from a volume

ec2.snapshots.create(:volume => ec2.volumes["vol-123"],
                     :description => "my snapshot")
# or:
ec2.volumes["vol-123"].create_snapshot("my snapshot")

Get a snapshot by ID

snapshot = ec2.snapshots["vol-123"]
snapshot.exists?

Get a map of snapshot IDs to snapshot status

ec2.snapshots.inject({}) { |m, s| m[i.id] = s.status; m }
# => { "snap-12345678" => :pending, "snap-87654321" => :completed }

Instance Attribute Summary

Attributes included from Core::Model

#config

Instance Method Summary collapse

Methods included from TaggedCollection

#tagged, #tagged_values

Methods inherited from Collection

#[]

Methods included from FilteredCollection

#filter, #filtered_request

Methods included from Core::Model

#client, #config_prefix, #inspect

Constructor Details

#initialize(options = {}) ⇒ SnapshotCollection



39
40
41
42
43
# File 'lib/aws/ec2/snapshot_collection.rb', line 39

def initialize(options = {})
  @owners = options[:owners] || []
  @restorable_by = options[:restorable_by] || []
  super(options)
end

Instance Method Details

#create(opts = {}) ⇒ Snapshot

Creates a snapshot of an Amazon EBS volume and stores it in Amazon S3. You can use snapshots for backups, to make identical copies of instance devices, and to save data before shutting down an instance. For more information about Amazon EBS, go to the Amazon Elastic Compute Cloud User Guide.

Options Hash (opts):

  • :volume (Volume)

    The Amazon EBS volume of which to take a snapshot.

  • :volume_id (String)

    The ID of the Amazon EBS volume of which to take a snapshot.

  • :description (String)

    An optional description of the snapshot. May contain up to 255 characters.



108
109
110
111
112
113
114
# File 'lib/aws/ec2/snapshot_collection.rb', line 108

def create opts = {}
  if volume = opts.delete(:volume)
    opts[:volume_id] = volume.id
  end
  resp = client.create_snapshot(opts)
  Snapshot.new(resp.snapshot_id, :config => config)
end

#each {|Snapshot| ... } ⇒ nil

Yields:

  • (Snapshot)

    Yields each snapshot in the collection.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aws/ec2/snapshot_collection.rb', line 47

def each(&block)
  opts = {}
  opts[:owner_ids] = @owners.map { |id| id.to_s } unless @owners.empty?
  opts[:restorable_by_user_ids] = @restorable_by.map { |id| id.to_s } unless
    @restorable_by.empty?
  resp = filtered_request(:describe_snapshots, opts)
  resp.snapshot_set.each do |v|
    snapshot = Snapshot.new(v.snapshot_id, :config => config)
    yield(snapshot)
  end
  nil
end

#restorable_by(*users) ⇒ ImageCollection



81
82
83
# File 'lib/aws/ec2/snapshot_collection.rb', line 81

def restorable_by(*users)
  collection_with(:restorable_by => @restorable_by + users)
end

#with_owner(*owners) ⇒ SnapshotCollection



68
69
70
# File 'lib/aws/ec2/snapshot_collection.rb', line 68

def with_owner(*owners)
  collection_with(:owners => @owners + owners)
end