Puppet Function: role::expand_search_namespaces

Defined in:
functions/expand_search_namespaces.pp
Function type:
Puppet Language

Summary

Sanitizes Role::SearchNamespaces for use in `role`.

Overview

role::expand_search_namespaces(String $separator, Variant[Role::SearchNamespace, Array[Role::SearchNamespace]] $search)Hash[String, String]

This function is used to sanitize user input and return a single map with namespace - separator entries.

Examples:

hiera configured namespaces


$search_namespaces = [
  '',
  { 'my_roles' => '_' },
  'public_roles',
]
$expanded = role::expand_search_namespaces('::', $search_namespaces)
# => {'' => '::', 'my_roles' => '_', 'public_roles' => '::' }

Parameters:

  • separator (String)

    Default separator to use when the namespace does not provide one.

  • search (Variant[Role::SearchNamespace, Array[Role::SearchNamespace]])

    ‘Role::SearchNamespace`s to expand.

Returns:

  • (Hash[String, String])

    Expanded configuration.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'functions/expand_search_namespaces.pp', line 18

function role::expand_search_namespaces(
  String $separator,
  Variant[Role::SearchNamespace, Array[Role::SearchNamespace]] $search,
) >> Hash[String, String] {


  $namespaces = [$search].flatten.unique.reduce({}) |Hash[String, String] $memo, Role::SearchNamespace $space| {
    if $space =~ String {
      $memo + { $space => $separator }
    }
    else {
      $memo + $space.reduce({}) |Hash[String, String] $spaces, Tuple $kv| {
        $key = $kv[0]
        $value = $kv[1]
        $_value = $value ? {
          undef   => $separator,
          default => $value,
        }
        $memo + { $key =>  $_value }
      }
    }
  }
}