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
|
2009-08-22 01:04:12 -04:00
|
|
|
|
2010-09-08 17:40:02 -04:00
|
|
|
require 'fog/aws/parsers/compute/terminate_instances'
|
2010-06-12 18:31:17 -04:00
|
|
|
|
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-05-24 17:22:35 -04:00
|
|
|
'Action' => 'TerminateInstances',
|
|
|
|
:idempotent => true,
|
2010-09-08 17:40:02 -04:00
|
|
|
:parser => Fog::Parsers::AWS::Compute::TerminateInstances.new
|
2010-03-16 01:15:33 -04:00
|
|
|
}.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
|
2010-04-09 22:21:59 -04:00
|
|
|
code = case instance['instanceState']['name']
|
2009-08-23 14:01:37 -04:00
|
|
|
when 'pending'
|
|
|
|
0
|
|
|
|
when 'running'
|
|
|
|
16
|
|
|
|
when 'shutting-down'
|
|
|
|
32
|
|
|
|
when 'terminated'
|
2010-07-23 00:57:34 -04:00
|
|
|
48
|
|
|
|
when 'stopping'
|
2009-08-23 14:01:37 -04:00
|
|
|
64
|
2010-07-23 00:57:34 -04:00
|
|
|
when 'stopped'
|
|
|
|
80
|
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
|
2010-05-26 01:26:20 -04:00
|
|
|
|
|
|
|
response
|
2009-09-12 14:29:55 -04:00
|
|
|
else
|
2010-05-26 21:03:22 -04:00
|
|
|
raise Fog::AWS::EC2::NotFound.new("The instance ID '#{instance_id}' does not exist")
|
2009-08-22 01:04:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-07-17 03:36:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|