11module Cucloud
22 # RdsUtils class - for interacting with the AWS relational database service
33 class RdsUtils
4+ # RDSInstanceAlreadyExist Class - capture erros when creating or restoring
5+ # an RDS instance which already exist
6+ class RDSInstanceAlreadyExist < StandardError
7+ end
8+
49 def initialize ( rds_client = Aws ::RDS ::Client . new )
510 @rds = rds_client
611 end
@@ -13,6 +18,69 @@ def get_instance(db_instance_identifier)
1318 resource . db_instance ( db_instance_identifier )
1419 end
1520
21+ # Determine if a givne db instance exist
22+ # @param db_instance_identifier [String] RDS instance identifier
23+ # @return [boolean]
24+ def does_db_exist? ( db_instance_identifier )
25+ get_instance ( db_instance_identifier ) . instance_create_time
26+ true
27+ rescue Aws ::RDS ::Errors ::DBInstanceNotFound
28+ false
29+ end
30+
31+ # Delete a givne db instance
32+ # @param db_instance_identifier [String] RDS instance identifier
33+ # @param db_snapshot_identifier [String] Name for final snapshot, default is nil
34+ def delete_db_instance ( db_instance_identifier , db_snapshot_identifier = nil )
35+ if does_db_exist? ( db_instance_identifier )
36+ if db_snapshot_identifier . nil?
37+ @rds . delete_db_instance ( db_instance_identifier : db_instance_identifier , skip_final_snapshot : true )
38+ else
39+ @rds . delete_db_instance ( db_instance_identifier : db_instance_identifier ,
40+ final_db_snapshot_identifier : db_snapshot_identifier )
41+ end
42+
43+ @rds . wait_until ( :db_instance_deleted , db_instance_identifier : db_instance_identifier )
44+ else
45+ raise Aws ::RDS ::Errors ::DBInstanceNotFound . new ( db_instance_identifier , '' )
46+ end
47+ end
48+
49+ # Restore DB from a snapshot
50+ # @param db_instance_identifier [String] RDS instance identifier
51+ # @param db_snapshot_identifier [String] Name for final snapshot, default is nil
52+ def restore_db ( db_instance_identifier , restore_from , options = { } )
53+ raise RDSInstanceAlreadyExist if does_db_exist? ( db_instance_identifier )
54+
55+ db_snapshot_identifier =
56+ options [ :db_snapshot_identifier ] . nil? ? find_latest_snapshot ( restore_from ) : options [ :db_snapshot_identifier ]
57+ options [ :db_instance_identifier ] = db_instance_identifier
58+ options [ :db_snapshot_identifier ] = db_snapshot_identifier
59+ @rds . restore_db_instance_from_db_snapshot ( options )
60+ end
61+
62+ # Delete a givne db instance
63+ # @param db_instance_identifier [String] RDS instance identifier
64+ # @return [String] Most recent snapshot ID for given RDS instance
65+ def find_latest_snapshot ( db_instance_identifier , snapshot_type = 'manual' )
66+ latest_snapshot_time = Time . new ( 2002 )
67+ latest_snap_shot = nil
68+ snapshots_info = @rds . describe_db_snapshots (
69+ db_instance_identifier : db_instance_identifier , snapshot_type : snapshot_type
70+ ) [ :db_snapshots ]
71+
72+ snapshots_info . each do |snapshot_info |
73+ next if snapshot_info [ :status ] != 'available'
74+
75+ if latest_snapshot_time . to_i < snapshot_info [ :snapshot_create_time ] . to_i
76+ latest_snapshot_time = snapshot_info [ :snapshot_create_time ] . to_i
77+ latest_snap_shot = snapshot_info
78+ end
79+ end
80+
81+ latest_snap_shot . nil? ? nil : latest_snap_shot [ :db_snapshot_identifier ]
82+ end
83+
1684 # Begins the creation of a snapshot of the given RDS instance.
1785 # This is a non-blocking call so it will return before the snapshot
1886 # is created and available.
0 commit comments