Skip to content

Commit 25b0fb7

Browse files
Copilottobio
andcommitted
Read password_wo from config instead of plan for write-only attributes
Co-authored-by: tobio <[email protected]>
1 parent 4832692 commit 25b0fb7

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

internal/elasticsearch/security/user/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import (
77
)
88

99
func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
10-
resp.Diagnostics.Append(r.update(ctx, req.Plan, &resp.State)...)
10+
resp.Diagnostics.Append(r.update(ctx, req.Plan, req.Config, &resp.State)...)
1111
}

internal/elasticsearch/security/user/update.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
1313
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1414
"github.com/hashicorp/terraform-plugin-framework/diag"
15+
"github.com/hashicorp/terraform-plugin-framework/path"
1516
"github.com/hashicorp/terraform-plugin-framework/resource"
1617
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
1718
"github.com/hashicorp/terraform-plugin-framework/types"
1819
)
1920

20-
func (r *userResource) update(ctx context.Context, plan tfsdk.Plan, state *tfsdk.State) diag.Diagnostics {
21+
func (r *userResource) update(ctx context.Context, plan tfsdk.Plan, config tfsdk.Config, state *tfsdk.State) diag.Diagnostics {
2122
var planData UserData
2223
var diags diag.Diagnostics
2324
diags.Append(plan.Get(ctx, &planData)...)
@@ -54,9 +55,16 @@ func (r *userResource) update(ctx context.Context, plan tfsdk.Plan, state *tfsdk
5455

5556
// Handle password fields - only set password if it's in the plan AND (it's a create OR it has changed from state)
5657
// Priority: password_wo > password > password_hash
57-
if utils.IsKnown(planData.PasswordWo) && (!hasState || !planData.PasswordWoVersion.Equal(stateData.PasswordWoVersion)) {
58+
// Read password_wo from config as per Terraform write-only attribute guidelines
59+
var passwordWoFromConfig types.String
60+
diags.Append(config.GetAttribute(ctx, path.Root("password_wo"), &passwordWoFromConfig)...)
61+
if diags.HasError() {
62+
return diags
63+
}
64+
65+
if utils.IsKnown(passwordWoFromConfig) && (!hasState || !planData.PasswordWoVersion.Equal(stateData.PasswordWoVersion)) {
5866
// Use write-only password - changes triggered by version change
59-
password := planData.PasswordWo.ValueString()
67+
password := passwordWoFromConfig.ValueString()
6068
user.Password = &password
6169
} else if utils.IsKnown(planData.Password) && (!hasState || !planData.Password.Equal(stateData.Password)) {
6270
password := planData.Password.ValueString()
@@ -121,9 +129,5 @@ func (r *userResource) update(ctx context.Context, plan tfsdk.Plan, state *tfsdk
121129
}
122130

123131
func (r *userResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
124-
diags := r.update(ctx, req.Plan, &resp.State)
125-
resp.Diagnostics.Append(diags...)
126-
if resp.Diagnostics.HasError() {
127-
return
128-
}
132+
resp.Diagnostics.Append(r.update(ctx, req.Plan, req.Config, &resp.State)...)
129133
}

0 commit comments

Comments
 (0)