2010-03-16 18:46:21 -04:00
module Fog
2011-06-16 19:28:54 -04:00
module Compute
class AWS
2010-03-16 18:46:21 -04:00
class Real
2011-08-24 21:37:00 -04:00
require 'fog/aws/parsers/compute/describe_snapshots'
2010-06-12 18:31:17 -04:00
2009-08-20 22:26:14 -04:00
# Describe all or specified snapshots
#
# ==== Parameters
2010-10-04 18:46:12 -04:00
# * filters<~Hash> - List of filters to limit results with
# * options<~Hash>:
2010-04-23 14:59:41 -04:00
# * 'Owner'<~String> - Owner of snapshot in ['self', 'amazon', account_id]
# * 'RestorableBy'<~String> - Account id of user who can create volumes from this snapshot
2009-08-20 22:26:14 -04:00
#
# ==== Returns
2009-11-02 21:48:49 -05:00
# * response<~Excon::Response>:
2009-08-20 22:26:14 -04:00
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'snapshotSet'<~Array>:
# * 'progress'<~String>: The percentage progress of the snapshot
# * 'snapshotId'<~String>: Id of the snapshot
# * 'startTime'<~Time>: Timestamp of when snapshot was initiated
# * 'status'<~String>: Snapshot state, in ['pending', 'completed']
# * 'volumeId'<~String>: Id of volume that snapshot contains
2011-05-19 12:31:56 -04:00
#
# {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSnapshots.html]
2010-10-04 18:46:12 -04:00
def describe_snapshots ( filters = { } , options = { } )
unless filters . is_a? ( Hash )
2011-09-02 13:30:10 -04:00
Fog :: Logger . warning ( " describe_snapshots with #{ filters . class } param is deprecated, use describe_snapshots('snapshot-id' => []) instead [light_black]( #{ caller . first } )[/] " )
2010-10-04 18:46:12 -04:00
filters = { 'snapshot-id' = > [ * filters ] }
end
unless options . empty?
2011-09-02 13:30:10 -04:00
Fog :: Logger . warning ( " describe_snapshots with a second param is deprecated, use describe_snapshots(options) instead [light_black]( #{ caller . first } )[/] " )
2010-10-04 18:46:12 -04:00
end
2010-10-13 17:52:55 -04:00
2010-10-04 18:46:12 -04:00
for key in [ 'ExecutableBy' , 'ImageId' , 'Owner' , 'RestorableBy' ]
if filters . has_key? ( key )
options [ key ] = filters . delete ( key )
end
end
options [ 'RestorableBy' ] || = 'self'
2011-06-20 16:49:37 -04:00
params = Fog :: AWS . indexed_filters ( filters ) . merge! ( options )
2009-08-20 22:26:14 -04:00
request ( {
2010-05-24 17:22:35 -04:00
'Action' = > 'DescribeSnapshots' ,
:idempotent = > true ,
2011-06-16 19:28:54 -04:00
:parser = > Fog :: Parsers :: Compute :: AWS :: DescribeSnapshots . new
2010-10-04 18:46:12 -04:00
} . merge! ( params ) )
2009-08-20 22:26:14 -04:00
end
2009-07-13 22:14:59 -04:00
end
2009-08-20 22:26:14 -04:00
2010-03-16 18:46:21 -04:00
class Mock
2009-07-13 22:14:59 -04:00
2010-10-04 18:46:12 -04:00
def describe_snapshots ( filters = { } , options = { } )
unless filters . is_a? ( Hash )
2011-09-02 13:30:10 -04:00
Fog :: Logger . warning ( " describe_snapshots with #{ filters . class } param is deprecated, use describe_snapshots('snapshot-id' => []) instead [light_black]( #{ caller . first } )[/] " )
2010-10-04 18:46:12 -04:00
filters = { 'snapshot-id' = > [ * filters ] }
end
unless options . empty?
2011-09-02 13:30:10 -04:00
Fog :: Logger . warning ( " describe_snapshots with a second param is deprecated, use describe_snapshots(options) instead [light_black]( #{ caller . first } )[/] " )
2010-10-04 18:46:12 -04:00
end
2009-11-20 14:08:08 -05:00
response = Excon :: Response . new
2010-10-04 18:46:12 -04:00
2011-05-19 18:35:33 -04:00
snapshot_set = self . data [ :snapshots ] . values
2010-10-04 18:46:12 -04:00
if filters . delete ( 'owner-alias' )
2011-09-02 13:30:10 -04:00
Fog :: Logger . warning ( " describe_snapshots with owner-alias is not mocked [light_black]( #{ caller . first } )[/] " )
2010-10-04 18:46:12 -04:00
end
2011-08-29 15:46:09 -04:00
if ( restorable_by = filters . delete ( 'RestorableBy' ) ) && restorable_by != 'self'
2011-09-02 13:30:10 -04:00
Fog :: Logger . warning ( " describe_snapshots with RestorableBy other than 'self' (wanted #{ restorable_by . inspect } ) is not mocked [light_black]( #{ caller . first } )[/] " )
2010-11-02 14:43:26 -04:00
end
2010-10-04 18:46:12 -04:00
2011-03-23 13:33:27 -04:00
snapshot_set = apply_tag_filters ( snapshot_set , filters )
2011-08-29 15:46:09 -04:00
2010-10-04 18:46:12 -04:00
aliases = {
'description' = > 'description' ,
'owner-id' = > 'ownerId' ,
'progress' = > 'progress' ,
'snapshot-id' = > 'snapshotId' ,
'start-time' = > 'startTime' ,
'status' = > 'status' ,
'volume-id' = > 'volumeId' ,
'volume-size' = > 'volumeSize'
}
2011-08-29 15:46:09 -04:00
2010-10-04 18:46:12 -04:00
for filter_key , filter_value in filters
aliased_key = aliases [ filter_key ]
snapshot_set = snapshot_set . reject { | snapshot | ! [ * filter_value ] . include? ( snapshot [ aliased_key ] ) }
2009-08-20 22:26:14 -04:00
end
2011-08-29 15:46:09 -04:00
2010-10-04 18:46:12 -04:00
snapshot_set . each do | snapshot |
case snapshot [ 'status' ]
when 'in progress' , 'pending'
2011-05-18 19:10:46 -04:00
if Time . now - snapshot [ 'startTime' ] > = Fog :: Mock . delay * 2
2010-10-04 18:46:12 -04:00
snapshot [ 'progress' ] = '100%'
snapshot [ 'status' ] = 'completed'
2011-05-18 19:10:46 -04:00
elsif Time . now - snapshot [ 'startTime' ] > = Fog :: Mock . delay
2010-10-04 18:46:12 -04:00
snapshot [ 'progress' ] = '50%'
snapshot [ 'status' ] = 'in progress'
else
snapshot [ 'progress' ] = '0%'
snapshot [ 'status' ] = 'in progress'
2009-08-20 22:26:14 -04:00
end
end
end
2010-10-04 18:46:12 -04:00
response . status = 200
response . body = {
'requestId' = > Fog :: AWS :: Mock . request_id ,
'snapshotSet' = > snapshot_set
}
response
2009-08-20 22:26:14 -04:00
end
end
2009-07-13 22:14:59 -04:00
end
end
end