From a4b3a7207adc8995d3565bc68e45b4b5199538f3 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Thu, 14 Jun 2018 17:03:54 -0400 Subject: [PATCH] Update callers of MiqSchedule#towhat to #resource_type --- app/models/miq_schedule.rb | 26 ++++++++++++------------- app/models/miq_widget.rb | 12 ++++++------ app/models/service_template.rb | 3 +-- db/fixtures/miq_schedules.yml | 4 ++-- spec/factories/miq_schedule.rb | 6 +++--- spec/models/miq_schedule_filter_spec.rb | 24 +++++++++++------------ spec/models/miq_schedule_spec.rb | 4 ++-- 7 files changed, 39 insertions(+), 40 deletions(-) diff --git a/app/models/miq_schedule.rb b/app/models/miq_schedule.rb index fd7c2cace3a..3c3cc36f944 100644 --- a/app/models/miq_schedule.rb +++ b/app/models/miq_schedule.rb @@ -2,8 +2,8 @@ class MiqSchedule < ApplicationRecord include DeprecationMixin deprecate_attribute :towhat, :resource_type - validates :name, :uniqueness => {:scope => [:userid, :towhat]} - validates :name, :description, :towhat, :run_at, :presence => true + validates :name, :uniqueness => {:scope => [:userid, :resource_type]} + validates :name, :description, :resource_type, :run_at, :presence => true validate :validate_run_at, :validate_file_depot before_save :set_start_time_and_prod_default @@ -25,11 +25,11 @@ class MiqSchedule < ApplicationRecord where("updated_at > ?", time) } - scope :filter_matches_with, ->(exp) { where(:filter => exp) } - scope :with_prod_default_not_in, ->(prod) { where.not(:prod_default => prod).or(where(:prod_default => nil)) } - scope :without_adhoc, -> { where(:adhoc => nil) } - scope :with_towhat, ->(towhat) { where(:towhat => towhat) } - scope :with_userid, ->(userid) { where(:userid => userid) } + scope :filter_matches_with, ->(exp) { where(:filter => exp) } + scope :with_prod_default_not_in, ->(prod) { where.not(:prod_default => prod).or(where(:prod_default => nil)) } + scope :without_adhoc, -> { where(:adhoc => nil) } + scope :with_towhat, ->(resource_type) { where(:resource_type => resource_type) } + scope :with_userid, ->(userid) { where(:userid => userid) } serialize :sched_action serialize :filter @@ -45,7 +45,7 @@ class MiqSchedule < ApplicationRecord def set_start_time_and_prod_default run_at # Internally this will correct :start_time to UTC - self.prod_default = "system" if SYSTEM_SCHEDULE_CLASSES.include?(towhat.to_s) + self.prod_default = "system" if SYSTEM_SCHEDULE_CLASSES.include?(resource_type.to_s) end def run_at @@ -73,7 +73,7 @@ def self.queue_scheduled_work(id, _rufus_job_id, at, _params) end method = sched.sched_action[:method] rescue nil - _log.info("Queueing start of schedule id: [#{id}] [#{sched.name}] [#{sched.towhat}] [#{method}]") + _log.info("Queueing start of schedule id: [#{id}] [#{sched.name}] [#{sched.resource_type}] [#{method}]") action = "action_" + method @@ -86,7 +86,7 @@ def self.queue_scheduled_work(id, _rufus_job_id, at, _params) :msg_timeout => 1200 ) - _log.info("Queueing start of schedule id: [#{id}] [#{sched.name}] [#{sched.towhat}] [#{method}]...complete") + _log.info("Queueing start of schedule id: [#{id}] [#{sched.name}] [#{sched.resource_type}] [#{method}]...complete") msg elsif sched.resource.respond_to?(method) sched.resource.send(method, *sched.sched_action[:args]) @@ -122,12 +122,12 @@ def target_ids # Let RBAC evaluate the filter's MiqExpression, and return the first value (the target ids) my_filter = get_filter return [] if my_filter.nil? - Rbac.filtered(towhat, :filter => my_filter).pluck(:id) + Rbac.filtered(resource_type, :filter => my_filter).pluck(:id) end def get_targets # TODO: Add support to invoke_actions, get_targets, and get_filter to call class methods in addition to the normal instance methods - return [Object.const_get(towhat)] if sched_action.kind_of?(Hash) && ALLOWED_CLASS_METHOD_ACTIONS.include?(sched_action[:method]) + return [Object.const_get(resource_type)] if sched_action.kind_of?(Hash) && ALLOWED_CLASS_METHOD_ACTIONS.include?(sched_action[:method]) my_filter = get_filter if my_filter.nil? @@ -135,7 +135,7 @@ def get_targets return [] end - Rbac.filtered(towhat, :filter => my_filter) + Rbac.filtered(resource_type, :filter => my_filter) end def get_filter diff --git a/app/models/miq_widget.rb b/app/models/miq_widget.rb index c6bb9b40a2a..4d799fa0bd4 100644 --- a/app/models/miq_widget.rb +++ b/app/models/miq_widget.rb @@ -521,12 +521,12 @@ def sync_schedule(schedule_info) end sched = MiqSchedule.create!( - :name => description, - :description => description, - :sched_action => {:method => "generate_widget"}, - :filter => MiqExpression.new("=" => {"field" => "MiqWidget-id", "value" => id}), - :towhat => self.class.name, - :run_at => { + :name => description, + :description => description, + :sched_action => {:method => "generate_widget"}, + :filter => MiqExpression.new("=" => {"field" => "MiqWidget-id", "value" => id}), + :resource_type => self.class.name, + :run_at => { :interval => {:value => value, :unit => unit}, :tz => server_tz, :start_time => sched_time diff --git a/app/models/service_template.rb b/app/models/service_template.rb index 59ef13ede18..ba8f03bf939 100644 --- a/app/models/service_template.rb +++ b/app/models/service_template.rb @@ -421,8 +421,7 @@ def order(user_or_id, options = nil, request_options = nil, schedule_time = nil) :name => "Order #{self.class.name} #{id} at #{time}", :description => "Order #{self.class.name} #{id} at #{time}", :sched_action => {:args => [user.id, options, request_options], :method => "queue_order"}, - :resource_id => id, - :towhat => "ServiceTemplate", + :resource => self, :run_at => { :interval => {:unit => "once"}, :start_time => time, diff --git a/db/fixtures/miq_schedules.yml b/db/fixtures/miq_schedules.yml index e14410ff5be..74a8b2bc46c 100644 --- a/db/fixtures/miq_schedules.yml +++ b/db/fixtures/miq_schedules.yml @@ -19,7 +19,7 @@ :value: 1 :sched_action: :method: vm_scan - :towhat: Vm + :resource_type: Vm - :attributes: :name: "Sample: Hourly VM Analysis" :userid: system @@ -40,4 +40,4 @@ :value: 1 :sched_action: :method: vm_scan - :towhat: Vm + :resource_type: Vm diff --git a/spec/factories/miq_schedule.rb b/spec/factories/miq_schedule.rb index 9134c7216d5..829f45c219d 100644 --- a/spec/factories/miq_schedule.rb +++ b/spec/factories/miq_schedule.rb @@ -2,7 +2,7 @@ factory :miq_schedule_validation, :class => :MiqSchedule do sequence(:name) { |n| "schedule_#{seq_padded_for_sorting(n)}" } description "test" - towhat "MiqReport" + resource_type "MiqReport" run_at {} sched_action {} end @@ -12,7 +12,7 @@ sched_action = {:method => "test"} sequence(:name) { |n| "schedule_#{seq_padded_for_sorting(n)}" } description "test" - towhat "MiqReport" + resource_type "MiqReport" run_at run_at sched_action sched_action end @@ -23,7 +23,7 @@ filter = {:uri_parts => {:instance => 'test', :message => 'create'}, :ui => { :ui_attrs => [], :ui_object => {} }, :parameters => {'request' => 'test_request', 'key1' => 'value1'}} sequence(:name) { |n| "automate_schedule_#{seq_padded_for_sorting(n)}" } description "test_automation" - towhat "AutomationRequest" + resource_type "AutomationRequest" run_at run_at sched_action sched_action filter filter diff --git a/spec/models/miq_schedule_filter_spec.rb b/spec/models/miq_schedule_filter_spec.rb index a84435d0506..28fae24dc8c 100644 --- a/spec/models/miq_schedule_filter_spec.rb +++ b/spec/models/miq_schedule_filter_spec.rb @@ -10,15 +10,15 @@ @vm4 = FactoryGirl.create(:vm_vmware, :name => "Special Test VM") @vm_single_schedule = FactoryGirl.create(:miq_schedule, - :towhat => "Vm", - :sched_action => {:method => "vm_scan"}, - :filter => MiqExpression.new("=" => {"field" => "Vm-name", "value" => "Special Test VM"}) + :resource_type => "Vm", + :sched_action => {:method => "vm_scan"}, + :filter => MiqExpression.new("=" => {"field" => "Vm-name", "value" => "Special Test VM"}) ) @vm_all_schedule = FactoryGirl.create(:miq_schedule, - :towhat => "Vm", - :sched_action => {:method => "vm_scan"}, - :filter => MiqExpression.new("IS NOT NULL" => {"field" => "Vm-name"}) + :resource_type => "Vm", + :sched_action => {:method => "vm_scan"}, + :filter => MiqExpression.new("IS NOT NULL" => {"field" => "Vm-name"}) ) # Schedule froma saved search @@ -27,15 +27,15 @@ :filter => MiqExpression.new("=" => {"field" => "Vm-name", "value" => "Test VM 2"}) ) @vm_search_schedule = FactoryGirl.create(:miq_schedule, - :towhat => "Vm", + :resource_type => "Vm", :sched_action => {:method => "vm_scan"}, :miq_search_id => @search.id ) # DB Baskup Schedule @db_backup = FactoryGirl.create(:miq_schedule, - :towhat => "DatabaseBackup", - :sched_action => {:method => "db_backup"} + :resource_type => "DatabaseBackup", + :sched_action => {:method => "db_backup"} ) end @@ -44,9 +44,9 @@ MiqReport.seed_report("Vendor and Guest OS") @report = MiqReport.first @report_schedule = FactoryGirl.create(:miq_schedule, - :towhat => "MiqReport", - :sched_action => {:method => "run_report"}, - :filter => MiqExpression.new("=" => {"field" => "MiqReport-id", "value" => @report.id}) + :resource_type => "MiqReport", + :sched_action => {:method => "run_report"}, + :filter => MiqExpression.new("=" => {"field" => "MiqReport-id", "value" => @report.id}) ) end diff --git a/spec/models/miq_schedule_spec.rb b/spec/models/miq_schedule_spec.rb index 181c58bc9d8..3d6facb0a48 100644 --- a/spec/models/miq_schedule_spec.rb +++ b/spec/models/miq_schedule_spec.rb @@ -514,7 +514,7 @@ context "valid action_automation_request" do let(:admin) { FactoryGirl.create(:user_miq_request_approver) } let(:automate_sched) do - MiqSchedule.create(:name => "test_method", :towhat => "AutomationRequest", + MiqSchedule.create(:name => "test_method", :resource_type => "AutomationRequest", :userid => admin.userid, :enabled => true, :run_at => {:interval => {:value => "1", :unit => "daily"}, :start_time => 2.hours.from_now.utc.to_i}, @@ -541,7 +541,7 @@ before do @valid_schedules = [] @valid_run_ats.each do |run_at| - @valid_schedules << FactoryGirl.create(:miq_schedule_validation, :run_at => run_at, :file_depot => file_depot, :sched_action => {:method => "db_backup"}, :towhat => "DatabaseBackup") + @valid_schedules << FactoryGirl.create(:miq_schedule_validation, :run_at => run_at, :file_depot => file_depot, :sched_action => {:method => "db_backup"}, :resource_type => "DatabaseBackup") end @schedule = @valid_schedules.first end