Skip to content

Commit

Permalink
(MODULES-6895) translate_hash_to_trigger -> from_manifest_hash
Browse files Browse the repository at this point in the history
 - Update the V1::Trigger method, renaming it from the less
   descriptive translate_hash_to_trigger to the more descriptive
   from_manifest_hash, given there are a number of Trigger
   representations in-use within the helpers, and it is getting
   confusing.

 - Update tests / call sites accordingly
  • Loading branch information
Iristyle committed Apr 17, 2018
1 parent f445b6b commit ef56798
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
8 changes: 4 additions & 4 deletions lib/puppet/provider/scheduled_task/taskscheduler_api2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def trigger=(value)
# Win32::TaskScheduler ends up appending this trigger to the
# list of triggers for the task, while #add_trigger is only able
# to replace existing triggers. *shrug*
task.trigger = PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.translate_hash_to_trigger(trigger_hash)
task.trigger = PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.from_manifest_hash(trigger_hash)
end
end

Expand Down Expand Up @@ -250,7 +250,7 @@ def flush
def triggers_same?(current_trigger, desired_trigger)
return false unless current_trigger['schedule'] == desired_trigger['schedule']
return false if current_trigger.has_key?('enabled') && !current_trigger['enabled']
return false if PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.translate_hash_to_trigger(desired_trigger)['trigger_type'] != PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.translate_hash_to_trigger(current_trigger)['trigger_type']
return false if PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.from_manifest_hash(desired_trigger)['trigger_type'] != PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.from_manifest_hash(current_trigger)['trigger_type']

desired = desired_trigger.dup
desired['start_date'] ||= current_trigger['start_date'] if current_trigger.has_key?('start_date')
Expand All @@ -259,7 +259,7 @@ def triggers_same?(current_trigger, desired_trigger)
desired['on'] ||= current_trigger['on'] if current_trigger.has_key?('on')
desired['day_of_week'] ||= current_trigger['day_of_week'] if current_trigger.has_key?('day_of_week')

PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.translate_hash_to_trigger(current_trigger) == PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.translate_hash_to_trigger(desired)
PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.from_manifest_hash(current_trigger) == PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.from_manifest_hash(desired)
end

def validate_trigger(value)
Expand All @@ -274,7 +274,7 @@ def validate_trigger(value)
self.fail "'enabled' is read-only on scheduled_task triggers and should be removed ('enabled' is usually provided in puppet resource scheduled_task)."
end

PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.translate_hash_to_trigger(t)
PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.from_manifest_hash(t)
end

true
Expand Down
54 changes: 27 additions & 27 deletions lib/puppet_x/puppetlabs/scheduled_task/trigger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,76 +377,76 @@ def self.time_trigger_once_now
}
end

# puppet_trigger is a hash created from a manifest
def self.translate_hash_to_trigger(puppet_trigger)
# manifest_hash is a hash created from a manifest
def self.from_manifest_hash(manifest_hash)
trigger = time_trigger_once_now

if puppet_trigger['enabled'] == false
if manifest_hash['enabled'] == false
trigger['flags'] |= Flag::TASK_TRIGGER_FLAG_DISABLED
else
trigger['flags'] &= ~Flag::TASK_TRIGGER_FLAG_DISABLED
end

extra_keys = puppet_trigger.keys.sort - ValidManifestKeys
extra_keys = manifest_hash.keys.sort - ValidManifestKeys
raise Puppet::Error.new("Unknown trigger option(s): #{Puppet::Parameter.format_value_for_display(extra_keys)}") unless extra_keys.empty?
raise Puppet::Error.new("Must specify 'start_time' when defining a trigger") unless puppet_trigger['start_time']
raise Puppet::Error.new("Must specify 'start_time' when defining a trigger") unless manifest_hash['start_time']

case puppet_trigger['schedule']
case manifest_hash['schedule']
when 'daily'
trigger['trigger_type'] = :TASK_TIME_TRIGGER_DAILY
trigger['type'] = {
'days_interval' => Integer(puppet_trigger['every'] || 1)
'days_interval' => Integer(manifest_hash['every'] || 1)
}
when 'weekly'
trigger['trigger_type'] = :TASK_TIME_TRIGGER_WEEKLY
trigger['type'] = {
'weeks_interval' => Integer(puppet_trigger['every'] || 1)
'weeks_interval' => Integer(manifest_hash['every'] || 1)
}

days_of_week = puppet_trigger['day_of_week'] || Day.names
days_of_week = manifest_hash['day_of_week'] || Day.names
trigger['type']['days_of_week'] = Day.names_to_bitfield(days_of_week)
when 'monthly'
trigger['type'] = {
'months' => Month.indexes_to_bitfield(puppet_trigger['months'] || (1..12).to_a),
'months' => Month.indexes_to_bitfield(manifest_hash['months'] || (1..12).to_a),
}

if puppet_trigger.keys.include?('on')
if puppet_trigger.has_key?('day_of_week') or puppet_trigger.has_key?('which_occurrence')
if manifest_hash.keys.include?('on')
if manifest_hash.has_key?('day_of_week') or manifest_hash.has_key?('which_occurrence')
raise Puppet::Error.new("Neither 'day_of_week' nor 'which_occurrence' can be specified when creating a monthly date-based trigger")
end

