|
| 1 | +name: yaml-math-and-objects |
| 2 | +description: A Pulumi YAML program that demonstrates how to do math and manipulate objects in YAML. |
| 3 | +runtime: yaml |
| 4 | + |
| 5 | +## Stack Config |
| 6 | +config: |
| 7 | + # The length of the string to generate. |
| 8 | + length: |
| 9 | + type: integer |
| 10 | + default: 16 |
| 11 | + # When this config is set to true, we then want to implement some logic related to the string generation. |
| 12 | + useLowerCase: |
| 13 | + type: boolean |
| 14 | + default: true |
| 15 | + # An array of key-value pairs as strings used to build the keepers map array. |
| 16 | + configKeepersArray: |
| 17 | + type: array |
| 18 | + items: |
| 19 | + type: string |
| 20 | + default: |
| 21 | + - "configKey1:configValue1" |
| 22 | + - "configKey2:configValue2" |
| 23 | + |
| 24 | + |
| 25 | +## Variables used by the RandomString resource below constructed from the config and functions and a little help from the Command resource. |
| 26 | +variables: |
| 27 | + # Get the length from the config. |
| 28 | + stringLength: ${length} |
| 29 | + |
| 30 | + # Build keepersArrayPart1 using the structured config (which is limited to an array of strings) by leveraging pulumi-std functions |
| 31 | + # to build the requisite map array. |
| 32 | + keepersArrayPart1: |
| 33 | + # Build the required map array from an array of strings such that each pair of strings is used to create the key-value pair. |
| 34 | + fn::invoke: |
| 35 | + function: std:map |
| 36 | + arguments: |
| 37 | + args: |
| 38 | + # Split the string of strings created by joining the array of strings from the config. |
| 39 | + # This is needed for the map function to build the array of key-value pairs. |
| 40 | + fn::invoke: |
| 41 | + function: std:split |
| 42 | + arguments: |
| 43 | + separator: ":" |
| 44 | + text: |
| 45 | + # Build one big string of strings from the array of strings in the config. |
| 46 | + fn::invoke: |
| 47 | + function: std:join |
| 48 | + arguments: |
| 49 | + # The array of strings from the config. |
| 50 | + input: ${configKeepersArray} |
| 51 | + separator: ":" |
| 52 | + return: result |
| 53 | + return: result |
| 54 | + return: result |
| 55 | + |
| 56 | + # Hard-coded map for the second part of the keepers array to have something to merge. |
| 57 | + keepersArrayPart2: |
| 58 | + part2aKey: "part2aValue" |
| 59 | + part2bKey: "part2bValue" |
| 60 | + |
| 61 | + # Combine the two parts of the keepers map arrays into a single map array to be used in the RandomString resource. |
| 62 | + keepersArrayCombined: |
| 63 | + fn::invoke: |
| 64 | + function: std:merge |
| 65 | + arguments: |
| 66 | + input: |
| 67 | + - ${keepersArrayPart1} |
| 68 | + - ${keepersArrayPart2} |
| 69 | + return: result |
| 70 | + |
| 71 | + # Lowercase settings object to use for the RandomString resource. |
| 72 | + lowerCaseSettings: |
| 73 | + # Just passing along the config value as part of this variable. |
| 74 | + uselower: ${useLowerCase} |
| 75 | + # Want to only set minLower based on length and only set it if useLowerCase is true. |
| 76 | + # Since we don't have a way to run logic and math in YAML, we can use the Pulumi Command resource (see below) to do this. |
| 77 | + # But we need to use the srd-pareint function to convert the string output from the Command resource to an integer for the RandomString resource. |
| 78 | + minLower: |
| 79 | + fn::invoke: |
| 80 | + function: std:parseint |
| 81 | + arguments: |
| 82 | + input: ${calculateMinLower.stdout} |
| 83 | + return: result |
| 84 | + |
| 85 | +## Resources |
| 86 | +resources: |
| 87 | + # Creating a RandomString resource (https://www.pulumi.com/registry/packages/random/api-docs/randomstring/) |
| 88 | + randomString: |
| 89 | + type: random:RandomString |
| 90 | + properties: |
| 91 | + # Must be a number. Taken from config. |
| 92 | + length: ${stringLength} |
| 93 | + # Must be an array of key-value pairs. Built from the config and hard-coded values (see above). |
| 94 | + keepers: ${keepersArrayCombined} |
| 95 | + # Must be a boolean. Taken from config. |
| 96 | + lower: ${lowerCaseSettings.uselower} |
| 97 | + # Must be a number. Calculated value based on boolean logic and math using the Command provider (see below). |
| 98 | + minLower: ${lowerCaseSettings.minLower} |
| 99 | + |
| 100 | + # Since we don't yet have math operations (other than `sum`) in YAML, |
| 101 | + # see https://github.com/pulumi/pulumi-std/issues/70 |
| 102 | + # we can use the Pulumi Command resource to do math. |
| 103 | + # The output is then referenced by the `lowerCaseSettings` variable. |
| 104 | + calculateMinLower: |
| 105 | + type: command:local:Command |
| 106 | + properties: |
| 107 | + # we want about a quarter of the password to be lowercase letters if useLowerCase is true. |
| 108 | + create: if ${useLowerCase}; then echo "${stringLength} / 4" | bc; else echo 0; fi |
| 109 | + |
| 110 | + |
| 111 | +# Stack outputs |
| 112 | +outputs: |
| 113 | + # Generated string |
| 114 | + randomString: ${randomString.result} |
| 115 | + # Lowercase settings reflecting the config-based value and the calculated minLower value. |
| 116 | + lowerCaseSettings: ${lowerCaseSettings} |
| 117 | + # The keepers array that was built using the config and hard-coded values. |
| 118 | + keepersArrayCombined: ${keepersArrayCombined} |
0 commit comments