Permalink
Browse files

* Added VBDs and VDIs models and collections

* Server.destroy now removes all the associated VDIs by default
  • Loading branch information...
1 parent d813b47 commit fe75f425cf5471faa824a2cbb92adc3ff6e23c60 @rubiojr committed Mar 30, 2012
@@ -23,29 +23,31 @@ class XenServer < Fog::Service
model :storage_repository
collection :pools
model :pool
+ collection :vbds
model :vbd
+ collection :vdis
+ model :vdi
request_path 'fog/xenserver/requests/compute'
request :create_server
request :create_vif
request :create_vm
request :destroy_server
request :destroy_vbd
+ request :destroy_vdi
request :hard_shutdown_server
- request :get_host
request :get_hosts
request :get_network
request :get_networks
request :get_storage_repository
request :get_storage_repositories
- request :get_vif
request :get_vifs
- request :get_vm
request :get_vms
+ request :get_vm
request :start_vm
request :get_pool
request :get_pools
- request :get_vbds
- request :get_vbd
+ request :get_record
+ request :get_records
request :set_affinity
class Real
@@ -57,7 +57,7 @@ def initialize(attributes={})
end
def vbds
- __vbds.collect {|vbd| Fog::Compute::XenServer::VBD.new(connection.get_vbd_by_ref( vbd ))}
+ __vbds.collect {|vbd| connection.vbds.get vbd }
#__vbds.collect {|vbd| connection.get_vbd_by_ref( vbd ) }
end
@@ -69,16 +69,16 @@ def hard_shutdown
def destroy
raise "VM still running. Power it off first." if running?
- __vbds.each do |vbd|
- connection.destroy_vbd( vbd )
+ vbds.each do |vbd|
+ connection.destroy_vdi( vbd.vdi.reference ) if vbd.type == "Disk"
end
connection.destroy_server( reference )
true
end
def refresh
requires :reference
- data = connection.get_vm_by_ref( reference )
+ data = connection.get_record( reference, 'VM' )
merge_attributes( data )
end
@@ -88,11 +88,11 @@ def vifs
# associations
def networks
- __vifs.collect {|vif| Fog::Compute::XenServer::VIF.new(connection.get_vif_by_ref( vif ))}
+ __vifs.collect {|vif| Fog::Compute::XenServer::VIF.new(connection.get_record( vif, 'VIF' ))}
end
def running_on
- Fog::Compute::XenServer::Host.new(connection.get_host_by_ref( resident_on ))
+ Fog::Compute::XenServer::Host.new(connection.get_record( resident_on, 'host' ))
end
def home_hypervisor
@@ -13,16 +13,25 @@ class VBD < Fog::Model
attribute :uuid
attribute :currently_attached
attribute :reserved
- attribute :vdi, :aliases => :VDI
+ attribute :__vdi, :aliases => :VDI
attribute :vm, :aliases => :VM
+ attribute :device
+ attribute :status_detail
+ attribute :type
+ attribute :userdevice
#ignore_attributes :current_operations, :qos_supported_algorithms, :qos_algorithm_params, :qos_algorithm_type, :other_config,
# :runtime_properties
def initialize(attributes={})
- @uuid ||= 0
super
end
+
+ def vdi
+ #Fog::Compute::XenServer::VDI.new(connection.get_record( __vdi, 'VDI' ))
+ connection.vdis.get __vdi
+ end
+
end
end
@@ -0,0 +1,30 @@
+require 'fog/core/collection'
+require 'fog/xenserver/models/compute/vbd'
+
+module Fog
+ module Compute
+ class XenServer
+
+ class Vbds < Fog::Collection
+
+ model Fog::Compute::XenServer::VBD
+
+ def all(options = {})
+ data = connection.get_records 'VBD'
+ #data.delete_if { |vm| vm[:is_a_template] and !options[:include_templates] }
+ load(data)
+ end
+
+ def get( vbd_ref )
+ if vbd_ref && vbd = connection.get_record( vbd_ref, 'VBD' )
+ new(vbd)
+ end
+ rescue Fog::XenServer::NotFound
+ nil
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,33 @@
+require 'fog/core/model'
+
+module Fog
+ module Compute
+ class XenServer
+
+ class VDI < Fog::Model
+ # API Reference here:
+ # http://docs.vmd.citrix.com/XenServer/5.6.0/1.0/en_gb/api/?c=VDI
+
+ identity :reference
+
+ attribute :uuid
+ attribute :is_a_snapshot
+ attribute :name, :aliases => :name_label
+ attribute :description, :aliases => :name_description
+ attribute :__parent, :aliases => :parent
+ attribute :virtual_size, :aliases => :parent
+ attribute :__vbds, :aliases => :VBDs
+ attribute :__sr, :aliases => :SR
+ attribute :sharable
+ attribute :readonly
+
+ def initialize(attributes={})
+ @uuid ||= 0
+ super
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,30 @@
+require 'fog/core/collection'
+require 'fog/xenserver/models/compute/vdi'
+
+module Fog
+ module Compute
+ class XenServer
+
+ class Vdis < Fog::Collection
+
+ model Fog::Compute::XenServer::VDI
+
+ def all(options = {})
+ data = connection.get_records 'VDI'
+ #data.delete_if { |vm| vm[:is_a_template] and !options[:include_templates] }
+ load(data)
+ end
+
+ def get( vdi_ref )
+ if vdi_ref && vdi = connection.get_record( vdi_ref, 'VDI' )
+ new(vdi)
+ end
+ rescue Fog::XenServer::NotFound
+ nil
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,19 @@
+module Fog
+ module Parsers
+ module XenServer
+ class GetRecords < Fog::Parsers::XenServer::Base
+
+ def reset
+ @response = []
+ end
+
+ def parse( data )
+ parser = Fog::Parsers::XenServer::Base.new
+ data.each_pair {|reference, hash| @response << parser.parse( hash ).merge(:reference => reference) }
+ end
+
+ end
+
+ end
+ end
+end
@@ -13,11 +13,13 @@ def create_vm( name_label, template = nil, network = nil, extra_args = {})
if template.kind_of? String
data = get_vm( template )
+ template_string = template
template = Fog::Compute::XenServer::Server.new(data)
end
begin
#FIXME: need to check that template exist actually
+ raise "Template #{template_string} does not exist" if template.allowed_operations.nil?
raise 'Clone Operation not Allowed' unless template.allowed_operations.include?('clone')
# Clone the VM template
@@ -1,30 +0,0 @@
-module Fog
- module Compute
- class XenServer
-
- class Real
-
- require 'fog/xenserver/parser'
-
- def get_host( name_label )
- host_ref = @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'host.get_by_name_label'}, name_label)
- get_host_by_ref( host_ref )
- end
-
- def get_host_by_ref( host_ref )
- @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'host.get_record'}, host_ref).merge(:reference => host_ref)
- end
-
- end
-
- class Mock
-
- def get_host( uuid )
- Fog::Mock.not_implemented
- end
-
- end
-
- end
- end
-end
@@ -6,15 +6,19 @@ class Real
require 'fog/xenserver/parser'
- def get_vbd_by_ref( ref, options = {} )
- @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VBD.get_record'}, ref).merge(:reference => ref)
+ def get_record( ref, klass, options = {} )
+ get_record_by_ref ref, klass, options
+ end
+
+ def get_record_by_ref( ref, klass, options = {} )
+ @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => "#{klass}.get_record"}, ref).merge(:reference => ref)
end
end
class Mock
- def get_vbds
+ def get_record_by_ref
Fog::Mock.not_implemented
end
@@ -0,0 +1,25 @@
+module Fog
+ module Compute
+ class XenServer
+
+ class Real
+
+ require 'fog/xenserver/parsers/get_records'
+
+ def get_records( klass, options = {} )
+ @connection.request(:parser => Fog::Parsers::XenServer::GetRecords.new, :method => "#{klass}.get_all_records")
+ end
+
+ end
+
+ class Mock
+
+ def get_vms
+ Fog::Mock.not_implemented
+ end
+
+ end
+
+ end
+ end
+end
@@ -4,10 +4,10 @@ class XenServer
class Real
- require 'fog/xenserver/parsers/get_vbds'
+ require 'fog/xenserver/parsers/get_records'
def get_vbds( options = {} )
- @connection.request(:parser => Fog::Parsers::XenServer::GetVBDs.new, :method => 'VBD.get_all_records')
+ @connection.request(:parser => Fog::Parsers::XenServer::GetRecords.new, :method => 'VBD.get_all_records')
end
end
@@ -1,32 +0,0 @@
-module Fog
- module Compute
- class XenServer
- class Real
-
- require 'fog/xenserver/parser'
-
- def get_vif( name_label )
- vif_ref = @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VIF.get_by_name_label'}, name_label)
- get_vif_by_ref( vif_ref )
- end
-
- def get_vif_by_ref( vif_ref )
- @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VIF.get_record'}, vif_ref).merge(:reference => vif_ref)
- end
-
- end
-
- class Mock
-
- def get_vif( uuid )
- Fog::Mock.not_implemented
- end
-
- def get_vif_by_ref( uuid )
- Fog::Mock.not_implemented
- end
-
- end
- end
- end
-end

0 comments on commit fe75f42

Please sign in to comment.