2010-10-04 14:02:08 -07:00
|
|
|
require 'fog/core/model'
|
2010-03-16 15:46:21 -07:00
|
|
|
|
2009-08-14 09:19:40 -07:00
|
|
|
module Fog
|
2011-06-16 16:28:54 -07:00
|
|
|
module Compute
|
|
|
|
class AWS
|
2009-08-14 09:19:40 -07:00
|
|
|
|
|
|
|
class Volume < Fog::Model
|
|
|
|
|
2010-11-15 11:49:16 -08:00
|
|
|
identity :id, :aliases => 'volumeId'
|
2009-10-23 22:23:55 -07:00
|
|
|
|
2010-11-15 11:49:16 -08:00
|
|
|
attribute :attached_at, :aliases => 'attachTime'
|
|
|
|
attribute :availability_zone, :aliases => 'availabilityZone'
|
|
|
|
attribute :created_at, :aliases => 'createTime'
|
|
|
|
attribute :delete_on_termination, :aliases => 'deleteOnTermination'
|
2009-09-18 00:01:10 -07:00
|
|
|
attribute :device
|
2010-11-15 11:49:16 -08:00
|
|
|
attribute :server_id, :aliases => 'instanceId'
|
2009-09-05 20:50:40 -07:00
|
|
|
attribute :size
|
2010-11-15 11:49:16 -08:00
|
|
|
attribute :snapshot_id, :aliases => 'snapshotId'
|
|
|
|
attribute :state, :aliases => 'status'
|
|
|
|
attribute :tags, :aliases => 'tagSet'
|
2010-04-23 15:21:17 -07:00
|
|
|
|
2009-08-14 09:19:40 -07:00
|
|
|
def initialize(attributes = {})
|
2010-04-27 11:19:24 -07:00
|
|
|
# assign server first to prevent race condition with new_record?
|
|
|
|
self.server = attributes.delete(:server)
|
2009-08-14 09:19:40 -07:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2009-09-20 09:21:03 -07:00
|
|
|
def destroy
|
2009-12-05 14:53:42 -08:00
|
|
|
requires :id
|
2009-11-21 13:56:39 -08:00
|
|
|
|
2010-11-19 13:45:45 -08:00
|
|
|
connection.delete_volume(id)
|
2009-09-05 20:50:40 -07:00
|
|
|
true
|
2009-08-14 09:19:40 -07:00
|
|
|
end
|
|
|
|
|
2010-04-27 11:19:24 -07:00
|
|
|
def ready?
|
|
|
|
state == 'available'
|
|
|
|
end
|
|
|
|
|
2009-08-14 09:19:40 -07:00
|
|
|
def save
|
2010-10-04 16:08:34 -07:00
|
|
|
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
|
2011-06-28 15:26:27 -07:00
|
|
|
requires :availability_zone
|
|
|
|
requires_one :size, :snapshot_id
|
2009-11-21 13:56:39 -08:00
|
|
|
|
2010-11-19 13:45:45 -08:00
|
|
|
data = connection.create_volume(availability_zone, size, snapshot_id).body
|
2009-08-14 09:19:40 -07:00
|
|
|
new_attributes = data.reject {|key,value| key == 'requestId'}
|
2009-09-23 21:23:54 -07:00
|
|
|
merge_attributes(new_attributes)
|
2010-01-08 11:29:07 -08:00
|
|
|
if @server
|
|
|
|
self.server = @server
|
2009-10-20 19:39:57 -07:00
|
|
|
end
|
2009-09-05 20:50:40 -07:00
|
|
|
true
|
2009-08-14 09:19:40 -07:00
|
|
|
end
|
|
|
|
|
2011-06-28 14:25:29 -07:00
|
|
|
def server
|
|
|
|
requires :server_id
|
|
|
|
connection.servers('instance-id' => server_id)
|
|
|
|
end
|
|
|
|
|
2010-10-04 16:08:34 -07:00
|
|
|
def server=(new_server)
|
|
|
|
if new_server
|
|
|
|
attach(new_server)
|
|
|
|
else
|
|
|
|
detach
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-06 10:48:12 -07:00
|
|
|
def snapshots
|
2009-12-05 14:53:42 -08:00
|
|
|
requires :id
|
|
|
|
connection.snapshots(:volume => self)
|
2009-09-06 10:48:12 -07:00
|
|
|
end
|
2010-10-11 13:40:34 -07:00
|
|
|
|
2011-09-17 10:50:02 +01:00
|
|
|
def snapshot(description)
|
|
|
|
requires :id
|
|
|
|
connection.create_snapshot(id, description)
|
|
|
|
end
|
|
|
|
|
2011-06-27 16:39:15 -07:00
|
|
|
def force_detach
|
|
|
|
detach(true)
|
|
|
|
end
|
|
|
|
|
2009-11-11 22:22:13 -08:00
|
|
|
private
|
|
|
|
|
2010-04-27 13:39:37 -07:00
|
|
|
def attachmentSet=(new_attachment_set)
|
|
|
|
merge_attributes(new_attachment_set.first || {})
|
|
|
|
end
|
|
|
|
|
2010-01-08 11:29:07 -08:00
|
|
|
def attach(new_server)
|
2009-11-11 22:22:13 -08:00
|
|
|
if new_record?
|
2010-01-08 11:29:07 -08:00
|
|
|
@server = new_server
|
2010-11-19 13:45:45 -08:00
|
|
|
self.availability_zone = new_server.availability_zone
|
2010-01-08 11:29:07 -08:00
|
|
|
elsif new_server
|
2010-04-26 11:44:02 -07:00
|
|
|
requires :device
|
2010-01-08 11:29:07 -08:00
|
|
|
@server = nil
|
2010-11-19 13:45:45 -08:00
|
|
|
self.server_id = new_server.id
|
|
|
|
connection.attach_volume(server_id, id, device)
|
2010-04-27 11:19:24 -07:00
|
|
|
reload
|
2009-11-11 22:22:13 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-27 16:39:15 -07:00
|
|
|
def detach(force = false)
|
2010-11-01 12:40:03 -07:00
|
|
|
@server = nil
|
2010-11-19 13:45:45 -08:00
|
|
|
self.server_id = nil
|
2010-11-01 12:40:03 -07:00
|
|
|
unless new_record?
|
2011-06-27 16:39:15 -07:00
|
|
|
connection.detach_volume(id, 'Force' => force)
|
2010-11-01 12:40:03 -07:00
|
|
|
reload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-14 09:19:40 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|