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

        # Terminate specified instances
        #
        # ==== Parameters
        # * instance_id<~Array> - Ids of instances to terminates
        #
        # ==== Returns
        # # * response<~Excon::Response>:
        #   * body<~Hash>:
        #     * 'requestId'<~String> - Id of request
        #     * 'instancesSet'<~Array>:
        #       * 'instanceId'<~String> - id of the terminated instance
        #       * 'previousState'<~Hash>: previous state of instance
        #         * 'code'<~Integer> - previous status code
        #         * 'name'<~String> - name of previous state
        #       * 'shutdownState'<~Hash>: shutdown state of instance
        #         * 'code'<~Integer> - current status code
        #         * 'name'<~String> - name of current state
        #
        # {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-TerminateInstances.html]
        def terminate_instances(instance_id)
          params = Fog::AWS.indexed_param('InstanceId', instance_id)
          request({
            'Action'    => 'TerminateInstances',
            :idempotent => true,
            :parser     => Fog::Parsers::AWS::Compute::TerminateInstances.new
          }.merge!(params))
        end
      end

      class Mock
        def terminate_instances(instance_id)
          response = Excon::Response.new
          instance_id = [*instance_id]
          if (self.data[:instances].keys & instance_id).length == instance_id.length
            response.body = {
              'requestId'     => Fog::AWS::Mock.request_id,
              'instancesSet'  => []
            }
            response.status = 200
            for id in instance_id
              instance = self.data[:instances][id]
              instance['classicLinkSecurityGroups'] = nil
              instance['classicLinkVpcId'] = nil
              self.data[:deleted_at][id] = Time.now
              code = case instance['instanceState']['name']
              when 'pending'
                0
              when 'running'
                16
              when 'shutting-down'
                32
              when 'terminated'
                48
              when 'stopping'
                64
              when 'stopped'
                80
              end
              state = { 'name' => 'shutting-down', 'code' => 32}
              response.body['instancesSet'] << {
                'instanceId'    => id,
                'previousState' => instance['instanceState'],
                'currentState'  => state
              }
              instance['instanceState'] = state
            end

            describe_addresses.body['addressesSet'].each do |address|
              if instance_id.include?(address['instanceId'])
                disassociate_address(address['publicIp'], address['associationId'])
              end
            end

            describe_volumes.body['volumeSet'].each do |volume|
              if volume['attachmentSet'].first && instance_id.include?(volume['attachmentSet'].first['instanceId'])
                detach_volume(volume['volumeId'])
              end
            end

            response
          else
            raise Fog::AWS::Compute::NotFound.new("The instance ID '#{instance_id}' does not exist")
          end
        end
      end
    end
  end
end