2010-03-16 18:46:21 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
module EC2
|
|
|
|
class Real
|
2009-08-22 01:04:12 -04:00
|
|
|
|
2010-03-16 18:46:21 -04:00
|
|
|
require 'fog/aws/parsers/ec2/terminate_instances'
|
2009-08-22 01:04:12 -04:00
|
|
|
|
|
|
|
# Terminate specified instances
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * instance_id<~Array> - Ids of instances to terminates
|
|
|
|
#
|
|
|
|
# ==== Returns
|
2009-11-02 21:48:49 -05:00
|
|
|
# # * response<~Excon::Response>:
|
2009-08-22 01:04:12 -04:00
|
|
|
# * 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)
|
2010-01-31 17:07:26 -05:00
|
|
|
params = AWS.indexed_param('InstanceId', instance_id)
|
2009-08-22 01:04:12 -04:00
|
|
|
request({
|
2010-03-16 01:15:33 -04:00
|
|
|
'Action' => 'TerminateInstances',
|
|
|
|
:parser => Fog::Parsers::AWS::EC2::TerminateInstances.new
|
|
|
|
}.merge!(params))
|
2009-08-22 01:04:12 -04:00
|
|
|
end
|
|
|
|
|
2009-07-17 03:36:42 -04:00
|
|
|
end
|
|
|
|
|
2010-03-16 18:46:21 -04:00
|
|
|
class Mock
|
2009-08-22 01:04:12 -04:00
|
|
|
|
|
|
|
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
|
2010-04-09 16:37:45 -04:00
|
|
|
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
|
2009-08-23 14:01:37 -04:00
|
|
|
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
|
2009-08-23 14:01:37 -04:00
|
|
|
end
|
2010-03-16 18:46:21 -04:00
|
|
|
state = { 'name' => 'shutting-down', 'code' => 32}
|
2009-08-22 01:04:12 -04:00
|
|
|
response.body['instancesSet'] << {
|
2010-04-09 16:37:45 -04:00
|
|
|
'instanceId' => id,
|
2010-03-16 18:46:21 -04:00
|
|
|
'previousState' => instance['instanceState'],
|
|
|
|
'currentState' => state
|
2009-08-22 01:04:12 -04:00
|
|
|
}
|
2010-03-16 18:46:21 -04:00
|
|
|
instance['instanceState'] = state
|
2010-04-09 16:37:45 -04:00
|
|
|
end
|
2010-04-01 13:51:35 -04:00
|
|
|
|
2010-04-09 16:37:45 -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
|
2010-04-09 16:37:45 -04:00
|
|
|
end
|
2010-04-01 13:51:35 -04:00
|
|
|
|
2010-04-09 16:37:45 -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
|
2009-08-22 01:04:12 -04:00
|
|
|
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))
|
2009-08-22 01:04:12 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-07-17 03:36:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|