File: describe_instances.rb

package info (click to toggle)
ruby-fog-aws 3.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,140 kB
  • sloc: ruby: 73,328; javascript: 14; makefile: 9; sh: 4
file content (265 lines) | stat: -rw-r--r-- 14,588 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
module Fog
  module AWS
    class Compute
      class Real
        require 'fog/aws/parsers/compute/describe_instances'

        # Describe all or specified instances
        #
        # ==== Parameters
        # * filters<~Hash> - List of filters to limit results with
        #   * Also allows for passing of optional parameters to fetch instances in batches:
        #     * 'maxResults' - The number of instances to return for the request
        #     * 'nextToken' - The token to fetch the next set of items. This is returned by a previous request.
        # ==== Returns
        # * response<~Excon::Response>:
        #   * body<~Hash>:
        #     * 'requestId'<~String> - Id of request
        #     * 'nextToken' - The token to use when requesting the next set of items when fetching items in batches.
        #     * 'reservationSet'<~Array>:
        #       * 'groupSet'<~Array> - Group names for reservation
        #       * 'ownerId'<~String> - AWS Access Key ID of reservation owner
        #       * 'reservationId'<~String> - Id of the reservation
        #       * 'instancesSet'<~Array>:
        #         * instance<~Hash>:
        #           * 'architecture'<~String> - architecture of image in [i386, x86_64]
        #           * 'amiLaunchIndex'<~Integer> - reference to instance in launch group
        #           * 'blockDeviceMapping'<~Array>
        #             * 'attachTime'<~Time> - time of volume attachment
        #             * 'deleteOnTermination'<~Boolean> - whether or not to delete volume on termination
        #             * 'deviceName'<~String> - specifies how volume is exposed to instance
        #             * 'status'<~String> - status of attached volume
        #             * 'volumeId'<~String> - Id of attached volume
        #           * 'dnsName'<~String> - public dns name, blank until instance is running
        #           * 'ebsOptimized'<~Boolean> - Whether the instance is optimized for EBS I/O
        #           * 'imageId'<~String> - image id of ami used to launch instance
        #           * 'instanceId'<~String> - id of the instance
        #           * 'instanceState'<~Hash>:
        #             * 'code'<~Integer> - current status code
        #             * 'name'<~String> - current status name
        #           * 'instanceType'<~String> - type of instance
        #           * 'ipAddress'<~String> - public ip address assigned to instance
        #           * 'kernelId'<~String> - id of kernel used to launch instance
        #           * 'keyName'<~String> - name of key used launch instances or blank
        #           * 'launchTime'<~Time> - time instance was launched
        #           * 'monitoring'<~Hash>:
        #             * 'state'<~Boolean - state of monitoring
        #           * 'placement'<~Hash>:
        #             * 'availabilityZone'<~String> - Availability zone of the instance
        #           * 'platform'<~String> - Platform of the instance (e.g., Windows).
        #           * 'productCodes'<~Array> - Product codes for the instance
        #           * 'privateDnsName'<~String> - private dns name, blank until instance is running
        #           * 'privateIpAddress'<~String> - private ip address assigned to instance
        #           * 'rootDeviceName'<~String> - specifies how the root device is exposed to the instance
        #           * 'rootDeviceType'<~String> - root device type used by AMI in [ebs, instance-store]
        #           * 'ramdiskId'<~String> - Id of ramdisk used to launch instance
        #           * 'reason'<~String> - reason for most recent state transition, or blank
        #
        # {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstances.html]
        def describe_instances(filters = {})
          unless filters.is_a?(Hash)
            Fog::Logger.deprecation("describe_instances with #{filters.class} param is deprecated, use describe_instances('instance-id' => []) instead [light_black](#{caller.first})[/]")
            filters = {'instance-id' => [*filters]}
          end
          params = {}

          next_token  = filters.delete('nextToken') || filters.delete('NextToken')
          max_results = filters.delete('maxResults') || filters.delete('MaxResults')

          if filters['instance-id']
            instance_ids = filters.delete('instance-id')
            instance_ids = [instance_ids] unless instance_ids.is_a?(Array)
            instance_ids.each_with_index do |id, index|
              params.merge!("InstanceId.#{index}" => id)
            end
          end

          params['NextToken']  = next_token if next_token
          params['MaxResults'] = max_results if max_results
          params.merge!(Fog::AWS.indexed_filters(filters))

          request({
            'Action'    => 'DescribeInstances',
            :idempotent => true,
            :parser     => Fog::Parsers::AWS::Compute::DescribeInstances.new
          }.merge!(params))
        end
      end

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

          response = Excon::Response.new

          instance_set = self.data[:instances].values
          instance_set = apply_tag_filters(instance_set, filters, 'instanceId')

          aliases = {
            'architecture'             => 'architecture',
            'availability-zone'        => 'availabilityZone',
            'client-token'             => 'clientToken',
            'dns-name'                 => 'dnsName',
            'group-id'                 => 'groupId',
            'image-id'                 => 'imageId',
            'instance-id'              => 'instanceId',
            'instance-lifecycle'       => 'instanceLifecycle',
            'instance-type'            => 'instanceType',
            'ip-address'               => 'ipAddress',
            'kernel-id'                => 'kernelId',
            'key-name'                 => 'key-name',
            'launch-index'             => 'launchIndex',
            'launch-time'              => 'launchTime',
            'monitoring-state'         => 'monitoringState',
            'owner-id'                 => 'ownerId',
            'placement-group-name'     => 'placementGroupName',
            'platform'                 => 'platform',
            'private-dns-name'         => 'privateDnsName',
            'private-ip-address'       => 'privateIpAddress',
            'product-code'             => 'productCode',
            'ramdisk-id'               => 'ramdiskId',
            'reason'                   => 'reason',
            'requester-id'             => 'requesterId',
            'reservation-id'           => 'reservationId',
            'root-device-name'         => 'rootDeviceName',
            'root-device-type'         => 'rootDeviceType',
            'spot-instance-request-id' => 'spotInstanceRequestId',
            'subnet-id'                => 'subnetId',
            'virtualization-type'      => 'virtualizationType',
            'vpc-id'                   => 'vpcId'
          }
          block_device_mapping_aliases = {
            'attach-time'           => 'attachTime',
            'delete-on-termination' => 'deleteOnTermination',
            'device-name'           => 'deviceName',
            'status'                => 'status',
            'volume-id'             => 'volumeId',
          }
          instance_state_aliases = {
            'code' => 'code',
            'name' => 'name'
          }
          state_reason_aliases = {
            'code'    => 'code',
            'message' => 'message'
          }
          for filter_key, filter_value in filters
            if block_device_mapping_key = filter_key.split('block-device-mapping.')[1]
              aliased_key = block_device_mapping_aliases[block_device_mapping_key]
              instance_set = instance_set.reject{|instance| !instance['blockDeviceMapping'].find {|block_device_mapping| [*filter_value].include?(block_device_mapping[aliased_key])}}
            elsif instance_state_key = filter_key.split('instance-state-')[1]
              aliased_key = instance_state_aliases[instance_state_key]
              instance_set = instance_set.reject{|instance| ![*filter_value].include?(instance['instanceState'][aliased_key])}
            elsif state_reason_key = filter_key.split('state-reason-')[1]
              aliased_key = state_reason_aliases[state_reason_key]
              instance_set = instance_set.reject{|instance| ![*filter_value].include?(instance['stateReason'][aliased_key])}
            elsif filter_key == "availability-zone"
              aliased_key = aliases[filter_key]
              instance_set = instance_set.reject{|instance| ![*filter_value].include?(instance['placement'][aliased_key])}
            elsif filter_key == "group-name"
              instance_set = instance_set.reject {|instance| !instance['groupSet'].include?(filter_value)}
            elsif filter_key == "group-id"
              group_ids = [*filter_value]
              security_group_names = self.data[:security_groups].values.select { |sg| group_ids.include?(sg['groupId']) }.map { |sg| sg['groupName'] }
              instance_set = instance_set.reject {|instance| (security_group_names & instance['groupSet']).empty?}
            else
              aliased_key = aliases[filter_key]
              instance_set = instance_set.reject {|instance| ![*filter_value].include?(instance[aliased_key])}
            end
          end

          brand_new_instances = instance_set.select do |instance|
            instance['instanceState']['name'] == 'pending' &&
              Time.now - instance['launchTime'] < Fog::Mock.delay * 2
          end

          # Error if filtering for a brand new instance directly
          if (filters['instance-id'] || filters['instanceId']) && !brand_new_instances.empty?
            raise Fog::AWS::Compute::NotFound.new("The instance ID '#{brand_new_instances.first['instanceId']}' does not exist")
          end

          # Otherwise don't include it in the list
          instance_set = instance_set.reject {|instance| brand_new_instances.include?(instance) }

          response.status = 200
          reservation_set = {}

          instance_set.each do |instance|
            case instance['instanceState']['name']
            when 'pending'
              if Time.now - instance['launchTime'] >= Fog::Mock.delay * 2
                instance['ipAddress']         = Fog::AWS::Mock.ip_address
                instance['originalIpAddress'] = instance['ipAddress']
                instance['dnsName']           = Fog::AWS::Mock.dns_name_for(instance['ipAddress'])
                instance['instanceState']     = { 'code' => 16, 'name' => 'running' }
              end
            when 'rebooting'
              instance['instanceState'] = { 'code' => 16, 'name' => 'running' }
            when 'stopping'
              instance['instanceState'] = { 'code' => 0, 'name' => 'stopped' }
              instance['stateReason'] = { 'code' => 0 }
            when 'shutting-down'
              if Time.now - self.data[:deleted_at][instance['instanceId']] >= Fog::Mock.delay * 2
                self.data[:deleted_at].delete(instance['instanceId'])
                self.data[:instances].delete(instance['instanceId'])
              elsif Time.now - self.data[:deleted_at][instance['instanceId']] >= Fog::Mock.delay
                instance['instanceState'] = { 'code' => 48, 'name' => 'terminating' }
              end
            when 'terminating'
              if Time.now - self.data[:deleted_at][instance['instanceId']] >= Fog::Mock.delay
                self.data[:deleted_at].delete(instance['instanceId'])
                self.data[:instances].delete(instance['instanceId'])
              end
            end

            if self.data[:instances][instance['instanceId']]

              nics = self.data[:network_interfaces].select{|ni,ni_conf|
                ni_conf['attachment']['instanceId'] == instance['instanceId']
              }
              instance['networkInterfaces'] = nics.map{|ni,ni_conf|
                {
                  'ownerId' => ni_conf['ownerId'],
                  'subnetId' => ni_conf['subnetId'],
                  'vpcId' => ni_conf['vpcId'],
                  'networkInterfaceId' => ni_conf['networkInterfaceId'],
                  'groupSet' => ni_conf['groupSet'],
                  'attachmentId' => ni_conf['attachment']['attachmentId']
                }
              }
              if nics.count > 0

                instance['privateIpAddress'] = nics.sort_by {|ni, ni_conf|
                  ni_conf['attachment']['deviceIndex']
                }.map{ |ni, ni_conf| ni_conf['privateIpAddress'] }.first

                instance['privateDnsName'] = Fog::AWS::Mock.private_dns_name_for(instance['privateIpAddress'])
              else
                instance['privateIpAddress'] = ''
                instance['privateDnsName'] = ''
              end

              reservation_set[instance['reservationId']] ||= {
                'groupSet'      => instance['groupSet'],
                'groupIds'      => instance['groupIds'],
                'instancesSet'  => [],
                'ownerId'       => instance['ownerId'],
                'reservationId' => instance['reservationId']
              }
              reservation_set[instance['reservationId']]['instancesSet'] << instance.reject{|key,value| !['amiLaunchIndex', 'architecture', 'blockDeviceMapping', 'clientToken', 'dnsName', 'ebsOptimized', 'hypervisor', 'iamInstanceProfile', 'imageId', 'instanceId', 'instanceState', 'instanceType', 'ipAddress', 'kernelId', 'keyName', 'launchTime', 'monitoring', 'networkInterfaces', 'ownerId', 'placement', 'platform', 'privateDnsName', 'privateIpAddress', 'productCodes', 'ramdiskId', 'reason', 'rootDeviceName', 'rootDeviceType', 'spotInstanceRequestId', 'stateReason', 'subnetId', 'virtualizationType'].include?(key)}.merge('tagSet' => self.data[:tag_sets][instance['instanceId']])
            end
          end

          response.body = {
            'requestId'       => Fog::AWS::Mock.request_id,
            'reservationSet' => reservation_set.values
          }
          response
        end
      end
    end
  end
end