2010-03-16 15:46:21 -07:00
|
|
|
module Fog
|
2011-06-16 16:28:54 -07:00
|
|
|
module Compute
|
|
|
|
class AWS
|
2010-03-16 15:46:21 -07:00
|
|
|
class Real
|
2009-08-25 19:49:55 -07:00
|
|
|
|
2011-08-24 20:37:00 -05:00
|
|
|
require 'fog/aws/parsers/compute/attach_volume'
|
2010-06-12 15:31:17 -07:00
|
|
|
|
2009-08-25 19:49:55 -07:00
|
|
|
# Attach an Amazon EBS volume with a running instance, exposing as specified device
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * instance_id<~String> - Id of instance to associate volume with
|
|
|
|
# * volume_id<~String> - Id of amazon EBS volume to associate with instance
|
|
|
|
# * device<~String> - Specifies how the device is exposed to the instance (e.g. "/dev/sdh")
|
|
|
|
#
|
|
|
|
# ==== Returns
|
2009-11-02 18:48:49 -08:00
|
|
|
# * response<~Excon::Response>:
|
2009-08-25 19:49:55 -07:00
|
|
|
# * body<~Hash>:
|
|
|
|
# * 'attachTime'<~Time> - Time of attachment was initiated at
|
|
|
|
# * 'device'<~String> - Device as it is exposed to the instance
|
|
|
|
# * 'instanceId'<~String> - Id of instance for volume
|
|
|
|
# * 'requestId'<~String> - Id of request
|
|
|
|
# * 'status'<~String> - Status of volume
|
|
|
|
# * 'volumeId'<~String> - Reference to volume
|
2011-05-19 09:31:56 -07:00
|
|
|
#
|
|
|
|
# {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-AttachVolume.html]
|
2009-08-25 19:49:55 -07:00
|
|
|
def attach_volume(instance_id, volume_id, device)
|
2010-03-15 22:15:33 -07:00
|
|
|
request(
|
|
|
|
'Action' => 'AttachVolume',
|
|
|
|
'VolumeId' => volume_id,
|
|
|
|
'InstanceId' => instance_id,
|
|
|
|
'Device' => device,
|
2010-05-24 14:22:35 -07:00
|
|
|
:idempotent => true,
|
2011-06-16 16:28:54 -07:00
|
|
|
:parser => Fog::Parsers::Compute::AWS::AttachVolume.new
|
2010-03-15 22:15:33 -07:00
|
|
|
)
|
2009-08-25 19:49:55 -07:00
|
|
|
end
|
|
|
|
|
2009-07-26 19:22:27 -07:00
|
|
|
end
|
|
|
|
|
2010-03-16 15:46:21 -07:00
|
|
|
class Mock
|
2009-08-25 19:49:55 -07:00
|
|
|
|
|
|
|
def attach_volume(instance_id, volume_id, device)
|
2009-11-20 11:08:08 -08:00
|
|
|
response = Excon::Response.new
|
2009-10-22 20:46:46 -07:00
|
|
|
if instance_id && volume_id && device
|
2009-08-25 19:49:55 -07:00
|
|
|
response.status = 200
|
2011-05-19 15:35:33 -07:00
|
|
|
instance = self.data[:instances][instance_id]
|
|
|
|
volume = self.data[:volumes][volume_id]
|
2009-10-22 20:46:46 -07:00
|
|
|
if instance && volume
|
2011-03-22 11:17:02 -07:00
|
|
|
unless volume['status'] == 'available'
|
2011-06-16 16:28:54 -07:00
|
|
|
raise Fog::Compute::AWS::Error.new("Client.VolumeInUse => Volume #{volume_id} is unavailable")
|
2011-03-22 11:17:02 -07:00
|
|
|
end
|
2011-03-21 23:31:15 -04:00
|
|
|
|
2009-10-22 20:46:46 -07:00
|
|
|
data = {
|
|
|
|
'attachTime' => Time.now,
|
|
|
|
'device' => device,
|
|
|
|
'instanceId' => instance_id,
|
|
|
|
'status' => 'attaching',
|
|
|
|
'volumeId' => volume_id
|
|
|
|
}
|
2010-03-31 21:28:32 -07:00
|
|
|
volume['attachmentSet'] = [data]
|
2010-04-01 14:07:26 -07:00
|
|
|
volume['status'] = 'attaching'
|
2009-10-22 20:46:46 -07:00
|
|
|
response.status = 200
|
|
|
|
response.body = {
|
|
|
|
'requestId' => Fog::AWS::Mock.request_id
|
|
|
|
}.merge!(data)
|
2010-05-25 22:26:20 -07:00
|
|
|
response
|
|
|
|
elsif !instance
|
2011-06-16 16:28:54 -07:00
|
|
|
raise Fog::Compute::AWS::NotFound.new("The instance ID '#{instance_id}' does not exist.")
|
2010-05-25 22:26:20 -07:00
|
|
|
elsif !volume
|
2011-06-16 16:28:54 -07:00
|
|
|
raise Fog::Compute::AWS::NotFound.new("The volume '#{volume_id}' does not exist.")
|
2009-10-22 20:46:46 -07:00
|
|
|
end
|
2009-08-25 19:49:55 -07:00
|
|
|
else
|
2010-05-25 22:26:20 -07:00
|
|
|
message = 'MissingParameter => '
|
2009-10-22 20:46:46 -07:00
|
|
|
if !instance_id
|
2010-05-25 22:26:20 -07:00
|
|
|
message << 'The request must contain the parameter instance_id'
|
2009-10-22 20:46:46 -07:00
|
|
|
elsif !volume_id
|
2010-05-25 22:26:20 -07:00
|
|
|
message << 'The request must contain the parameter volume_id'
|
2009-10-22 20:46:46 -07:00
|
|
|
else
|
2010-05-25 22:26:20 -07:00
|
|
|
message << 'The request must contain the parameter device'
|
2009-10-22 20:46:46 -07:00
|
|
|
end
|
2011-06-16 16:28:54 -07:00
|
|
|
raise Fog::Compute::AWS::Error.new(message)
|
2009-08-25 19:49:55 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2009-07-26 19:22:27 -07:00
|
|
|
end
|
|
|
|
end
|
2010-03-16 15:46:21 -07:00
|
|
|
end
|