From 233eabd6ed4706a81687475e23f204f819398fec Mon Sep 17 00:00:00 2001 From: Benjamin Bennett Date: Thu, 1 Sep 2022 19:02:36 +0100 Subject: [PATCH] Modifying all other resources to handle to use same plan modifier as integer resource (#303) --- internal/provider/resource_id.go | 29 +++++++++++++++++++++++-- internal/provider/resource_password.go | 30 +++++++++++++++++++++++++- internal/provider/resource_pet.go | 16 +++++++++++++- internal/provider/resource_shuffle.go | 18 +++++++++++++++- internal/provider/resource_string.go | 27 ++++++++++++++++++++++- internal/provider/resource_uuid.go | 18 +++++++++++++++- 6 files changed, 131 insertions(+), 7 deletions(-) diff --git a/internal/provider/resource_id.go b/internal/provider/resource_id.go index 95629bd2..313c006b 100644 --- a/internal/provider/resource_id.go +++ b/internal/provider/resource_id.go @@ -16,6 +16,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" "github.com/terraform-providers/terraform-provider-random/internal/diagnostics" + "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers" ) var _ provider.ResourceType = (*idResourceType)(nil) @@ -47,7 +48,7 @@ exist concurrently. }, Optional: true, PlanModifiers: []tfsdk.AttributePlanModifier{ - resource.RequiresReplace(), + planmodifiers.RequiresReplaceIfValuesNotNull(), }, }, "byte_length": { @@ -73,27 +74,42 @@ exist concurrently. "case-sensitive letters, digits and the characters `_` and `-`.", Type: types.StringType, Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, "b64_std": { Description: "The generated id presented in base64 without additional transformations.", Type: types.StringType, Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, "hex": { Description: "The generated id presented in padded hexadecimal digits. This result will " + "always be twice as long as the requested byte length.", Type: types.StringType, Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, "dec": { Description: "The generated id presented in non-padded decimal digits.", Type: types.StringType, Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, "id": { Description: "The generated id presented in base64 without additional transformations or prefix.", Type: types.StringType, Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, }, }, nil @@ -165,7 +181,16 @@ func (r *idResource) Read(context.Context, resource.ReadRequest, *resource.ReadR // Update is intentionally left blank as all required and optional attributes force replacement of the resource // through the RequiresReplace AttributePlanModifier. -func (r *idResource) Update(context.Context, resource.UpdateRequest, *resource.UpdateResponse) { +func (r *idResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var model idModelV0 + + resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...) + + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, &model)...) } // Delete does not need to explicitly call resp.State.RemoveResource() as this is automatically handled by the diff --git a/internal/provider/resource_password.go b/internal/provider/resource_password.go index ada43b33..19699e45 100644 --- a/internal/provider/resource_password.go +++ b/internal/provider/resource_password.go @@ -103,6 +103,15 @@ func (r *passwordResource) Read(ctx context.Context, req resource.ReadRequest, r // Update is intentionally left blank as all required and optional attributes force replacement of the resource // through the RequiresReplace AttributePlanModifier. func (r *passwordResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var model passwordModelV2 + + resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...) + + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, &model)...) } // Delete does not need to explicitly call resp.State.RemoveResource() as this is automatically handled by the @@ -281,7 +290,7 @@ func passwordSchemaV2() tfsdk.Schema { }, Optional: true, PlanModifiers: []tfsdk.AttributePlanModifier{ - resource.RequiresReplace(), + planmodifiers.RequiresReplaceIfValuesNotNull(), }, }, @@ -312,6 +321,7 @@ func passwordSchemaV2() tfsdk.Schema { PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Bool{Value: true}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -323,6 +333,7 @@ func passwordSchemaV2() tfsdk.Schema { PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Bool{Value: true}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -334,6 +345,7 @@ func passwordSchemaV2() tfsdk.Schema { PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Bool{Value: true}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -346,6 +358,7 @@ func passwordSchemaV2() tfsdk.Schema { PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.NumberNumericAttributePlanModifier(), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, DeprecationMessage: "**NOTE**: This is deprecated, use `numeric` instead.", }, @@ -358,6 +371,7 @@ func passwordSchemaV2() tfsdk.Schema { PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.NumberNumericAttributePlanModifier(), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -369,6 +383,7 @@ func passwordSchemaV2() tfsdk.Schema { PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Int64{Value: 0}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -380,6 +395,7 @@ func passwordSchemaV2() tfsdk.Schema { PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Int64{Value: 0}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -391,6 +407,7 @@ func passwordSchemaV2() tfsdk.Schema { PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Int64{Value: 0}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -402,6 +419,7 @@ func passwordSchemaV2() tfsdk.Schema { PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Int64{Value: 0}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -414,6 +432,7 @@ func passwordSchemaV2() tfsdk.Schema { Computed: true, PlanModifiers: []tfsdk.AttributePlanModifier{ resource.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -422,6 +441,9 @@ func passwordSchemaV2() tfsdk.Schema { Type: types.StringType, Computed: true, Sensitive: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, "bcrypt_hash": { @@ -429,12 +451,18 @@ func passwordSchemaV2() tfsdk.Schema { Type: types.StringType, Computed: true, Sensitive: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, "id": { Description: "A static value used internally by Terraform, this should not be referenced in configurations.", Computed: true, Type: types.StringType, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, }, } diff --git a/internal/provider/resource_pet.go b/internal/provider/resource_pet.go index 40c570e7..dc50da16 100644 --- a/internal/provider/resource_pet.go +++ b/internal/provider/resource_pet.go @@ -36,7 +36,7 @@ func (r *petResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnos }, Optional: true, PlanModifiers: []tfsdk.AttributePlanModifier{ - resource.RequiresReplace(), + planmodifiers.RequiresReplaceIfValuesNotNull(), }, }, "length": { @@ -47,6 +47,7 @@ func (r *petResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnos PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Int64{Value: 2}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, "prefix": { @@ -63,12 +64,16 @@ func (r *petResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnos PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.String{Value: "-"}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, "id": { Description: "The random pet name.", Type: types.StringType, Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, }, }, nil @@ -131,6 +136,15 @@ func (r *petResource) Read(ctx context.Context, req resource.ReadRequest, resp * // Update is intentionally left blank as all required and optional attributes force replacement of the resource // through the RequiresReplace AttributePlanModifier. func (r *petResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var model petModelV0 + + resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...) + + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, &model)...) } // Delete does not need to explicitly call resp.State.RemoveResource() as this is automatically handled by the diff --git a/internal/provider/resource_shuffle.go b/internal/provider/resource_shuffle.go index 2e855d3e..e577279a 100644 --- a/internal/provider/resource_shuffle.go +++ b/internal/provider/resource_shuffle.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers" "github.com/terraform-providers/terraform-provider-random/internal/random" ) @@ -30,7 +31,7 @@ func (r *shuffleResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Dia }, Optional: true, PlanModifiers: []tfsdk.AttributePlanModifier{ - resource.RequiresReplace(), + planmodifiers.RequiresReplaceIfValuesNotNull(), }, }, "seed": { @@ -73,11 +74,17 @@ func (r *shuffleResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Dia ElemType: types.StringType, }, Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, "id": { Description: "A static value used internally by Terraform, this should not be referenced in configurations.", Type: types.StringType, Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, }, }, nil @@ -165,6 +172,15 @@ func (r *shuffleResource) Read(ctx context.Context, req resource.ReadRequest, re // Update is intentionally left blank as all required and optional attributes force replacement of the resource // through the RequiresReplace AttributePlanModifier. func (r *shuffleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var model shuffleModelV0 + + resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...) + + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, &model)...) } // Delete does not need to explicitly call resp.State.RemoveResource() as this is automatically handled by the diff --git a/internal/provider/resource_string.go b/internal/provider/resource_string.go index 320b3c04..26a77bd7 100644 --- a/internal/provider/resource_string.go +++ b/internal/provider/resource_string.go @@ -40,7 +40,7 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn }, Optional: true, PlanModifiers: []tfsdk.AttributePlanModifier{ - resource.RequiresReplace(), + planmodifiers.RequiresReplaceIfValuesNotNull(), }, }, @@ -69,6 +69,7 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Bool{Value: true}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -80,6 +81,7 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Bool{Value: true}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -91,6 +93,7 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Bool{Value: true}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -103,6 +106,7 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.NumberNumericAttributePlanModifier(), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, DeprecationMessage: "**NOTE**: This is deprecated, use `numeric` instead.", }, @@ -115,6 +119,7 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.NumberNumericAttributePlanModifier(), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -126,6 +131,7 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Int64{Value: 0}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -137,6 +143,7 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Int64{Value: 0}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -148,6 +155,7 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Int64{Value: 0}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -159,6 +167,7 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn PlanModifiers: []tfsdk.AttributePlanModifier{ planmodifiers.DefaultValue(types.Int64{Value: 0}), planmodifiers.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -171,6 +180,7 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn Computed: true, PlanModifiers: []tfsdk.AttributePlanModifier{ resource.RequiresReplace(), + resource.UseStateForUnknown(), }, }, @@ -178,12 +188,18 @@ func (r stringResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagn Description: "The generated random string.", Type: types.StringType, Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, "id": { Description: "The generated random string.", Computed: true, Type: types.StringType, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, }, }, nil @@ -260,6 +276,15 @@ func (r *stringResource) Read(ctx context.Context, req resource.ReadRequest, res // Update is intentionally left blank as all required and optional attributes force replacement of the resource // through the RequiresReplace AttributePlanModifier. func (r *stringResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var model stringModelV2 + + resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...) + + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, &model)...) } // Delete does not need to explicitly call resp.State.RemoveResource() as this is automatically handled by the diff --git a/internal/provider/resource_uuid.go b/internal/provider/resource_uuid.go index dba22af2..f0dcc3d9 100644 --- a/internal/provider/resource_uuid.go +++ b/internal/provider/resource_uuid.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" "github.com/terraform-providers/terraform-provider-random/internal/diagnostics" + "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers" ) var _ provider.ResourceType = (*uuidResourceType)(nil) @@ -34,18 +35,24 @@ func (r *uuidResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagno }, Optional: true, PlanModifiers: []tfsdk.AttributePlanModifier{ - resource.RequiresReplace(), + planmodifiers.RequiresReplaceIfValuesNotNull(), }, }, "result": { Description: "The generated uuid presented in string format.", Type: types.StringType, Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, "id": { Description: "The generated uuid presented in string format.", Type: types.StringType, Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + resource.UseStateForUnknown(), + }, }, }, }, nil @@ -103,6 +110,15 @@ func (r *uuidResource) Read(ctx context.Context, req resource.ReadRequest, resp // Update is intentionally left blank as all required and optional attributes force replacement of the resource // through the RequiresReplace AttributePlanModifier. func (r *uuidResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var model uuidModelV0 + + resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...) + + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, &model)...) } // Delete does not need to explicitly call resp.State.RemoveResource() as this is automatically handled by the