2010-03-16 18:46:21 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
module EC2
|
|
|
|
class Real
|
2009-08-23 14:01:37 -04:00
|
|
|
|
|
|
|
# Describe all or specified instances
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * instance_id<~Array> - List of instance ids to describe, defaults to all
|
|
|
|
#
|
|
|
|
# ==== Returns
|
2009-11-02 21:48:49 -05:00
|
|
|
# * response<~Excon::Response>:
|
2009-08-23 14:01:37 -04:00
|
|
|
# * body<~Hash>:
|
|
|
|
# * 'requestId'<~String> - Id of request
|
|
|
|
# * 'reservationSet'<~Array>:
|
|
|
|
# * 'groupSet'<~Array> - Group names for reservation
|
|
|
|
# * 'ownerId'<~String> - AWS Access Key ID of reservation owner
|
|
|
|
# * 'reservationId'<~String> - Id of the reservation
|
|
|
|
# * 'instancesSet'<~Array>:
|
|
|
|
# * instance<~Hash>:
|
2010-02-20 16:12:04 -05:00
|
|
|
# * 'architecture'<~String> - architecture of image in [i386, x86_64]
|
2009-08-23 14:01:37 -04:00
|
|
|
# * 'amiLaunchIndex'<~Integer> - reference to instance in launch group
|
2010-02-20 16:12:04 -05:00
|
|
|
# * 'blockDeviceMapping'<~Array>
|
|
|
|
# * 'attachTime'<~Time> - time of volume attachment
|
|
|
|
# * 'deleteOnTermination'<~Boolean> - whether or not to delete volume on termination
|
|
|
|
# * 'deviceName'<~String> - specifies how volume is exposed to instance
|
|
|
|
# * 'status'<~String> - status of attached volume
|
|
|
|
# * 'volumeId'<~String> - Id of attached volume
|
2009-08-23 14:01:37 -04:00
|
|
|
# * 'dnsName'<~String> - public dns name, blank until instance is running
|
|
|
|
# * 'imageId'<~String> - image id of ami used to launch instance
|
|
|
|
# * 'instanceId'<~String> - id of the instance
|
|
|
|
# * 'instanceState'<~Hash>:
|
|
|
|
# * 'code'<~Integer> - current status code
|
|
|
|
# * 'name'<~String> - current status name
|
|
|
|
# * 'instanceType'<~String> - type of instance
|
2010-02-20 16:12:04 -05:00
|
|
|
# * 'ipAddress'<~String> - public ip address assigned to instance
|
|
|
|
# * 'kernelId'<~String> - id of kernel used to launch instance
|
2009-08-23 14:01:37 -04:00
|
|
|
# * 'keyName'<~String> - name of key used launch instances or blank
|
|
|
|
# * 'launchTime'<~Time> - time instance was launched
|
|
|
|
# * 'monitoring'<~Hash>:
|
|
|
|
# * 'state'<~Boolean - state of monitoring
|
|
|
|
# * 'placement'<~Hash>:
|
|
|
|
# * 'availabilityZone'<~String> - Availability zone of the instance
|
|
|
|
# * 'productCodes'<~Array> - Product codes for the instance
|
|
|
|
# * 'privateDnsName'<~String> - private dns name, blank until instance is running
|
2010-02-20 16:12:04 -05:00
|
|
|
# * 'privateIpAddress'<~String> - private ip address assigned to instance
|
|
|
|
# * 'rootDeviceName'<~String> - specifies how the root device is exposed to the instance
|
|
|
|
# * 'rootDeviceType'<~String> - root device type used by AMI in [ebs, instance-store]
|
2009-08-23 14:01:37 -04:00
|
|
|
# * 'ramdiskId'<~String> - Id of ramdisk used to launch instance
|
|
|
|
# * 'reason'<~String> - reason for most recent state transition, or blank
|
|
|
|
def describe_instances(instance_id = [])
|
2010-01-31 17:07:26 -05:00
|
|
|
params = AWS.indexed_param('InstanceId', instance_id)
|
2009-08-23 14:01:37 -04:00
|
|
|
request({
|
2010-05-17 14:30:11 -04:00
|
|
|
'Action' => 'DescribeInstances',
|
2010-05-17 23:45:59 -04:00
|
|
|
:idempotent => true,
|
2010-05-17 14:30:11 -04:00
|
|
|
:parser => Fog::Parsers::AWS::EC2::DescribeInstances.new
|
2010-03-16 01:15:33 -04:00
|
|
|
}.merge!(params))
|
2009-08-23 14:01:37 -04:00
|
|
|
end
|
|
|
|
|
2009-07-13 22:14:59 -04:00
|
|
|
end
|
2009-08-23 14:01:37 -04:00
|
|
|
|
2010-03-16 18:46:21 -04:00
|
|
|
class Mock
|
2009-07-13 22:14:59 -04:00
|
|
|
|
2010-04-07 23:53:59 -04:00
|
|
|
def describe_instances(instance_id = [])
|
2009-11-20 14:08:08 -05:00
|
|
|
response = Excon::Response.new
|
2009-08-23 14:01:37 -04:00
|
|
|
instance_id = [*instance_id]
|
|
|
|
if instance_id != []
|
2010-03-16 18:46:21 -04:00
|
|
|
instance_set = @data[:instances].reject {|key,value| !instance_id.include?(key)}.values
|
2009-08-23 14:01:37 -04:00
|
|
|
else
|
2010-03-16 18:46:21 -04:00
|
|
|
instance_set = @data[:instances].values
|
2009-08-23 14:01:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if instance_id.length == 0 || instance_id.length == instance_set.length
|
|
|
|
response.status = 200
|
|
|
|
reservation_set = {}
|
2010-04-13 17:42:16 -04:00
|
|
|
|
2009-08-23 14:01:37 -04:00
|
|
|
instance_set.each do |instance|
|
2010-04-13 17:42:16 -04:00
|
|
|
case instance['instanceState']['name']
|
|
|
|
when 'pending'
|
|
|
|
if Time.now - instance['launchTime'] > Fog::Mock.delay
|
2010-05-13 16:20:47 -04:00
|
|
|
instance['ipAddress'] = Fog::AWS::Mock.ip_address
|
2010-05-13 16:59:33 -04:00
|
|
|
instance['dnsName'] = Fog::AWS::Mock.dns_name_for(instance['ipAddress'])
|
2010-04-13 17:42:16 -04:00
|
|
|
instance['privateIpAddress'] = Fog::AWS::Mock.ip_address
|
2010-05-13 16:59:33 -04:00
|
|
|
instance['privateDnsName'] = Fog::AWS::Mock.private_dns_name_for(instance['privateIpAddress'])
|
2010-04-13 17:42:16 -04:00
|
|
|
instance['instanceState'] = { 'code' => 16, 'name' => 'running' }
|
|
|
|
end
|
|
|
|
when 'rebooting'
|
|
|
|
instance['instanceState'] = { 'code' => 16, 'name' => 'running' }
|
|
|
|
when 'shutting-down'
|
|
|
|
if Time.now - @data[:deleted_at][instance['instanceId']] > Fog::Mock.delay * 2
|
|
|
|
@data[:deleted_at].delete(instance['instanceId'])
|
|
|
|
@data[:instances].delete(instance['instanceId'])
|
|
|
|
elsif Time.now - @data[:deleted_at][instance['instanceId']] > Fog::Mock.delay
|
|
|
|
instance['instanceState'] = { 'code' => 16, 'name' => 'terminating' }
|
|
|
|
end
|
|
|
|
when 'terminating'
|
|
|
|
if Time.now - @data[:deleted_at][instance['instanceId']] > Fog::Mock.delay
|
|
|
|
@data[:deleted_at].delete(instance['instanceId'])
|
|
|
|
@data[:instances].delete(instance['instanceId'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @data[:instances][instance['instanceId']]
|
|
|
|
reservation_set[instance['reservationId']] ||= {
|
|
|
|
'groupSet' => instance['groupSet'],
|
|
|
|
'instancesSet' => [],
|
|
|
|
'ownerId' => instance['ownerId'],
|
|
|
|
'reservationId' => instance['reservationId']
|
|
|
|
}
|
2010-05-27 14:27:08 -04:00
|
|
|
reservation_set[instance['reservationId']]['instancesSet'] << instance.reject{|key,value| !['amiLaunchIndex', 'architecture', 'blockDeviceMapping', 'dnsName', 'imageId', 'instanceId', 'instanceState', 'instanceType', 'ipAddress', 'kernelId', 'keyName', 'launchTime', 'monitoring', 'placement', 'privateDnsName', 'privateIpAddress', 'productCodes', 'ramdiskId', 'reason', 'rootDeviceType'].include?(key)}
|
2010-04-13 17:42:16 -04:00
|
|
|
end
|
2009-08-23 14:01:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
response.body = {
|
|
|
|
'requestId' => Fog::AWS::Mock.request_id,
|
|
|
|
'reservationSet' => reservation_set.values
|
|
|
|
}
|
2010-05-26 01:26:20 -04:00
|
|
|
response
|
2009-08-23 14:01:37 -04:00
|
|
|
else
|
2010-05-26 21:03:22 -04:00
|
|
|
raise Fog::AWS::EC2::NotFound.new("The instance ID #{instance_id.inspect} does not exist")
|
2009-08-23 14:01:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-07-13 22:14:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|