-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
Copy pathinput_variable.go
115 lines (102 loc) · 3.46 KB
/
input_variable.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
109
110
111
112
113
114
115
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package stackconfig
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/terraform/internal/stacks/stackconfig/typeexpr"
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/zclconf/go-cty/cty"
)
// InputVariable is a declaration of an input variable within a stack
// configuration. Callers must provide the values for these variables.
type InputVariable struct {
Name string
Type TypeConstraint
DefaultValue cty.Value
Description string
Sensitive bool
Ephemeral bool
DeclRange tfdiags.SourceRange
}
// TypeConstraint represents all of the type constraint information for either
// an input variable or an output value.
//
// After initial decoding only Expression is populated, and it has not yet been
// analyzed at all so is not even guaranteed to be a valid type constraint
// expression.
//
// For configurations loaded through the main entry point [LoadConfigDir],
// Constraint is populated with the result of decoding Expression as a type
// constraint only if the expression is a valid type constraint expression.
// When loading through shallower entry points such as [DecodeFileBody],
// Constraint is not populated.
//
// Defaults is populated only if Constraint is, and if not nil represents any
// default values from the type constraint expression.
type TypeConstraint struct {
Expression hcl.Expression
Constraint cty.Type
Defaults *typeexpr.Defaults
}
func decodeInputVariableBlock(block *hcl.Block) (*InputVariable, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
ret := &InputVariable{
Name: block.Labels[0],
DeclRange: tfdiags.SourceRangeFromHCL(block.DefRange),
}
if !hclsyntax.ValidIdentifier(ret.Name) {
diags = diags.Append(invalidNameDiagnostic(
"Invalid name for input variable",
block.LabelRanges[0],
))
return nil, diags
}
content, hclDiags := block.Body.Content(inputVariableBlockSchema)
diags = diags.Append(hclDiags)
if attr, ok := content.Attributes["type"]; ok {
ret.Type.Expression = attr.Expr
}
if attr, ok := content.Attributes["default"]; ok {
val, hclDiags := attr.Expr.Value(nil)
diags = diags.Append(hclDiags)
if val == cty.NilVal {
val = cty.DynamicVal
}
ret.DefaultValue = val
}
if attr, ok := content.Attributes["description"]; ok {
hclDiags := gohcl.DecodeExpression(attr.Expr, nil, &ret.Description)
diags = diags.Append(hclDiags)
}
if attr, ok := content.Attributes["sensitive"]; ok {
hclDiags := gohcl.DecodeExpression(attr.Expr, nil, &ret.Sensitive)
diags = diags.Append(hclDiags)
}
if attr, ok := content.Attributes["ephemeral"]; ok {
hclDiags := gohcl.DecodeExpression(attr.Expr, nil, &ret.Ephemeral)
diags = diags.Append(hclDiags)
}
for _, block := range content.Blocks {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Custom variable validation not yet supported",
Detail: "Input variables for a stack configuration do not yet support custom variable validation.",
Subject: block.DefRange.Ptr(),
})
}
return ret, diags
}
var inputVariableBlockSchema = &hcl.BodySchema{
Attributes: []hcl.AttributeSchema{
{Name: "type", Required: true},
{Name: "default", Required: false},
{Name: "description", Required: false},
{Name: "sensitive", Required: false},
{Name: "ephemeral", Required: false},
},
Blocks: []hcl.BlockHeaderSchema{
{Type: "validation"},
},
}