From 1d76ae7169706dbb45a4dcd954204a054085b6c6 Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 3 Dec 2019 14:57:18 -0500 Subject: [PATCH 1/2] Add queue name and specs for cloud volume queue tasks. --- app/models/cloud_volume.rb | 21 +++++++- spec/models/cloud_volume_spec.rb | 82 ++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 6 deletions(-) diff --git a/app/models/cloud_volume.rb b/app/models/cloud_volume.rb index 13e79e13528..6d6fde0a6c2 100644 --- a/app/models/cloud_volume.rb +++ b/app/models/cloud_volume.rb @@ -35,18 +35,26 @@ def self.class_by_ems(ext_management_system) ext_management_system && ext_management_system.class::CloudVolume end + # Create a cloud volume 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_volume+ method. + # def self.create_volume_queue(userid, ext_management_system, options = {}) task_opts = { :action => "creating Cloud Volume for user #{userid}", :userid => userid } + queue_opts = { - :class_name => "CloudVolume", + :class_name => 'CloudVolume', :method_name => 'create_volume', :role => 'ems_operations', + :queue_name => ext_management_system.queue_name_for_ems_operations, :zone => ext_management_system.my_zone, :args => [ext_management_system.id, options] } + MiqTask.generic_action_with_callback(task_opts, queue_opts) end @@ -70,11 +78,15 @@ def self.raw_create_volume(_ext_management_system, _options = {}) raise NotImplementedError, _("raw_create_volume must be implemented in a subclass") end + # Update a cloud volume 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_volume_queue(userid, options = {}) task_opts = { :action => "updating Cloud Volume for user #{userid}", :userid => userid } + queue_opts = { :class_name => self.class.name, :method_name => 'update_volume', @@ -83,6 +95,7 @@ def update_volume_queue(userid, options = {}) :zone => ext_management_system.my_zone, :args => [options] } + MiqTask.generic_action_with_callback(task_opts, queue_opts) end @@ -98,19 +111,25 @@ def raw_update_volume(_options = {}) raise NotImplementedError, _("raw_update_volume must be implemented in a subclass") end + # Delete a cloud volume 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_volume_queue(userid) task_opts = { :action => "deleting Cloud Volume for user #{userid}", :userid => userid } + queue_opts = { :class_name => self.class.name, :method_name => 'delete_volume', :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/cloud_volume_spec.rb b/spec/models/cloud_volume_spec.rb index e5736b94895..5a21e555bfc 100644 --- a/spec/models/cloud_volume_spec.rb +++ b/spec/models/cloud_volume_spec.rb @@ -1,9 +1,81 @@ -describe CloudVolume do +RSpec.describe CloudVolume do + let(:disks) { FactoryBot.create_list(:disk, 2) } + let(:ems) { FactoryBot.create(:ems_vmware) } + let(:cloud_volume) { FactoryBot.create(:cloud_volume, :ext_management_system => ems, :attachments => disks) } + let(:user) { FactoryBot.create(:user, :userid => 'test') } + it ".available" do - disk = FactoryBot.create(:disk) - FactoryBot.create(:cloud_volume, :attachments => [disk]) - cv2 = FactoryBot.create(:cloud_volume) + cloud_volumes_no_backing_disks = FactoryBot.create_list(:cloud_volume, 2) + expect(described_class.available).to eq(cloud_volumes_no_backing_disks) + end + + context 'queued methods' do + it 'queues a create task with create_volume_queue' do + task_id = described_class.create_volume_queue(user.userid, ems) + + expect(MiqTask.find(task_id)).to have_attributes( + :name => "creating Cloud Volume 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_volume', + :role => 'ems_operations', + :zone => ems.my_zone, + :args => [ems.id, {}] + ) + end + + it 'requires a userid and ems for a queued create task' do + expect { described_class.create_volume_queue }.to raise_error(ArgumentError) + expect { described_class.create_volume_queue(user.userid) }.to raise_error(ArgumentError) + end + + it 'queues an update task with update_volume_queue' do + options = {:name => 'updated_volume_name'} + task_id = cloud_volume.update_volume_queue(user.userid, options) + + expect(MiqTask.find(task_id)).to have_attributes( + :name => "updating Cloud Volume 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 => 'update_volume', + :role => 'ems_operations', + :zone => ems.my_zone, + :args => [options] + ) + end + + it 'requires a userid for a queued update task' do + expect { cloud_volume.update_volume_queue }.to raise_error(ArgumentError) + end + + it 'queues a delete task with delete_volume_queue' do + task_id = cloud_volume.delete_volume_queue(user.userid) + + expect(MiqTask.find(task_id)).to have_attributes( + :name => "deleting Cloud Volume 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_volume', + :role => 'ems_operations', + :zone => ems.my_zone, + :args => [] + ) + end - expect(described_class.available).to eq([cv2]) + it 'requires a userid for a queued delete task' do + expect { cloud_volume.delete_volume_queue }.to raise_error(ArgumentError) + end end end From 335c620b103815a94c39224b43e3e8dfa09a8a1b Mon Sep 17 00:00:00 2001 From: Daniel Berger Date: Tue, 3 Dec 2019 15:11:37 -0500 Subject: [PATCH 2/2] Add queue_name to update queue, test queue name. --- app/models/cloud_volume.rb | 1 + spec/models/cloud_volume_spec.rb | 3 +++ 2 files changed, 4 insertions(+) diff --git a/app/models/cloud_volume.rb b/app/models/cloud_volume.rb index 6d6fde0a6c2..610a9938382 100644 --- a/app/models/cloud_volume.rb +++ b/app/models/cloud_volume.rb @@ -92,6 +92,7 @@ def update_volume_queue(userid, options = {}) :method_name => 'update_volume', :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/cloud_volume_spec.rb b/spec/models/cloud_volume_spec.rb index 5a21e555bfc..6105fe7f121 100644 --- a/spec/models/cloud_volume_spec.rb +++ b/spec/models/cloud_volume_spec.rb @@ -23,6 +23,7 @@ :class_name => described_class.name, :method_name => 'create_volume', :role => 'ems_operations', + :queue_name => 'generic', :zone => ems.my_zone, :args => [ems.id, {}] ) @@ -47,6 +48,7 @@ :class_name => described_class.name, :method_name => 'update_volume', :role => 'ems_operations', + :queue_name => 'generic', :zone => ems.my_zone, :args => [options] ) @@ -69,6 +71,7 @@ :class_name => described_class.name, :method_name => 'delete_volume', :role => 'ems_operations', + :queue_name => 'generic', :zone => ems.my_zone, :args => [] )