Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for VM delete #184

Merged
merged 1 commit into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/models/manageiq/providers/vmware/cloud_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agrare we have modified the code to resemble the behaviour of VCD. Now, it is not possible to delete a VM that is running (powered on). To this end we had to redefine the supports_terminate? by looking at the current_state of the VM.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I like that, we actually have a helper method for this that you can use, https://github.com/ManageIQ/manageiq/blob/master/app/models/vm/operations/power.rb#L22-L24

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @agrare. I’ll make another PR to use this.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking for exceptions we are simply passing to the underlying provider connection. Looking at OpenStack and Amazon, this is exactly how these two are handled. In case an exception occurs it will be propagated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 perfect

service.process_task(response.body)
end
update_attributes!(:raw_power_state => "off")
end
end
15 changes: 15 additions & 0 deletions spec/models/manageiq/providers/vmware/cloud_manager/vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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