Skip to content

Commit

Permalink
add ToAPIModel helper
Browse files Browse the repository at this point in the history
  • Loading branch information
fairclothjm committed Dec 20, 2023
1 parent bed131b commit ddf65df
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 45 deletions.
37 changes: 37 additions & 0 deletions internal/framework/model/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package model

import (
"encoding/json"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/diag"
)

// ToAPIModel is helper to translate Vault response data to its respective
// Vault API data model
func ToAPIModel(data, model any, diagnostics diag.Diagnostics) error {
jsonData, err := json.Marshal(data)
if err != nil {
msg := "Unable to marshal Vault response"
diagnostics.AddError(
msg,
"An unexpected error occurred while attempting to marshal the Vault response.\n\n"+
"Error: "+err.Error(),
)

return fmt.Errorf(msg)
}

err = json.Unmarshal(jsonData, &model)
if err != nil {
msg := "Unable to unmarshal data to API model"
diagnostics.AddError(
msg,
"An unexpected error occurred while attempting to unmarshal the data.\n\n"+
"Error: "+err.Error(),
)

return fmt.Errorf(msg)
}
return nil
}
26 changes: 2 additions & 24 deletions internal/vault/sys/password_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sys

import (
"context"
"encoding/json"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -11,6 +10,7 @@ import (

"github.com/hashicorp/terraform-provider-vault/internal/framework/base"
"github.com/hashicorp/terraform-provider-vault/internal/framework/client"
"github.com/hashicorp/terraform-provider-vault/internal/framework/model"
)

// Ensure the implementation satisfies the resource.ResourceWithConfigure interface
Expand Down Expand Up @@ -138,8 +138,6 @@ func (r *PasswordPolicyResource) Read(ctx context.Context, req resource.ReadRequ
return
}

// TODO: refactor the following read, marshal and unmarshal into a helper?

name := state.Name.ValueString()
if name == "" {
name = state.ID.ValueString()
Expand Down Expand Up @@ -167,28 +165,8 @@ func (r *PasswordPolicyResource) Read(ctx context.Context, req resource.ReadRequ
return
}

jsonData, err := json.Marshal(policyResp.Data)
if err != nil {
resp.Diagnostics.AddError(
"Unable to marshal Vault response",
"An unexpected error occurred while attempting to marshal the Vault response.\n\n"+
"Error: "+err.Error(),
)

return
}

var readResp PasswordPolicyAPIModel
err = json.Unmarshal(jsonData, &readResp)
if err != nil {
resp.Diagnostics.AddError(
"Unable to unmarshal data to API model",
"An unexpected error occurred while attempting to unmarshal the data.\n\n"+
"Error: "+err.Error(),
)

return
}
model.ToAPIModel(policyResp.Data, &readResp, resp.Diagnostics)

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

Expand Down
21 changes: 0 additions & 21 deletions internal/vault/sys/password_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,6 @@ func TestAccPasswordPolicy(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "policy"),
),
},
// {
// // unfortunately two steps are needed when testing import,
// // since the tf-plugin-sdk does not allow for specifying environment variables :(
// // neither does have any support for generic post-step functions.
// // It is possible that this will cause issues if we ever want to support parallel tests.
// // We would have to update the SDK to suport specifying extra env vars by step.
// PreConfig: func() {
// t.Setenv(consts.EnvVarVaultNamespaceImport, ns)
// },
// ImportState: true,
// ImportStateVerify: true,
// ResourceName: resourceName,
// },
// {
// // needed for the import step above :(
// Config: testAccPasswordPolicyConfig(ns, policyName, testPolicyUpdated),
// PreConfig: func() {
// os.Unsetenv(consts.EnvVarVaultNamespaceImport)
// },
// PlanOnly: true,
// },
},
})
}
Expand Down

0 comments on commit ddf65df

Please sign in to comment.