Skip to content

Commit

Permalink
Merge pull request #29 from pillarsdotnet/master
Browse files Browse the repository at this point in the history
Add types for parameter validation.
  • Loading branch information
bastelfreak authored Jun 25, 2018
2 parents f383cf5 + 269f92c commit b62e9a2
Show file tree
Hide file tree
Showing 28 changed files with 159 additions and 81 deletions.
16 changes: 8 additions & 8 deletions manifests/daily.pp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
# }
#
define cron::daily (
Optional[String[1]] $command = undef,
Enum['absent','present'] $ensure = 'present',
Variant[Integer,String[1]] $minute = 0,
Variant[Integer,String[1]] $hour = 0,
Array[String] $environment = [],
String[1] $user = 'root',
String[4,4] $mode = '0644',
Optional[String] $description = undef,
Optional[String[1]] $command = undef,
Cron::Job_ensure $ensure = 'present',
Cron::Minute $minute = 0,
Cron::Hour $hour = 0,
Cron::Environment $environment = [],
Cron::User $user = 'root',
Cron::Mode $mode = '0644',
Optional[String] $description = undef,
) {

cron::job { $title:
Expand Down
14 changes: 7 additions & 7 deletions manifests/hourly.pp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
# }
#
define cron::hourly (
Optional[String[1]] $command = undef,
Enum['absent','present'] $ensure = 'present',
Variant[Integer,String[1]] $minute = 0,
Array[String] $environment = [],
String[1] $user = 'root',
String[4,4] $mode = '0644',
Optional[String] $description = undef,
Optional[String[1]] $command = undef,
Cron::Job_ensure $ensure = 'present',
Cron::Minute $minute = 0,
Cron::Environment $environment = [],
Cron::User $user = 'root',
Cron::Mode $mode = '0644',
Optional[String] $description = undef,
) {

cron::job { $title:
Expand Down
34 changes: 11 additions & 23 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,17 @@
# }
#
class cron (
String[1] $service_name,
String[1] $package_name,
Boolean $manage_package = true,
Boolean $manage_service = true,
Variant[
Boolean,
Enum[
'running',
'stopped',
]
] $service_ensure = 'running',
Variant[
Boolean,
Enum[
'manual',
'mask',
]
] $service_enable = true,
String[1] $package_ensure = 'installed',
Array[String] $users_allow = [],
Array[String] $users_deny = [],
Boolean $manage_users_allow = false,
Boolean $manage_users_deny = false,
String[1] $service_name,
String[1] $package_name,
Boolean $manage_package = true,
Boolean $manage_service = true,
Cron::Service_ensure $service_ensure = 'running',
Cron::Service_enable $service_enable = true,
Cron::Package_ensure $package_ensure = 'installed',
Array[Cron::User] $users_allow = [],
Array[Cron::User] $users_deny = [],
Boolean $manage_users_allow = false,
Boolean $manage_users_deny = false,
) {

contain '::cron::install'
Expand Down
26 changes: 14 additions & 12 deletions manifests/job.pp
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,22 @@
# }
#
define cron::job (
Optional[String[1]] $command = undef,
Enum['absent','present'] $ensure = 'present',
Variant[Integer,String[1]] $minute = '*',
Variant[Integer,String[1]] $hour = '*',
Variant[Integer,String[1]] $date = '*',
Variant[Integer,String[1]] $month = '*',
Variant[Integer,String[1]] $weekday = '*',
Optional[String[1]] $special = undef,
Array[String] $environment = [],
String[1] $user = 'root',
String[4,4] $mode = '0644',
Optional[String] $description = undef,
Optional[String[1]] $command = undef,
Cron::Job_ensure $ensure = 'present',
Cron::Minute $minute = '*',
Cron::Hour $hour = '*',
Cron::Date $date = '*',
Cron::Month $month = '*',
Cron::Weekday $weekday = '*',
Cron::Special $special = undef,
Cron::Environment $environment = [],
Cron::User $user = 'root',
Cron::Mode $mode = '0644',
Optional[String] $description = undef,
) {

assert_type(Cron::Jobname, $title)

case $ensure {
'absent': {
file { "job_${title}":
Expand Down
24 changes: 12 additions & 12 deletions manifests/job/multiple.pp
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@
define cron::job::multiple(
Array[Struct[{
Optional['command'] => String[1],
Optional['minute'] => Variant[Integer,String[1]],
Optional['hour'] => Variant[Integer,String[1]],
Optional['date'] => Variant[Integer,String[1]],
Optional['month'] => Variant[Integer,String[1]],
Optional['weekday'] => Variant[Integer,String[1]],
Optional['special'] => String[1],
Optional['environment'] => Array[String],
Optional['user'] => String[1],
Optional['minute'] => Cron::Minute,
Optional['hour'] => Cron::Hour,
Optional['date'] => Cron::Date,
Optional['month'] => Cron::Month,
Optional['weekday'] => Cron::Weekday,
Optional['special'] => Cron::Special,
Optional['environment'] => Cron::Environment,
Optional['user'] => Cron::User,
Optional['description'] => String,
}]] $jobs,
Enum['absent','present'] $ensure = 'present',
Array[String] $environment = [],
String[4,4] $mode = '0644',
}]] $jobs,
Cron::Job_ensure $ensure = 'present',
Cron::Environment $environment = [],
Cron::Mode $mode = '0644',
) {
case $ensure {
'absent': {
Expand Down
18 changes: 9 additions & 9 deletions manifests/monthly.pp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
# }
#
define cron::monthly (
Optional[String[1]] $command = undef,
Enum['absent','present'] $ensure = 'present',
Variant[Integer,String[1]] $minute = 0,
Variant[Integer,String[1]] $hour = 0,
Variant[Integer,String[1]] $date = 1,
Array[String] $environment = [],
String[1] $user = 'root',
String[4,4] $mode = '0644',
Optional[String] $description = undef,
Optional[String[1]] $command = undef,
Cron::Job_ensure $ensure = 'present',
Cron::Minute $minute = 0,
Cron::Hour $hour = 0,
Cron::Date $date = 1,
Cron::Environment $environment = [],
Cron::User $user = 'root',
Cron::Mode $mode = '0644',
Optional[String] $description = undef,
) {

cron::job { $title:
Expand Down
18 changes: 9 additions & 9 deletions manifests/weekly.pp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
# }
#
define cron::weekly (
Optional[String[1]] $command = undef,
Enum['absent','present'] $ensure = 'present',
Variant[Integer,String[1]] $minute = 0,
Variant[Integer,String[1]] $hour = 0,
Variant[Integer,String[1]] $weekday = 0,
String[1] $user = 'root',
String[4,4] $mode = '0644',
Array[String] $environment = [],
Optional[String] $description = undef,
Optional[String[1]] $command = undef,
Cron::Job_ensure $ensure = 'present',
Cron::Minute $minute = 0,
Cron::Hour $hour = 0,
Cron::Weekday $weekday = 0,
Cron::User $user = 'root',
Cron::Mode $mode = '0644',
Cron::Environment $environment = [],
Optional[String] $description = undef,
) {

cron::job { $title:
Expand Down
2 changes: 1 addition & 1 deletion templates/users.epp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% | Array[String] $users | %>
<% | Array[Cron::User] $users | %>

# Managed by Puppet

Expand Down
5 changes: 5 additions & 0 deletions types/date.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Valid $date (day of month) parameter to Cron::Job.
type Cron::Date = Variant[
Integer[1,31],
Pattern[/\A(([1-9]|[1-2][0-9]|3[0-1])|(\*|(([1-9]|[1-2][0-9]|3[0-1])-([1-9]|[1-2][0-9]|3[0-1])))(\/([1-9]|[1-2][0-9]|3[0-1]))?)\z/]
]
3 changes: 3 additions & 0 deletions types/deb_version.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Valid .deb version string.
# See https://www.debian.org/doc/debian-policy/#s-f-version
type Cron::Deb_version = Pattern[/(?i:\A(((0|[1-9][0-9]*):)?[0-9]([a-z0-9.+-~]*|[a-z0-9.+~]*-[a-z0-9+.~]+))\z)/]
2 changes: 2 additions & 0 deletions types/environment.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Valid $environment parameter to Cron::Job.
type Cron::Environment = Array[Pattern[/(?i:\A[a-z_][a-z0-9_]*=[^\0]*\z)/]]
5 changes: 5 additions & 0 deletions types/hour.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Valid $hour parameter to Cron::Job.
type Cron::Hour = Variant[
Integer[0,23],
Pattern[/\A(([0-9]|1[0-9]|2[0-3])|(\*|(([0-9]|1[0-9]|2[0-3])-([0-9]|1[0-9]|2[0-3])))(\/([1-9]|1[0-9]|2[0-3]))?)\z/]
]
2 changes: 2 additions & 0 deletions types/job_ensure.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Valid $ensure parameter to Cron::Job.
type Cron::Job_ensure = Enum['absent', 'present']
13 changes: 13 additions & 0 deletions types/jobname.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Valid $title parameter to Cron::Job.
# This is the name of the /etc/cron.d/ file.
# The Ubuntu run-parts manpage specifies (^[a-zA-Z0-9_-]+$).
# For Cronie, the documentation is (unfortunately) in the code:
# - Ignore files starting with "." or "#"
# - Ignore the CRON_HOSTNAME file (default ".cron.hostname").
# - Ignore files whose length is zero or greater than NAME_MAX (default 255).
# - Ignore files whose name ends in "~".
# - Ignore files whose name ends in ".rpmsave", ".rpmorig", or ".rpmnew".
# We will use the most restrictive combination.
# See http://manpages.ubuntu.com/manpages/zesty/en/man8/run-parts.8.html
# See https://github.com/cronie-crond/cronie/blob/master/src/database.c#L625
type Cron::Jobname = Pattern[/(?i:\A[a-z0-9_-]{1,255}\z)/]
5 changes: 5 additions & 0 deletions types/minute.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Valid $minute parameter to Cron::Job.
type Cron::Minute = Variant[
Integer[0,59],
Pattern[/\A(([0-9]|[1-5][0-9])|(\*|(([0-9]|[1-5][0-9])-([0-9]|[1-5][0-9])))(\/([1-9]|[1-5][0-9]))?)\z/]
]
2 changes: 2 additions & 0 deletions types/mode.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Valid $mode parameter to Cron::Job.
type Cron::Mode = Pattern[/\A[0-7]{4}\z/]
6 changes: 6 additions & 0 deletions types/month.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Valid $month parameter to Cron::Job.
type Cron::Month = Variant[
Cron::Monthname,
Integer[1,12],
Pattern[/\A(([1-9]|1[0-2])|(\*|(([1-9]|1[0-2])-([1-9]|1[0-2])))(\/([1-9]|1[0-2]))?)\z/]
]
3 changes: 3 additions & 0 deletions types/monthname.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Short-names for each month.
type Cron::Monthname = Enum['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec']
2 changes: 2 additions & 0 deletions types/package_ensure.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Valid $service_ensure parameter to Cron.
type Cron::Package_ensure = Variant[Cron::Package_state,Cron::Deb_version,Cron::Rpm_version]
2 changes: 2 additions & 0 deletions types/package_state.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Valid $ensure parameter to Package resource. Excludes version numbers.
type Cron::Package_state = Enum['absent', 'installed', 'held', 'latest', 'present', 'purged']
3 changes: 3 additions & 0 deletions types/rpm_version.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Valid .rpm version string.
# See http://www.perlmonks.org/?node_id=237724
type Cron::Rpm_version = Pattern[/\A[^-]+(-[^-])?\z/]
2 changes: 2 additions & 0 deletions types/second.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Valid $second parameter to Cron::Job.
type Cron::Second = Cron::Minute
5 changes: 5 additions & 0 deletions types/service_enable.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Valid $service_enable parameter to Cron.
type Cron::Service_Enable = Variant[
Boolean,
Enum['manual','mask']
]
5 changes: 5 additions & 0 deletions types/service_ensure.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Valid $service_ensure parameter to Cron.
type Cron::Service_ensure = Variant[
Boolean,
Enum['running','stopped']
]
12 changes: 12 additions & 0 deletions types/special.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Valid $special parameter to Cron::Job.
type Cron::Special = Optional[
Enum['@annually',
'@daily',
'@hourly',
'@midnight',
'@monthly',
'@reboot',
'@weekly',
'@yearly',
]
]
2 changes: 2 additions & 0 deletions types/user.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Valid $user parameter to Cron::Job.
type Cron::User = Pattern[/(?i:\A\w[a-z0-9_-]{0,30}[a-z0-9_$-]\z)/]
6 changes: 6 additions & 0 deletions types/weekday.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Valid $weekday parameter to Cron::Job.
type Cron::Weekday = Variant[
Cron::Weekdayname,
Integer[0,7],
Pattern[/\A([0-7]|(\*|([0-7]-[0-7]))(\/[1-7])?)\z/]
]
3 changes: 3 additions & 0 deletions types/weekdayname.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Short names for each day of the week.
type Cron::Weekdayname = Enum['Sun','Mon','Tue','Wed',
'Thu','Fri','Sat']

0 comments on commit b62e9a2

Please sign in to comment.