From 042780967d87c0bc9524a57d71cf174099398791 Mon Sep 17 00:00:00 2001 From: Vojko Rozic Date: Mon, 19 Feb 2018 11:53:17 +0100 Subject: [PATCH] Add support for VM delete This commit now adds support for removal of a stopped VM from vApp. Besides removal operation it overrides the `supports :terminate` feature because the VM must be powered off to allow to be deleted from VCD. Signed-off-by: Vojko Rozic Signed-off-by: Gregor Berginc --- .../manageiq/providers/vmware/cloud_manager.rb | 6 ++++++ .../vmware/cloud_manager/vm/operations.rb | 17 +++++++++++++++++ .../providers/vmware/cloud_manager/vm_spec.rb | 15 +++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/app/models/manageiq/providers/vmware/cloud_manager.rb b/app/models/manageiq/providers/vmware/cloud_manager.rb index 5a6e5da89..4d19777de 100644 --- a/app/models/manageiq/providers/vmware/cloud_manager.rb +++ b/app/models/manageiq/providers/vmware/cloud_manager.rb @@ -81,6 +81,12 @@ def vm_restart(vm, _options = {}) $vcloud_log.error("vm=[#{vm.name}], error: #{err}") end + def vm_destroy(vm, _options = {}) + vm.vm_destroy + rescue => err + $vcloud_log.error("vm=[#{vm.name}], error: #{err}") + end + def self.display_name(number = 1) n_('Cloud Provider (VMware vCloud)', 'Cloud Providers (VMware vCloud)', number) end diff --git a/app/models/manageiq/providers/vmware/cloud_manager/vm/operations.rb b/app/models/manageiq/providers/vmware/cloud_manager/vm/operations.rb index b6bf7cb2b..94a04d64f 100644 --- a/app/models/manageiq/providers/vmware/cloud_manager/vm/operations.rb +++ b/app/models/manageiq/providers/vmware/cloud_manager/vm/operations.rb @@ -1,3 +1,20 @@ module ManageIQ::Providers::Vmware::CloudManager::Vm::Operations + extend ActiveSupport::Concern + include_concern 'Power' + + included do + supports :terminate do + unsupported_reason_add(:terminate, "The VM is powered on") unless current_state == "off" + end + end + + def raw_destroy + raise "VM has no #{ui_lookup(:table => "ext_management_systems")}, unable to destroy VM" unless ext_management_system + ext_management_system.with_provider_connection do |service| + response = service.delete_vapp(ems_ref) + service.process_task(response.body) + end + update_attributes!(:raw_power_state => "off") + end end diff --git a/spec/models/manageiq/providers/vmware/cloud_manager/vm_spec.rb b/spec/models/manageiq/providers/vmware/cloud_manager/vm_spec.rb index e452b02ad..5ab36c506 100644 --- a/spec/models/manageiq/providers/vmware/cloud_manager/vm_spec.rb +++ b/spec/models/manageiq/providers/vmware/cloud_manager/vm_spec.rb @@ -35,4 +35,19 @@ include_examples "Vm operation is not available" end end + + context "when destroyed" do + let(:ems) { FactoryGirl.create(:ems_vmware_cloud) } + let(:vm) { FactoryGirl.create(:vm_vmware_cloud, :ext_management_system => ems) } + let(:connection) { double("connection") } + let(:response) { double("response", :body => nil) } + + it "deletes the virtual machine" do + allow(ems).to receive(:with_provider_connection).and_yield(connection) + expect(connection).to receive(:delete_vapp).and_return(response) + expect(connection).to receive(:process_task).and_return(true) + + vm.raw_destroy + end + end end