/
managed_resource_type.go
45 lines (38 loc) · 1.25 KB
/
managed_resource_type.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
package protocol5
import (
"context"
"github.com/apparentlymart/terraform-provider/internal/tfplugin5"
"github.com/apparentlymart/terraform-provider/tfprovider/internal/common"
)
type ManagedResourceType struct {
client tfplugin5.ProviderClient
typeName string
schema *common.ManagedResourceTypeSchema
}
func (rt *ManagedResourceType) Read(ctx context.Context, req common.ManagedResourceReadRequest) (common.ManagedResourceReadResponse, common.Diagnostics) {
resp := common.ManagedResourceReadResponse{}
dv, diags := encodeDynamicValue(req.PreviousValue, rt.schema.Content)
if diags.HasErrors() {
return resp, diags
}
rawResp, err := rt.client.ReadResource(ctx, &tfplugin5.ReadResource_Request{
TypeName: rt.typeName,
CurrentState: dv,
Private: req.OpaquePrivate,
})
diags = append(diags, common.RPCErrorDiagnostics(err)...)
if err != nil {
return resp, diags
}
diags = append(diags, decodeDiagnostics(rawResp.Diagnostics)...)
if raw := rawResp.NewState; raw != nil {
v, moreDiags := decodeDynamicValue(raw, rt.schema.Content)
resp.RefreshedValue = v
diags = append(diags, moreDiags...)
}
resp.OpaquePrivate = rawResp.Private
return resp, diags
}
func (rt *ManagedResourceType) Sealed() common.Sealed {
return common.Sealed{}
}