Skip to content

Commit

Permalink
Merge pull request #19620 from djberg96/queue_name_for_cloud_template…
Browse files Browse the repository at this point in the history
…_ems_operations

Add queue name for cloud template ems operations
  • Loading branch information
agrare authored Dec 10, 2019
2 parents 40d7e17 + ad8db60 commit 257698e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
19 changes: 19 additions & 0 deletions app/models/manageiq/providers/cloud_manager/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand All @@ -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

Expand All @@ -57,19 +64,25 @@ 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}",
:userid => userid
}

queue_opts = {
:class_name => self.class.name,
: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]
}

MiqTask.generic_action_with_callback(task_opts, queue_opts)
end

Expand All @@ -85,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

Expand Down
81 changes: 79 additions & 2 deletions spec/models/manageiq/providers/cloud_manager/template_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
describe TemplateCloud do
describe "actions" 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
expect(subject).to receive(:reconnect_events)
expect(subject).to receive(:classify_with_parent_folder_path)
Expand All @@ -8,4 +12,77 @@
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

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)

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

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

0 comments on commit 257698e

Please sign in to comment.