|
| 1 | +// Meta-argument constructor functions. Refer to the meta-arguments tab on |
| 2 | +// https://developer.hashicorp.com/terraform/language for more information. |
| 3 | +// |
| 4 | +// This can be used as arguments to the `_meta` parameter for any resource |
| 5 | +// or data source constructor generated by libgenerator. |
| 6 | + |
| 7 | +local h = import './helpers.libsonnet'; |
| 8 | + |
| 9 | +// newMeta will generate an object that can be mixed into any resource or data |
| 10 | +// source to set the Terraform meta arguments. |
| 11 | +local newMeta(count=null, depends_on=null, for_each=null, provider=null, lifecycle=null) = |
| 12 | + local maybeCount = |
| 13 | + if count != null then |
| 14 | + { count: count } |
| 15 | + else |
| 16 | + {}; |
| 17 | + |
| 18 | + local maybeDependsOn = |
| 19 | + if depends_on != null then |
| 20 | + { depends_on: depends_on } |
| 21 | + else |
| 22 | + {}; |
| 23 | + |
| 24 | + local maybeForEach = |
| 25 | + if for_each != null then |
| 26 | + { for_each: for_each } |
| 27 | + else |
| 28 | + {}; |
| 29 | + |
| 30 | + local maybeProvider = |
| 31 | + if provider != null then |
| 32 | + { provider: provider } |
| 33 | + else |
| 34 | + {}; |
| 35 | + |
| 36 | + local maybeLifecycle = |
| 37 | + if lifecycle != null then |
| 38 | + { lifecycle: lifecycle } |
| 39 | + else |
| 40 | + {}; |
| 41 | + |
| 42 | + maybeCount |
| 43 | + + maybeDependsOn |
| 44 | + + maybeForEach |
| 45 | + + maybeProvider |
| 46 | + + maybeLifecycle; |
| 47 | + |
| 48 | + |
| 49 | +// newModuleMeta will generate an object that can be mixed into any module call to set the Terraform meta arguments. |
| 50 | +local newModuleMeta(count=null, depends_on=null, for_each=null, providers=null) = |
| 51 | + local maybeCount = |
| 52 | + if count != null then |
| 53 | + { count: count } |
| 54 | + else |
| 55 | + {}; |
| 56 | + |
| 57 | + local maybeDependsOn = |
| 58 | + if depends_on != null then |
| 59 | + { depends_on: depends_on } |
| 60 | + else |
| 61 | + {}; |
| 62 | + |
| 63 | + local maybeForEach = |
| 64 | + if for_each != null then |
| 65 | + { for_each: for_each } |
| 66 | + else |
| 67 | + {}; |
| 68 | + |
| 69 | + local maybeProviders = |
| 70 | + if providers != null then |
| 71 | + if std.isObject(providers) then |
| 72 | + { providers: providers } |
| 73 | + else |
| 74 | + error 'providers meta argument must be a map' |
| 75 | + else |
| 76 | + {}; |
| 77 | + |
| 78 | + maybeCount |
| 79 | + + maybeDependsOn |
| 80 | + + maybeForEach |
| 81 | + + maybeProviders; |
| 82 | + |
| 83 | + |
| 84 | +// newLifecycle will generate a new lifecycle block. Note that unlike the other functions, this includes type checking |
| 85 | +// due to the Terraform requirement that the lifecycle block only supports literal values only. As such, it is easier to |
| 86 | +// do a type check on the args since there is no possibility to use complex Terraform expressions (which will reduce to |
| 87 | +// a string type in jsonnet). |
| 88 | +local newLifecycle( |
| 89 | + create_before_destroy=null, |
| 90 | + prevent_destroy=null, |
| 91 | + ignore_changes=null, |
| 92 | + replace_triggered_by=null, |
| 93 | + precondition=null, |
| 94 | + postcondition=null, |
| 95 | + ) = |
| 96 | + local maybeCreateBeforeDestroy = |
| 97 | + if create_before_destroy != null then |
| 98 | + if std.isBoolean(create_before_destroy) then |
| 99 | + { create_before_destroy: create_before_destroy } |
| 100 | + else |
| 101 | + error 'lifecycle meta argument attr create_before_destroy must be a boolean' |
| 102 | + else |
| 103 | + {}; |
| 104 | + |
| 105 | + local maybePreventDestroy = |
| 106 | + if prevent_destroy != null then |
| 107 | + if std.isBoolean(prevent_destroy) then |
| 108 | + { prevent_destroy: prevent_destroy } |
| 109 | + else |
| 110 | + error 'lifecycle meta argument attr prevent_destroy must be a boolean' |
| 111 | + else |
| 112 | + {}; |
| 113 | + |
| 114 | + local maybeIgnoreChanges = |
| 115 | + if ignore_changes != null then |
| 116 | + if h.isStringArray(ignore_changes) then |
| 117 | + { ignore_changes: ignore_changes } |
| 118 | + else |
| 119 | + error 'lifecycle meta argument attr ignore_changes must be a string array' |
| 120 | + else |
| 121 | + {}; |
| 122 | + |
| 123 | + local maybeReplaceTriggeredBy = |
| 124 | + if replace_triggered_by != null then |
| 125 | + if h.isStringArray(replace_triggered_by) then |
| 126 | + { replace_triggered_by: replace_triggered_by } |
| 127 | + else |
| 128 | + error 'lifecycle meta argument attr replace_triggered_by must be a string array' |
| 129 | + else |
| 130 | + {}; |
| 131 | + |
| 132 | + local maybePrecondition = |
| 133 | + if precondition != null then |
| 134 | + if std.isArray(precondition) then |
| 135 | + { precondition: precondition } |
| 136 | + else |
| 137 | + error 'lifecycle meta argument attr precondition must be an array of condition blocks' |
| 138 | + else |
| 139 | + {}; |
| 140 | + |
| 141 | + local maybePostcondition = |
| 142 | + if postcondition != null then |
| 143 | + if std.isArray(postcondition) then |
| 144 | + { postcondition: postcondition } |
| 145 | + else |
| 146 | + error 'lifecycle meta argument attr postcondition must be an array of condition blocks' |
| 147 | + else |
| 148 | + {}; |
| 149 | + |
| 150 | + maybeCreateBeforeDestroy |
| 151 | + + maybePreventDestroy |
| 152 | + + maybeIgnoreChanges |
| 153 | + + maybeReplaceTriggeredBy |
| 154 | + + maybePrecondition |
| 155 | + + maybePostcondition; |
| 156 | + |
| 157 | + |
| 158 | +// newCondition will generate a new condition block that can be used as part of precondition or postcondition in the |
| 159 | +// lifecycle block. |
| 160 | +local newCondition(condition, error_message) = |
| 161 | + { |
| 162 | + condition: condition, |
| 163 | + error_message: error_message, |
| 164 | + }; |
| 165 | + |
| 166 | + |
| 167 | +// root object |
| 168 | +{ |
| 169 | + meta:: { |
| 170 | + new:: newMeta, |
| 171 | + newForModule:: newModuleMeta, |
| 172 | + lifecycle:: { |
| 173 | + new:: newLifecycle, |
| 174 | + condition:: { |
| 175 | + new:: newCondition, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | +} |
0 commit comments