Skip to content

Commit

Permalink
normalize resource model name across CRUD operations (#2118)
Browse files Browse the repository at this point in the history
  • Loading branch information
fairclothjm authored Dec 21, 2023
1 parent f27b0bc commit 06bf794
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions internal/vault/sys/password_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,27 @@ func (r *PasswordPolicyResource) Schema(ctx context.Context, req resource.Schema
//
// https://developer.hashicorp.com/terraform/plugin/framework/resources/create
func (r *PasswordPolicyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan PasswordPolicyModel
var data PasswordPolicyModel

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

client, err := client.GetClient(ctx, r.Meta(), plan.Namespace.ValueString())
client, err := client.GetClient(ctx, r.Meta(), data.Namespace.ValueString())
if err != nil {
resp.Diagnostics.AddError("Error Configuring Resource Client", err.Error())
return
}

data := map[string]interface{}{
"policy": plan.Policy.ValueString(),
vaultRequest := map[string]interface{}{
"policy": data.Policy.ValueString(),
}
path := r.path(plan.Name.ValueString())
path := r.path(data.Name.ValueString())
// vault returns a nil response on success
_, err = client.Logical().Write(path, data)
_, err = client.Logical().Write(path, vaultRequest)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create Resource",
Expand All @@ -113,33 +113,33 @@ func (r *PasswordPolicyResource) Create(ctx context.Context, req resource.Create
}

// write the ID to state which is required for backwards compatibility
plan.ID = types.StringValue(plan.Name.ValueString())
data.ID = types.StringValue(data.Name.ValueString())

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

// Read is called during the terraform apply, terraform plan, and terraform
// refresh commands.
//
// https://developer.hashicorp.com/terraform/plugin/framework/resources/read
func (r *PasswordPolicyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state PasswordPolicyModel
var data PasswordPolicyModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

client, err := client.GetClient(ctx, r.Meta(), state.Namespace.ValueString())
client, err := client.GetClient(ctx, r.Meta(), data.Namespace.ValueString())
if err != nil {
resp.Diagnostics.AddError("Error Configuring Resource Client", err.Error())
return
}

// read the name from the id field to support the import command
name := state.ID.ValueString()
name := data.ID.ValueString()
path := r.path(name)
policyResp, err := client.Logical().Read(path)
if err != nil {
Expand Down Expand Up @@ -170,42 +170,42 @@ func (r *PasswordPolicyResource) Read(ctx context.Context, req resource.ReadRequ
return
}

state.Policy = types.StringValue(readResp.Policy)
data.Policy = types.StringValue(readResp.Policy)

// write the name to state to support the import command
state.Name = types.StringValue(name)
data.Name = types.StringValue(name)

// write the ID to state which is required for backwards compatibility
state.ID = types.StringValue(name)
data.ID = types.StringValue(name)

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

// Update is called during the terraform apply command
//
// https://developer.hashicorp.com/terraform/plugin/framework/resources/update
func (r *PasswordPolicyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan PasswordPolicyModel
var data PasswordPolicyModel

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

client, err := client.GetClient(ctx, r.Meta(), plan.Namespace.ValueString())
client, err := client.GetClient(ctx, r.Meta(), data.Namespace.ValueString())
if err != nil {
resp.Diagnostics.AddError("Error Configuring Resource Client", err.Error())
return
}

data := map[string]interface{}{
"policy": plan.Policy.ValueString(),
vaultRequest := map[string]interface{}{
"policy": data.Policy.ValueString(),
}
path := r.path(plan.Name.ValueString())
path := r.path(data.Name.ValueString())
// vault returns a nil response on success
_, err = client.Logical().Write(path, data)
_, err = client.Logical().Write(path, vaultRequest)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Update Resource",
Expand All @@ -218,32 +218,32 @@ func (r *PasswordPolicyResource) Update(ctx context.Context, req resource.Update
}

// write the ID to state which is required for backwards compatibility
plan.ID = types.StringValue(plan.Name.ValueString())
data.ID = types.StringValue(data.Name.ValueString())

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

// Delete is called during the terraform apply command
//
// https://developer.hashicorp.com/terraform/plugin/framework/resources/delete
func (r *PasswordPolicyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var plan PasswordPolicyModel
var data PasswordPolicyModel

// Read Terraform state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &plan)...)
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

client, err := client.GetClient(ctx, r.Meta(), plan.Namespace.ValueString())
client, err := client.GetClient(ctx, r.Meta(), data.Namespace.ValueString())
if err != nil {
resp.Diagnostics.AddError("Error Configuring Resource Client", err.Error())
return
}

path := r.path(plan.Name.ValueString())
path := r.path(data.Name.ValueString())

_, err = client.Logical().Delete(path)
if err != nil {
Expand Down

0 comments on commit 06bf794

Please sign in to comment.