Skip to content

Commit

Permalink
Merge pull request #158 from engineyard/rds-promote-modifying
Browse files Browse the repository at this point in the history
update #promote_read_replica mock
  • Loading branch information
geemus committed Aug 3, 2015
2 parents cfc0b87 + e0575cb commit 2ca24dc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
7 changes: 6 additions & 1 deletion lib/fog/aws/models/rds/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ def remove_tags(tag_keys)

def promote_read_replica
requires :id
service.promote_read_replica(id)

data = service.promote_read_replica(id).body["PromoteReadReplicaResult"]["DBInstance"]

merge_attributes(data)
end

alias promote promote_read_replica

def modify(immediately, options)
options[:security_group_names] ||= options['DBSecurityGroups']
params = self.class.new(options).attributes_to_params
Expand Down
2 changes: 1 addition & 1 deletion lib/fog/aws/requests/rds/describe_db_instances.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def describe_db_instances(identifier=nil, opts={})

response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
"ResponseMetadata" => { "RequestId" => Fog::AWS::Mock.request_id },
"DescribeDBInstancesResult" => { "DBInstances" => server_set }
}
response
Expand Down
5 changes: 3 additions & 2 deletions lib/fog/aws/requests/rds/describe_db_snapshots.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ def describe_db_snapshots(opts={})
response = Excon::Response.new
snapshots = self.data[:snapshots].values
if opts[:identifier]
snapshots = snapshots.select{|snapshot| snapshot['DBInstanceIdentifier'] == opts[:identifier]}
snapshots = snapshots.select { |snapshot| snapshot['DBInstanceIdentifier'] == opts[:identifier] }

end

if opts[:snapshot_id]
snapshots = snapshots.select{|snapshot| snapshot['DBSnapshotIdentifier'] == opts[:snapshot_id]}
snapshots = snapshots.select { |snapshot| snapshot['DBSnapshotIdentifier'] == opts[:snapshot_id] }
raise Fog::AWS::RDS::NotFound.new("DBSnapshot #{opts[:snapshot_id]} not found") if snapshots.empty?
end

Expand Down
45 changes: 27 additions & 18 deletions lib/fog/aws/requests/rds/promote_read_replica.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,43 @@ def promote_read_replica(identifier, backup_retention_period = nil, preferred_ba
params['BackupRetentionPeriod'] = backup_retention_period if backup_retention_period
params['PreferredBackupWindow'] = preferred_backup_window if preferred_backup_window
request({
'Action' => 'PromoteReadReplica',
'Action' => 'PromoteReadReplica',
'DBInstanceIdentifier' => identifier,
:parser => Fog::Parsers::AWS::RDS::PromoteReadReplica.new
:parser => Fog::Parsers::AWS::RDS::PromoteReadReplica.new
}.merge(params))
end
end

class Mock
def promote_read_replica(identifier, backup_retention_period = nil, preferred_backup_window = nil)
if self.data[:servers][identifier]
data = {
'BackupRetentionPeriod' => backup_retention_period || 1,
'PreferredBackupWindow' => preferred_backup_window || '08:00-08:30',
'DBInstanceIdentifier' => identifier,
}
server = self.data[:servers][identifier]
server || raise(Fog::AWS::RDS::NotFound.new("DBInstance #{identifier} not found"))

if server["ReadReplicaSourceDBInstanceIdentifier"].nil?
raise(Fog::AWS::RDS::Error.new("InvalidDBInstanceState => DB Instance is not a read replica."))
end

db_instance = self.data[:servers][identifier].merge(data)
self.data[:modify_time] = Time.now

response = Excon::Response.new
response.body = {
"ResponseMetadata" => { "RequestId" => Fog::AWS::Mock.request_id },
"PromoteReadReplicaResult" => { "DBInstance" => db_instance }
data = {
'BackupRetentionPeriod' => backup_retention_period || 1,
'PreferredBackupWindow' => preferred_backup_window || '08:00-08:30',
'DBInstanceIdentifier' => identifier,
'DBInstanceStatus' => "modifying",
'PendingModifiedValues' => {
'ReadReplicaSourceDBInstanceIdentifier' => nil,
}
response.status = 200
response
else
raise Fog::AWS::RDS::NotFound.new("DBInstance #{identifier} not found")
end
}

server.merge!(data)

response = Excon::Response.new
response.body = {
"ResponseMetadata" => { "RequestId" => Fog::AWS::Mock.request_id },
"PromoteReadReplicaResult" => { "DBInstance" => server }
}
response.status = 200
response
end
end
end
Expand Down
15 changes: 13 additions & 2 deletions tests/models/rds/server_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@
@instance.wait_for { state == 'rebooting' }
@instance.wait_for { ready? }

tests('#create_read_replica').succeeds do
replica = nil

tests('#create_read_replica').succeeds do
replica = @instance_with_final_snapshot.create_read_replica(uniq_id('fog-replica'))
@instance_with_final_snapshot.reload
returns([replica.id]) { @instance_with_final_snapshot.read_replica_identifiers }
Expand All @@ -98,10 +99,20 @@

# FinalDBSnapshotIdentifier can not be specified when deleting a replica instance
raises(Fog::AWS::RDS::Error) { replica.destroy("foobar") }
end

replica.destroy
tests('#promote_read_replica').succeeds do
replica.promote.wait_for { state != "modifying" }

replica.read_replica_source == nil
end

tests('#promote_read_replica', 'master').raises(Fog::AWS::RDS::Error) {
@instance_with_final_snapshot.promote
}

replica && replica.destroy

test("Destroying with a final snapshot") do
final_snapshot_id = uniq_id('fog-test-snapshot')

Expand Down

0 comments on commit 2ca24dc

Please sign in to comment.