1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/compute/requests/aws/describe_volumes.rb

117 lines
4.6 KiB
Ruby
Raw Normal View History

2010-03-16 18:46:21 -04:00
module Fog
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
# * filters<~Hash> - List of filters to limit results with
2009-08-15 18:19:07 -04:00
#
# ==== Returns
# * 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
# * '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
#
# {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeVolumes.html]
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({
'Action' => 'DescribeVolumes',
:idempotent => true,
:parser => Fog::Parsers::Compute::AWS::DescribeVolumes.new
}.merge!(params))
2009-08-15 18:19:07 -04:00
end
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
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
2011-05-19 18:35:33 -04:00
volume_set = self.data[:volumes].values
volume_set = apply_tag_filters(volume_set, filters)
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
volume_set.each do |volume|
case volume['status']
when 'attaching'
if Time.now - volume['attachmentSet'].first['attachTime'] >= Fog::Mock.delay
volume['attachmentSet'].first['status'] = 'in-use'
volume['status'] = 'in-use'
end
when 'creating'
if Time.now - volume['createTime'] >= Fog::Mock.delay
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']]}
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
end
end
end