Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Puppet stdlib - Is there a validate_in function
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Peter Foley  
View profile  
 More options Apr 28 2012, 1:11 am
From: Peter Foley <pe...@ifoley.id.au>
Date: Sat, 28 Apr 2012 15:11:22 +1000
Local: Sat, Apr 28 2012 1:11 am
Subject: Puppet stdlib - Is there a validate_in function

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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Krzysztof Wilczynski  
View profile  
 More options Apr 28 2012, 2:30 pm
From: Krzysztof Wilczynski <krzysztof.wilczyn...@linux.com>
Date: Sat, 28 Apr 2012 11:30:47 -0700 (PDT)
Local: Sat, Apr 28 2012 2:30 pm
Subject: Re: Puppet stdlib - Is there a validate_in function

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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter  
View profile  
 More options Apr 29 2012, 10:25 am
From: Peter <pe...@ifoley.id.au>
Date: Sun, 29 Apr 2012 07:25:48 -0700 (PDT)
Local: Sun, Apr 29 2012 10:25 am
Subject: Re: Puppet stdlib - Is there a validate_in function
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Krzysztof Wilczynski  
View profile  
 More options Apr 29 2012, 12:21 pm
From: Krzysztof Wilczynski <krzysztof.wilczyn...@linux.com>
Date: Sun, 29 Apr 2012 09:21:33 -0700 (PDT)
Local: Sun, Apr 29 2012 12:21 pm
Subject: Re: Puppet stdlib - Is there a validate_in function

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »