FAQ
Hi,

Trying to write a generic function to deal with either arrays or strings.
Here's a cutdown version of what I'm writing highlighting the problem I
have.

All i'm trying to do is take 'server1.testing.com' from the variable
$server (the variable being either a string or an array) and assign it to
$bar, or assign 'server3.testing.com' if it isn't assigned at all.

<code>

   # $server = 'server1.testing.com,server2.testing.com'
   $server = [ 'server1.testing.com','server2.testing.com' ]

   $fu = $server ? {
     '' => 'server3.testing.com',
     default => is_array($server) ? {
       false => $server ? {
* default => split($server, ',')*
       },
       default => $server[0],
     }
   }

   $bar = is_array($fu) ? {
     false => $fu,
     true => $fu[0],
   }

</code>

Why can't I just split($server, ',')[0] ? Can I assign some of the result
of the split to the variable? Does someone else have an example of a more
elegant way to do this?



--
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 puppet-users+unsubscribe@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Jcbollinger at Jul 11, 2013 at 2:26 pm

    On Thursday, July 11, 2013 12:35:55 AM UTC-5, Michael Dodwell wrote:
    Hi,

    Trying to write a generic function to deal with either arrays or strings.
    Here's a cutdown version of what I'm writing highlighting the problem I
    have.

    All i'm trying to do is take 'server1.testing.com' from the variable
    $server (the variable being either a string or an array) and assign it to
    $bar, or assign 'server3.testing.com' if it isn't assigned at all.

    <code>

    # $server = 'server1.testing.com,server2.testing.com'
    $server = [ 'server1.testing.com','server2.testing.com' ]

    $fu = $server ? {
    '' => 'server3.testing.com',
    default => is_array($server) ? {
    false => $server ? {
    * default => split($server, ',')*
    },
    default => $server[0],
    }
    }

    $bar = is_array($fu) ? {
    false => $fu,
    true => $fu[0],
    }

    </code>

    Why can't I just split($server, ',')[0] ? Can I assign some of the result
    of the split to the variable? Does someone else have an example of a more
    elegant way to do this?
    I think it's a limitation of the Puppet DSL parser. You should be able to
    work around it by assigning the split result to a variable and then
    indexing into that variable. I think there are more elegant ways to write
    this, however. I might do something along these lines:

    $default_server = 'server3.testing.com'
    if is_array($server) {
       $server_array = $server
    } elsif $server != '' {
       $server_array = split( $server, ',' )
    } else {
       $server_array = []
    }
    $safe_server_array = concat( $server_array, [$default_server] )
    bar = $safe_server_array[0]

    That could be modified to avoid the intermediate "server_array" variable,
    at the cost of duplicating the concat() call in each of the first two
    conditional alternatives, and changing the value assigned in the third.

    Really, though, I think you are overengineering this a bit. Why do you
    need to support accepting multiple servers in both delimited list and array
    form? You would simplify your life by choosing just one of those. The
    simplest approach would be to support only strings, possibly containing
    delimited lists. Life could still be pretty simple if you want to support
    a single server as either a string or a one-element array, however,
    provided that you accept multiple servers only in array form. For example,
    you might do something like this:

    $default_server = 'server3.testing.com'
    $server_array = flatten( concat( any2array( ${server} ), [$default_server]
    ) )
    $bar = $server_array[0]


    Note: the flatten(), concat(), and any2array() functions in these examples
    come from Puppetlabs "stdlib" add-in module, available from the Forge.


    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 puppet-users+unsubscribe@googlegroups.com.
    To post to this group, send email to puppet-users@googlegroups.com.
    Visit this group at http://groups.google.com/group/puppet-users.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppuppet-users @
categoriespuppet
postedJul 11, '13 at 5:35a
activeJul 11, '13 at 2:26p
posts2
users2
websitepuppetlabs.com

2 users in discussion

Michael Dodwell: 1 post Jcbollinger: 1 post

People

Translate

site design / logo © 2021 Grokbase