Skip to content

Commit

Permalink
fix(replaced_when): fixed bug that the value (prior state) was not po…
Browse files Browse the repository at this point in the history
…pulated when condition was false
  • Loading branch information
teneko committed Sep 14, 2022
1 parent d65c20e commit 1143996
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
File renamed without changes.
Empty file.
23 changes: 23 additions & 0 deletions examples/replaced_when/file_inexistence_check/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
terraform {
required_providers {
value = {
source = "github.com/pseudo-dynamic/value"
version = "0.1.0"
}
}
}

resource "value_replaced_when" "findme_inexistence" {
for_each = {
"findme" : {
fullname = "${path.module}/findme"
}
}

condition = !fileexists(each.value.fullname)
}

output "findme_inexistence_check" {
// On craetion this should contain a non-null value.value
value = value_replaced_when.findme_inexistence
}
16 changes: 8 additions & 8 deletions internal/provider/replaced_when_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ func (r *ReplacedWhenResource) ModifyPlan(ctx context.Context, req resource.Modi
}

var newValue types.String

if !suppliedCondition.Unknown {
if currentValue.IsUnknown() || suppliedCondition.Value {
// First creation of attribute / state value won't never be null again
// OR supplied condition (config) is known (the latter condition is also
// true if the unknown condition has been computed once and didn't change
// and is therefore not unknown anymore)

var currentValueNotOnceSet = currentValue.IsUnknown() || currentValue.IsNull()

if currentValueNotOnceSet || !suppliedCondition.Unknown {
// Empty value (state) / state value won't never be unknown or null again
// OR supplied condition (config) is known (the latter condition is also
// true if the unknown condition has been computed once and didn't change
// and is therefore not unknown anymore)
if currentValueNotOnceSet || suppliedCondition.Value {
// if currentValue.IsUnknown() {
// newValue = types.String{Null: true}
// } else {
Expand Down
4 changes: 3 additions & 1 deletion internal/provider/replaced_when_resource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ resource creation and resource deletion as change you can try the following appr
"value": {
Type: types.StringType,
Computed: true,
Description: `If the very first condition is false or remains false, then the value remains unchanged.
Description: `If the very first condition is false, then the value will be once initialized by a random value.
If the condition is false or remains false, then the value remains unchanged.
The condition change from true to false does not trigger a replacement of those who use the value as
target for replace_triggered_by.
Expand Down

0 comments on commit 1143996

Please sign in to comment.