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-09-12 14:58:05 -04:00
2011-08-24 21:37:00 -04:00
require 'fog/aws/parsers/compute/describe_images'
2010-06-12 18:31:17 -04:00
2009-09-12 14:58:05 -04:00
# Describe all or specified images.
#
# ==== Params
2010-10-04 18:46:12 -04:00
# * filters<~Hash> - List of filters to limit results with
# * filters and/or the following
2009-09-12 14:58:05 -04:00
# * 'ExecutableBy'<~String> - Only return images that the executable_by
# user has explicit permission to launch
# * 'ImageId'<~Array> - Ids of images to describe
# * 'Owner'<~String> - Only return images belonging to owner.
#
# ==== Returns
2009-11-02 21:48:49 -05:00
# * response<~Excon::Response>:
2009-09-12 14:58:05 -04:00
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'imagesSet'<~Array>:
# * 'architecture'<~String> - Architecture of the image
2010-07-25 16:13:35 -04:00
# * 'blockDeviceMapping'<~Array> - An array of mapped block devices
2010-12-05 23:27:42 -05:00
# * 'description'<~String> - Description of image
2009-09-12 14:58:05 -04:00
# * 'imageId'<~String> - Id of the image
# * 'imageLocation'<~String> - Location of the image
2011-08-04 15:40:57 -04:00
# * 'imageOwnerAlias'<~String> - Alias of the owner of the image
2009-09-12 14:58:05 -04:00
# * 'imageOwnerId'<~String> - Id of the owner of the image
# * 'imageState'<~String> - State of the image
# * 'imageType'<~String> - Type of the image
# * 'isPublic'<~Boolean> - Whether or not the image is public
# * 'kernelId'<~String> - Kernel id associated with image, if any
# * 'platform'<~String> - Operating platform of the image
# * 'productCodes'<~Array> - Product codes for the image
# * 'ramdiskId'<~String> - Ramdisk id associated with image, if any
2010-07-25 12:24:08 -04:00
# * 'rootDeviceName'<~String> - Root device name, e.g. /dev/sda1
# * 'rootDeviceType'<~String> - Root device type, ebs or instance-store
2010-12-21 16:16:42 -05:00
# * 'virtualizationType'<~String> - Type of virtualization
2011-05-19 12:31:56 -04:00
#
# {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeImages.html]
2010-10-04 18:46:12 -04:00
def describe_images ( filters = { } )
options = { }
for key in [ 'ExecutableBy' , 'ImageId' , 'Owner' ]
2010-10-12 14:00:59 -04:00
if filters . is_a? ( Hash ) && filters . key? ( key )
2010-10-20 20:10:52 -04:00
options [ key ] = filters . delete ( key )
2010-10-04 18:46:12 -04:00
end
2009-09-12 14:58:05 -04:00
end
2011-06-20 16:49:37 -04:00
params = Fog :: AWS . indexed_filters ( filters ) . merge! ( options )
2009-09-12 14:58:05 -04:00
request ( {
2010-05-24 17:22:35 -04:00
'Action' = > 'DescribeImages' ,
:idempotent = > true ,
2011-06-16 19:28:54 -04:00
:parser = > Fog :: Parsers :: Compute :: AWS :: DescribeImages . new
2010-10-20 20:02:31 -04:00
} . merge! ( params ) )
2009-07-13 22:14:59 -04:00
end
2009-09-12 14:58:05 -04:00
2009-07-13 22:14:59 -04:00
end
2009-09-12 14:58:05 -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_images ( filters = { } )
2011-01-19 16:18:01 -05:00
unless filters . is_a? ( Hash )
2011-09-02 13:30:10 -04:00
Fog :: Logger . warning ( " describe_images with #{ filters . class } param is deprecated, use describe_images('image-id' => []) instead [light_black]( #{ caller . first } )[/] " )
2011-08-03 20:48:29 -04:00
filters = { 'image-id' = > [ * filters ] }
2011-01-19 16:18:01 -05:00
end
2011-08-03 19:49:03 -04:00
2011-01-19 16:18:01 -05:00
if filters . keys . any? { | key | key =~ / ^block-device / }
2011-09-02 13:30:10 -04:00
Fog :: Logger . warning ( " describe_images block-device-mapping filters are not yet mocked [light_black]( #{ caller . first } )[/] " )
2011-01-19 16:18:01 -05:00
Fog :: Mock . not_implemented
end
2011-08-03 19:49:03 -04:00
2011-08-04 18:18:48 -04:00
if owner = filters . delete ( 'Owner' )
if owner == 'self'
filters [ 'owner-id' ] = self . data [ :owner_id ]
else
filters [ 'owner-alias' ] = owner
end
end
2011-01-19 16:18:01 -05:00
response = Excon :: Response . new
2011-08-03 19:49:03 -04:00
2011-01-19 16:18:01 -05:00
aliases = {
'architecture' = > 'architecture' ,
'description' = > 'description' ,
'hypervisor' = > 'hypervisor' ,
'image-id' = > 'imageId' ,
'image-type' = > 'imageType' ,
'is-public' = > 'isPublic' ,
'kernel-id' = > 'kernelId' ,
'manifest-location' = > 'manifestLocation' ,
2011-08-03 19:49:03 -04:00
'name' = > 'name' ,
2011-08-04 15:40:57 -04:00
'owner-alias' = > 'imageOwnerAlias' ,
2011-01-19 16:18:01 -05:00
'owner-id' = > 'imageOwnerId' ,
'ramdisk-id' = > 'ramdiskId' ,
'root-device-name' = > 'rootDeviceName' ,
'root-device-type' = > 'rootDeviceType' ,
'state' = > 'imageState' ,
'virtualization-type' = > 'virtualizationType'
}
2011-08-03 19:49:03 -04:00
2011-05-19 18:35:33 -04:00
image_set = self . data [ :images ] . values
2011-08-03 19:49:03 -04:00
2011-01-19 16:18:01 -05:00
for filter_key , filter_value in filters
2011-08-03 20:48:29 -04:00
if tag_key = filter_key . split ( 'tag:' ) [ 1 ]
image_set = image_set . reject { | image | ! [ * filter_value ] . include? ( image [ 'tagSet' ] [ tag_key ] ) }
else
aliased_key = aliases [ filter_key ]
image_set = image_set . reject { | image | ! [ * filter_value ] . include? ( image [ aliased_key ] ) }
end
2011-01-19 16:18:01 -05:00
end
2011-08-16 17:23:37 -04:00
image_set = image_set . map do | image |
case image [ 'imageState' ]
when 'pending'
if Time . now - image [ 'registered' ] > = Fog :: Mock . delay
image [ 'imageState' ] = 'available'
end
end
image . reject { | key , value | [ 'registered' ] . include? ( key ) }
end
2011-01-19 16:18:01 -05:00
response . status = 200
response . body = {
'requestId' = > Fog :: AWS :: Mock . request_id ,
'imagesSet' = > image_set
}
response
2009-09-12 14:58:05 -04:00
end
end
2009-07-13 22:14:59 -04:00
end
end
end