Hey Dan,

 In each iteration of the reduce function the memo value is replaced by the
result value of the block. The final return value of reduce is the result
of the last iteration of the block. The result of the block is the value of
the last statement executed. Your block has two possible results:
    1) $result + $group when the condition is met
    2) Undef otherwise (which I think might be the result of the if
statement)

  Since user1 doesn't match the condition in the last iteration the result
of the reduce is Undef.

  To correct this you could add an else clause that just returns $result,
e.g.

  $usergroups = $hostgroups.reduce([]) |$result, $group| {
    $members = $groups[$group]["members"]
    warning("[$username] $group: $members")
    if ( $username in $members ) {
      warning("[$username] Added $group")
      $result + $group
    } else {
      $result
    }
  }

  Hope this helps.
    - Steve


On Sat, Feb 9, 2019 at 12:52 PM Daniel Kinon <dan.ki...@gmail.com> wrote:

> Hello,
>     I'm attempting to use the reduce function(
> https://puppet.com/docs/puppet/6.2/function.html#reduce) to consolidate
> hiera facts into data structures I can use with puppet resources.  I've run
> into an issue that I'm not sure how to solve.  When the data passed to the
> reduce function results in 2 or more iterations, I get the data return I
> expect.  When the data passed only results in 1 iterations, I get back no
> results when I expect one result.  Here is the applicable code snippet
> (hiera lookups replaced with dummy data):
> ~~~
>   $groups  = {
>     "group1" => {
>       "gid"     => 901,
>       "members" => ["user1","user2","user3","user4"],
>     },
>     "group2" => {
>       "gid"     => 902,
>       "members" => ["user2"],
>     }
>   }
>   $hostgroups = [ "group1", "group2" ]
>   $usernames = ["user1","user2"]
>
>   $usernames.each |$username| {
>     $usergroups = $hostgroups.reduce([]) |$result, $group| {
>       $members = $groups[$group]["members"]
>       warning("[$username] $group: $members")
>       if ( $username in $members ) {
>         warning("[$username] Added $group")
>         $result + $group
>       }
>     }
>     warning("[$username] groups: $usergroups")
>     warning()
>   }
> ~~~
>
> The output of this code shows user1 with an empty array of groups when it
> should have group1 while user2 has the expected 2 group output:
> ~~~
> Warning: Scope(Class[Base::Test]): [user1] group1: [user1, user2, user3,
> user4]
> Warning: Scope(Class[Base::Test]): [user1] Added group1
> Warning: Scope(Class[Base::Test]): [user1] group2: [user2]
> Warning: Scope(Class[Base::Test]): [user1] groups:
> Warning: Scope(Class[Base::Test]):
> Warning: Scope(Class[Base::Test]): [user2] group1: [user1, user2, user3,
> user4]
> Warning: Scope(Class[Base::Test]): [user2] Added group1
> Warning: Scope(Class[Base::Test]): [user2] group2: [user2]
> Warning: Scope(Class[Base::Test]): [user2] Added group2
> Warning: Scope(Class[Base::Test]): [user2] groups: [group1, group2]
> Warning: Scope(Class[Base::Test]):
> ~~~
>
> Let me know if I'm missing something otherwise this seems like it might be
> a bug.
> Thanks,
> -Dan
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/bc076bfc-86b2-40c9-a07e-05a632909048%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/bc076bfc-86b2-40c9-a07e-05a632909048%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/CALGSqjJ4yEJkY__0%2Bm4b6kugbhOq2kV%2Ba3jYMgueWec4ikx_mA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to