summaryrefslogtreecommitdiff
path: root/lib/chef/resource/helpers/cron_validations.rb
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-03-23 15:06:48 -0700
committerTim Smith <tsmith84@gmail.com>2020-03-23 15:06:48 -0700
commit198913c4676a00ceb31c6d752d54705c8599d96f (patch)
treec108637b2a0e4c6d7861df0cc59d1824f93e4c16 /lib/chef/resource/helpers/cron_validations.rb
parent77a4e649f337cc10ea26866636b77c683ba84f42 (diff)
downloadchef-cron_validations.tar.gz
Make this a helper module not a mixincron_validations
Stop abusing the mixin name and stop using the mixin like the kitchen drawer of junk it's becoming. Signed-off-by: Tim Smith <tsmith@chef.io>
Diffstat (limited to 'lib/chef/resource/helpers/cron_validations.rb')
-rw-r--r--lib/chef/resource/helpers/cron_validations.rb75
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/chef/resource/helpers/cron_validations.rb b/lib/chef/resource/helpers/cron_validations.rb
new file mode 100644
index 0000000000..d09113d64c
--- /dev/null
+++ b/lib/chef/resource/helpers/cron_validations.rb
@@ -0,0 +1,75 @@
+#
+# Copyright:: Copyright 2020, Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+class Chef
+ module ResourceHelpers
+ # a collection of methods for validating cron times. Used in the various cron-like resources
+ module CronValidations
+ # validate a provided value is between two other provided values
+ # we also allow * as a valid input
+ # @param spec the value to validate
+ # @param min the lowest value allowed
+ # @param max the highest value allowed
+ # @return [Boolean] valid or not?
+ def self.validate_numeric(spec, min, max)
+ return true if spec == "*"
+
+ if spec.respond_to? :to_int
+ return spec >= min && spec <= max
+ end
+
+ # Lists of invidual values, ranges, and step values all share the validity range for type
+ spec.split(%r{\/|-|,}).each do |x|
+ next if x == "*"
+ return false unless x =~ /^\d+$/
+
+ x = x.to_i
+ return false unless x >= min && x <= max
+ end
+ true
+ end
+
+ # validate the provided month value to be jan - dec, 1 - 12, or *
+ # @param spec the value to validate
+ # @return [Boolean] valid or not?
+ def self.validate_month(spec)
+ return true if spec == "*"
+
+ if spec.respond_to? :to_int
+ validate_numeric(spec, 1, 12)
+ elsif spec.respond_to? :to_str
+ # Named abbreviations are permitted but not as part of a range or with stepping
+ return true if %w{jan feb mar apr may jun jul aug sep oct nov dec}.include? spec.downcase
+
+ # 1-12 are legal for months
+ validate_numeric(spec, 1, 12)
+ else
+ false
+ end
+ end
+
+ # validate the provided day of the week is sun-sat, 0-7, or *
+ # @param spec the value to validate
+ # @return [Boolean] valid or not?
+ def self.validate_dow(spec)
+ spec == "*" ||
+ validate_numeric(spec, 0, 7) ||
+ %w{sun mon tue wed thu fri sat}.include?(String(spec).downcase)
+ end
+ end
+ end
+end