-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ephemeral: support write-only attributes
Only write-only attributes are allowed to be set to ephemeral values. Ephemeral values are set to null in both the plan and state. This commit only lays the basis, there is further work needed to support this both for stacks and the terminal UI interfaces.
- Loading branch information
1 parent
30b9379
commit 8a22769
Showing
17 changed files
with
271 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package ephemeral | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/internal/lang/marks" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
// RemoveEphemeralValuesForMarshaling takes a value that possibly contains ephemeral | ||
// values and returns an equal value without ephemeral values. If an attribute contains | ||
// an ephemeral value it will be set to null. | ||
func RemoveEphemeralValuesForMarshaling(value cty.Value) cty.Value { | ||
// We currently have no error case, so we can ignore the error | ||
val, _ := cty.Transform(value, func(p cty.Path, v cty.Value) (cty.Value, error) { | ||
_, givenMarks := v.Unmark() | ||
if _, isEphemeral := givenMarks[marks.Ephemeral]; isEphemeral { | ||
// We'll strip the ephemeral mark but retain any other marks | ||
// that might be present on the input. | ||
delete(givenMarks, marks.Ephemeral) | ||
if !v.IsKnown() { | ||
// If the source value is unknown then we must leave it | ||
// unknown because its final type might be more precise | ||
// than the associated type constraint and returning a | ||
// typed null could therefore over-promise on what the | ||
// final result type will be. | ||
// We're deliberately constructing a fresh unknown value | ||
// here, rather than returning the one we were given, | ||
// because we need to discard any refinements that the | ||
// unknown value might be carrying that definitely won't | ||
// be honored when we force the final result to be null. | ||
return cty.UnknownVal(v.Type()).WithMarks(givenMarks), nil | ||
} | ||
return cty.NullVal(v.Type()).WithMarks(givenMarks), nil | ||
} | ||
return v, nil | ||
}) | ||
return val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package ephemeral | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/internal/lang/marks" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func TestEphemeral_removeEphemeralValuesForMarshaling(t *testing.T) { | ||
for name, tc := range map[string]struct { | ||
input cty.Value | ||
want cty.Value | ||
}{ | ||
"empty case": { | ||
input: cty.NullVal(cty.DynamicPseudoType), | ||
want: cty.NullVal(cty.DynamicPseudoType), | ||
}, | ||
"ephemeral marks case": { | ||
input: cty.ObjectVal(map[string]cty.Value{ | ||
"ephemeral": cty.StringVal("ephemeral_value").Mark(marks.Ephemeral), | ||
"normal": cty.StringVal("normal_value"), | ||
}), | ||
want: cty.ObjectVal(map[string]cty.Value{ | ||
"ephemeral": cty.NullVal(cty.String), | ||
"normal": cty.StringVal("normal_value"), | ||
}), | ||
}, | ||
"sensitive marks case": { | ||
input: cty.ObjectVal(map[string]cty.Value{ | ||
"sensitive": cty.StringVal("sensitive_value").Mark(marks.Sensitive), | ||
"normal": cty.StringVal("normal_value"), | ||
}), | ||
want: cty.ObjectVal(map[string]cty.Value{ | ||
"sensitive": cty.StringVal("sensitive_value").Mark(marks.Sensitive), | ||
"normal": cty.StringVal("normal_value"), | ||
}), | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
got := RemoveEphemeralValuesForMarshaling(tc.input) | ||
|
||
if !got.RawEquals(tc.want) { | ||
t.Errorf("got %#v, want %#v", got, tc.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.