-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatten_map_data_source.go
108 lines (92 loc) · 3.53 KB
/
flatten_map_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
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
108
package mapfunc
import (
"context"
"fmt"
"golang.org/x/exp/maps"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/mschuchard/terraform-provider-stdlib/internal"
)
// ensure the implementation satisfies the expected interfaces
var _ datasource.DataSource = &flattenMapDataSource{}
// helper pseudo-constructor to simplify provider server and testing implementation
func NewFlattenMapDataSource() datasource.DataSource {
return &flattenMapDataSource{}
}
// data source implementation
type flattenMapDataSource struct{}
// maps the data source schema data to the model
type flattenMapDataSourceModel struct {
ID types.String `tfsdk:"id"`
Param types.List `tfsdk:"param"`
Result types.Map `tfsdk:"result"`
}
// data source metadata
func (_ *flattenMapDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_flatten_map"
}
// define the provider-level schema for configuration data
func (_ *flattenMapDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": util.IDStringAttribute(),
"param": schema.ListAttribute{
Description: "Input list of maps to flatten.",
ElementType: types.MapType{
ElemType: types.StringType,
},
Required: true,
Validators: []validator.List{
listvalidator.SizeAtLeast(1),
},
},
"result": schema.MapAttribute{
Computed: true,
Description: "Function result storing the flattened map.",
ElementType: types.StringType,
},
},
MarkdownDescription: "Return the flattened map of an input list of maps parameter. Note that if a key is repeated then the last entry will overwrite any previous entries in the result.",
}
}
// read executes the actual function
func (_ *flattenMapDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
// determine input values
var state flattenMapDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
// initialize input list of maps, nested maps, and output map
var inputList []types.Map
resp.Diagnostics.Append(state.Param.ElementsAs(ctx, &inputList, false)...)
if resp.Diagnostics.HasError() {
return
}
var nestedMap map[string]string
outputMap := map[string]string{}
// iterate through list of maps, convert each map, and merge each map into new map
for _, nestedTFMap := range inputList {
resp.Diagnostics.Append(nestedTFMap.ElementsAs(ctx, &nestedMap, false)...)
if resp.Diagnostics.HasError() {
return
}
maps.Copy(outputMap, nestedMap)
}
// store number of entries of output map as id
state.ID = types.StringValue(fmt.Sprint(len(outputMap)))
var mapConvertDiags diag.Diagnostics
state.Result, mapConvertDiags = types.MapValueFrom(ctx, types.StringType, outputMap)
resp.Diagnostics.Append(mapConvertDiags...)
// set state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "Determined flattened map", map[string]any{"success": true})
}