1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/compute/requests/aws/terminate_instances.rb

97 lines
3.3 KiB
Ruby
Raw Normal View History

2010-03-16 18:46:21 -04:00
module Fog
module AWS
2010-09-08 17:40:02 -04:00
class Compute
2010-03-16 18:46:21 -04:00
class Real
2011-01-07 19:52:09 -05:00
require 'fog/compute/parsers/aws/terminate_instances'
2010-06-12 18:31:17 -04:00
# 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 = AWS.indexed_param('InstanceId', instance_id)
request({
'Action' => 'TerminateInstances',
:idempotent => true,
2010-09-08 17:40:02 -04:00
:parser => Fog::Parsers::AWS::Compute::TerminateInstances.new
}.merge!(params))
end
end
2010-03-16 18:46:21 -04:00
class Mock
def terminate_instances(instance_id)
2009-11-20 14:08:08 -05:00
response = Excon::Response.new
2009-09-12 14:29:55 -04:00
instance_id = [*instance_id]
2011-05-19 18:35:33 -04:00
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
2011-05-19 18:35:33 -04:00
instance = self.data[:instances][id]
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
2010-03-16 18:46:21 -04:00
state = { 'name' => 'shutting-down', 'code' => 32}
response.body['instancesSet'] << {
'instanceId' => id,
2010-03-16 18:46:21 -04:00
'previousState' => instance['instanceState'],
'currentState' => state
}
2010-03-16 18:46:21 -04:00
instance['instanceState'] = state
end
2010-04-01 13:51:35 -04:00
describe_addresses.body['addressesSet'].each do |address|
if instance_id.include?(address['instanceId'])
disassociate_address(address['publicIp'])
2010-04-01 13:51:35 -04:00
end
end
2010-04-01 13:51:35 -04:00
describe_volumes.body['volumeSet'].each do |volume|
if volume['attachmentSet'].first && instance_id.include?(volume['attachmentSet'].first['instanceId'])
detach_volume(volume['volumeId'])
2010-04-01 13:51:35 -04:00
end
end
response
2009-09-12 14:29:55 -04:00
else
2010-09-09 20:50:38 -04:00
raise Fog::AWS::Compute::NotFound.new("The instance ID '#{instance_id}' does not exist")
end
end
end
end
end
end