Class: AwsHelpers::RDS::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_helpers/rds/snapshot.rb

Instance Method Summary collapse

Constructor Details

#initialize(rds_client, iam_client, db_instance_id, use_name = false) ⇒ Snapshot

Returns a new instance of Snapshot


15
16
17
18
19
20
21
# File 'lib/aws_helpers/rds/snapshot.rb', line 15

def initialize(rds_client, iam_client, db_instance_id, use_name = false)
  @rds_client = rds_client
  @iam_client = iam_client
  @db_instance_id = db_instance_id
  @snapshot_id = nil
  @use_name = use_name
end

Instance Method Details

#createObject


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aws_helpers/rds/snapshot.rb', line 23

def create
  Instance.new(@rds_client, @db_instance_id).poll_available

  now = DateTime.now.strftime('%Y-%m-%d-%H-%M')
  name = tag_name
  snapshot_name = "#{name ? name : @db_instance_id}-#{now}"
  puts snapshot_name
  snapshot = @rds_client.create_db_snapshot(
    db_instance_identifier: @db_instance_id,
    db_snapshot_identifier: snapshot_name
  )
  @snapshot_id = snapshot[:db_snapshot][:db_snapshot_identifier]
  SnapshotProgress.report(self)
end

#delete(options = nil) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aws_helpers/rds/snapshot.rb', line 38

def delete(options = nil)
  delete_time = Time.subtract_period(Time.now, options)

  response = @rds_client.describe_db_snapshots(db_instance_identifier: @db_instance_id, snapshot_type:'manual')
  response[:db_snapshots].each { |snapshot|
    create_time = snapshot[:snapshot_create_time]
    if create_time < delete_time
      db_snapshot_identifier = snapshot[:db_snapshot_identifier]
      puts "Deleting #{db_snapshot_identifier}"
      @rds_client.delete_db_snapshot(db_snapshot_identifier: db_snapshot_identifier)
    end
  }
end

#describeObject


57
58
59
# File 'lib/aws_helpers/rds/snapshot.rb', line 57

def describe
  @rds_client.describe_db_snapshots(db_snapshot_identifier: @snapshot_id)[:db_snapshots].first if @snapshot_id
end

#latestObject


52
53
54
55
# File 'lib/aws_helpers/rds/snapshot.rb', line 52

def latest
  response = @rds_client.describe_db_snapshots(db_instance_identifier: @db_instance_id, snapshot_type:'manual')
  response[:db_snapshots].sort_by! { |snapshot| snapshot[:snapshot_create_time] }.last
end