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/aws/requests/ec2/terminate_instances.rb

93 lines
3.1 KiB
Ruby
Raw Normal View History

2010-03-16 18:46:21 -04:00
module Fog
module AWS
module EC2
class Real
2010-03-16 18:46:21 -04:00
require 'fog/aws/parsers/ec2/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
def terminate_instances(instance_id)
params = AWS.indexed_param('InstanceId', instance_id)
request({
'Action' => 'TerminateInstances',
:parser => Fog::Parsers::AWS::EC2::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]
2010-03-16 18:46:21 -04:00
if (@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 = @data[:instances][id]
@data[:deleted_at][id] = Time.now
2009-09-12 14:29:55 -04:00
# TODO: the codes are mostly educated guessing, not certainty
code = case instance['state']
when 'pending'
0
when 'running'
16
when 'shutting-down'
32
when 'terminated'
64
2009-09-12 14:29:55 -04:00
when 'rebooting'
128
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
2009-09-12 14:29:55 -04:00
else
response.status = 400
2009-11-20 14:08:08 -05:00
raise(Excon::Errors.status_error({:expects => 200}, response))
end
response
end
end
end
end
end