diff --git a/CHANGES.md b/CHANGES.md index 509b29e..d9a7c5b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/README.md b/README.md index 37072fb..d4377af 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ terraform { required_providers { ct = { source = "poseidon/util" - version = "0.2.0" + version = "0.2.2" } } } diff --git a/util/datasource_replace.go b/util/datasource_replace.go index 3726adc..897b7b2 100644 --- a/util/datasource_replace.go +++ b/util/datasource_replace.go @@ -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", diff --git a/util/datasource_replace_test.go b/util/datasource_replace_test.go index e5e95dc..865566b 100644 --- a/util/datasource_replace_test.go +++ b/util/datasource_replace_test.go @@ -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), diff --git a/util/resource_register.go b/util/resource_register.go index fac1aa2..a1c9433 100644 --- a/util/resource_register.go +++ b/util/resource_register.go @@ -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" ) @@ -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, @@ -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 + }, } } diff --git a/util/resource_register_test.go b/util/resource_register_test.go index d261e4f..1939d32 100644 --- a/util/resource_register_test.go +++ b/util/resource_register_test.go @@ -47,7 +47,7 @@ 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), @@ -55,7 +55,7 @@ func TestRegister(t *testing.T) { ), }, // Empty content does NOT change value - r.TestStep{ + { Config: registerUnsetSHA, Check: r.ComposeTestCheckFunc( r.TestCheckResourceAttr("util_register.example", "value", registerInitialExpected), @@ -63,7 +63,7 @@ func TestRegister(t *testing.T) { ), }, // Non-empty content DOES change value - r.TestStep{ + { Config: registerUpdateSHA, Check: r.ComposeTestCheckFunc( r.TestCheckResourceAttr("util_register.example", "value", registerUpdateExpected), @@ -71,7 +71,7 @@ func TestRegister(t *testing.T) { ), }, // suppress noisy diffs that won't affect value - r.TestStep{ + { Config: registerUnsetSHA, PlanOnly: true, Check: r.ComposeTestCheckFunc(