trigger['trigger_type'] = :TASK_TIME_TRIGGER_MONTHLYDATE
trigger['type']['days'] = Days.indexes_to_bitfield(puppet_trigger['on'])
elsif puppet_trigger.keys.include?('which_occurrence') or puppet_trigger.keys.include?('day_of_week')
raise Puppet::Error.new('which_occurrence cannot be specified as an array') if puppet_trigger['which_occurrence'].is_a?(Array)
trigger['type']['days'] = Days.indexes_to_bitfield(manifest_hash['on'])
elsif manifest_hash.keys.include?('which_occurrence') or manifest_hash.keys.include?('day_of_week')
raise Puppet::Error.new('which_occurrence cannot be specified as an array') if manifest_hash['which_occurrence'].is_a?(Array)
%w{day_of_week which_occurrence}.each do |field|
raise Puppet::Error.new("#{field} must be specified when creating a monthly day-of-week based trigger") unless puppet_trigger.has_key?(field)
raise Puppet::Error.new("#{field} must be specified when creating a monthly day-of-week based trigger") unless manifest_hash.has_key?(field)
end

trigger['trigger_type'] = :TASK_TIME_TRIGGER_MONTHLYDOW
trigger['type']['weeks'] = Occurrence.name_to_constant(puppet_trigger['which_occurrence'])
trigger['type']['days_of_week'] = Day.names_to_bitfield(puppet_trigger['day_of_week'])
trigger['type']['weeks'] = Occurrence.name_to_constant(manifest_hash['which_occurrence'])
trigger['type']['days_of_week'] = Day.names_to_bitfield(manifest_hash['day_of_week'])
else
raise Puppet::Error.new("Don't know how to create a 'monthly' schedule with the options: #{puppet_trigger.keys.sort.join(', ')}")
raise Puppet::Error.new("Don't know how to create a 'monthly' schedule with the options: #{manifest_hash.keys.sort.join(', ')}")
end
when 'once'
raise Puppet::Error.new("Must specify 'start_date' when defining a one-time trigger") unless puppet_trigger['start_date']
raise Puppet::Error.new("Must specify 'start_date' when defining a one-time trigger") unless manifest_hash['start_date']

trigger['trigger_type'] = :TASK_TIME_TRIGGER_ONCE
else
raise Puppet::Error.new("Unknown schedule type: #{puppet_trigger["schedule"].inspect}")
raise Puppet::Error.new("Unknown schedule type: #{manifest_hash["schedule"].inspect}")
end

integer_interval = -1
if puppet_trigger['minutes_interval']
integer_interval = Integer(puppet_trigger['minutes_interval'])
if manifest_hash['minutes_interval']
integer_interval = Integer(manifest_hash['minutes_interval'])
raise Puppet::Error.new('minutes_interval must be an integer greater or equal to 0') if integer_interval < 0
trigger['minutes_interval'] = integer_interval
end

integer_duration = -1
if puppet_trigger['minutes_duration']
integer_duration = Integer(puppet_trigger['minutes_duration'])
if manifest_hash['minutes_duration']
integer_duration = Integer(manifest_hash['minutes_duration'])
raise Puppet::Error.new('minutes_duration must be an integer greater than minutes_interval and equal to or greater than 0') if integer_duration <= integer_interval && integer_duration != 0
trigger['minutes_duration'] = integer_duration
end
Expand All @@ -461,7 +461,7 @@ def self.translate_hash_to_trigger(puppet_trigger)
raise Puppet::Error.new('minutes_interval cannot be set without minutes_duration also being set to a number greater than 0')
end

if start_date = puppet_trigger['start_date']
if start_date = manifest_hash['start_date']
start_date = Date.parse(start_date)
raise Puppet::Error.new("start_date must be on or after 1753-01-01") unless start_date >= Date.new(1753, 1, 1)

Expand All @@ -470,7 +470,7 @@ def self.translate_hash_to_trigger(puppet_trigger)
trigger['start_day'] = start_date.day
end

start_time = Time.parse(puppet_trigger['start_time'])
start_time = Time.parse(manifest_hash['start_time'])
trigger['start_hour'] = start_time.hour
trigger['start_minute'] = start_time.min

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
:command => 'C:\Windows\System32\notepad.exe'
).translate_hash_to_trigger(trigger_hash)
else
PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.translate_hash_to_trigger(trigger_hash)
PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.from_manifest_hash(trigger_hash)
end
end

Expand Down Expand Up @@ -1190,7 +1190,7 @@ def time_component
end
end

describe '#translate_hash_to_trigger' do
describe '#from_manifest_hash' do
before :each do
@puppet_trigger = {
'start_date' => '2011-1-1',
Expand All @@ -1202,7 +1202,7 @@ def time_component
if provider.is_a?(Puppet::Type::Scheduled_task::ProviderWin32_taskscheduler)
provider.translate_hash_to_trigger(@puppet_trigger)
else
PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.translate_hash_to_trigger(@puppet_trigger)
PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.from_manifest_hash(@puppet_trigger)
end
end

Expand Down Expand Up @@ -1575,7 +1575,7 @@ def time_component
expect(provider.validate_trigger(triggers_to_validate)).to eq(true)
end

it 'should use the exception from translate_hash_to_trigger when it fails' do
it 'should use the exception from from_manifest_hash when it fails' do
triggers_to_validate = [
{'schedule' => 'once', 'start_date' => '2011-09-13', 'start_time' => '13:50'},
{'schedule' => 'monthly', 'this is invalid' => true}
Expand Down Expand Up @@ -1781,7 +1781,7 @@ def time_component
if resource.provider.is_a?(Puppet::Type::Scheduled_task::ProviderWin32_taskscheduler)
translater = resource.provider.method(:translate_hash_to_trigger)
else
translater = PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.method(:translate_hash_to_trigger)
translater = PuppetX::PuppetLabs::ScheduledTask::Trigger::V1.method(:from_manifest_hash)
end
@mock_task.expects(:trigger=).with(translater.call(@trigger[1]))
@mock_task.expects(:trigger=).with(translater.call(@trigger[2]))
Expand Down

0 comments on commit ef56798

Please sign in to comment.