/
utils.go
84 lines (76 loc) · 2.59 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
package provider
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
tfReflect "github.com/squat/terraform-provider-datacrunch/internal/provider/reflect"
"net/http"
"net/http/httputil"
"reflect"
)
func debugResponse(response *http.Response) string {
dumpReq, err := httputil.DumpRequest(response.Request, true)
if err != nil {
dumpReq, err = httputil.DumpRequest(response.Request, false)
if err != nil {
return err.Error()
}
}
dumpRes, err := httputil.DumpResponse(response, true)
if err != nil {
dumpRes, err = httputil.DumpResponse(response, false)
if err != nil {
return err.Error()
}
}
return fmt.Sprintf("**Request**:\n%s\n**Response**:\n%s", string(dumpReq), string(dumpRes))
}
func reflectJSONKey(data any, key string) reflect.Value {
jsonIfied, err := json.Marshal(data)
if err != nil {
panic(fmt.Errorf("failed to marshal data: %w", err))
}
var jsonMap map[string]interface{}
err = json.Unmarshal(jsonIfied, &jsonMap)
if err != nil {
panic(fmt.Errorf("failed to unmarshal data: %w", err))
}
return reflect.ValueOf(jsonMap[key])
}
func merge(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse, target interface{}) {
var plan types.Object
var state types.Object
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(state.As(ctx, target, basetypes.ObjectAsOptions{
UnhandledNullAsEmpty: true,
UnhandledUnknownAsEmpty: true,
})...)
if resp.Diagnostics.HasError() {
return
}
// we need a tftypes.Value for this Object to be able to use it with
// our reflection code
obj := types.ObjectType{AttrTypes: plan.AttributeTypes(ctx)}
val, err := plan.ToTerraformValue(ctx)
if err != nil {
resp.Diagnostics.Append(diag.NewErrorDiagnostic("Object Conversion Error", "An unexpected error was encountered trying to convert object. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error()))
return
}
resp.Diagnostics.Append(tfReflect.Into(ctx, obj, val, target, tfReflect.Options{
UnhandledNullAsEmpty: true,
UnhandledUnknownAsEmpty: true,
}, path.Empty())...)
}