FAQ
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.

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet-users@googlegroups.com.
To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.

Search Discussions

  • Krzysztof Wilczynski at Apr 28, 2012 at 6:30 pm
    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

    --
    You received this message because you are subscribed to the Google Groups "Puppet Users" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/7ekhlWECNEcJ.
    To post to this group, send email to puppet-users@googlegroups.com.
    To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
  • Peter at Apr 29, 2012 at 2:25 pm
    Hi KW,

    On Apr 29, 4:30 am, Krzysztof Wilczynski
    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

    --
    You received this message because you are subscribed to the Google Groups "Puppet Users" group.
    To post to this group, send email to puppet-users@googlegroups.com.
    To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
  • Krzysztof Wilczynski at Apr 29, 2012 at 4:21 pm
    Hi,
    On Sunday, April 29, 2012 3:25:48 PM UTC+1, Peter wrote:

    Hi KW,

    On Apr 29, 4:30 am, Krzysztof Wilczynski
    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?
    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


    --
    You received this message because you are subscribed to the Google Groups "Puppet Users" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/Q9mf6835WtIJ.
    To post to this group, send email to puppet-users@googlegroups.com.
    To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppuppet-users @
categoriespuppet
postedApr 28, '12 at 5:11a
activeApr 29, '12 at 4:21p
posts4
users2
websitepuppetlabs.com

2 users in discussion

Peter: 2 posts Krzysztof Wilczynski: 2 posts

People

Translate

site design / logo © 2022 Grokbase