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/aws/models/ec2/volume.rb

89 lines
2 KiB
Ruby
Raw Normal View History

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
extend Fog::Deprecation
deprecate(:status, :state)
2009-08-14 12:19:40 -04:00
identity :id, 'volumeId'
attribute :attached_at, 'attachTime'
attribute :availability_zone, 'availabilityZone'
attribute :created_at, 'createTime'
attribute :device
2010-01-08 14:29:07 -05:00
attribute :server_id, 'instanceId'
attribute :size
attribute :snapshot_id, 'snapshotId'
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
requires :id
connection.delete_volume(@id)
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)
else
detach
end
2009-10-06 21:55:39 -04:00
end
2009-08-14 12:19:40 -04:00
def save
requires :availability_zone, :size
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
end
true
2009-08-14 12:19:40 -04:00
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
private
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
@availability_zone = new_server.availability_zone
elsif new_server
requires :device
2010-01-08 14:29:07 -05:00
@server = nil
@server_id = new_server.id
connection.attach_volume(@server_id, @id, @device)
end
end
def detach
2010-01-08 14:29:07 -05:00
@server = nil
@server_id = nil
unless new_record?
connection.detach_volume(@id)
end
end
2009-08-14 12:19:40 -04:00
end
end
end
end