From ddf65df2727baec6d7941a54e493d9909f80db58 Mon Sep 17 00:00:00 2001 From: JM Faircloth Date: Tue, 19 Dec 2023 17:03:45 -0600 Subject: [PATCH] add ToAPIModel helper --- internal/framework/model/model.go | 37 ++++++++++++++++++++++ internal/vault/sys/password_policy.go | 26 ++------------- internal/vault/sys/password_policy_test.go | 21 ------------ 3 files changed, 39 insertions(+), 45 deletions(-) create mode 100644 internal/framework/model/model.go diff --git a/internal/framework/model/model.go b/internal/framework/model/model.go new file mode 100644 index 000000000..fcfb263c1 --- /dev/null +++ b/internal/framework/model/model.go @@ -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 +} diff --git a/internal/vault/sys/password_policy.go b/internal/vault/sys/password_policy.go index 385e63e74..ebc24f1c5 100644 --- a/internal/vault/sys/password_policy.go +++ b/internal/vault/sys/password_policy.go @@ -2,7 +2,6 @@ package sys import ( "context" - "encoding/json" "fmt" "github.com/hashicorp/terraform-plugin-framework/resource" @@ -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 @@ -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() @@ -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) diff --git a/internal/vault/sys/password_policy_test.go b/internal/vault/sys/password_policy_test.go index 81f1de94c..5dd24507e 100644 --- a/internal/vault/sys/password_policy_test.go +++ b/internal/vault/sys/password_policy_test.go @@ -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, - // }, }, }) }