Skip to content

Commit

Permalink
Modifying all other resources to handle to use same plan modifier as …
Browse files Browse the repository at this point in the history
…integer resource (#303)
  • Loading branch information
bendbennett committed Sep 1, 2022
1 parent 16d2e85 commit 233eabd
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 7 deletions.
29 changes: 27 additions & 2 deletions internal/provider/resource_id.go
Expand Up @@ -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)
Expand Down Expand Up @@ -47,7 +48,7 @@ exist concurrently.
},
Optional: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
resource.RequiresReplace(),
planmodifiers.RequiresReplaceIfValuesNotNull(),
},
},
"byte_length": {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion internal/provider/resource_password.go
Expand Up @@ -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
Expand Down Expand Up @@ -281,7 +290,7 @@ func passwordSchemaV2() tfsdk.Schema {
},
Optional: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
resource.RequiresReplace(),
planmodifiers.RequiresReplaceIfValuesNotNull(),
},
},

Expand Down Expand Up @@ -312,6 +321,7 @@ func passwordSchemaV2() tfsdk.Schema {
PlanModifiers: []tfsdk.AttributePlanModifier{
planmodifiers.DefaultValue(types.Bool{Value: true}),
planmodifiers.RequiresReplace(),
resource.UseStateForUnknown(),
},
},

Expand All @@ -323,6 +333,7 @@ func passwordSchemaV2() tfsdk.Schema {
PlanModifiers: []tfsdk.AttributePlanModifier{
planmodifiers.DefaultValue(types.Bool{Value: true}),
planmodifiers.RequiresReplace(),
resource.UseStateForUnknown(),
},
},

Expand All @@ -334,6 +345,7 @@ func passwordSchemaV2() tfsdk.Schema {
PlanModifiers: []tfsdk.AttributePlanModifier{
planmodifiers.DefaultValue(types.Bool{Value: true}),
planmodifiers.RequiresReplace(),
resource.UseStateForUnknown(),
},
},

Expand All @@ -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.",
},
Expand All @@ -358,6 +371,7 @@ func passwordSchemaV2() tfsdk.Schema {
PlanModifiers: []tfsdk.AttributePlanModifier{
planmodifiers.NumberNumericAttributePlanModifier(),
planmodifiers.RequiresReplace(),
resource.UseStateForUnknown(),
},
},

Expand All @@ -369,6 +383,7 @@ func passwordSchemaV2() tfsdk.Schema {
PlanModifiers: []tfsdk.AttributePlanModifier{
planmodifiers.DefaultValue(types.Int64{Value: 0}),
planmodifiers.RequiresReplace(),
resource.UseStateForUnknown(),
},
},

Expand All @@ -380,6 +395,7 @@ func passwordSchemaV2() tfsdk.Schema {
PlanModifiers: []tfsdk.AttributePlanModifier{
planmodifiers.DefaultValue(types.Int64{Value: 0}),
planmodifiers.RequiresReplace(),
resource.UseStateForUnknown(),
},
},

Expand All @@ -391,6 +407,7 @@ func passwordSchemaV2() tfsdk.Schema {
PlanModifiers: []tfsdk.AttributePlanModifier{
planmodifiers.DefaultValue(types.Int64{Value: 0}),
planmodifiers.RequiresReplace(),
resource.UseStateForUnknown(),
},
},

Expand All @@ -402,6 +419,7 @@ func passwordSchemaV2() tfsdk.Schema {
PlanModifiers: []tfsdk.AttributePlanModifier{
planmodifiers.DefaultValue(types.Int64{Value: 0}),
planmodifiers.RequiresReplace(),
resource.UseStateForUnknown(),
},
},

Expand All @@ -414,6 +432,7 @@ func passwordSchemaV2() tfsdk.Schema {
Computed: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
resource.RequiresReplace(),
resource.UseStateForUnknown(),
},
},

Expand All @@ -422,19 +441,28 @@ func passwordSchemaV2() tfsdk.Schema {
Type: types.StringType,
Computed: true,
Sensitive: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
resource.UseStateForUnknown(),
},
},

"bcrypt_hash": {
Description: "A bcrypt hash of the generated random string.",
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(),
},
},
},
}
Expand Down
16 changes: 15 additions & 1 deletion internal/provider/resource_pet.go
Expand Up @@ -36,7 +36,7 @@ func (r *petResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnos
},
Optional: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
resource.RequiresReplace(),
planmodifiers.RequiresReplaceIfValuesNotNull(),
},
},
"length": {
Expand All @@ -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": {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion internal/provider/resource_shuffle.go
Expand Up @@ -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"
)

Expand All @@ -30,7 +31,7 @@ func (r *shuffleResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Dia
},
Optional: true,
PlanModifiers: []tfsdk.AttributePlanModifier{
resource.RequiresReplace(),
planmodifiers.RequiresReplaceIfValuesNotNull(),
},
},
"seed": {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 233eabd

Please sign in to comment.