2010-03-16 18:46:21 -04:00
module Fog
2011-06-16 19:28:54 -04:00
module Compute
class AWS
2010-03-16 18:46:21 -04:00
class Real
2009-08-15 18:19:07 -04:00
2011-08-24 21:37:00 -04:00
require 'fog/aws/parsers/compute/create_volume'
2010-06-12 18:31:17 -04:00
2009-08-15 18:19:07 -04:00
# 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.
2012-08-01 09:56:22 -04:00
# * options<~Hash>
# * 'SnapshotId'<~String> - Optional, snapshot to create volume from
# * 'VolumeType'<~String> - Optional, volume type. standard or io1, default is standard.
2013-07-03 07:37:11 -04:00
# * 'Iops'<~Integer> - Number of IOPS the volume supports. Required if VolumeType is io1, must be between 1 and 4000.
2009-08-15 18:19:07 -04:00
#
# ==== Returns
2009-11-02 21:48:49 -05:00
# * response<~Excon::Response>:
2009-08-15 18:19:07 -04:00
# * 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
2012-08-01 09:56:22 -04:00
# * 'volumeType'<~String> - Type of volume
# * 'iops'<~Integer> - Number of IOPS the volume supports
2011-05-19 12:31:56 -04:00
#
# {Amazon API Reference}[http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-CreateVolume.html]
2012-08-01 09:56:22 -04:00
def create_volume ( availability_zone , size , options = { } )
unless options . is_a? ( Hash )
Fog :: Logger . deprecation ( " create_volume with a bare snapshot_id is deprecated, use create_volume(availability_zone, size, 'SnapshotId' => snapshot_id) instead [light_black]( #{ caller . first } )[/] " )
options = { 'SnapshotId' = > options }
end
request ( {
2010-03-16 01:15:33 -04:00
'Action' = > 'CreateVolume' ,
'AvailabilityZone' = > availability_zone ,
'Size' = > size ,
2011-06-16 19:28:54 -04:00
:parser = > Fog :: Parsers :: Compute :: AWS :: CreateVolume . new
2012-08-01 09:56:22 -04:00
} . merge ( options ) )
2009-08-15 18:19:07 -04:00
end
2009-07-13 22:14:59 -04:00
end
2010-03-16 18:46:21 -04:00
class Mock
2009-08-15 18:19:07 -04:00
2012-08-01 09:56:22 -04:00
def create_volume ( availability_zone , size , options = { } )
unless options . is_a? ( Hash )
Fog :: Logger . deprecation ( " create_volume with a bare snapshot_id is deprecated, use create_volume(availability_zone, size, 'SnapshotId' => snapshot_id) instead [light_black]( #{ caller . first } )[/] " )
options = { 'SnapshotId' = > options }
end
2009-11-20 14:08:08 -05:00
response = Excon :: Response . new
2012-08-01 09:56:22 -04:00
if availability_zone && ( size || options [ 'SnapshotId' ] )
snapshot = self . data [ :snapshots ] [ options [ 'SnapshotId' ] ]
if options [ 'SnapshotId' ] && ! snapshot
raise Fog :: Compute :: AWS :: NotFound . new ( " The snapshot ' #{ options [ 'SnapshotId' ] } ' does not exist. " )
2011-03-22 14:17:02 -04:00
end
2011-03-21 23:43:16 -04:00
2011-08-15 01:22:37 -04:00
if snapshot && size && size < snapshot [ 'volumeSize' ]
2012-08-01 09:56:22 -04:00
raise Fog :: Compute :: AWS :: NotFound . new ( " The snapshot ' #{ options [ 'SnapshotId' ] } ' has size #{ snapshot [ 'volumeSize' ] } which is greater than #{ size } . " )
2011-06-20 18:22:57 -04:00
elsif snapshot && ! size
size = snapshot [ 'volumeSize' ]
end
2012-08-01 09:56:22 -04:00
if options [ 'VolumeType' ] == 'io1'
2012-08-17 18:56:26 -04:00
iops = options [ 'Iops' ]
if ! iops
2012-08-01 09:56:22 -04:00
raise Fog :: Compute :: AWS :: Error . new ( " InvalidParameterCombination => The parameter iops must be specified for io1 volumes. " )
end
if size < 10
raise Fog :: Compute :: AWS :: Error . new ( " InvalidParameterValue => Volume of #{ size } GiB is too small; minimum is 10GiB. " )
end
2012-08-17 18:56:26 -04:00
if ( iops_to_size_ratio = iops . to_f / size . to_f ) > 10 . 0
2012-08-01 09:56:22 -04:00
raise Fog :: Compute :: AWS :: Error . new ( " InvalidParameterValue => Iops to volume size ratio of #{ " %.1f " % iops_to_size_ratio } is too high; maximum is 10.0 " )
end
2012-08-17 18:56:26 -04:00
if iops < 100
raise Fog :: Compute :: AWS :: Error . new ( " VolumeIOPSLimit => Volume iops of #{ iops } is too low; minimum is 100. " )
end
2013-07-03 07:37:11 -04:00
if iops > 4000
raise Fog :: Compute :: AWS :: Error . new ( " VolumeIOPSLimit => Volume iops of #{ iops } is too high; maximum is 4000. " )
2012-08-17 18:56:26 -04:00
end
2012-08-01 09:56:22 -04:00
end
2009-10-22 23:46:46 -04:00
response . status = 200
volume_id = Fog :: AWS :: Mock . volume_id
data = {
'availabilityZone' = > availability_zone ,
'attachmentSet' = > [ ] ,
'createTime' = > Time . now ,
2012-08-01 09:56:22 -04:00
'iops' = > options [ 'Iops' ] ,
2009-10-22 23:46:46 -04:00
'size' = > size ,
2012-08-01 09:56:22 -04:00
'snapshotId' = > options [ 'SnapshotId' ] ,
2009-10-22 23:46:46 -04:00
'status' = > 'creating' ,
2012-08-01 09:56:22 -04:00
'volumeId' = > volume_id ,
'volumeType' = > options [ 'VolumeType' ] || 'standard'
2009-10-22 23:46:46 -04:00
}
2011-05-19 18:35:33 -04:00
self . data [ :volumes ] [ volume_id ] = data
2009-10-22 23:46:46 -04:00
response . body = {
'requestId' = > Fog :: AWS :: Mock . request_id
2012-08-01 09:56:22 -04:00
} . merge! ( data . reject { | key , value | ! [ 'availabilityZone' , 'createTime' , 'size' , 'snapshotId' , 'status' , 'volumeId' , 'volumeType' ] . include? ( key ) } )
2009-10-22 23:46:46 -04:00
else
response . status = 400
response . body = {
'Code' = > 'MissingParameter'
}
unless availability_zone
2009-10-24 01:23:55 -04:00
response . body [ 'Message' ] = 'The request must contain the parameter availability_zone'
2009-10-22 23:46:46 -04:00
else
2009-10-24 01:23:55 -04:00
response . body [ 'Message' ] = 'The request must contain the parameter size'
2009-10-22 23:46:46 -04:00
end
end
2009-08-15 18:19:07 -04:00
response
end
end
2009-07-13 22:14:59 -04:00
end
end
2009-08-15 18:19:07 -04:00
end