Skip to content

Commit 2060cc7

Browse files
authored
schema: Replace Expr with Constraint (#174)
* internal/schema: Replace Expr with Constraint * schema: Replace Expr w/ Constraint * schema: Update all short tests * schema: Update remaining (merge) tests * schema: Use SkipLiteralComplexTypes for AnyExpression in OneOf context * deps: Pin hcl-lang to 63aa628 * deps: Pin hcl-lang to 7857971 * schema: replace module var constraint LiteralType w/ AnyExpression
1 parent f2ec0d9 commit 2060cc7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1525
-3207
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/hashicorp/go-cleanhttp v0.5.2
88
github.com/hashicorp/go-version v1.6.0
99
github.com/hashicorp/hc-install v0.5.1
10-
github.com/hashicorp/hcl-lang v0.0.0-20230215175403-7a4806322d6e
10+
github.com/hashicorp/hcl-lang v0.0.0-20230406172145-78579711860f
1111
github.com/hashicorp/hcl/v2 v2.16.2
1212
github.com/hashicorp/terraform-exec v0.18.1
1313
github.com/hashicorp/terraform-json v0.16.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mO
2727
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
2828
github.com/hashicorp/hc-install v0.5.1 h1:eCqToNCob7m2R8kM8Gr7XcVmcRSz9ppCFSVZbMh0X+0=
2929
github.com/hashicorp/hc-install v0.5.1/go.mod h1:iDPCnzKo+SzToOh25R8OWpLdhhy7yBfJX3PmVWiYhrM=
30-
github.com/hashicorp/hcl-lang v0.0.0-20230215175403-7a4806322d6e h1:nieP1CFS9p/eQUmh6gRVkVgtQPOCCR/+XObCcveoAYU=
31-
github.com/hashicorp/hcl-lang v0.0.0-20230215175403-7a4806322d6e/go.mod h1:alGNoXhoyCKUDHKM6iK88G5bgPym9agemDf6AEB26j0=
30+
github.com/hashicorp/hcl-lang v0.0.0-20230406172145-78579711860f h1:MTbd7Mi+g1X/9f9j7p4GqFZFJvFtiwPxPdIqc3xnLrQ=
31+
github.com/hashicorp/hcl-lang v0.0.0-20230406172145-78579711860f/go.mod h1:SalMwbKCDSR7kF34FdPAikCsQjPVgYvwyiROrMWbyEw=
3232
github.com/hashicorp/hcl/v2 v2.16.2 h1:mpkHZh/Tv+xet3sy3F9Ld4FyI2tUpWe9x3XtPx9f1a0=
3333
github.com/hashicorp/hcl/v2 v2.16.2/go.mod h1:JRmR89jycNkrrqnMmvPDMd56n1rQJ2Q6KocSLCMCXng=
3434
github.com/hashicorp/terraform-exec v0.18.1 h1:LAbfDvNQU1l0NOQlTuudjczVhHj061fNX5H8XZxHlH4=

internal/schema/0.12/connection_block.go

Lines changed: 27 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ func connectionBlock(v *version.Version) *schema.BlockSchema {
2323
HoverURL: "https://www.terraform.io/docs/language/resources/provisioners/connection.html",
2424
Attributes: map[string]*schema.AttributeSchema{
2525
"type": {
26-
Expr: schema.ExprConstraints{
27-
schema.LegacyLiteralValue{
28-
Val: cty.StringVal("ssh"),
26+
Constraint: schema.OneOf{
27+
schema.LiteralValue{
28+
Value: cty.StringVal("ssh"),
2929
Description: lang.Markdown("Use SSH to connect and provision the instance"),
3030
},
31-
schema.LegacyLiteralValue{
32-
Val: cty.StringVal("winrm"),
31+
schema.LiteralValue{
32+
Value: cty.StringVal("winrm"),
3333
Description: lang.Markdown("Use WinRM to connect and provision the instance"),
3434
},
3535
},
@@ -39,56 +39,38 @@ func connectionBlock(v *version.Version) *schema.BlockSchema {
3939
Description: lang.Markdown("Connection type to use - `ssh` (default) or `winrm`"),
4040
},
4141
"user": {
42-
Expr: schema.ExprConstraints{
43-
schema.TraversalExpr{OfType: cty.String},
44-
schema.LiteralTypeExpr{Type: cty.String},
45-
},
42+
Constraint: schema.AnyExpression{OfType: cty.String},
4643
IsOptional: true,
4744
Description: lang.Markdown("The user that we should use for the connection. " +
4845
"Defaults to `root` when using type `ssh` and defaults to `Administrator` " +
4946
"when using type `winrm`."),
5047
},
5148
"password": {
52-
Expr: schema.ExprConstraints{
53-
schema.TraversalExpr{OfType: cty.String},
54-
schema.LiteralTypeExpr{Type: cty.String},
55-
},
49+
Constraint: schema.AnyExpression{OfType: cty.String},
5650
IsOptional: true,
5751
Description: lang.Markdown("The password we should use for the connection. " +
5852
"In some cases this is specified by the provider."),
5953
},
6054
"host": {
61-
Expr: schema.ExprConstraints{
62-
schema.TraversalExpr{OfType: cty.String},
63-
schema.LiteralTypeExpr{Type: cty.String},
64-
},
55+
Constraint: schema.AnyExpression{OfType: cty.String},
6556
IsRequired: true,
6657
Description: lang.Markdown("The address of the resource to connect to"),
6758
},
6859
"port": {
69-
Expr: schema.ExprConstraints{
70-
schema.TraversalExpr{OfType: cty.String},
71-
schema.LiteralTypeExpr{Type: cty.String},
72-
},
60+
Constraint: schema.AnyExpression{OfType: cty.String},
7361
IsOptional: true,
7462
Description: lang.Markdown("The port to connect to. Defaults to `22` " +
7563
"when using type `ssh` and defaults to `5985` when using type `winrm`."),
7664
},
7765
"timeout": {
78-
Expr: schema.ExprConstraints{
79-
schema.TraversalExpr{OfType: cty.String},
80-
schema.LiteralTypeExpr{Type: cty.String},
81-
},
66+
Constraint: schema.AnyExpression{OfType: cty.String},
8267
IsOptional: true,
8368
Description: lang.Markdown("The timeout to wait for the connection to become " +
8469
"available. Should be provided as a string like `30s` or `5m`. " +
8570
"Defaults to 5 minutes."),
8671
},
8772
"script_path": {
88-
Expr: schema.ExprConstraints{
89-
schema.TraversalExpr{OfType: cty.String},
90-
schema.LiteralTypeExpr{Type: cty.String},
91-
},
73+
Constraint: schema.AnyExpression{OfType: cty.String},
9274
IsOptional: true,
9375
Description: lang.Markdown("The path used to copy scripts meant for remote execution."),
9476
},
@@ -113,99 +95,66 @@ func ConnectionDependentBodies(v *version.Version) map[schema.SchemaKey]*schema.
11395
m[ssh] = &schema.BodySchema{
11496
Attributes: map[string]*schema.AttributeSchema{
11597
"private_key": {
116-
Expr: schema.ExprConstraints{
117-
schema.TraversalExpr{OfType: cty.String},
118-
schema.LiteralTypeExpr{Type: cty.String},
119-
},
98+
Constraint: schema.AnyExpression{OfType: cty.String},
12099
IsOptional: true,
121100
Description: lang.Markdown("The contents of an SSH key to use for the connection. " +
122101
"This takes preference over the password if provided."),
123102
},
124103
"certificate": {
125-
Expr: schema.ExprConstraints{
126-
schema.TraversalExpr{OfType: cty.String},
127-
schema.LiteralTypeExpr{Type: cty.String},
128-
},
104+
Constraint: schema.AnyExpression{OfType: cty.String},
129105
IsOptional: true,
130106
Description: lang.Markdown("The contents of a signed CA Certificate. The argument " +
131107
"must be used in conjunction with a `private_key`."),
132108
},
133109
"agent": {
134-
Expr: schema.ExprConstraints{
135-
schema.TraversalExpr{OfType: cty.Bool},
136-
schema.LiteralTypeExpr{Type: cty.Bool},
137-
},
110+
Constraint: schema.AnyExpression{OfType: cty.Bool},
138111
IsOptional: true,
139112
Description: lang.Markdown("Set to `false` to disable using `ssh-agent` to authenticate. " +
140113
"On Windows the only supported SSH authentication agent is " +
141114
"[Pageant](http://the.earth.li/~sgtatham/putty/0.66/htmldoc/Chapter9.html#pageant)."),
142115
},
143116
"agent_identity": {
144-
Expr: schema.ExprConstraints{
145-
schema.TraversalExpr{OfType: cty.String},
146-
schema.LiteralTypeExpr{Type: cty.String},
147-
},
117+
Constraint: schema.AnyExpression{OfType: cty.String},
148118
IsOptional: true,
149119
Description: lang.Markdown("The preferred identity from the ssh agent for authentication."),
150120
},
151121
"host_key": {
152-
Expr: schema.ExprConstraints{
153-
schema.TraversalExpr{OfType: cty.String},
154-
schema.LiteralTypeExpr{Type: cty.String},
155-
},
122+
Constraint: schema.AnyExpression{OfType: cty.String},
156123
IsOptional: true,
157124
Description: lang.Markdown("The public key from the remote host or the signing CA, used to verify the connection."),
158125
},
159126
"bastion_host": {
160-
Expr: schema.ExprConstraints{
161-
schema.TraversalExpr{OfType: cty.String},
162-
schema.LiteralTypeExpr{Type: cty.String},
163-
},
127+
Constraint: schema.AnyExpression{OfType: cty.String},
164128
IsOptional: true,
165129
Description: lang.Markdown("Setting this enables the bastion host connection. " +
166130
"This host will be connected to first, and then the `host` connection will be made from there."),
167131
},
168132
"bastion_host_key": {
169-
Expr: schema.ExprConstraints{
170-
schema.TraversalExpr{OfType: cty.String},
171-
schema.LiteralTypeExpr{Type: cty.String},
172-
},
133+
Constraint: schema.AnyExpression{OfType: cty.String},
173134
IsOptional: true,
174135
Description: lang.Markdown("The public key from the remote host or the signing CA, " +
175136
"used to verify the host connection."),
176137
},
177138
"bastion_port": {
178-
Expr: schema.ExprConstraints{
179-
schema.TraversalExpr{OfType: cty.Number},
180-
schema.LiteralTypeExpr{Type: cty.Number},
181-
},
139+
Constraint: schema.AnyExpression{OfType: cty.Number},
182140
IsOptional: true,
183141
Description: lang.Markdown("The port to use connect to the bastion host. " +
184142
"Defaults to the value of the `port` field."),
185143
},
186144
"bastion_user": {
187-
Expr: schema.ExprConstraints{
188-
schema.TraversalExpr{OfType: cty.String},
189-
schema.LiteralTypeExpr{Type: cty.String},
190-
},
145+
Constraint: schema.AnyExpression{OfType: cty.String},
191146
IsOptional: true,
192147
Description: lang.Markdown("The user for the connection to the bastion host. " +
193148
"Defaults to the value of the `user` field."),
194149
},
195150
"bastion_password": {
196-
Expr: schema.ExprConstraints{
197-
schema.TraversalExpr{OfType: cty.String},
198-
schema.LiteralTypeExpr{Type: cty.String},
199-
},
151+
Constraint: schema.AnyExpression{OfType: cty.String},
200152
IsOptional: true,
201153
Description: lang.Markdown("The password we should use for the bastion host. " +
202154
"Defaults to the value of the `password` field."),
203155
},
204156
"bastion_private_key": {
205-
Expr: schema.ExprConstraints{
206-
schema.TraversalExpr{OfType: cty.String},
207-
schema.LiteralTypeExpr{Type: cty.String},
208-
},
157+
Constraint: schema.AnyExpression{OfType: cty.String},
209158
IsOptional: true,
210159
Description: lang.Markdown("The contents of an SSH key file to use for the bastion host. " +
211160
"Defaults to the value of the `private_key` field."),
@@ -216,10 +165,7 @@ func ConnectionDependentBodies(v *version.Version) map[schema.SchemaKey]*schema.
216165
// See https://github.com/hashicorp/terraform/commit/3031aca9
217166
if v.GreaterThanOrEqual(v0_12_7) {
218167
m[ssh].Attributes["bastion_certificate"] = &schema.AttributeSchema{
219-
Expr: schema.ExprConstraints{
220-
schema.TraversalExpr{OfType: cty.String},
221-
schema.LiteralTypeExpr{Type: cty.String},
222-
},
168+
Constraint: schema.AnyExpression{OfType: cty.String},
223169
IsOptional: true,
224170
Description: lang.Markdown("The contents of a signed CA Certificate. The `certificate` argument " +
225171
"must be used in conjunction with a `bastion_private_key`."),
@@ -237,37 +183,25 @@ func ConnectionDependentBodies(v *version.Version) map[schema.SchemaKey]*schema.
237183
m[winRm] = &schema.BodySchema{
238184
Attributes: map[string]*schema.AttributeSchema{
239185
"https": {
240-
Expr: schema.ExprConstraints{
241-
schema.TraversalExpr{OfType: cty.Bool},
242-
schema.LiteralTypeExpr{Type: cty.Bool},
243-
},
186+
Constraint: schema.AnyExpression{OfType: cty.Bool},
244187
IsOptional: true,
245188
Description: lang.Markdown("Set to `true` to connect using HTTPS instead of HTTP."),
246189
},
247190
"insecure": {
248-
Expr: schema.ExprConstraints{
249-
schema.TraversalExpr{OfType: cty.Bool},
250-
schema.LiteralTypeExpr{Type: cty.Bool},
251-
},
191+
Constraint: schema.AnyExpression{OfType: cty.Bool},
252192
IsOptional: true,
253193
Description: lang.Markdown("Set to `true` to not validate the HTTPS certificate chain."),
254194
},
255195
"use_ntlm": {
256-
Expr: schema.ExprConstraints{
257-
schema.TraversalExpr{OfType: cty.Bool},
258-
schema.LiteralTypeExpr{Type: cty.Bool},
259-
},
196+
Constraint: schema.AnyExpression{OfType: cty.Bool},
260197
IsOptional: true,
261198
Description: lang.Markdown("Set to `true` to use NTLM authentication, rather than default " +
262199
"(basic authentication), removing the requirement for basic authentication to be enabled " +
263200
"within the target guest. Read more about remote connection authentication at " +
264201
"[docs.microsoft.com](https://docs.microsoft.com/en-gb/windows/win32/winrm/authentication-for-remote-connections)."),
265202
},
266203
"cacert": {
267-
Expr: schema.ExprConstraints{
268-
schema.TraversalExpr{OfType: cty.String},
269-
schema.LiteralTypeExpr{Type: cty.String},
270-
},
204+
Constraint: schema.AnyExpression{OfType: cty.String},
271205
IsOptional: true,
272206
Description: lang.Markdown("The CA certificate to validate against."),
273207
},

internal/schema/0.12/data_block.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,18 @@ func datasourceBlockSchema(v *version.Version) *schema.BlockSchema {
5252
},
5353
Attributes: map[string]*schema.AttributeSchema{
5454
"provider": {
55-
Expr: schema.ExprConstraints{
56-
schema.TraversalExpr{OfScopeId: refscope.ProviderScope},
57-
},
55+
Constraint: schema.Reference{OfScopeId: refscope.ProviderScope},
5856
IsOptional: true,
5957
Description: lang.Markdown("Reference to a `provider` configuration block, e.g. `mycloud.west` or `mycloud`"),
6058
IsDepKey: true,
6159
SemanticTokenModifiers: lang.SemanticTokenModifiers{lang.TokenModifierDependent},
6260
},
6361
"depends_on": {
64-
Expr: schema.ExprConstraints{
65-
schema.SetExpr{
66-
Elem: schema.ExprConstraints{
67-
schema.TraversalExpr{OfScopeId: refscope.DataScope},
68-
schema.TraversalExpr{OfScopeId: refscope.ModuleScope},
69-
schema.TraversalExpr{OfScopeId: refscope.ResourceScope},
70-
},
62+
Constraint: schema.Set{
63+
Elem: schema.OneOf{
64+
schema.Reference{OfScopeId: refscope.DataScope},
65+
schema.Reference{OfScopeId: refscope.ModuleScope},
66+
schema.Reference{OfScopeId: refscope.ResourceScope},
7167
},
7268
},
7369
IsOptional: true,

internal/schema/0.12/locals_block.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ func localsBlockSchema() *schema.BlockSchema {
2626
ScopeId: refscope.LocalScope,
2727
AsExprType: true,
2828
},
29-
Expr: schema.ExprConstraints{
30-
schema.TraversalExpr{OfType: cty.DynamicPseudoType},
31-
schema.LiteralTypeExpr{Type: cty.DynamicPseudoType},
32-
},
29+
Constraint: schema.AnyExpression{OfType: cty.DynamicPseudoType},
3330
},
3431
},
3532
}

internal/schema/0.12/module_block.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func moduleBlockSchema() *schema.BlockSchema {
3434
Body: &schema.BodySchema{
3535
Attributes: map[string]*schema.AttributeSchema{
3636
"source": {
37-
Expr: schema.LiteralTypeOnly(cty.String),
37+
Constraint: schema.LiteralType{Type: cty.String},
3838
Description: lang.Markdown("Source where to load the module from, " +
3939
"a local directory (e.g. `./module`) or a remote address - e.g. " +
4040
"`hashicorp/consul/aws` (Terraform Registry address) or " +
@@ -52,7 +52,7 @@ func moduleBlockSchema() *schema.BlockSchema {
5252
},
5353
},
5454
"version": {
55-
Expr: schema.LiteralTypeOnly(cty.String),
55+
Constraint: schema.LiteralType{Type: cty.String},
5656
IsOptional: true,
5757
Description: lang.Markdown("Constraint to set the version of the module, e.g. `~> 1.0`." +
5858
" Only applicable to modules in a module registry."),
@@ -63,13 +63,9 @@ func moduleBlockSchema() *schema.BlockSchema {
6363
},
6464
},
6565
"providers": {
66-
Expr: schema.ExprConstraints{
67-
schema.MapExpr{
68-
Name: "map of provider references",
69-
Elem: schema.ExprConstraints{
70-
schema.TraversalExpr{OfScopeId: refscope.ProviderScope},
71-
},
72-
},
66+
Constraint: schema.Map{
67+
Name: "map of provider references",
68+
Elem: schema.Reference{OfScopeId: refscope.ProviderScope},
7369
},
7470
IsOptional: true,
7571
Description: lang.Markdown("Explicit mapping of providers which the module uses"),

internal/schema/0.12/output_block.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,26 @@ func outputBlockSchema() *schema.BlockSchema {
3434
Body: &schema.BodySchema{
3535
Attributes: map[string]*schema.AttributeSchema{
3636
"description": {
37-
Expr: schema.LiteralTypeOnly(cty.String),
37+
Constraint: schema.LiteralType{Type: cty.String},
3838
IsOptional: true,
3939
Description: lang.PlainText("Human-readable description of the output (for documentation and UI)"),
4040
},
4141
"value": {
42-
Expr: schema.ExprConstraints{
43-
schema.TraversalExpr{OfType: cty.DynamicPseudoType},
44-
schema.LiteralTypeExpr{Type: cty.DynamicPseudoType},
45-
},
42+
Constraint: schema.AnyExpression{OfType: cty.DynamicPseudoType},
4643
IsRequired: true,
4744
Description: lang.PlainText("Value, typically a reference to an attribute of a resource or a data source"),
4845
},
4946
"sensitive": {
50-
Expr: schema.LiteralTypeOnly(cty.Bool),
47+
Constraint: schema.LiteralType{Type: cty.Bool},
5148
IsOptional: true,
5249
Description: lang.PlainText("Whether the output contains sensitive material and should be hidden in the UI"),
5350
},
5451
"depends_on": {
55-
Expr: schema.ExprConstraints{
56-
schema.SetExpr{
57-
Elem: schema.ExprConstraints{
58-
schema.TraversalExpr{OfScopeId: refscope.DataScope},
59-
schema.TraversalExpr{OfScopeId: refscope.ModuleScope},
60-
schema.TraversalExpr{OfScopeId: refscope.ResourceScope},
61-
},
52+
Constraint: schema.Set{
53+
Elem: schema.OneOf{
54+
schema.Reference{OfScopeId: refscope.DataScope},
55+
schema.Reference{OfScopeId: refscope.ModuleScope},
56+
schema.Reference{OfScopeId: refscope.ResourceScope},
6257
},
6358
},
6459
IsOptional: true,

internal/schema/0.12/provider_block.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ func providerBlockSchema(v *version.Version) *schema.BlockSchema {
4040
},
4141
Attributes: map[string]*schema.AttributeSchema{
4242
"alias": {
43-
Expr: schema.LiteralTypeOnly(cty.String),
43+
Constraint: schema.LiteralType{Type: cty.String},
4444
IsOptional: true,
4545
Description: lang.Markdown("Alias for using the same provider with different configurations for different resources, e.g. `eu-west`"),
4646
},
4747
"version": {
48-
Expr: schema.LiteralTypeOnly(cty.String),
48+
Constraint: schema.LiteralType{Type: cty.String},
4949
IsOptional: true,
5050
Description: lang.Markdown("Specifies a version constraint for the provider, e.g. `~> 1.0`"),
5151
},

0 commit comments

Comments
 (0)