2010-03-16 18:46:21 -04:00
|
|
|
require 'fog/model'
|
|
|
|
|
2009-08-14 12:19:40 -04:00
|
|
|
module Fog
|
|
|
|
module AWS
|
2010-03-16 18:46:21 -04:00
|
|
|
module EC2
|
2009-08-14 12:19:40 -04:00
|
|
|
|
|
|
|
class Volume < Fog::Model
|
2010-04-23 18:21:17 -04:00
|
|
|
extend Fog::Deprecation
|
|
|
|
deprecate(:status, :state)
|
2009-08-14 12:19:40 -04:00
|
|
|
|
2009-12-05 17:53:42 -05:00
|
|
|
identity :id, 'volumeId'
|
2009-10-24 01:23:55 -04:00
|
|
|
|
2010-01-08 18:05:29 -05:00
|
|
|
attribute :attached_at, 'attachTime'
|
2009-09-05 23:50:40 -04:00
|
|
|
attribute :availability_zone, 'availabilityZone'
|
2010-01-08 18:05:29 -05:00
|
|
|
attribute :created_at, 'createTime'
|
2009-09-18 03:01:10 -04:00
|
|
|
attribute :device
|
2010-01-08 14:29:07 -05:00
|
|
|
attribute :server_id, 'instanceId'
|
2009-09-05 23:50:40 -04:00
|
|
|
attribute :size
|
|
|
|
attribute :snapshot_id, 'snapshotId'
|
2010-04-23 18:21:17 -04:00
|
|
|
attribute :state, '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-12-05 17:53:42 -05:00
|
|
|
requires :id
|
2009-11-21 16:56:39 -05:00
|
|
|
|
2009-12-05 17:53:42 -05:00
|
|
|
connection.delete_volume(@id)
|
2009-09-05 23:50:40 -04:00
|
|
|
true
|
2009-08-14 12:19:40 -04:00
|
|
|
end
|
|
|
|
|
2010-01-08 14:29:07 -05:00
|
|
|
def server=(new_server)
|
|
|
|
if new_server
|
|
|
|
attach(new_server)
|
2009-11-12 01:22:13 -05:00
|
|
|
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-12-05 17:53:42 -05:00
|
|
|
requires :availability_zone, :size
|
2009-11-21 16:56:39 -05:00
|
|
|
|
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)
|
2010-01-08 14:29:07 -05:00
|
|
|
if @server
|
|
|
|
self.server = @server
|
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-12-05 17:53:42 -05:00
|
|
|
requires :id
|
2009-11-21 16:56:39 -05:00
|
|
|
|
2009-12-05 17:53:42 -05:00
|
|
|
connection.snapshots(:volume => self)
|
2009-09-06 13:48:12 -04:00
|
|
|
end
|
|
|
|
|
2009-11-12 01:22:13 -05:00
|
|
|
private
|
|
|
|
|
2010-01-08 14:29:07 -05:00
|
|
|
def attach(new_server)
|
2009-11-12 01:22:13 -05:00
|
|
|
if new_record?
|
2010-01-08 14:29:07 -05:00
|
|
|
@server = new_server
|
|
|
|
@availability_zone = new_server.availability_zone
|
|
|
|
elsif new_server
|
2010-04-26 14:44:02 -04:00
|
|
|
requires :device
|
2010-01-08 14:29:07 -05:00
|
|
|
@server = nil
|
|
|
|
@server_id = new_server.id
|
|
|
|
connection.attach_volume(@server_id, @id, @device)
|
2009-11-12 01:22:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def detach
|
2010-01-08 14:29:07 -05:00
|
|
|
@server = nil
|
|
|
|
@server_id = nil
|
2009-11-12 01:22:13 -05:00
|
|
|
unless new_record?
|
2009-12-05 17:53:42 -05:00
|
|
|
connection.detach_volume(@id)
|
2009-11-12 01:22:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-14 12:19:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|