1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/compute/models/aws/volume.rb

108 lines
2.7 KiB
Ruby
Raw Normal View History

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
module Compute
class AWS
2009-08-14 12:19:40 -04:00
class Volume < Fog::Model
identity :id, :aliases => 'volumeId'
attribute :attached_at, :aliases => 'attachTime'
attribute :availability_zone, :aliases => 'availabilityZone'
attribute :created_at, :aliases => 'createTime'
attribute :delete_on_termination, :aliases => 'deleteOnTermination'
attribute :device
attribute :server_id, :aliases => 'instanceId'
attribute :size
attribute :snapshot_id, :aliases => 'snapshotId'
attribute :state, :aliases => 'status'
attribute :tags, :aliases => 'tagSet'
2009-08-14 12:19:40 -04:00
def initialize(attributes = {})
# 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
requires :id
connection.delete_volume(id)
true
2009-08-14 12:19:40 -04:00
end
def ready?
state == 'available'
end
2009-08-14 12:19:40 -04:00
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
requires :availability_zone, :size
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
end
true
2009-08-14 12:19:40 -04:00
end
def server=(new_server)
if new_server
attach(new_server)
else
detach
end
end
2009-09-06 13:48:12 -04:00
def snapshots
requires :id
connection.snapshots(:volume => self)
2009-09-06 13:48:12 -04:00
end
2010-10-11 16:40:34 -04:00
private
def attachmentSet=(new_attachment_set)
merge_attributes(new_attachment_set.first || {})
end
2010-01-08 14:29:07 -05:00
def attach(new_server)
if new_record?
2010-01-08 14:29:07 -05:00
@server = new_server
self.availability_zone = new_server.availability_zone
2010-01-08 14:29:07 -05:00
elsif new_server
requires :device
2010-01-08 14:29:07 -05:00
@server = nil
self.server_id = new_server.id
connection.attach_volume(server_id, id, device)
reload
end
end
def detach
2010-01-08 14:29:07 -05:00
@server = nil
self.server_id = nil
unless new_record?
connection.detach_volume(id)
reload
end
end
2010-11-01 15:40:03 -04:00
def force_detach
@server = nil
self.server_id = nil
2010-11-01 15:40:03 -04:00
unless new_record?
connection.detach_volume(id, 'Force' => true)
2010-11-01 15:40:03 -04:00
reload
end
end
2009-08-14 12:19:40 -04:00
end
end
end
end