1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[AWS] CopySnapshot. supports cross-region snapshot copying.

This commit is contained in:
Shai Rosenfeld & Jacob Burkhart 2012-12-18 13:18:59 -08:00 committed by Shai Rosenfeld
parent 1450a637f0
commit 3d6402f7a8
4 changed files with 101 additions and 1 deletions

View file

@ -65,6 +65,7 @@ module Fog
request :create_tags
request :create_volume
request :create_vpc
request :copy_snapshot
request :delete_dhcp_options
request :delete_internet_gateway
request :delete_key_pair
@ -320,7 +321,7 @@ module Fog
@region = options[:region] ||= 'us-east-1'
@instrumentor = options[:instrumentor]
@instrumentor_name = options[:instrumentor_name] || 'fog.aws.compute'
@version = options[:version] || '2012-07-20'
@version = options[:version] || '2012-12-01'
if @endpoint = options[:endpoint]
endpoint = URI.parse(@endpoint)

View file

@ -0,0 +1,22 @@
module Fog
module Parsers
module Compute
module AWS
class CopySnapshot < Fog::Parsers::Base
def end_element(name)
case name
when 'snapshotId'
@response[name] = value
when 'requestId'
@response[name] = value
end
end
end
end
end
end
end

View file

@ -0,0 +1,58 @@
module Fog
module Compute
class AWS
class Real
require 'fog/aws/parsers/compute/copy_snapshot'
# Copy a snapshot to a different region
#
# ==== Parameters
# * source_snapshot_id<~String> - Id of snapshot
# * source_region<~String> - Region to move it from
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - id of request
# * 'snapshotId'<~String> - id of snapshot
#
# {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-CopySnapshot.html]
def copy_snapshot(source_snapshot_id, source_region, description = nil)
request(
'Action' => 'CopySnapshot',
'SourceSnapshotId'=> source_snapshot_id,
'SourceRegion' => source_region,
'Description' => description,
:parser => Fog::Parsers::Compute::AWS::CopySnapshot.new
)
end
end
class Mock
#
# Usage
#
# AWS[:compute].copy_snapshot("snap-1db0a957", 'us-east-1')
#
def copy_snapshot(source_snapshot_id, source_region, description = nil)
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

View file

@ -16,6 +16,11 @@ Shindo.tests('Fog::Compute[:aws] | snapshot requests', ['aws']) do
'snapshotSet' => [@snapshot_format.merge('tagSet' => {})]
}
@snapshot_copy_result = {
'requestId' => String,
'snapshotId' => String
}
@volume = Fog::Compute[:aws].volumes.create(:availability_zone => 'us-east-1a', :size => 1)
tests('success') do
@ -39,10 +44,24 @@ Shindo.tests('Fog::Compute[:aws] | snapshot requests', ['aws']) do
Fog::Compute[:aws].describe_snapshots('snapshot-id' => @snapshot_id).body
end
tests("#copy_snapshot (#{@snapshot_id}, 'us-east-1')").formats(@snapshot_copy_result) do
data = Fog::Compute.new(:provider => :aws, :region => "us-west-1").copy_snapshot(@snapshot_id, "us-east-1").body
@west_snapshot_id = data['snapshotId']
data
end
tests("#delete_snapshots(#{@snapshot_id})").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute[:aws].delete_snapshot(@snapshot_id).body
end
#NOTE: waiting for the copy to complete can sometimes take up to 5 minutes (but sometimes it's nearly instant)
#for faster tests: comment out the rest of this block
Fog.wait_for { Fog::Compute.new(:provider => :aws, :region => "us-west-1").snapshots.get(@west_snapshot_id) }
tests("#delete_snapshots(#{@west_snapshot_id})").formats(AWS::Compute::Formats::BASIC) do
Fog::Compute.new(:provider => :aws, :region => "us-west-1").delete_snapshot(@west_snapshot_id).body
end
end
tests('failure') do