2011-03-04 15:03:51 -05:00
|
|
|
require 'fog/core/model'
|
|
|
|
|
|
|
|
module Fog
|
2011-06-16 19:28:54 -04:00
|
|
|
module Compute
|
|
|
|
class Linode
|
2011-03-04 15:03:51 -05:00
|
|
|
class Disk < Fog::Model
|
|
|
|
identity :id
|
|
|
|
attribute :name
|
|
|
|
attribute :type
|
|
|
|
|
2011-03-07 16:05:20 -05:00
|
|
|
def save
|
|
|
|
requires :server
|
2012-12-22 21:45:05 -05:00
|
|
|
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
|
2011-03-08 16:33:02 -05:00
|
|
|
@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
|
2012-12-22 18:25:24 -05:00
|
|
|
end
|
2011-03-07 16:05:20 -05:00
|
|
|
|
|
|
|
def destroy
|
|
|
|
requires :identity, :server
|
2012-12-22 18:25:24 -05:00
|
|
|
service.linode_disk_delete server.id, id
|
2011-03-07 16:05:20 -05:00
|
|
|
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
|
2012-12-22 18:25:24 -05:00
|
|
|
self.id = service.linode_disk_create(server.id, "#{@name}_#{@type}", @type, @size).body['DATA']['DiskID']
|
2011-03-07 16:05:20 -05:00
|
|
|
reload
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_disk_from_image
|
2012-12-22 18:25:24 -05:00
|
|
|
disk = service.linode_disk_createfromdistribution server.id, @image.id, "#{@name}_main", @size, @password
|
2011-03-07 16:05:20 -05:00
|
|
|
self.id = disk.body['DATA']['DiskID']
|
|
|
|
reload
|
2012-12-22 18:25:24 -05:00
|
|
|
end
|
2011-03-07 16:05:20 -05:00
|
|
|
|
|
|
|
def create_disk_from_stack_script
|
2012-12-22 18:25:24 -05:00
|
|
|
disk = service.linode_disk_createfromstackscript(server.id, @stack_script.id, @image.id,
|
2011-03-08 16:33:02 -05:00
|
|
|
"#{@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
|