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/linode/disk.rb

63 lines
1.8 KiB
Ruby
Raw Normal View History

2011-03-04 15:03:51 -05:00
require 'fog/core/model'
module Fog
module Linode
class Compute
class Disk < Fog::Model
identity :id
attribute :name
attribute :type
2011-03-07 16:05:20 -05:00
def save
requires :server
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
@type, @image, @stack_script, @name, @password, @size =
attributes.values_at :type, :image, :stack_script, :name, :password, :size
2011-03-07 16:05:20 -05:00
create_disk
end
def destroy
requires :identity, :server
connection.linode_disk_delete server.id, id
end
def server
@server
end
private
def server=(server)
@server = server
end
def create_disk
case
when @image && @stack_script then create_disk_from_stack_script
when @image then create_disk_from_image
when @type then create_disk_type
else raise 'disk cannot be created'
end
end
def create_disk_type
self.id = connection.linode_disk_create(server.id, "#{@name}_#{@type}", @type, @size).body['DATA']['DiskID']
reload
end
def create_disk_from_image
disk = connection.linode_disk_createfromdistribution server.id, @image.id, "#{@name}_main", @size, @password
self.id = disk.body['DATA']['DiskID']
reload
end
def create_disk_from_stack_script
disk = connection.linode_disk_createfromstackscript(server.id, @stack_script.id, @image.id,
"#{@name}_main", @size, @password, @stack_script.options)
2011-03-07 16:05:20 -05:00
self.id = disk.body['DATA']['DiskID']
reload
2011-03-04 15:03:51 -05:00
end
end
end
end
end