Skip to content

Commit

Permalink
Improve util_register plan diff to show expected value
Browse files Browse the repository at this point in the history
* In CustomizedDiff, instead of just marking `value` as computed
(known at apply), set it to its new known value. Terraform plans
will show the expected content value after apply
* As before, changing content to "" or null has no affect
  • Loading branch information
dghubble committed Mar 31, 2022
1 parent 373096a commit ce542c3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
10 changes: 7 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ Notable changes between releases.

## Latest

## v0.2.2

* Improve `util_register` plan diff to show expected value

## v0.2.1

* Rename `util_register` field from `set` to `content`
* Fix `util_register` to mark `value` attribute as computed to propagate changes
* Rename `util_register` field from `set` to `content` ([#9](https://github.com/poseidon/terraform-provider-util/pull/9))
* Fix `util_register` to mark `value` attribute as computed to propagate changes ([#8](https://github.com/poseidon/terraform-provider-util/pull/8))

## v0.2.0

* Add `util_register` resource for storing values
* Add `util_register` resource for storing values ([#7](https://github.com/poseidon/terraform-provider-util/pull/7))

## v0.1.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ terraform {
required_providers {
ct = {
source = "poseidon/util"
version = "0.2.0"
version = "0.2.2"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions util/datasource_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ func datasourceReplace() *schema.Resource {
ReadContext: datasourceReplaceRead,

Schema: map[string]*schema.Schema{
"content": &schema.Schema{
"content": {
Type: schema.TypeString,
Required: true,
},
"replacements": &schema.Schema{
"replacements": {
Type: schema.TypeMap,
Elem: schema.TypeString,
Required: true,
},
"replaced": &schema.Schema{
"replaced": {
Type: schema.TypeString,
Computed: true,
Description: "content with replacements performed",
Expand Down
4 changes: 2 additions & 2 deletions util/datasource_replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func TestReplace(t *testing.T) {
r.UnitTest(t, r.TestCase{
Providers: testProviders,
Steps: []r.TestStep{
r.TestStep{
{
Config: replaceExample1,
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttr("data.util_replace.example", "replaced", replaceExpected1),
),
},
r.TestStep{
{
Config: replaceExample2,
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttr("data.util_replace.example", "replaced", replaceExpected2),
Expand Down
14 changes: 8 additions & 6 deletions util/resource_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -17,7 +16,7 @@ func resourceRegister() *schema.Resource {
UpdateContext: registerUpdate,
DeleteContext: registerDelete,
Schema: map[string]*schema.Schema{
"content": &schema.Schema{
"content": {
Type: schema.TypeString,
// Allow content to be null
Optional: true,
Expand All @@ -30,16 +29,19 @@ func resourceRegister() *schema.Resource {
DiffSuppressOnRefresh: true,
DiffSuppressFunc: registerDiffSuppress,
},
"value": &schema.Schema{
"value": {
Type: schema.TypeString,
Computed: true,
Description: "Computed register value",
},
},
// Changes to content (that are non-empty) mark value as computed
CustomizeDiff: customdiff.ComputedIf("value", func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) bool {
return d.HasChange("content") && d.Get("content").(string) != ""
}),
CustomizeDiff: func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
if content := d.Get("content").(string); d.HasChange("content") && content != "" {
d.SetNew("value", content)
}
return nil
},
}
}

Expand Down
8 changes: 4 additions & 4 deletions util/resource_register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ func TestRegister(t *testing.T) {
Providers: testProviders,
Steps: []r.TestStep{
// set initial value
r.TestStep{
{
Config: registerInitial,
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttr("util_register.example", "value", registerInitialExpected),
r.TestCheckOutput("out", registerInitialExpected),
),
},
// Empty content does NOT change value
r.TestStep{
{
Config: registerUnsetSHA,
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttr("util_register.example", "value", registerInitialExpected),
r.TestCheckOutput("out", registerInitialExpected),
),
},
// Non-empty content DOES change value
r.TestStep{
{
Config: registerUpdateSHA,
Check: r.ComposeTestCheckFunc(
r.TestCheckResourceAttr("util_register.example", "value", registerUpdateExpected),
r.TestCheckOutput("out", registerUpdateExpected),
),
},
// suppress noisy diffs that won't affect value
r.TestStep{
{
Config: registerUnsetSHA,
PlanOnly: true,
Check: r.ComposeTestCheckFunc(
Expand Down

0 comments on commit ce542c3

Please sign in to comment.