Skip to content

Commit

Permalink
Update callers of MiqSchedule#towhat to #resource_type
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunne committed Jul 23, 2018
1 parent 8829e85 commit a4b3a72
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 40 deletions.
26 changes: 13 additions & 13 deletions app/models/miq_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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])
Expand Down Expand Up @@ -122,20 +122,20 @@ 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?
_log.warn("[#{name}] Filter is empty")
return []
end

Rbac.filtered(towhat, :filter => my_filter)
Rbac.filtered(resource_type, :filter => my_filter)
end

def get_filter
Expand Down
12 changes: 6 additions & 6 deletions app/models/miq_widget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions db/fixtures/miq_schedules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
:value: 1
:sched_action:
:method: vm_scan
:towhat: Vm
:resource_type: Vm
- :attributes:
:name: "Sample: Hourly VM Analysis"
:userid: system
Expand All @@ -40,4 +40,4 @@
:value: 1
:sched_action:
:method: vm_scan
:towhat: Vm
:resource_type: Vm
6 changes: 3 additions & 3 deletions spec/factories/miq_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
24 changes: 12 additions & 12 deletions spec/models/miq_schedule_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

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

Expand Down
4 changes: 2 additions & 2 deletions spec/models/miq_schedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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
Expand Down

0 comments on commit a4b3a72

Please sign in to comment.