File: describe_spot_instance_requests.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 (79 lines) | stat: -rw-r--r-- 3,923 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
module Fog
  module AWS
    class Compute
      class Real
        require 'fog/aws/parsers/compute/spot_instance_requests'

        # Describe all or specified spot instance requests
        #
        # ==== Parameters
        # * filters<~Hash> - List of filters to limit results with
        #
        # ==== Returns
        # * response<~Excon::Response>:
        #   * body<~Hash>:
        #     * 'requestId'<~String> - Id of request
        #     * 'spotInstanceRequestSet'<~Array>:
        #       * 'createTime'<~Time> - time of instance request creation
        #       * 'instanceId'<~String> - instance id if one has been launched to fulfill request
        #       * 'launchedAvailabilityZone'<~String> - availability zone of instance if one has been launched to fulfill request
        #       * 'launchSpecification'<~Hash>:
        #         * 'blockDeviceMapping'<~Hash> - list of block device mappings for instance
        #         * 'groupSet'<~String> - security group(s) for instance
        #         * 'keyName'<~String> - keypair name for instance
        #         * 'imageId'<~String> - AMI for instance
        #         * 'instanceType'<~String> - type for instance
        #         * 'monitoring'<~Boolean> - monitoring status for instance
        #         * 'subnetId'<~String> - VPC subnet ID for instance
        #       * 'productDescription'<~String> - general description of AMI
        #       * 'spotInstanceRequestId'<~String> - id of spot instance request
        #       * 'spotPrice'<~Float> - maximum price for instances to be launched
        #       * 'state'<~String> - spot instance request state
        #       * 'type'<~String> - spot instance request type
        #
        # {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSpotInstanceRequests.html]
        def describe_spot_instance_requests(filters = {})
          params = Fog::AWS.indexed_filters(filters)
          request({
            'Action'    => 'DescribeSpotInstanceRequests',
            :idempotent => true,
            :parser     => Fog::Parsers::AWS::Compute::SpotInstanceRequests.new
          }.merge!(params))
        end
      end

      class Mock
        def describe_spot_instance_requests(filters = {})
          response = Excon::Response.new
          spot_requests = self.data[:spot_requests].values

          if id = Array(filters['spot-instance-request-id']).first
            spot_requests = spot_requests.select { |r| r['spotInstanceRequestId'] == id }
          end

          spot_requests.select { |r| r['instanceId'].nil? }.each do |request|
            run_instance_options = {
              'BlockDeviceMapping'    => request['launchSpecification']['blockDeviceMapping'],
              'EbsOptimized'          => request['launchSpecification']['ebsOptimized'],
              'KeyName'               => request['launchSpecification']['keyName'],
              'SecurityGroupId'       => request['launchSpecification']['groupSet'].first,
              'SpotInstanceRequestId' => request['spotInstanceRequestId'],
              'SubnetId'              => request['launchSpecification']['subnetId']
            }
            instances = run_instances(request['launchSpecification']['imageId'], 1,1, run_instance_options).body['instancesSet']

            request['instanceId'] = instances.first['instanceId']
            request['state'] = 'active'
            request['fault'] = {'code' => 'fulfilled', 'message' => 'Your Spot request is fulfilled.'}
            request['launchedAvailabilityZone'] = instances.first['placement']['availabilityZone']

            self.data[:spot_requests][request['spotInstanceRequestId']] = request
          end

          response.body = {'spotInstanceRequestSet' => spot_requests, 'requestId' => Fog::AWS::Mock.request_id}
          response
        end
      end
    end
  end
end