-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source.go
72 lines (63 loc) · 1.58 KB
/
data_source.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
package plugin
import (
"context"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/zclconf/go-cty/cty"
)
type DataSources map[string]*DataSource
func (ds DataSources) Validate() hcl.Diagnostics {
if ds == nil {
return nil
}
var diags hcl.Diagnostics
for name, source := range ds {
if source == nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Incomplete DataSourceSchema",
Detail: "DataSource '" + name + "' not loaded",
})
} else {
diags = append(diags, source.Validate()...)
}
}
return diags
}
type DataSource struct {
DataFunc RetrieveDataFunc
Args hcldec.Spec
Config hcldec.Spec
}
func (ds *DataSource) Validate() hcl.Diagnostics {
var diags hcl.Diagnostics
if ds.DataFunc == nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Incomplete DataSourceSchema",
Detail: "DataSource function not loaded",
})
}
return diags
}
func (ds *DataSource) Execute(ctx context.Context, params *RetrieveDataParams) (Data, hcl.Diagnostics) {
if ds == nil {
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Missing DataSource schema",
}}
}
if ds.DataFunc == nil {
return nil, hcl.Diagnostics{{
Severity: hcl.DiagError,
Summary: "Incomplete DataSourceSchema",
Detail: "datasource function not loaded",
}}
}
return ds.DataFunc(ctx, params)
}
type RetrieveDataParams struct {
Config cty.Value
Args cty.Value
}
type RetrieveDataFunc func(ctx context.Context, params *RetrieveDataParams) (Data, hcl.Diagnostics)