/
evaluated_string_variable_data_source.go
99 lines (85 loc) · 3.25 KB
/
evaluated_string_variable_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
package provider
import (
"context"
"fmt"
dvc_server "github.com/devcyclehq/go-server-sdk/v2"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type evaluatedStringVariableDataSourceType struct{}
func (t evaluatedStringVariableDataSourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Evaluated Variable data source. Each instance of this data source represents a single evaluated variable, under a single userdata context.",
Attributes: map[string]tfsdk.Attribute{
"user": userDataSchema(),
"value": {
MarkdownDescription: "Value of the Variable",
Computed: true,
Type: types.StringType,
},
"default_value": {
MarkdownDescription: "Default value of the Variable. Used as a fallback in case there is no variation value set.",
Required: true,
Type: types.StringType,
},
"key": {
Required: true,
MarkdownDescription: "Variable ID or key. Recommended to use the key when not managing an entire project in Terraform.",
PlanModifiers: tfsdk.AttributePlanModifiers{
tfsdk.RequiresReplace(),
},
Type: types.StringType,
},
"id": {
Computed: true,
Type: types.StringType,
},
},
}, nil
}
func (t evaluatedStringVariableDataSourceType) NewDataSource(ctx context.Context, in tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
provider, diags := convertProviderType(in)
return evaluatedStringVariableDataSource{
provider: provider,
}, diags
}
type evaluatedStringVariableDataSourceData struct {
Key types.String `tfsdk:"key"`
Value types.String `tfsdk:"value"`
User evaluatedVariableDataSourceDataUser `tfsdk:"user"`
DefaultValue types.String `tfsdk:"default_value"`
Id types.String `tfsdk:"id"`
}
type evaluatedStringVariableDataSource struct {
provider provider
}
func (d evaluatedStringVariableDataSource) Read(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
var data evaluatedStringVariableDataSourceData
if !d.provider.configured {
resp.Diagnostics.AddError(
"Provider not configured",
"The provider hasn't been configured before apply, likely because it depends on an unknown value from another resource. Authentication is required to be configured.",
)
return
}
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
userData := dvc_server.DVCUser{
UserId: "" + data.User.Id.Value,
}
variable, err := d.provider.ServerClient.Variable(userData, data.Key.Value, data.DefaultValue.Value)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read Variable, got error: %s", err))
return
}
data.Key = types.String{Value: variable.Key}
data.Id = data.Key
data.Value = types.String{Value: variable.Value.(string)}
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}