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

first pass at mocking volumes

This commit is contained in:
Wesley Beary 2009-08-15 15:19:07 -07:00
parent 7d70746c23
commit 7e89914f1f
5 changed files with 197 additions and 78 deletions

View file

@ -54,7 +54,12 @@ module Fog
request_id.join('-')
end
def self.snapshot_id
"snap-#{hex(8)}"
end
def self.volume_id
"vol-#{hex(8)}"
end
private

View file

@ -175,7 +175,7 @@ module Fog
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
if Fog.mocking?
@data = { 'addressesSet' => [] }
@data = { :deleted_at => {}, 'addressesSet' => [], 'volumeSet' => [] }
end
end

View file

@ -1,32 +1,65 @@
module Fog
module AWS
class EC2
unless Fog.mocking?
module Fog
module AWS
class EC2
# Create an EBS volume
#
# ==== Parameters
# * availability_zone<~String> - availability zone to create volume in
# * size<~Integer> - Size in GiBs for volume. Must be between 1 and 1024.
# * snapshot_id<~String> - Optional, snapshot to create volume from
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'availabilityZone'<~String> - Availability zone for volume
# * 'createTime'<~Time> - Timestamp for creation
# * 'size'<~Integer> - Size in GiBs for volume
# * 'snapshotId'<~String> - Snapshot volume was created from, if any
# * 'status's<~String> - State of volume
# * 'volumeId'<~String> - Reference to volume
def create_volume(availability_zone, size, snapshot_id = nil)
request({
'Action' => 'CreateVolume',
'AvailabilityZone' => availability_zone,
'Size' => size,
'SnapshotId' => snapshot_id
}, Fog::Parsers::AWS::EC2::CreateVolume.new)
end
# Create an EBS volume
#
# ==== Parameters
# * availability_zone<~String> - availability zone to create volume in
# * size<~Integer> - Size in GiBs for volume. Must be between 1 and 1024.
# * snapshot_id<~String> - Optional, snapshot to create volume from
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'volumeId'<~String> - Reference to volume
# * 'size'<~Integer> - Size in GiBs for volume
# * 'status's<~String> - State of volume
# * 'createTime'<~Time> - Timestamp for creation
# * 'availabilityZone'<~String> - Availability zone for volume
# * 'snapshotId'<~String> - Snapshot volume was created from, if any
def create_volume(availability_zone, size, snapshot_id = nil)
request({
'Action' => 'CreateVolume',
'AvailabilityZone' => availability_zone,
'Size' => size,
'SnapshotId' => snapshot_id
}, Fog::Parsers::AWS::EC2::CreateVolume.new)
end
end
end
end
else
module Fog
module AWS
class EC2
def create_volume(availability_zone, size, snapshot_id = nil)
response = Fog::Response.new
response.status = 200
data = {
'availabilityZone' => availability_zone,
'attachmentSet' => [],
'createTime' => Time.now,
'size' => size,
'snapshotId' => snapshot_id || '',
'status' => 'creating',
'volumeId' => Fog::AWS::Mock.volume_id
}
@data['volumeSet'] << data
response.body = {
'requestId' => Fog::AWS::Mock.request_id
}.merge!(data.reject {|key,value| !['availabilityZone','createTime','size','snapshotId','status','volumeId'].include?(key) })
response
end
end
end
end
end

View file

