Skip to content

Commit

Permalink
Merge pull request #269 from tekken/rds_snapshot_improvements
Browse files Browse the repository at this point in the history
Rds snapshot improvements
  • Loading branch information
geemus authored Jul 5, 2016
2 parents 95a296c + 6c0043b commit fbfba3d
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/fog/aws/parsers/rds/copy_db_snapshot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Fog
module Parsers
module AWS
module RDS
require 'fog/aws/parsers/rds/snapshot_parser'

class CopyDBSnapshot < Fog::Parsers::AWS::RDS::SnapshotParser
def reset
@response = { 'CopyDBSnapshotResult' => {}, 'ResponseMetadata' => {} }
super
end

def start_element(name, attrs = [])
super
end

def end_element(name)
case name
when 'DBSnapshot' then
@response['CopyDBSnapshotResult']['DBSnapshot'] = @db_snapshot
@db_snapshot = fresh_snapshot
when 'RequestId'
@response['ResponseMetadata'][name] = value
else
super
end
end
end
end
end
end
end
24 changes: 24 additions & 0 deletions lib/fog/aws/parsers/rds/modify_db_snapshot_attribute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Fog
module Parsers
module AWS
module RDS
class ModifyDbSnapshotAttribute < Fog::Parsers::Base
def reset
@response = { 'ModifyDbSnapshotAttributeResult' => {}, 'ResponseMetadata' => {} }
end

def start_element(name, attrs = [])
super
end

def end_element(name)
case name
when 'RequestId'
@response['ResponseMetadata'][name] = value
end
end
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/fog/aws/rds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class AuthorizationAlreadyExists < Fog::Errors::Error; end
request :describe_db_snapshots
request :create_db_snapshot
request :delete_db_snapshot
request :modify_db_snapshot_attribute
request :copy_db_snapshot

request :create_db_parameter_group
request :delete_db_parameter_group
Expand Down
53 changes: 53 additions & 0 deletions lib/fog/aws/requests/rds/copy_db_snapshot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Fog
module AWS
class RDS
class Real
require 'fog/aws/parsers/rds/copy_db_snapshot'

# Copy a db snapshot
#
# ==== Parameters
# * source_db_snapshot_identifier<~String> - Id of db snapshot
# * target_db_snapshot_identifier<~String> - Desired Id of the db snapshot copy
# * 'copy_tags'<~Boolean> - true to copy all tags from the source DB snapshot to the target DB snapshot; otherwise false.
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
#
# {Amazon API Reference}[http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CopyDBSnapshot.html]
def copy_db_snapshot(source_db_snapshot_identifier, target_db_snapshot_identifier, copy_tags = false)
request(
'Action' => 'CopyDBSnapshot',
'SourceDBSnapshotIdentifier' => source_db_snapshot_identifier,
'TargetDBSnapshotIdentifier' => target_db_snapshot_identifier,
'CopyTags' => copy_tags,
:parser => Fog::Parsers::AWS::RDS::CopyDBSnapshot.new
)
end
end

class Mock
#
# Usage
#
# Fog::AWS[:rds].copy_db_snapshot("snap-original-id", "snap-backup-id", true)
#

def copy_db_snapshot(source_db_snapshot_identifier, target_db_snapshot_identifier, copy_tags = false)
response = Excon::Response.new
response.status = 200
snapshot_id = Fog::AWS::Mock.snapshot_id
data = {
'snapshotId' => snapshot_id,
}
self.data[:snapshots][snapshot_id] = data
response.body = {
'requestId' => Fog::AWS::Mock.request_id
}.merge!(data)
response
end
end
end
end
end
48 changes: 48 additions & 0 deletions lib/fog/aws/requests/rds/modify_db_snapshot_attribute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Fog
module AWS
class RDS
class Real
require 'fog/aws/parsers/rds/modify_db_snapshot_attribute'

