On Tuesday, June 12, 2018 at 3:14:45 PM UTC-5, Tim.Mooney wrote:
> Let's say you had *many* parameters that you wanted to set defaults for
> (but allow overrides on an individual basis) based on a single parameter.
>
The simplest and most straightforward alternative I can think of is simply
to reposition your classes. What I mean by that is instead of using a
params class to set Puppet-level default parameter values, make the target
class a wrapper that handles parameter defaults and adjustments, and passes
them off to a private, internal class. Truly, the internal class part is
optional, but it provides for a separation of concerns, and maps more
cleanly onto your example.
For instance,
*modules/sandbox/manifests/internal.pp*:
#
# This module exists only to serve as a sandbox where we can experiment
with
# puppet code.
#
# @api private
#
class sandbox::internal(
# No parameter defaults needed or useful at this level
Enum['typeA', 'typeB', 'combined'] $server_type,
String $service_name,
) {
notice("sandbox initialized.\n")
notice("\$server_type = ${server_type}\n")
notice("\$service_name = ${service_name}\n")
}
*modules/sandbox/manifests/init.pp*:
#
# a 'sandbox' class for managing class parameters
#
class sandbox(
Enum['typeA', 'typeB', 'combined'] $server_type = 'combined',
Optional[String] $service_name = undef,
) {
if $service_name =~ NotUndef {
$_service_name_actual = $service_name
} else {
$_type_to_name = {
'combined' => 'sandbox-typeA+typeB',
'typeA' => 'sandbox-typeA',
'typeB' => 'sandbox-typeB',
# No other alternatives are possible
}
$_service_name_actual = $_type_to_name[$service_type]
}
# Resource-like class declarations can be ok for private,
# internal classes:
class { 'sandbox::internal':
server_type => $server_type,
service_name => $_service_name_actual,
}
}
That's actually a variation on a tried and true pattern from before even
parameterized classes (it was applied in defined types), though the
separation of the parameter handling from the meat of the class / type is
less common. Data types are really helpful here, though, because they
afford a much clearer and more convenient way to check whether the user
supplied a parameter value than is possible without.
John
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-users/530aa164-474a-426c-bd3b-45ea3ab9e0fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.