Skip to content

Commit

Permalink
Merge pull request ManageIQ#17545 from bdunne/schedule_changes_move_o…
Browse files Browse the repository at this point in the history
…ut_of_reserves

Remove hacked relations
  • Loading branch information
Fryguy authored Aug 1, 2018
2 parents ee216b0 + 1ff0da7 commit 4a9c9d7
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 60 deletions.
37 changes: 16 additions & 21 deletions app/models/miq_schedule.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class MiqSchedule < ApplicationRecord
include ReservedMixin
reserve_attribute :resource_id, :big_integer
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 @@ -14,6 +14,7 @@ class MiqSchedule < ApplicationRecord

belongs_to :file_depot
belongs_to :miq_search
belongs_to :resource, :polymorphic => true
belongs_to :zone

scope :in_zone, lambda { |zone_name|
Expand All @@ -24,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 @@ -42,15 +43,9 @@ class MiqSchedule < ApplicationRecord
default_value_for :enabled, true
default_value_for(:zone_id) { MiqServer.my_server.zone_id }

def resource
# HACK: this should be a real relation, but for now it's using a reserve_attribute for backport reasons
return unless resource_id
towhat.safe_constantize.find_by(:id => resource_id)
end

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 @@ -78,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 @@ -91,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 @@ -127,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
9 changes: 2 additions & 7 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ServiceTemplate < ApplicationRecord
belongs_to :service_template_catalog

has_many :dialogs, -> { distinct }, :through => :resource_actions
has_many :miq_schedules, :as => :resource, :dependent => :destroy

has_many :miq_requests, :as => :source, :dependent => :nullify
has_many :active_requests, -> { where(:request_state => MiqRequest::ACTIVE_STATES) }, :as => :source, :class_name => "MiqRequest"
Expand Down Expand Up @@ -394,11 +395,6 @@ def queue_order(user_id, options, request_options)
)
end

def miq_schedules
schedule_ids = Reserve.where(:resource_type => "MiqSchedule").collect { |r| r.resource_id if r.reserved == {:resource_id => id} }.compact
MiqSchedule.where(:towhat => "ServiceTemplate", :id => schedule_ids)
end

def order(user_or_id, options = nil, request_options = nil, schedule_time = nil)
user = user_or_id.kind_of?(User) ? user_or_id : User.find(user_or_id)
workflow = provision_workflow(user, options, request_options)
Expand All @@ -413,8 +409,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
14 changes: 7 additions & 7 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 Expand Up @@ -729,7 +729,7 @@
end

it "and does not respond to the method" do
schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource.id, :sched_action => {:method => "test_method"})
schedule = FactoryGirl.create(:miq_schedule, :resource => resource, :sched_action => {:method => "test_method"})

expect($log).to receive(:warn) do |message|
expect(message).to include("no such action: [test_method], aborting schedule")
Expand All @@ -739,17 +739,17 @@
end

it "and responds to the method" do
schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource.id, :sched_action => {:method => "test_method"})
schedule = FactoryGirl.create(:miq_schedule, :resource => resource, :sched_action => {:method => "name"})

expect(resource).to receive("test_method").once
expect_any_instance_of(Host).to receive("name").once

MiqSchedule.queue_scheduled_work(schedule.id, nil, "abc", nil)
end

it "and responds to the method with arguments" do
schedule = FactoryGirl.create(:miq_schedule, :towhat => resource.class.name, :resource_id => resource.id, :sched_action => {:method => "test_method", :args => ["abc", 123, :a => 1]})
schedule = FactoryGirl.create(:miq_schedule, :resource => resource, :sched_action => {:method => "name", :args => ["abc", 123, :a => 1]})

expect(resource).to receive("test_method").once.with("abc", 123, :a => 1)
expect_any_instance_of(Host).to receive("name").once.with("abc", 123, :a => 1)

MiqSchedule.queue_scheduled_work(schedule.id, nil, "abc", nil)
end
Expand Down
3 changes: 1 addition & 2 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,7 @@
expect(result[:schedule]).to have_attributes(
:name => "Order ServiceTemplate #{service_template.id} at #{time}",
:sched_action => {:args => [user.id, {}, {}], :method => "queue_order"},
:towhat => "ServiceTemplate",
:resource_id => service_template.id
:resource => service_template
)
end

Expand Down

0 comments on commit 4a9c9d7

Please sign in to comment.