2010-03-16 15:46:21 -07:00
module Fog
2011-06-16 16:28:54 -07:00
module Compute
class AWS
2010-03-16 15:46:21 -07:00
class Real
2011-01-07 16:52:09 -08:00
require 'fog/compute/parsers/aws/describe_snapshots'
2010-06-12 15:31:17 -07:00
2009-08-20 19:26:14 -07:00
# Describe all or specified snapshots
#
# ==== Parameters
2010-10-04 15:46:12 -07:00
# * filters<~Hash> - List of filters to limit results with
# * options<~Hash>:
2010-04-23 11:59:41 -07: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 19:26:14 -07:00
#
# ==== Returns
2009-11-02 18:48:49 -08:00
# * response<~Excon::Response>:
2009-08-20 19:26:14 -07: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 09:31:56 -07:00
#
# {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSnapshots.html]
2010-10-04 15:46:12 -07:00
def describe_snapshots ( filters = { } , options = { } )
unless filters . is_a? ( Hash )
Formatador . display_line ( " [yellow][WARN] describe_snapshots with #{ filters . class } param is deprecated, use describe_snapshots('snapshot-id' => []) instead[/] [light_black]( #{ caller . first } )[/] " )
filters = { 'snapshot-id' = > [ * filters ] }
end
unless options . empty?
Formatador . display_line ( " [yellow][WARN] describe_snapshots with a second param is deprecated, use describe_snapshots(options) instead[/] [light_black]( #{ caller . first } )[/] " )
end
2010-10-13 14:52:55 -07:00
2010-10-04 15:46:12 -07: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 13:49:37 -07:00
params = Fog :: AWS . indexed_filters ( filters ) . merge! ( options )
2009-08-20 19:26:14 -07:00
request ( {
2010-05-24 14:22:35 -07:00
'Action' = > 'DescribeSnapshots' ,
:idempotent = > true ,
2011-06-16 16:28:54 -07:00
:parser = > Fog :: Parsers :: Compute :: AWS :: DescribeSnapshots . new
2010-10-04 15:46:12 -07:00
} . merge! ( params ) )
2009-08-20 19:26:14 -07:00
end
2009-07-13 19:14:59 -07:00
end
2009-08-20 19:26:14 -07:00
2010-03-16 15:46:21 -07:00
class Mock
2009-07-13 19:14:59 -07:00
2010-10-04 15:46:12 -07:00
def describe_snapshots ( filters = { } , options = { } )
unless filters . is_a? ( Hash )
Formatador . display_line ( " [yellow][WARN] describe_snapshots with #{ filters . class } param is deprecated, use describe_snapshots('snapshot-id' => []) instead[/] [light_black]( #{ caller . first } )[/] " )
filters = { 'snapshot-id' = > [ * filters ] }
end
unless options . empty?
Formatador . display_line ( " [yellow][WARN] describe_snapshots with a second param is deprecated, use describe_snapshots(options) instead[/] [light_black]( #{ caller . first } )[/] " )
end
2009-11-20 11:08:08 -08:00
response = Excon :: Response . new
2010-10-04 15:46:12 -07:00
2011-05-19 15:35:33 -07:00
snapshot_set = self . data [ :snapshots ] . values
2010-10-04 15:46:12 -07:00
if filters . delete ( 'owner-alias' )
Formatador . display_line ( " [yellow][WARN] describe_snapshots with owner-alias is not mocked[/] [light_black]( #{ caller . first } )[/] " )
end
2010-11-02 11:43:26 -07:00
if filters . delete ( 'RestorableBy' )
Formatador . display_line ( " [yellow][WARN] describe_snapshots with RestorableBy is not mocked[/] [light_black]( #{ caller . first } )[/] " )
end
2010-10-04 15:46:12 -07:00
2011-03-23 13:33:27 -04:00
snapshot_set = apply_tag_filters ( snapshot_set , filters )
2010-10-04 15:46:12 -07:00
aliases = {
'description' = > 'description' ,
'owner-id' = > 'ownerId' ,
'progress' = > 'progress' ,
'snapshot-id' = > 'snapshotId' ,
'start-time' = > 'startTime' ,
'status' = > 'status' ,
'volume-id' = > 'volumeId' ,
'volume-size' = > 'volumeSize'
}
2011-03-23 13:33:27 -04:00
2010-10-04 15:46:12 -07: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 19:26:14 -07:00
end
2011-03-23 13:33:27 -04:00
2010-10-04 15:46:12 -07:00
snapshot_set . each do | snapshot |
case snapshot [ 'status' ]
when 'in progress' , 'pending'
2011-05-18 16:10:46 -07:00
if Time . now - snapshot [ 'startTime' ] > = Fog :: Mock . delay * 2
2010-10-04 15:46:12 -07:00
snapshot [ 'progress' ] = '100%'
snapshot [ 'status' ] = 'completed'
2011-05-18 16:10:46 -07:00
elsif Time . now - snapshot [ 'startTime' ] > = Fog :: Mock . delay
2010-10-04 15:46:12 -07:00
snapshot [ 'progress' ] = '50%'
snapshot [ 'status' ] = 'in progress'
else
snapshot [ 'progress' ] = '0%'
snapshot [ 'status' ] = 'in progress'
2009-08-20 19:26:14 -07:00
end
end
end
2010-10-04 15:46:12 -07:00
response . status = 200
response . body = {
'requestId' = > Fog :: AWS :: Mock . request_id ,
'snapshotSet' = > snapshot_set
}
response
2009-08-20 19:26:14 -07:00
end
end
2009-07-13 19:14:59 -07:00
end
end
end