Puppet Function: role::translate_with_map

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

Overview

role::translate_with_map(String $role, Hash[ Variant[String, Regexp], String ] $map, Boolean $strict = false)String

Translate a role by executing a gsubstr(pattern, value, ā€˜G’) with patterns and values from the provided map.

Parameters:

  • role (String)

    Role to perform translate on.

  • map (Hash[ Variant[String, Regexp], String ])

    A map with gsubstr translations with key as pattern and value as replacement.

  • strict (Boolean) (defaults to: false)

    Experimental parameter. If enabled, fails the puppet run when a role includes characters that are in the target map present as values. This would indicate that a role is being provided which would not have expected replacement values in use. (This could happen during a migration, for example.

Returns:

  • (String)

    Translated role.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'functions/translate_with_map.pp', line 13

function role::translate_with_map(
  String $role,
  Hash[
    Variant[String, Regexp],
    String
  ] $map,
  Boolean $strict = false,
) >> String {
  $map.reduce($role) |String $memo, Tuple[Variant[String, Regexp], String] $translate| {
    if $translate[1] in $role {
      if $strict {
        fail("Role includes remapped values: '${translate[1]}' found in '${role}'")
      }
      else {
        warning("Role includes remapped values: '${translate[1]}' found in '${role}'")
      }
    }
    $memo.regsubst($translate[0], $translate[1], 'G')
  }
}