# Modify db snapshot attributes
#
# ==== Parameters
# * db_snapshot_identifier<~String> - Id of snapshot to modify
# * attributes<~Hash>:
# * 'Add.MemberId'<~Array> - One or more account ids to grant rds create permission to
# * 'Remove.MemberId'<~Array> - One or more account ids to revoke rds create permission from
#
# {Amazon API Reference}[http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_ModifyDBSnapshotAttribute.html]
#
def modify_db_snapshot_attribute(db_snapshot_identifier, attributes)
params = {}
params.merge!(Fog::AWS.indexed_param('ValuesToAdd.member.%d', attributes['Add.MemberId'] || []))
params.merge!(Fog::AWS.indexed_param('ValuesToRemove.member.%d', attributes['Remove.MemberId'] || []))
request({
'Action' => 'ModifyDBSnapshotAttribute',
'DBSnapshotIdentifier' => db_snapshot_identifier,
:idempotent => true,
'AttributeName' => "restore",
:parser => Fog::Parsers::AWS::RDS::ModifyDbSnapshotAttribute.new
}.merge!(params))
end
end
class Mock
#
# Usage
#
# Fog::AWS[:rds].modify_db_snapshot_attribute("snap-identifier", {"Add.MemberId"=>"389480430104"})
#

def modify_db_snapshot_attribute(db_snapshot_identifier, attributes)
response = Excon::Response.new
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id
}.merge!(data)
response
end
end
end
end
end
67 changes: 67 additions & 0 deletions tests/requests/rds/db_snapshot_tests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
Shindo.tests('Fog::Rds[:aws] | db snapshot requests', ['aws']) do

@snapshot_format = {
'AllocatedStorage' => Integer,
'AvailabilityZone' => Fog::Nullable::String,
'Engine' => String,
'EngineVersion' => String,
'InstanceCreateTime' => Time,
'DBInstanceIdentifier' => String,
'Iops' => Fog::Nullable::Integer,
'MasterUsername' => String,
'Port' => Fog::Nullable::Integer,
'Status' => String,
'StorageType' => String,
'SnapshotType' => String
}

@snapshots_format = {
'requestId' => String
}

@snapshot_copy_result = {
'requestId' => String,
'snapshotId' => String
}

@rds_identity = "test_rds"
response = Fog::AWS[:rds].create_db_instance(@rds_identity,{
"DBInstanceClass"=>"db.m3.xlarge",
"Engine"=>"PostgreSQL",
"AllocatedStorage"=>100,
"MasterUserPassword"=>"password",
"MasterUsername"=>"username"
})

@rds = Fog::AWS[:rds].servers.get(@rds_identity)

tests('success') do
@snapshot_id = "testRdsSnapshot"
tests("#create_snapshot(#{@rds.identity})").formats(@snapshot_format) do
Fog::AWS[:rds].create_db_snapshot(@rds.identity,@snapshot_id).body["CreateDBSnapshotResult"]["DBSnapshot"]
end

Fog.wait_for { Fog::AWS[:rds].snapshots.get(@snapshot_id) }
Fog::AWS[:rds].snapshots.get(@snapshot_id).wait_for { ready? }

tests("#modify_db_snapshot_attribute").formats(@snapshots_format) do
Fog::AWS[:rds].modify_db_snapshot_attribute(@snapshot_id, {"Add.MemberId"=>["389480430104"]}).body
end

tests("#copy_db_snapshot (#{@snapshot_id}, target_snapshot_id)").formats(@snapshot_copy_result) do
data = Fog::AWS[:rds].copy_db_snapshot(@snapshot_id, "target_snapshot_id").body
pp data.inspect
data
end
end
tests('failure') do

tests("#delete_snapshot('snap-00000000')").raises(Fog::AWS::RDS::NotFound) do
Fog::AWS[:rds].delete_db_snapshot(@rds.identity)
end

end

@rds.destroy

end

0 comments on commit fbfba3d

Please sign in to comment.