diff --git a/lib/fog/xenserver/compute.rb b/lib/fog/xenserver/compute.rb index e9699d962..14e77db80 100644 --- a/lib/fog/xenserver/compute.rb +++ b/lib/fog/xenserver/compute.rb @@ -35,6 +35,7 @@ module Fog collection :pbds model :pbd model :guest_metrics + model :vbd_metrics request_path 'fog/xenserver/requests/compute' request :create_server @@ -43,7 +44,9 @@ module Fog request :destroy_vif request :clone_server request :destroy_server - request :destroy_vbd + request :unplug_vbd + request :eject_vbd + request :insert_vbd request :destroy_vdi request :shutdown_server request :start_vm diff --git a/lib/fog/xenserver/models/compute/server.rb b/lib/fog/xenserver/models/compute/server.rb index dc0a99c0c..fd0642979 100644 --- a/lib/fog/xenserver/models/compute/server.rb +++ b/lib/fog/xenserver/models/compute/server.rb @@ -54,9 +54,9 @@ module Fog stop('hard') vbds.each do |vbd| if vbd.type == "Disk" - connection.destroy_vbd( vbd.reference ) \ + vbd.unplug \ if vbd.allowed_operations.include?("unplug") - connection.destroy_vdi( vbd.vdi.reference ) \ + vbd.vdi.destroy \ if vbd.vdi.allowed_operations.include?("destroy") end end diff --git a/lib/fog/xenserver/models/compute/vbd.rb b/lib/fog/xenserver/models/compute/vbd.rb index 0edd39d4d..2ba2f4151 100644 --- a/lib/fog/xenserver/models/compute/vbd.rb +++ b/lib/fog/xenserver/models/compute/vbd.rb @@ -14,7 +14,6 @@ module Fog attribute :currently_attached attribute :allowed_operations attribute :current_operations - attribute :reserved attribute :__vdi, :aliases => :VDI attribute :__vm, :aliases => :VM attribute :device @@ -25,8 +24,12 @@ module Fog attribute :empty attribute :type attribute :mode + attribute :storage_lock attribute :runtime_properties attribute :unpluggable + attribute :bootable + attribute :empty + attribute :__metrics, :aliases => :metrics # # May return nil @@ -42,6 +45,27 @@ module Fog connection.servers.get __vm end + def unplug + connection.unplug_vbd reference + end + + def unplug_force + connection.unplug_force_vbd reference + end + + def eject + connection.eject_vbd reference + end + + def insert(vdi) + connection.insert_vbd reference, vdi.reference + end + + def metrics + rec = connection.get_record( __metrics, 'VBD_metrics' ) + Fog::Compute::XenServer::VbdMetrics.new(rec) + end + end end diff --git a/lib/fog/xenserver/models/compute/vbd_metrics.rb b/lib/fog/xenserver/models/compute/vbd_metrics.rb new file mode 100644 index 000000000..5a2571a60 --- /dev/null +++ b/lib/fog/xenserver/models/compute/vbd_metrics.rb @@ -0,0 +1,27 @@ +require 'fog/core/model' + +module Fog + module Compute + class XenServer + + class VbdMetrics < Fog::Model + # API Reference here: + # http://docs.vmd.citrix.com/XenServer/5.6.0/1.0/en_gb/api/?c=VBD_metrics + + identity :reference + + attribute :uuid + attribute :last_updated + attribute :other_config + attribute :io_read_kbs + attribute :io_write_kbs + + def initialize(attributes = {}) + super + self.last_updated = attributes[:last_updated].to_time + end + end + + end + end +end diff --git a/lib/fog/xenserver/requests/compute/destroy_vbd.rb b/lib/fog/xenserver/requests/compute/eject_vbd.rb similarity index 65% rename from lib/fog/xenserver/requests/compute/destroy_vbd.rb rename to lib/fog/xenserver/requests/compute/eject_vbd.rb index 7f481fc35..5412da29f 100644 --- a/lib/fog/xenserver/requests/compute/destroy_vbd.rb +++ b/lib/fog/xenserver/requests/compute/eject_vbd.rb @@ -4,15 +4,15 @@ module Fog class Real - def destroy_vbd( vbd_ref, extra_args = {}) - @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VBD.unplug'}, vbd_ref) + def eject_vbd(ref, extra_args = {}) + @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VBD.eject'}, ref) end end class Mock - def destroy_vbd() + def eject_vbd(ref, extra_args = {}) Fog::Mock.not_implemented end diff --git a/lib/fog/xenserver/requests/compute/insert_vbd.rb b/lib/fog/xenserver/requests/compute/insert_vbd.rb new file mode 100644 index 000000000..383181b5d --- /dev/null +++ b/lib/fog/xenserver/requests/compute/insert_vbd.rb @@ -0,0 +1,22 @@ +module Fog + module Compute + class XenServer + + class Real + + def insert_vbd(ref, vdi_ref, extra_args = {}) + @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VBD.insert'}, ref, vdi_ref) + end + + end + + class Mock + + def insert_vbd(ref, vdi_ref, extra_args = {}) + Fog::Mock.not_implemented + end + + end + end + end +end diff --git a/lib/fog/xenserver/requests/compute/unplug_vbd.rb b/lib/fog/xenserver/requests/compute/unplug_vbd.rb new file mode 100644 index 000000000..4bae72dc0 --- /dev/null +++ b/lib/fog/xenserver/requests/compute/unplug_vbd.rb @@ -0,0 +1,30 @@ +module Fog + module Compute + class XenServer + + class Real + + def unplug_vbd( vbd_ref, extra_args = {}) + @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VBD.unplug'}, vbd_ref) + end + + def unplug_force_vbd(ref, extra_args = {}) + @connection.request({:parser => Fog::Parsers::XenServer::Base.new, :method => 'VBD.unplug_force'}, ref) + end + + end + + class Mock + + def unplug_vbd(ref, extra_args = {}) + Fog::Mock.not_implemented + end + + def unplug_force_vbd(ref, extra_args = {}) + Fog::Mock.not_implemented + end + + end + end + end +end diff --git a/tests/xenserver/models/compute/vbd_tests.rb b/tests/xenserver/models/compute/vbd_tests.rb index d82fc60d4..206da1f0a 100644 --- a/tests/xenserver/models/compute/vbd_tests.rb +++ b/tests/xenserver/models/compute/vbd_tests.rb @@ -6,6 +6,10 @@ Shindo.tests('Fog::Compute[:xenserver] | VBD model', ['VBD']) do tests('The VBD model should') do tests('have the action') do test('reload') { vbd.respond_to? 'reload' } + test('eject') { vbd.respond_to? 'eject' } + test('insert') { vbd.respond_to? 'insert' } + test('unplug') { vbd.respond_to? 'unplug' } + test('unplug_force') { vbd.respond_to? 'unplug_force' } end tests('have attributes') do model_attribute_hash = vbd.attributes @@ -18,7 +22,17 @@ Shindo.tests('Fog::Compute[:xenserver] | VBD model', ['VBD']) do :device, :status_detail, :type, - :userdevice + :allowed_operations, + :current_operations, + :status_code, + :storage_lock, + :mode, + :runtime_properties, + :unpluggable, + :userdevice, + :bootable, + :empty, + :__metrics ] tests("The VBD model should respond to") do attributes.each do |attribute| @@ -37,6 +51,23 @@ Shindo.tests('Fog::Compute[:xenserver] | VBD model', ['VBD']) do end tests("A real VBD should") do + test("have a valid OpaqueRef") do + puts vbd.reference + (vbd.reference =~ /OpaqueRef:/).eql?(0) and \ + vbd.reference != "OpaqueRef:NULL" + end + tests("belong to a VM when attached") do + vbds.each do |vbd| + test("#{vbd.uuid}") do + if vbd.currently_attached + (vbd.__vm =~ /OpaqueRef/).eql?(0) and \ + vbd.__vm != "OpaqueRef:NULL" + else + true + end + end + end + end vbds.each do |vbd| test("return a Fog::Compute::XenServer::VDI when type Disk") do if vbd.type == 'Disk' @@ -60,7 +91,19 @@ Shindo.tests('Fog::Compute[:xenserver] | VBD model', ['VBD']) do tests("return valid Server") do test("should be a Fog::Compute::XenServer::Server") { vbd.server.kind_of? Fog::Compute::XenServer::Server } end + test("return a VbdMetrics object") { vbd.metrics.kind_of? Fog::Compute::XenServer::VbdMetrics } + test("be able to be unplugged when type is CD") do + if vbd.type == "CD" + vbd.unpluggable == true + else + vbd.unpluggable == false + end + end end + tests("VBD Metrics should") do + test("have a last_updated Time property") { vbd.metrics.last_updated.kind_of? Time } + end + end