From 0cfac7662e2f74fe00107938377d5af566872787 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Mon, 9 Dec 2019 13:58:53 -0500 Subject: [PATCH 1/4] Add queue_name and specs to the create_image_queue method. --- .../providers/cloud_manager/template.rb | 9 ++++++ .../providers/cloud_manager/template_spec.rb | 28 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/models/manageiq/providers/cloud_manager/template.rb b/app/models/manageiq/providers/cloud_manager/template.rb index 8a06ea98f21..570466c8f2c 100644 --- a/app/models/manageiq/providers/cloud_manager/template.rb +++ b/app/models/manageiq/providers/cloud_manager/template.rb @@ -24,6 +24,11 @@ def self.class_by_ems(ext_management_system) ext_management_system.class::Template end + # Create a cloud image as a queued task and return the task id. The queue + # name and the queue zone are derived from the provided EMS instance. The EMS + # instance and a userid are mandatory. Any +options+ are forwarded as + # arguments to the +create_image+ method. + # def self.create_image_queue(userid, ext_management_system, options = {}) task_opts = { :action => "Creating Cloud Template for user #{userid}", @@ -35,8 +40,10 @@ def self.create_image_queue(userid, ext_management_system, options = {}) :method_name => 'create_image', :role => 'ems_operations', :zone => ext_management_system.my_zone, + :queue_name => ext_management_system.queue_name_for_ems_operations, :args => [ext_management_system.id, options] } + MiqTask.generic_action_with_callback(task_opts, queue_opts) end @@ -62,6 +69,7 @@ def update_image_queue(userid, options = {}) :action => "updating Cloud Template for user #{userid}", :userid => userid } + queue_opts = { :class_name => self.class.name, :method_name => 'update_image', @@ -70,6 +78,7 @@ def update_image_queue(userid, options = {}) :zone => ext_management_system.my_zone, :args => [options] } + MiqTask.generic_action_with_callback(task_opts, queue_opts) end diff --git a/spec/models/manageiq/providers/cloud_manager/template_spec.rb b/spec/models/manageiq/providers/cloud_manager/template_spec.rb index ecdc5a0818f..38945bd7f6f 100644 --- a/spec/models/manageiq/providers/cloud_manager/template_spec.rb +++ b/spec/models/manageiq/providers/cloud_manager/template_spec.rb @@ -1,5 +1,8 @@ -describe TemplateCloud do - describe "actions" do +RSpec.describe TemplateCloud do + let(:ems) { FactoryBot.create(:ems_cloud) } + let(:user) { FactoryBot.create(:user, :userid => 'test') } + + context "actions" do it "#post_create_actions" do expect(subject).to receive(:reconnect_events) expect(subject).to receive(:classify_with_parent_folder_path) @@ -8,4 +11,25 @@ subject.post_create_actions end end + + context "queued methods" do + it 'queues a create task with create_image_queue' do + task_id = described_class.create_image_queue(user.userid, ems) + + expect(MiqTask.find(task_id)).to have_attributes( + :name => "Creating Cloud Template for user #{user.userid}", + :state => "Queued", + :status => "Ok" + ) + + expect(MiqQueue.where(:class_name => described_class.name).first).to have_attributes( + :class_name => described_class.name, + :method_name => 'create_image', + :role => 'ems_operations', + :queue_name => 'generic', + :zone => ems.my_zone, + :args => [ems.id, {}] + ) + end + end end From f16e34d2907c7ed936986f660048aa8e5266c80c Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 10 Dec 2019 08:29:20 -0500 Subject: [PATCH 2/4] Add comments to update_image_queue. --- app/models/manageiq/providers/cloud_manager/template.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/models/manageiq/providers/cloud_manager/template.rb b/app/models/manageiq/providers/cloud_manager/template.rb index 570466c8f2c..10f460b5781 100644 --- a/app/models/manageiq/providers/cloud_manager/template.rb +++ b/app/models/manageiq/providers/cloud_manager/template.rb @@ -64,6 +64,9 @@ def self.create_image(ems_id, options) klass.raw_create_image(ext_management_system, options) end + # Update a cloud template as a queued task and return the task id. The queue + # name and the queue zone are derived from the EMS, and a userid is mandatory. + # def update_image_queue(userid, options = {}) task_opts = { :action => "updating Cloud Template for user #{userid}", From e9f624764999777f30d8763eb70158792e08f13a Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 10 Dec 2019 09:06:57 -0500 Subject: [PATCH 3/4] Add queue name to update task, change described class to actual name. --- .../providers/cloud_manager/template.rb | 1 + .../providers/cloud_manager/template_spec.rb | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/models/manageiq/providers/cloud_manager/template.rb b/app/models/manageiq/providers/cloud_manager/template.rb index 10f460b5781..693ed8a1e1c 100644 --- a/app/models/manageiq/providers/cloud_manager/template.rb +++ b/app/models/manageiq/providers/cloud_manager/template.rb @@ -78,6 +78,7 @@ def update_image_queue(userid, options = {}) :method_name => 'update_image', :instance_id => id, :role => 'ems_operations', + :queue_name => ext_management_system.queue_name_for_ems_operations, :zone => ext_management_system.my_zone, :args => [options] } diff --git a/spec/models/manageiq/providers/cloud_manager/template_spec.rb b/spec/models/manageiq/providers/cloud_manager/template_spec.rb index 38945bd7f6f..6d43c5bd090 100644 --- a/spec/models/manageiq/providers/cloud_manager/template_spec.rb +++ b/spec/models/manageiq/providers/cloud_manager/template_spec.rb @@ -1,6 +1,7 @@ -RSpec.describe TemplateCloud do +RSpec.describe ManageIQ::Providers::CloudManager::Template do let(:ems) { FactoryBot.create(:ems_cloud) } let(:user) { FactoryBot.create(:user, :userid => 'test') } + let(:cloud_template) { FactoryBot.create(:template_cloud, :ext_management_system => ems) } context "actions" do it "#post_create_actions" do @@ -31,5 +32,25 @@ :args => [ems.id, {}] ) end + + it 'queues an update task with update_image_queue' do + options = {:name => 'updated_image_name'} + task_id = cloud_template.update_image_queue(user.userid, options) + + expect(MiqTask.find(task_id)).to have_attributes( + :name => "updating Cloud Template for user #{user.userid}", + :state => "Queued", + :status => "Ok" + ) + + expect(MiqQueue.where(:class_name => cloud_template.class.name).first).to have_attributes( + :class_name => cloud_template.class.name, + :method_name => 'update_image', + :role => 'ems_operations', + :queue_name => 'generic', + :zone => ems.my_zone, + :args => [options] + ) + end end end From ad8db60e8b7709127a3c95792d05939f48a2bd5a Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 10 Dec 2019 09:15:58 -0500 Subject: [PATCH 4/4] Add specs for queued delete task, plus comments. --- .../providers/cloud_manager/template.rb | 6 ++++ .../providers/cloud_manager/template_spec.rb | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/app/models/manageiq/providers/cloud_manager/template.rb b/app/models/manageiq/providers/cloud_manager/template.rb index 693ed8a1e1c..42b6666793a 100644 --- a/app/models/manageiq/providers/cloud_manager/template.rb +++ b/app/models/manageiq/providers/cloud_manager/template.rb @@ -98,19 +98,25 @@ def raw_update_image(_options = {}) raise NotImplementedError, _("raw_update_image must be implemented in a subclass") end + # Delete a cloud template as a queued task and return the task id. The queue + # name and the queue zone are derived from the EMS, and a userid is mandatory. + # def delete_image_queue(userid) task_opts = { :action => "Deleting Cloud Template for user #{userid}", :userid => userid } + queue_opts = { :class_name => self.class.name, :method_name => 'delete_image', :instance_id => id, :role => 'ems_operations', + :queue_name => ext_management_system.queue_name_for_ems_operations, :zone => ext_management_system.my_zone, :args => [] } + MiqTask.generic_action_with_callback(task_opts, queue_opts) end diff --git a/spec/models/manageiq/providers/cloud_manager/template_spec.rb b/spec/models/manageiq/providers/cloud_manager/template_spec.rb index 6d43c5bd090..c22a9bef53d 100644 --- a/spec/models/manageiq/providers/cloud_manager/template_spec.rb +++ b/spec/models/manageiq/providers/cloud_manager/template_spec.rb @@ -33,6 +33,11 @@ ) end + it 'requires a userid and ems for a queued create task' do + expect { described_class.create_image_queue }.to raise_error(ArgumentError) + expect { described_class.create_image_queue(user.userid) }.to raise_error(ArgumentError) + end + it 'queues an update task with update_image_queue' do options = {:name => 'updated_image_name'} task_id = cloud_template.update_image_queue(user.userid, options) @@ -52,5 +57,32 @@ :args => [options] ) end + + it 'requires a userid for a queued update task' do + expect { cloud_template.update_image_queue }.to raise_error(ArgumentError) + end + + it 'queues a delete task with delete_image_queue' do + task_id = cloud_template.delete_image_queue(user.userid) + + expect(MiqTask.find(task_id)).to have_attributes( + :name => "Deleting Cloud Template for user #{user.userid}", + :state => "Queued", + :status => "Ok" + ) + + expect(MiqQueue.where(:class_name => described_class.name).first).to have_attributes( + :class_name => described_class.name, + :method_name => 'delete_image', + :role => 'ems_operations', + :queue_name => 'generic', + :zone => ems.my_zone, + :args => [] + ) + end + + it 'requires a userid for a queued delete task' do + expect { cloud_template.delete_image_queue }.to raise_error(ArgumentError) + end end end