2011-06-28 09:47:16 -04:00
|
|
|
require 'fog/core/model'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class Libvirt
|
|
|
|
|
|
|
|
class Volume < Fog::Model
|
|
|
|
|
|
|
|
identity :id
|
2011-08-01 09:18:00 -04:00
|
|
|
|
|
|
|
attribute :poolname
|
|
|
|
attribute :xml
|
2011-08-01 09:33:25 -04:00
|
|
|
attribute :create_persistent
|
2011-06-28 09:47:16 -04:00
|
|
|
|
2011-08-01 09:18:00 -04:00
|
|
|
# Can be created by passing in :xml => "<xml to create volume>"
|
|
|
|
# A volume always belongs to a pool, :pool => "<name of pool>"
|
|
|
|
#
|
|
|
|
# @returns volume created
|
|
|
|
def initialize(attributes={} )
|
|
|
|
self.xml ||= nil unless attributes[:xml]
|
|
|
|
self.poolname ||= nil unless attributes[:poolname]
|
2011-08-01 09:33:25 -04:00
|
|
|
self.create_persistent ||=true unless attribues[:create_persistent]
|
2011-08-01 09:18:00 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
# Takes a pool and xml to create the volume
|
|
|
|
def save
|
|
|
|
requires :xml
|
|
|
|
requires :poolname
|
|
|
|
|
|
|
|
unless xml.nil?
|
|
|
|
volume=nil
|
|
|
|
unless poolname.nil?
|
|
|
|
pool=connection.lookup_storage_pool_by_name(poolname)
|
2011-08-01 09:33:25 -04:00
|
|
|
if create_persistent
|
|
|
|
volume=pool.define_volume_xml(xml)
|
|
|
|
else
|
|
|
|
volume=pool.create_volume_xml(xml)
|
|
|
|
end
|
2011-08-01 09:18:00 -04:00
|
|
|
self.raw=volume
|
|
|
|
true
|
|
|
|
else
|
|
|
|
raise Fog::Errors::Error.new('Creating a new volume requires a pool name or uuid')
|
|
|
|
false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise Fog::Errors::Error.new('Creating a new volume requires non empty xml')
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-28 09:47:16 -04:00
|
|
|
|
2011-08-01 09:18:00 -04:00
|
|
|
# Destroy a volume
|
2011-06-28 09:47:16 -04:00
|
|
|
def destroy
|
|
|
|
requires :raw
|
|
|
|
raw.delete
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2011-08-01 09:18:00 -04:00
|
|
|
# Wipes a volume , zeroes disk
|
2011-06-28 09:47:16 -04:00
|
|
|
def wipe
|
|
|
|
requires :raw
|
|
|
|
raw.wipe
|
|
|
|
true
|
|
|
|
end
|
2011-08-01 09:18:00 -04:00
|
|
|
|
|
|
|
# Clones this volume to the name provided
|
2011-06-28 09:47:16 -04:00
|
|
|
def clone(name)
|
|
|
|
pool=@raw.pool
|
|
|
|
xml = REXML::Document.new(xml_desc)
|
|
|
|
xml.root.elements['/volume/name'].text=name
|
|
|
|
xml.root.elements['/volume/key'].text=name
|
|
|
|
xml.delete_element('/volume/target/path')
|
|
|
|
pool.create_volume_xml_from(xml.to_s,@raw)
|
2011-08-01 09:18:00 -04:00
|
|
|
return connection.volumes.get(:name => name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
|
|
|
requires :raw
|
|
|
|
raw.key
|
|
|
|
end
|
|
|
|
|
|
|
|
def path
|
|
|
|
requires :raw
|
|
|
|
raw.path
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
requires :raw
|
|
|
|
raw.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def xml_desc
|
|
|
|
requires :raw
|
|
|
|
raw.xml_desc
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def allocation
|
|
|
|
requires :raw
|
|
|
|
raw.info.allocation
|
2011-06-28 09:47:16 -04:00
|
|
|
end
|
|
|
|
|
2011-08-01 09:18:00 -04:00
|
|
|
def capacity
|
|
|
|
requires :raw
|
|
|
|
raw.info.capacity
|
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
requires :raw
|
|
|
|
raw.info.type
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-06-28 09:47:16 -04:00
|
|
|
private
|
|
|
|
def raw
|
|
|
|
@raw
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw=(new_raw)
|
|
|
|
@raw = new_raw
|
|
|
|
|
|
|
|
raw_attributes = {
|
|
|
|
:id => new_raw.key,
|
|
|
|
}
|
|
|
|
|
|
|
|
merge_attributes(raw_attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|