@ -1,24 +1,56 @@
module Fog
module AWS
class EC2
unless Fog.mocking?
module Fog
module AWS
class EC2
# Delete an EBS volume
#
# ==== Parameters
# * volume_id<~String> - Id of volume to delete.
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'return'<~Boolean> - success?
def delete_volume(volume_id)
request({
'Action' => 'DeleteVolume',
'VolumeId' => volume_id
}, Fog::Parsers::AWS::EC2::Basic.new)
end
# Delete an EBS volume
#
# ==== Parameters
# * volume_id<~String> - Id of volume to delete.
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'requestId'<~String> - Id of request
# * 'return'<~Boolean> - success?
def delete_volume(volume_id)
request({
'Action' => 'DeleteVolume',
'VolumeId' => volume_id
}, Fog::Parsers::AWS::EC2::Basic.new)
end
end
end
else
module Fog
module AWS
class EC2
def delete_volume(volume_id)
response = Fog::Response.new
initial_volumes_count = @data['volumeSet'].length
volume = @data['volumeSet'].select {|volume| volume['volumeId'] == volume_id}.first
if volume
@data[:deleted_at][volume['volumeId']] = Time.now
volume['status'] = 'deleting'
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'return' => true
}
else
response.status = 400
end
response
end
end
end
end
end

View file

@ -1,35 +1,84 @@
module Fog
module AWS
class EC2
unless Fog.mocking?
module Fog
module AWS
class EC2
# Describe all or specified volumes.
#
# ==== Parameters
# * volume_id<~Array> - List of volumes to describe, defaults to all
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'volumeSet'<~Array>:
# * 'availabilityZone'<~String> - Availability zone for volume
# * 'createTime'<~Time> - Timestamp for creation
# * 'size'<~Integer> - Size in GiBs for volume
# * 'snapshotId'<~String> - Snapshot volume was created from, if any
# * 'status'<~String> - State of volume
# * 'volumeId'<~String> - Reference to volume
# * 'attachmentSet'<~Array>:
# * 'attachmentTime'<~Time> - Timestamp for attachment
# * 'device'<~String> - How value is exposed to instance
# * 'instanceId'<~String> - Reference to attached instance
# * 'status'<~String> - Attachment state
# * 'volumeId'<~String> - Reference to volume
def describe_volumes(volume_id = [])
params = indexed_params('VolumeId', volume_id)
request({
'Action' => 'DescribeVolumes'
}.merge!(params), Fog::Parsers::AWS::EC2::DescribeVolumes.new)
end
# Describe all or specified volumes.
#
# ==== Parameters
# * volume_ids<~Array> - List of volumes to describe, defaults to all
#
# ==== Returns
# * response<~Fog::AWS::Response>:
# * body<~Hash>:
# * 'volumeSet'<~Array>:
# * 'volumeId'<~String> - Reference to volume
# * 'size'<~Integer> - Size in GiBs for volume
# * 'status'<~String> - State of volume
# * 'createTime'<~Time> - Timestamp for creation
# * 'availabilityZone'<~String> - Availability zone for volume
# * 'snapshotId'<~String> - Snapshot volume was created from, if any
# * 'attachmentSet'<~Array>:
# * 'attachmentTime'<~Time> - Timestamp for attachment
# * 'device'<~String> - How value is exposed to instance
# * 'instanceId'<~String> - Reference to attached instance
# * 'status'<~String> - Attachment state
# * 'volumeId'<~String> - Reference to volume
def describe_volumes(volume_ids = [])
params = indexed_params('VolumeId', volume_ids)
request({
'Action' => 'DescribeVolumes'
}.merge!(params), Fog::Parsers::AWS::EC2::DescribeVolumes.new)
end
end
end
else
module Fog
module AWS
class EC2
def describe_volumes(volume_id = [])
volume_id = [*volume_id]
response = Fog::Response.new
if volume_id != []
volume_set = @data['volumeSet'].select {|volume| volume_id.include?(volume['volumeId'])}
else
volume_set = @data['volumeSet']
end
volume_set.each do |volume|
case volume['status']
when 'creating'
if Time.now - volume['createTime'] > 2
volume['status'] = 'available'
end
when 'deleting'
if Time.now - @data[:deleted_at][volume['volumeId']] > 2
@data[:deleted_at].delete(volume['volumeId'])
volume_set.delete(volume)
end
end
end
if volume_id.length == 0 || volume_id.length == volume_set.length
response.status = 200
response.body = {
'requestId' => Fog::AWS::Mock.request_id,
'volumeSet' => volume_set
}
else
response.status = 400
end
response
end
end
end
end
end