/
data_source_hiera5_array.go
107 lines (87 loc) · 2.87 KB
/
data_source_hiera5_array.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package hiera5
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
)
var _ datasource.DataSource = &Hiera5ArrayDataSource{}
type Hiera5ArrayDataSource struct {
client hiera5
}
type Hiera5ArrayDataSourceModel struct {
ID types.String `tfsdk:"id"`
Key types.String `tfsdk:"key"`
Value types.List `tfsdk:"value"`
Default types.List `tfsdk:"default"`
Scope types.Map `tfsdk:"scope"`
}
func NewArrayDataSource() datasource.DataSource {
return &Hiera5ArrayDataSource{}
}
func (d *Hiera5ArrayDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "hiera5_array"
}
func (d *Hiera5ArrayDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(hiera5)
}
func (d *Hiera5ArrayDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": idAttribute,
"key": keyAttribute,
"value": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: valueDescription,
},
"default": schema.ListAttribute{
ElementType: types.StringType,
Optional: true,
Description: defaultDescription,
},
"scope": scopeOverrideAttribute,
},
}
}
func (d *Hiera5ArrayDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data Hiera5ArrayDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
scopeOverride, diag := processScopeOverrideAttribute(ctx, data.Scope)
resp.Diagnostics.Append(diag...)
if resp.Diagnostics.HasError() {
return
}
rawList, err := d.client.array(ctx, data.Key.String(), WithScopeOverride(scopeOverride))
if err != nil && data.Default.IsNull() {
resp.Diagnostics.AddAttributeError(path.Root("key"),
"key not in data",
"When key is unavailable and a default value is not set an error is raised")
}
if resp.Diagnostics.HasError() {
return
}
data.ID = data.Key
if err != nil {
data.Value = data.Default
} else {
listValue := []attr.Value{}
for _, v := range rawList {
listValue = append(listValue, types.StringValue(v.(string)))
}
result, diag := types.ListValue(types.StringType, listValue)
resp.Diagnostics.Append(diag...)
if resp.Diagnostics.HasError() {
return
}
data.Value = result
}
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}