File: describe_images.rb

package info (click to toggle)
ruby-fog-aws 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,816 kB
  • sloc: ruby: 68,587; makefile: 6
file content (128 lines) | stat: -rw-r--r-- 5,577 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module Fog
  module AWS
    class Compute
      class Real
        require 'fog/aws/parsers/compute/describe_images'

        # Describe all or specified images.
        #
        # ==== Params
        # * filters<~Hash> - List of filters to limit results with
        #   * filters and/or the following
        #   * '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
        # * response<~Excon::Response>:
        #   * body<~Hash>:
        #     * 'requestId'<~String> - Id of request
        #     * 'imagesSet'<~Array>:
        #       * 'architecture'<~String> - Architecture of the image
        #       * 'blockDeviceMapping'<~Array> - An array of mapped block devices
        #       * 'description'<~String> - Description of image
        #       * 'imageId'<~String> - Id of the image
        #       * 'imageLocation'<~String> - Location of the image
        #       * 'imageOwnerAlias'<~String> - Alias of the owner of the image
        #       * '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
        #       * 'rootDeviceName'<~String> - Root device name, e.g. /dev/sda1
        #       * 'rootDeviceType'<~String> - Root device type, ebs or instance-store
        #       * 'virtualizationType'<~String> - Type of virtualization
        #       * 'creationDate'time<~Datetime> - Date and time the image was created
        #       * 'enaSupport'<~Boolean> - whether or not the image supports enhanced networking
        #
        # {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeImages.html]
        def describe_images(filters = {})
          options = {}
          for key in ['ExecutableBy', 'ImageId', 'Owner']
            if filters.is_a?(Hash) && filters.key?(key)
              options.merge!(Fog::AWS.indexed_request_param(key, filters.delete(key)))
            end
          end
          params = Fog::AWS.indexed_filters(filters).merge!(options)
          request({
            'Action'    => 'DescribeImages',
            :idempotent => true,
            :parser     => Fog::Parsers::AWS::Compute::DescribeImages.new
          }.merge!(params))
        end
      end

      class Mock
        def describe_images(filters = {})
          unless filters.is_a?(Hash)
            Fog::Logger.deprecation("describe_images with #{filters.class} param is deprecated, use describe_images('image-id' => []) instead [light_black](#{caller.first})[/]")
            filters = {'image-id' => [*filters]}
          end

          if filters.keys.any? {|key| key =~ /^block-device/}
            Fog::Logger.warning("describe_images block-device-mapping filters are not yet mocked [light_black](#{caller.first})[/]")
            Fog::Mock.not_implemented
          end

          if owner = filters.delete('Owner')
            if owner == 'self'
              filters['owner-id'] = self.data[:owner_id]
            else
              filters['owner-alias'] = owner
            end
          end

          response = Excon::Response.new

          aliases = {
            'architecture'        => 'architecture',
            'description'         => 'description',
            'hypervisor'          => 'hypervisor',
            'image-id'            => 'imageId',
            'image-type'          => 'imageType',
            'is-public'           => 'isPublic',
            'kernel-id'           => 'kernelId',
            'manifest-location'   => 'manifestLocation',
            'name'                => 'name',
            'owner-alias'         => 'imageOwnerAlias',
            'owner-id'            => 'imageOwnerId',
            'ramdisk-id'          => 'ramdiskId',
            'root-device-name'    => 'rootDeviceName',
            'root-device-type'    => 'rootDeviceType',
            'state'               => 'imageState',
            'virtualization-type' => 'virtualizationType'
          }

          image_set = visible_images.values
          image_set = apply_tag_filters(image_set, filters, 'imageId')

          for filter_key, filter_value in filters
            aliased_key = aliases[filter_key]
            image_set = image_set.reject{|image| ![*filter_value].include?(image[aliased_key])}
          end

          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) }.merge('tagSet' => self.data[:tag_sets][image['imageId']])
          end

          response.status = 200
          response.body = {
            'requestId' => Fog::AWS::Mock.request_id,
            'imagesSet' => image_set
          }
          response
        end
      end
    end
  end
end