Puppet stdlib - Is there a validate_in function

51 views
Skip to first unread message

Peter Foley

unread,
Apr 28, 2012, 1:11:22 AM4/28/12
to puppet...@googlegroups.com
Hi List,

As part of my module pre-checks I would like to confirm that a value passed in for the state of a package is part of two valid values.  Currently I am doing this by:

  $valid_ensure_values   = [ "present", "absent" ]

  if ! ("$::{ensure}" in $::{valid_ensure_values}) {
      $test_ = inline_template("<%= ($::apt-cacher-ng::params::valid_ensure_values).join(', ') %>")

      fail("${module_name}::server - Invalid ensure value [currently - ${ensure}], valid values are [$::{valid_ensure_values}]")
  }

I was hoping that I could just simply do a:

  validate_in($::{ensure}, $::{valid_ensure_values})

I have checked the documentation at https://github.com/puppetlabs/puppetlabs-stdlib and looked in the lib/puppet/parser directory and cannot see anything.

Thanks,

Peter.

Krzysztof Wilczynski

unread,
Apr 28, 2012, 2:30:47 PM4/28/12
to puppet...@googlegroups.com
Hi,

[...]

  $valid_ensure_values   = [ "present", "absent" ]

  if ! ("$::{ensure}" in $::{valid_ensure_values}) {
      $test_ = inline_template("<%= ($::apt-cacher-ng::params::valid_ensure_values).join(', ') %>")

      fail("${module_name}::server - Invalid ensure value [currently - ${ensure}], valid values are [$::{valid_ensure_values}]")
  }

I was hoping that I could just simply do a:

  validate_in($::{ensure}, $::{valid_ensure_values})

How would that be different than using the "in" in your if statement? :) Or, what is wrong with using if? Unless I am missing something? :)

KW

Peter

unread,
Apr 29, 2012, 10:25:48 AM4/29/12
to Puppet Users
Hi KW,

On Apr 29, 4:30 am, Krzysztof Wilczynski
<krzysztof.wilczyn...@linux.com> wrote:
> Hi,
>
> > I was hoping that I could just simply do a:
> >   validate_in($::{ensure}, $::{valid_ensure_values})
>
> How would that be different than using the "in" in your if statement? :)
> Or, what is wrong with using if? Unless I am missing something? :)
>
> KW

Nothing is wrong with using if statements per-say.

To put my problem slightly differently, in the module I am working on
I have have five checks that would benefit by having the helper
function (it would be cleaner, easier to maintain and easier to read).

Also I hope that by doing the check purely in ruby it would be
slightly faster to process rather then return a couple of times.

To give you an example within my module in the param's file I have:

$valid_package_ensure_values = [ "present", "installed",
"latest", "absent", "purged", "held" ]
$valid_service_ensure_values = [ "running", "true",
"stopped", "false" ]
$valid_service_enable_values = [ "true", "false", "manual" ]
$valid_module_config_values = [ "file", "template" ]
$valid_module_tpml_loc_values = [ "module", "site" ]

If I had the validate_in helper function and used the other validate
functions in the stdlib the validation code would look like
(extracted):

------------------------------------------
class module::server::base ( $package_ensure = undef,
$service_ensure = undef,
$service_enable = undef,
$module_config = undef,
$template_loc = undef,
$use_storeconfig = undef,
$proxy_port = undef,
$cache_dir = undef,
$log_dir = undef,
$cache_net_adr = undef
) {

include module::params

validate_in(${package_ensure}, ${valid_package_ensure_values})
validate_in(${service_ensure}, ${valid_service_ensure_values})
validate_in(${service_enable}, ${valid_service_enable_values})
validate_in(${module_config}, ${valid_module_config_values})
validate_in(${template_loc}, ${valid_module_tpml_loc_values})

is_integer($proxy_port)

validate_absolute_path($cache_dir, $log_dir)

validate_bool($use_storeconfig)

# start using the values in code below
------------------------------------------

The above code is very easy to read and you could see at a glance
exactly what is happening. Also if the error message follows the
format described in my previous message debugging would be very
simple. I could see myself using the same pattern in other modules I
develop.

Bottom line instead of the current 35 lines of validation code I
have, I could replace it with the above 8 lines which would do the
same amount of work (4:1 ratio)!

Funnily enough after I sent the original email I stumbled on
puppetlabs-stdlib/lib/puppet/parser/functions/member.rb which is
almost exactly what I am looking for, I notice that you wrote it ;)

Thoughts?

Thanks,

Peter

Krzysztof Wilczynski

unread,
Apr 29, 2012, 12:21:33 PM4/29/12
to puppet...@googlegroups.com
Hi,

If you have a moment, then write to me directly :) We can work-out what is needed and I will happily create any function for you :)

KW
 
Reply all
Reply to author
Forward
0 new messages