-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathparameter.go
100 lines (86 loc) · 2.74 KB
/
parameter.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
package tpfprovider
import (
"context"
"encoding/json"
"math/big"
"os"
"strings"
"github.com/coder/terraform-provider-coder/v2/provider"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type parameterDataSourceModel struct {
Name types.String `tfsdk:"name"`
Type types.Dynamic `tfsdk:"type"`
Value types.Dynamic `tfsdk:"value"`
}
type parameterDataSource struct{}
func NewParameterDataSource() datasource.DataSource {
return ¶meterDataSource{}
}
func (m *parameterDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "coder_parameter"
}
func (m *parameterDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Required: true,
},
"type": schema.DynamicAttribute{
Required: true,
},
"value": schema.DynamicAttribute{
Computed: true,
},
},
}
}
func (m *parameterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data parameterDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
if data.Name.IsNull() {
resp.Diagnostics.AddError("name is required", "name")
return
}
ds := data.Name.ValueString()
parameterEnv := provider.ParameterEnvironmentVariable(ds)
rawValue, ok := os.LookupEnv(parameterEnv)
if !ok {
resp.Diagnostics.AddError("parameter not found", "name")
return
}
switch data.Type.UnderlyingValue().(type) {
case types.String:
data.Value = types.DynamicValue(types.StringValue(rawValue))
case types.Number:
// convert the raw value to a number
var floatVal float64
if err := json.NewDecoder(strings.NewReader(rawValue)).Decode(&floatVal); err != nil {
resp.Diagnostics.AddError("failed to parse value as number", "value")
return
}
data.Value = types.DynamicValue(types.NumberValue(big.NewFloat(floatVal)))
case types.Bool:
// convert the raw value to a bool
var boolVal bool
if err := json.NewDecoder(strings.NewReader(rawValue)).Decode(&boolVal); err != nil {
resp.Diagnostics.AddError("failed to parse value as bool", "value")
return
}
data.Value = types.DynamicValue(types.BoolValue(boolVal))
case types.List:
// TODO: handle list
resp.Diagnostics.AddError("TODO: list type not supported", "type")
return
case types.Map:
// TODO: handle map
resp.Diagnostics.AddError("TODO: map type not supported", "type")
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}