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