1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

mock attach/detach volume

This commit is contained in:
Wesley Beary 2009-08-25 19:49:55 -07:00
parent e438e1b318
commit 8a2ba3ca4e
4 changed files with 155 additions and 58 deletions

View file

@ -1,32 +1,71 @@
module Fog
module AWS
class EC2
unless Fog.mocking?
module Fog
module AWS
class EC2
# 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
# * response<~Fog::AWS::Response>:
# * 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
def attach_volume(instance_id, volume_id, device)
request({
'Action' => 'AttachVolume',
'VolumeId' => volume_id,
'InstanceId' => instance_id,
'Device' => device
}, Fog::Parsers::AWS::EC2::AttachVolume.new)
end
# Attach an Amazon EBS volume with a running instance, exposing as specified device
#
# ==== Parameters
# * volume_id<~String> - Id of amazon EBS volume to associate with instance
# * instance_id<~String> - Id of instance to associate volume with
# * device<~String> - Specifies how the device is exposed to the instance (e.g. "/dev/sdh")
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * 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
def attach_volume(volume_id, instance_id, device)
request({
'Action' => 'AttachVolume',
'VolumeId' => volume_id,
'InstanceId' => instance_id,
'Device' => device
}, Fog::Parsers::AWS::EC2::AttachVolume.new)
end
end
end
else
module Fog
module AWS
class EC2
def attach_volume(instance_id, volume_id, device)
response = Fog::Response.new
response.status = 200
instance = Fog::AWS::EC2.data[:instances][instance_id]
volume = Fog::AWS::EC2.data[:volumes][volume_id]
if instance && volume
data = {
'attachTime' => Time.now,
'device' => device,
'instanceId' => instance_id,
'status' => 'attaching',
'volumeId' => volume_id
}
volume['attachmentSet'] << data
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id
}.merge!(data)
else
response.status = 400
raise(Fog::Errors.status_error(200, 400, response))
end
response
end
end
end
end
end

View file

@ -1,32 +1,62 @@
module Fog
module AWS
class EC2
unless Fog.mocking?
module Fog
module AWS
class EC2
# Detach an Amazon EBS volume from a running instance
#
# ==== Parameters
# * volume_id<~String> - Id of amazon EBS volume to associate with instance
# * options<~Hash>:
# * 'Device'<~String> - Specifies how the device is exposed to the instance (e.g. "/dev/sdh")
# * 'Force'<~Boolean> - If true forces detach, can cause data loss/corruption
# * 'InstanceId'<~String> - Id of instance to associate volume with
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * 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
def detach_volume(volume_id, options = {})
request({
'Action' => 'DetachVolume',
'VolumeId' => volume_id
}.merge!(options), Fog::Parsers::AWS::EC2::DetachVolume.new)
end
# Detach an Amazon EBS volume from a running instance
#
# ==== Parameters
# * volume_id<~String> - Id of amazon EBS volume to associate with instance
# * options<~Hash>:
# * 'Device'<~String> - Specifies how the device is exposed to the instance (e.g. "/dev/sdh")
# * 'Force'<~Boolean> - If true forces detach, can cause data loss/corruption
# * 'InstanceId'<~String> - Id of instance to associate volume with
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * 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
def detach_volume(volume_id, options = {})
request({
'Action' => 'DetachVolume',
'VolumeId' => volume_id
}.merge!(options), Fog::Parsers::AWS::EC2::DetachVolume.new)
end
end
end
end
else
module Fog
module AWS
class EC2
def detach_volume(volume_id, options = {})
response = Fog::Response.new
response.status = 200
if volume = Fog::AWS::EC2.data[:volumes][volume_id]
data = volume['attachmentSet'].pop
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id
}.merge!(data)
else
response.status = 400
raise(Fog::Errors.status_error(200, 400, response))
end
response
end
end
end
end
end

View file

@ -14,13 +14,13 @@ describe 'EC2.attach_volume' do
end
eventually do
ec2.delete_volume(@volume_id)
ec2.terminate_instances([@instance_id])
ec2.terminate_instances(@instance_id)
end
end
it "should return proper attributes" do
eventually(128) do
actual = ec2.attach_volume(@volume_id, @instance_id, '/dev/sdh')
actual = ec2.attach_volume(@instance_id, @volume_id, '/dev/sdh')
actual.body['attachTime'].should be_a(Time)
actual.body['device'].should be_a(String)
actual.body['instanceId'].should be_a(String)
@ -31,4 +31,23 @@ describe 'EC2.attach_volume' do
end
end
describe 'failure' do
it "should raise a BadRequest error if the instance does not exist" do
@volume_id = ec2.create_volume('us-east-1a', 1).body['volumeId']
lambda {
ec2.attach_volume('i-00000000', @volume_id, '/dev/sdh')
}.should raise_error(Fog::Errors::BadRequest)
ec2.delete_volume(@volume_id)
end
it "should raise a BadRequest error if the address does not exist" do
@instance_id = ec2.run_instances('ami-5ee70037', 1, 1).body['instancesSet'].first['instanceId']
lambda {
ec2.attach_volume(@instance_id, 'vol-00000000', '/dev/sdh')
}.should raise_error(Fog::Errors::BadRequest)
ec2.terminate_instances(@instance_id)
end
end
end

View file

@ -7,7 +7,7 @@ describe 'EC2.detach_volume' do
@instance_id = ec2.run_instances('ami-5ee70037', 1, 1, {'Placement.AvailabilityZone' => 'us-east-1a'}).body['instancesSet'].first['instanceId']
@volume_id = ec2.create_volume('us-east-1a', 1).body['volumeId']
eventually(128) do
ec2.attach_volume(@volume_id, @instance_id, '/dev/sdh')
ec2.attach_volume(@instance_id, @volume_id, '/dev/sdh')
end
end
@ -31,4 +31,13 @@ describe 'EC2.detach_volume' do
end
end
describe 'failure' do
it "should raise a BadRequest error if the volume does not exist" do
lambda {
ec2.detach_volume('vol-00000000')
}.should raise_error(Fog::Errors::BadRequest)
end
end
end