/
server_readdatasource.go
80 lines (65 loc) · 2.28 KB
/
server_readdatasource.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
package fwserver
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)
// ReadDataSourceRequest is the framework server request for the
// ReadDataSource RPC.
type ReadDataSourceRequest struct {
Config *tfsdk.Config
DataSourceSchema fwschema.Schema
DataSource datasource.DataSource
ProviderMeta *tfsdk.Config
}
// ReadDataSourceResponse is the framework server response for the
// ReadDataSource RPC.
type ReadDataSourceResponse struct {
Diagnostics diag.Diagnostics
State *tfsdk.State
}
// ReadDataSource implements the framework server ReadDataSource RPC.
func (s *Server) ReadDataSource(ctx context.Context, req *ReadDataSourceRequest, resp *ReadDataSourceResponse) {
if req == nil {
return
}
if _, ok := req.DataSource.(datasource.DataSourceWithConfigure); ok {
logging.FrameworkTrace(ctx, "DataSource implements DataSourceWithConfigure")
configureReq := datasource.ConfigureRequest{
ProviderData: s.DataSourceConfigureData,
}
configureResp := datasource.ConfigureResponse{}
logging.FrameworkDebug(ctx, "Calling provider defined DataSource Configure")
req.DataSource.(datasource.DataSourceWithConfigure).Configure(ctx, configureReq, &configureResp)
logging.FrameworkDebug(ctx, "Called provider defined DataSource Configure")
resp.Diagnostics.Append(configureResp.Diagnostics...)
if resp.Diagnostics.HasError() {
return
}
}
readReq := datasource.ReadRequest{
Config: tfsdk.Config{
Schema: req.DataSourceSchema,
},
}
readResp := datasource.ReadResponse{
State: tfsdk.State{
Schema: req.DataSourceSchema,
},
}
if req.Config != nil {
readReq.Config = *req.Config
readResp.State.Raw = req.Config.Raw.Copy()
}
if req.ProviderMeta != nil {
readReq.ProviderMeta = *req.ProviderMeta
}
logging.FrameworkDebug(ctx, "Calling provider defined DataSource Read")
req.DataSource.Read(ctx, readReq, &readResp)
logging.FrameworkDebug(ctx, "Called provider defined DataSource Read")
resp.Diagnostics = readResp.Diagnostics
resp.State = &readResp.State
}