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
2009-08-15 18:19:07 -04:00
2011-01-07 19:52:09 -05:00
require 'fog/compute/parsers/aws/describe_volumes'
2010-06-12 18:31:17 -04:00
2009-08-15 18:19:07 -04:00
# Describe all or specified volumes.
#
# ==== Parameters
2010-10-04 18:46:12 -04:00
# * filters<~Hash> - List of filters to limit results with
2009-08-15 18:19:07 -04:00
#
# ==== Returns
2009-11-02 21:48:49 -05:00
# * response<~Excon::Response>:
2009-08-15 18:19:07 -04:00
# * body<~Hash>:
# * 'volumeSet'<~Array>:
# * 'availabilityZone'<~String> - Availability zone for volume
# * 'createTime'<~Time> - Timestamp for creation
# * 'size'<~Integer> - Size in GiBs for volume
# * 'snapshotId'<~String> - Snapshot volume was created from, if any
# * 'status'<~String> - State of volume
# * 'volumeId'<~String> - Reference to volume
# * 'attachmentSet'<~Array>:
# * 'attachmentTime'<~Time> - Timestamp for attachment
2010-11-15 14:49:16 -05:00
# * 'deleteOnTermination'<~Boolean> - Whether or not to delete volume on instance termination
2009-08-15 18:19:07 -04:00
# * 'device'<~String> - How value is exposed to instance
# * 'instanceId'<~String> - Reference to attached instance
# * 'status'<~String> - Attachment state
# * 'volumeId'<~String> - Reference to volume
2011-05-19 12:31:56 -04:00
#
# {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeVolumes.html]
2010-10-04 18:46:12 -04:00
def describe_volumes ( filters = { } )
unless filters . is_a? ( Hash )
Formatador . display_line ( " [yellow][WARN] describe_volumes with #{ filters . class } param is deprecated, use describe_volumes('volume-id' => []) instead[/] [light_black]( #{ caller . first } )[/] " )
filters = { 'volume-id' = > [ * filters ] }
end
params = AWS . indexed_filters ( filters )
2009-08-15 18:19:07 -04:00
request ( {
2010-05-24 17:22:35 -04:00
'Action' = > 'DescribeVolumes' ,
:idempotent = > true ,
2011-06-16 19:28:54 -04:00
:parser = > Fog :: Parsers :: Compute :: AWS :: DescribeVolumes . new
2010-03-16 01:15:33 -04:00
} . merge! ( params ) )
2009-08-15 18:19:07 -04:00
end
2009-07-13 22:14:59 -04:00
end
2009-08-15 18:19:07 -04:00
2010-03-16 18:46:21 -04:00
class Mock
2009-08-15 18:19:07 -04:00
2010-10-04 18:46:12 -04:00
def describe_volumes ( filters = { } )
unless filters . is_a? ( Hash )
Formatador . display_line ( " [yellow][WARN] describe_volumes with #{ filters . class } param is deprecated, use describe_volumes('volume-id' => []) instead[/] [light_black]( #{ caller . first } )[/] " )
filters = { 'volume-id' = > [ * filters ] }
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
volume_set = self . data [ :volumes ] . values
2011-03-23 13:33:27 -04:00
volume_set = apply_tag_filters ( volume_set , filters )
2010-10-04 18:46:12 -04:00
aliases = {
'availability-zone' = > 'availabilityZone' ,
'create-time' = > 'createTime' ,
'size' = > 'size' ,
'snapshot-id' = > 'snapshotId' ,
'status' = > 'status' ,
'volume-id' = > 'volumeId'
}
attachment_aliases = {
'attach-time' = > 'attachTime' ,
'delete-on-termination' = > 'deleteOnTermination' ,
'device' = > 'device' ,
'instance-id' = > 'instanceId' ,
'status' = > 'status'
}
for filter_key , filter_value in filters
if attachment_key = filter_key . split ( 'attachment.' ) [ 1 ]
aliased_key = permission_aliases [ filter_key ]
volume_set = volume_set . reject { | volume | ! volume [ 'attachmentSet' ] . detect { | attachment | [ * filter_value ] . include? ( attachment [ aliased_key ] ) } }
else
aliased_key = aliases [ filter_key ]
volume_set = volume_set . reject { | volume | ! [ * filter_value ] . include? ( volume [ aliased_key ] ) }
end
2009-08-15 18:19:07 -04:00
end
2010-10-04 18:46:12 -04:00
volume_set . each do | volume |
case volume [ 'status' ]
when 'attaching'
2011-05-18 19:10:46 -04:00
if Time . now - volume [ 'attachmentSet' ] . first [ 'attachTime' ] > = Fog :: Mock . delay
2010-10-04 18:46:12 -04:00
volume [ 'attachmentSet' ] . first [ 'status' ] = 'in-use'
volume [ 'status' ] = 'in-use'
end
when 'creating'
2011-05-18 19:10:46 -04:00
if Time . now - volume [ 'createTime' ] > = Fog :: Mock . delay
2010-10-04 18:46:12 -04:00
volume [ 'status' ] = 'available'
end
when 'deleting'
2011-05-19 18:35:33 -04:00
if Time . now - self . data [ :deleted_at ] [ volume [ 'volumeId' ] ] > = Fog :: Mock . delay
self . data [ :deleted_at ] . delete ( volume [ 'volumeId' ] )
self . data [ :volumes ] . delete ( volume [ 'volumeId' ] )
2009-08-15 18:19:07 -04:00
end
end
end
2011-05-19 18:35:33 -04:00
volume_set = volume_set . reject { | volume | ! self . data [ :volumes ] [ volume [ 'volumeId' ] ] }
2010-10-04 18:46:12 -04:00
response . status = 200
response . body = {
'requestId' = > Fog :: AWS :: Mock . request_id ,
'volumeSet' = > volume_set
}
response
2009-08-15 18:19:07 -04:00
end
end
2009-07-13 22:14:59 -04:00
end
end
end