diff --git a/lib/fog/aws/rds.rb b/lib/fog/aws/rds.rb index c53a5a6f2..29024c0e8 100644 --- a/lib/fog/aws/rds.rb +++ b/lib/fog/aws/rds.rb @@ -65,7 +65,8 @@ module Fog hash[region] = Hash.new do |region_hash, key| region_hash[key] = { :servers => {}, - :security_groups => {} + :security_groups => {}, + :snapshots => {} } end end diff --git a/lib/fog/aws/requests/rds/create_db_snapshot.rb b/lib/fog/aws/requests/rds/create_db_snapshot.rb index 24a2574db..427d27ea5 100644 --- a/lib/fog/aws/requests/rds/create_db_snapshot.rb +++ b/lib/fog/aws/requests/rds/create_db_snapshot.rb @@ -27,9 +27,45 @@ module Fog class Mock def create_db_snapshot(identifier, name) - Fog::Mock.not_implemented - end + response = Excon::Response.new + if data[:snapshots][name] + raise Fog::AWS::RDS::IndentifierTaken.new + end + server_data = data[:servers][identifier] + unless server_data + raise Fog::AWS::RDS::NotFound.new("DBInstance #{identifier} not found") + end + + # TODO: raise an error if the server isn't in 'available' state + + snapshot_data = { + 'Status' => 'creating', + #'SnapshotType' => 'manual', # In a newer RDS version + 'DBInstanceIdentifier' => identifier, + 'DBSnapshotIdentifier' => name, + 'InstanceCreateTime' => Time.now + } + # Copy attributes from server + %w(Engine EngineVersion AvailabilityZone AllocatedStorage MasterUsername InstanceCreateTime).each do |key| + snapshot_data[key] = server_data[key] + end + snapshot_data['Port'] = server_data['Endpoint']['Port'] + + self.data[:snapshots][name] = snapshot_data + + # TODO: put the server in 'modifying' state + + response.body = { + "ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id }, + "CreateDBSnapshotResult"=> {"DBSnapshot"=> snapshot_data.dup} + } + response.status = 200 + # SnapshotCreateTime is not part of the response. + self.data[:snapshots][name]['SnapshotCreateTime'] = Time.now + response + + end end end end diff --git a/lib/fog/aws/requests/rds/delete_db_instance.rb b/lib/fog/aws/requests/rds/delete_db_instance.rb index 170181f72..e9af95fba 100644 --- a/lib/fog/aws/requests/rds/delete_db_instance.rb +++ b/lib/fog/aws/requests/rds/delete_db_instance.rb @@ -34,8 +34,7 @@ module Fog response = Excon::Response.new unless skip_snapshot - # I don't know how to mock snapshot_identifier - Fog::Logger.warning("snapshot_identifier is not mocked [light_black](#{caller.first})[/]") + create_db_snapshot(identifier, snapshot_identifier) end if server_set = self.data[:servers].delete(identifier) diff --git a/lib/fog/aws/requests/rds/delete_db_snapshot.rb b/lib/fog/aws/requests/rds/delete_db_snapshot.rb index ea42c35ce..a08cfcc6e 100644 --- a/lib/fog/aws/requests/rds/delete_db_snapshot.rb +++ b/lib/fog/aws/requests/rds/delete_db_snapshot.rb @@ -12,11 +12,11 @@ module Fog # ==== Returns # * response<~Excon::Response>: # * body<~Hash>: - def delete_db_snapshot(group_name) + def delete_db_snapshot(name) request({ 'Action' => 'DeleteDBSnapshot', - 'DBSnapshotIdentifier' => group_name, + 'DBSnapshotIdentifier' => name, :parser => Fog::Parsers::AWS::RDS::DeleteDBSnapshot.new }) @@ -26,8 +26,19 @@ module Fog class Mock - def delete_db_snapshot(group_name) - Fog::Mock.not_implemented + def delete_db_snapshot(name) + # TODO: raise error if snapshot isn't 'available' + response = Excon::Response.new + snapshot_data = self.data[:snapshots].delete(name) + + raise Fog::AWS::RDS::NotFound.new("DBSnapshtoNotFound => #{name} not found") unless snapshot_data + + response.status = 200 + response.body = { + "ResponseMetadata"=> { "RequestId"=> Fog::AWS::Mock.request_id }, + "DeleteDBSnapshotResult"=> {"DBSnapshot"=> snapshot_data} + } + response end end diff --git a/lib/fog/aws/requests/rds/describe_db_snapshots.rb b/lib/fog/aws/requests/rds/describe_db_snapshots.rb index 473e076a1..389ab5d3f 100644 --- a/lib/fog/aws/requests/rds/describe_db_snapshots.rb +++ b/lib/fog/aws/requests/rds/describe_db_snapshots.rb @@ -33,7 +33,33 @@ module Fog class Mock def describe_db_snapshots(opts={}) - Fog::Mock.not_implemented + response = Excon::Response.new + snapshots = self.data[:snapshots].values + if opts[:identifier] + snapshots.select!{|snapshot| snapshot['DBInstanceIdentifier'] == opts[:identifier]} + end + + if opts[:snapshot_id] + snapshots.select!{|snapshot| snapshot['DBSnapshotIdentifier'] == opts[:snapshot_id]} + end + + snapshots.each do |snapshot| + case snapshot['Status'] + when 'creating' + if Time.now - snapshot['SnapshotCreateTime'] > Fog::Mock.delay + snapshot['Status'] = 'available' + end + end + end + + # Build response + response.status = 200 + response.body = { + "ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id }, + "DescribeDBSnapshotsResult" => { "DBSnapshots" => snapshots } + } + response + end end diff --git a/tests/aws/requests/rds/instance_tests.rb b/tests/aws/requests/rds/instance_tests.rb index 3f18cc0d2..8b6a66c39 100644 --- a/tests/aws/requests/rds/instance_tests.rb +++ b/tests/aws/requests/rds/instance_tests.rb @@ -58,8 +58,6 @@ Shindo.tests('AWS::RDS | instance requests', ['aws', 'rds']) do server.reload.wait_for { state == 'rebooting' } server.reload.wait_for { state == 'available'} - pending if Fog.mocking? - tests("#create_db_snapshot").formats(AWS::RDS::Formats::CREATE_DB_SNAPSHOT) do body = Fog::AWS[:rds].create_db_snapshot(@db_instance_id, @db_snapshot_id).body returns('creating'){ body['CreateDBSnapshotResult']['DBSnapshot']['Status']} @@ -72,6 +70,8 @@ Shindo.tests('AWS::RDS | instance requests', ['aws', 'rds']) do server.reload.wait_for { state == 'available' } + pending if Fog.mocking? + tests( "#create read replica").formats(AWS::RDS::Formats::CREATE_READ_REPLICA) do Fog::AWS[:rds].create_db_instance_read_replica(@db_replica_id, @db_instance_id).body end @@ -114,8 +114,6 @@ Shindo.tests('AWS::RDS | instance requests', ['aws', 'rds']) do end tests('failure') do - pending if Fog.mocking? - tests "deleting nonexisting instance" do raises(Fog::AWS::RDS::NotFound) {Fog::AWS[:rds].delete_db_instance('doesnexist', 'irrelevant')} end