-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18191 from jameswnl/v2vhost
Use MiqQueue for enabling/disabling a conversion_host (cherry picked from commit d4560c6) https://bugzilla.redhat.com/show_bug.cgi?id=1628489
- Loading branch information
Showing
5 changed files
with
206 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module ConversionHost::Configurations | ||
extend ActiveSupport::Concern | ||
module ClassMethods | ||
def notify_configuration_result(op, success, resource_info) | ||
Notification.create( | ||
:type => success ? :conversion_host_config_success : :conversion_host_config_failure, | ||
:options => { | ||
:op_name => op, | ||
:op_arg => resource_info, | ||
} | ||
) | ||
end | ||
|
||
def queue_configuration(op, instance_id, resource, params, auth_user = nil) | ||
task_opts = { | ||
:action => "Configuring a conversion_host: operation=#{op} resource=(type: #{resource.class.name} id:#{resource.id})", | ||
:userid => auth_user | ||
} | ||
queue_opts = { | ||
:class_name => name, | ||
:method_name => op, | ||
:instance_id => instance_id, | ||
:role => 'ems_operations', | ||
:zone => resource.ext_management_system.my_zone, | ||
:args => params | ||
} | ||
MiqTask.generic_action_with_callback(task_opts, queue_opts) | ||
end | ||
|
||
def enable_queue(params, auth_user = nil) | ||
resource_type = params[:resource_type] | ||
resource_id = params[:resource_id] | ||
resource = resource_type.constantize.find(resource_id) | ||
|
||
queue_configuration('enable', nil, resource, [params], auth_user) | ||
end | ||
|
||
def enable(params) | ||
_log.info("Enabling a conversion_host with parameters: #{params}") | ||
params.delete(:task_id) # in case this is being called through *_queue which will stick in a :task_id | ||
params.delete(:miq_task_id) # miq_queue.activate_miq_task will stick in a :miq_task_id | ||
|
||
vddk_url = params.delete("param_v2v_vddk_package_url") | ||
resource_id = params[:resource_id] | ||
resource_type = params[:resource_type] | ||
resource = resource_type.constantize.find(resource_id) | ||
|
||
conversion_host = new(params.merge(:resource => resource)) | ||
conversion_host.enable_conversion_host_role(vddk_url) | ||
conversion_host.save! | ||
conversion_host | ||
rescue StandardError => error | ||
raise | ||
ensure | ||
resource_info = "type=#{params[:resource_type]} id=#{params[:resource_id]}" | ||
notify_configuration_result('enable', error.nil?, resource_info) | ||
end | ||
end | ||
|
||
def disable_queue(auth_user = nil) | ||
self.class.queue_configuration('disable', id, resource, [], auth_user) | ||
end | ||
|
||
def disable | ||
resource_info = "type=#{resource.class.name} id=#{resource.id}" | ||
_log.info("Disabling a conversion_host #{resource_info}") | ||
|
||
disable_conversion_host_role | ||
destroy! | ||
rescue StandardError => error | ||
raise | ||
ensure | ||
self.class.notify_configuration_result('disable', error.nil?, resource_info) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FactoryGirl.define do | ||
factory :conversion_host do | ||
sequence(:name) { |n| "conversion_host_#{seq_padded_for_sorting(n)}" } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
describe ConversionHost do | ||
let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => vm) } | ||
let(:params) do | ||
{ | ||
:name => 'transformer', | ||
:resource_type => vm.class.name, | ||
:resource_id => vm.id | ||
} | ||
end | ||
|
||
context "processing configuration requests" do | ||
let(:vm) { FactoryGirl.create(:vm) } | ||
before(:each) do | ||
allow(ConversionHost).to receive(:new).and_return(conversion_host) | ||
end | ||
context ".enable" do | ||
let(:expected_notify) do | ||
{ | ||
:type => :conversion_host_config_success, | ||
:options => { | ||
:op_name => "enable", | ||
:op_arg => "type=#{params[:resource_type]} id=#{params[:resource_id]}" | ||
} | ||
} | ||
end | ||
it "to succeed and send notification" do | ||
allow(conversion_host).to receive(:enable_conversion_host_role) | ||
expect(Notification).to receive(:create).with(expected_notify) | ||
expect(described_class.enable(params)).to be_a(described_class) | ||
end | ||
|
||
it "to fail and send notification" do | ||
expected_notify[:type] = :conversion_host_config_failure | ||
allow(conversion_host).to receive(:enable_conversion_host_role).and_raise | ||
expect(Notification).to receive(:create).with(expected_notify) | ||
expect { described_class.enable(params) }.to raise_error(StandardError) | ||
end | ||
end | ||
|
||
context "#disable" do | ||
let(:expected_notify) do | ||
{ | ||
:type => :conversion_host_config_success, | ||
:options => { | ||
:op_name => "disable", | ||
:op_arg => "type=#{vm.class.name} id=#{vm.id}" | ||
} | ||
} | ||
end | ||
|
||
it "to succeed and send notification" do | ||
allow(conversion_host).to receive(:disable_conversion_host_role) | ||
expect(Notification).to receive(:create).with(expected_notify) | ||
conversion_host.disable | ||
end | ||
|
||
it "to fail and send notification" do | ||
expected_notify[:type] = :conversion_host_config_failure | ||
allow(conversion_host).to receive(:disable_conversion_host_role).and_raise | ||
expect(Notification).to receive(:create).with(expected_notify) | ||
expect { conversion_host.disable }.to raise_error(StandardError) | ||
end | ||
end | ||
end | ||
|
||
context "queuing configuration requests" do | ||
let(:ext_management_system) { FactoryGirl.create(:ext_management_system) } | ||
let(:vm) { FactoryGirl.create(:vm, :ext_management_system => ext_management_system) } | ||
let(:expected_task_action) { "Configuring a conversion_host: operation=#{op} resource=(type: #{vm.class.name} id:#{vm.id})" } | ||
|
||
context ".enable_queue" do | ||
let(:op) { 'enable' } | ||
|
||
it "to queue with a task" do | ||
task_id = described_class.enable_queue(params) | ||
expect(MiqTask.find(task_id)).to have_attributes(:name => expected_task_action) | ||
expect(MiqQueue.first).to have_attributes( | ||
:args => [params], | ||
:class_name => described_class.name, | ||
:method_name => "enable", | ||
:priority => MiqQueue::NORMAL_PRIORITY, | ||
:role => "ems_operations", | ||
:zone => vm.ext_management_system.my_zone | ||
) | ||
end | ||
|
||
it "to fail when the resource is not found" do | ||
vm.destroy! | ||
expect { described_class.enable_queue(params) }.to raise_error(ActiveRecord::RecordNotFound) | ||
end | ||
end | ||
|
||
context "#disable_queue" do | ||
let(:op) { 'disable' } | ||
|
||
it "to queue with a task" do | ||
task_id = conversion_host.disable_queue | ||
expect(MiqTask.find(task_id)).to have_attributes(:name => expected_task_action) | ||
expect(MiqQueue.first).to have_attributes( | ||
:args => [], | ||
:class_name => described_class.name, | ||
:instance_id => conversion_host.id, | ||
:method_name => "disable", | ||
:priority => MiqQueue::NORMAL_PRIORITY, | ||
:role => "ems_operations", | ||
:zone => conversion_host.resource.ext_management_system.my_zone | ||
) | ||
end | ||
end | ||
end | ||
end |