2009-08-14 12:19:40 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class EC2
|
|
|
|
|
|
|
|
class Volume < Fog::Model
|
|
|
|
|
2009-10-24 01:23:55 -04:00
|
|
|
identity :volume_id, 'volumeId'
|
|
|
|
|
2009-10-09 15:16:00 -04:00
|
|
|
attribute :attach_time, 'attachTime'
|
2009-09-05 23:50:40 -04:00
|
|
|
attribute :availability_zone, 'availabilityZone'
|
2009-09-18 03:01:10 -04:00
|
|
|
attribute :create_time, 'createTime'
|
|
|
|
attribute :device
|
2009-09-05 23:50:40 -04:00
|
|
|
attribute :instance_id, 'instanceId'
|
|
|
|
attribute :size
|
|
|
|
attribute :snapshot_id, 'snapshotId'
|
2009-10-06 23:35:46 -04:00
|
|
|
attribute :status
|
2009-08-14 12:19:40 -04:00
|
|
|
|
|
|
|
def initialize(attributes = {})
|
|
|
|
if attributes['attachmentSet']
|
2009-09-24 00:23:54 -04:00
|
|
|
attributes.merge!(attributes.delete('attachmentSet').first || {})
|
2009-08-14 12:19:40 -04:00
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2009-09-20 12:21:03 -04:00
|
|
|
def destroy
|
2009-11-21 16:56:39 -05:00
|
|
|
requires :volume_id
|
|
|
|
|
2009-08-14 12:19:40 -04:00
|
|
|
connection.delete_volume(@volume_id)
|
2009-09-05 23:50:40 -04:00
|
|
|
true
|
2009-08-14 12:19:40 -04:00
|
|
|
end
|
|
|
|
|
2009-10-06 21:55:39 -04:00
|
|
|
def instance=(new_instance)
|
2009-11-21 16:56:39 -05:00
|
|
|
requires :volume_id
|
|
|
|
|
2009-11-12 01:22:13 -05:00
|
|
|
if new_instance
|
|
|
|
attach(new_instance)
|
|
|
|
else
|
|
|
|
detach
|
2009-10-20 22:39:57 -04:00
|
|
|
end
|
2009-10-06 21:55:39 -04:00
|
|
|
end
|
|
|
|
|
2009-08-14 12:19:40 -04:00
|
|
|
def save
|
2009-11-21 16:56:39 -05:00
|
|
|
requires :availability_zone, :size, :snapshot_id
|
|
|
|
|
2009-08-14 12:19:40 -04:00
|
|
|
data = connection.create_volume(@availability_zone, @size, @snapshot_id).body
|
|
|
|
new_attributes = data.reject {|key,value| key == 'requestId'}
|
2009-09-24 00:23:54 -04:00
|
|
|
merge_attributes(new_attributes)
|
2009-10-20 22:39:57 -04:00
|
|
|
if @instance
|
2009-10-22 12:42:02 -04:00
|
|
|
self.instance = @instance
|
2009-10-20 22:39:57 -04:00
|
|
|
end
|
2009-09-05 23:50:40 -04:00
|
|
|
true
|
2009-08-14 12:19:40 -04:00
|
|
|
end
|
|
|
|
|
2009-09-06 13:48:12 -04:00
|
|
|
def snapshots
|
2009-11-21 16:56:39 -05:00
|
|
|
requires :volume_id
|
|
|
|
|
|
|
|
connection.snapshots(:volume_id => @volume_id)
|
2009-09-06 13:48:12 -04:00
|
|
|
end
|
|
|
|
|
2009-11-12 01:22:13 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def attach(new_instance)
|
|
|
|
if new_record?
|
|
|
|
@instance = new_instance
|
|
|
|
@availability_zone = new_instance.availability_zone
|
|
|
|
elsif new_instance
|
|
|
|
@instance = nil
|
|
|
|
@instance_id = new_instance.instance_id
|
|
|
|
connection.attach_volume(@instance_id, @volume_id, @device)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def detach
|
|
|
|
@instance = nil
|
|
|
|
@instance_id = nil
|
|
|
|
unless new_record?
|
|
|
|
connection.detach_volume(@volume_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-14 12:19:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|