-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bed131b
commit ddf65df
Showing
3 changed files
with
39 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters