Last active
April 10, 2018 16:56
-
-
Save reidmv/8df5a0f6201ce32f935b9371e4c9fb6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This function returns a boolean value based on whether or not the Puppet agent | |
# was invoked with --no-noop. | |
# | |
# `puppet agent -t` = true | |
# `puppet agent -t --no-noop` = false | |
# | |
function noop::true_unless_no_noop() { | |
$agent_specified_noop_setbycli ? { | |
true => $facts['agent_specified_noop'], | |
false => true, | |
} | |
} | |
# This function returns a boolean value representing an explicit noop setting | |
# for use with the noop resource metaparameter. The function considers: | |
# | |
# * Has an override parameter been specified? | |
# * Has the class-level noop parameter been set to true? | |
# * Is the agent-level noop setting true? | |
# | |
function noop::explicit_metaparameter($class_noop_override, $class_level_noop) { | |
$noop_override ? { | |
default => $class_noop_override, | |
undef => $facts['clientnoop'] ? { | |
true => true, | |
false => $class_level_noop, | |
}, | |
} | |
} | |
# This | |
class example::idaho ( | |
Optional[Boolean] $class_level_noop = noop::true_unless_no_noop(), | |
Optional[Boolean] $class_noop_override = undef, | |
) { | |
# Set local scope noop defaults. WARNING: this class should now be considered | |
# a "leaf" class. Do not call `include` or similar functions from this point | |
# forward without careful consideration of how these scope defaults will | |
# propagate. Doing so may cause unexpected side effects! | |
# | |
# https://puppet.com/docs/puppet/5.3/lang_scope.html | |
# https://puppet.com/docs/puppet/5.3/lang_defaults.html | |
# | |
if ($class_level_noop != undef) { noop($class_level_noop) } | |
# Get an explicit value for the noop metaparameter. It's necessary to do this | |
# because once we write `noop => $noop_override` on a resource in code, we | |
# lose whatever the default *would* have been. The value of $noop_override | |
# is calculated to be an explicit value equivalent to what value the resource | |
# would recieve if the metaparameter were not passed explicitly, taking into | |
# consideration both of the class-level noop adjustment parameters. | |
$noop_override = noop::explicit_metaparameter($class_noop_override, $class_level_noop) | |
# The noop metaparameter default is set automatically based on the scope | |
# defaults invoked by the noop() function call above. The explicit equivalent | |
# has been mocked in as a comment, for illustration. | |
file { '/tmp/idaho1.txt': | |
ensure => file, | |
content => "Content from example::idaho\nNo no-op override\n", | |
# noop => $class_level_noop, | |
} | |
# Enforcement of this resource may be toggled on and off by setting | |
# example::idaho::class_noop_override. If no override is passed, it's end | |
# behavior will be identical to the resource above. | |
file { '/tmp/idaho2.txt': | |
ensure => file, | |
content => "Content from example::idaho\nThere is a no-op override\n", | |
noop => $noop_override, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment