1
- // Copyright (c) HashiCorp, Inc.
2
- // SPDX-License-Identifier: BUSL-1.1
3
-
4
1
package configs
5
2
6
3
import (
@@ -18,86 +15,36 @@ import (
18
15
type QueryFile struct {
19
16
// Providers defines a set of providers that are available to the list blocks
20
17
// within this query file.
21
- Providers map [string ]* Provider
22
-
18
+ Providers map [string ]* Provider
23
19
ProviderConfigs []* Provider
24
20
25
- Locals []* Local
26
-
21
+ Locals []* Local
27
22
Variables []* Variable
28
23
29
- // Lists defines the list of List blocks within the query file.
30
- Lists []* List
24
+ // ListResources is a slice of List blocks within the query file.
25
+ ListResources []* Resource
31
26
32
27
VariablesDeclRange hcl.Range
33
28
}
34
29
35
- // List represents a single list block within a query file.
36
- //
37
- // Each list block represents a single Terraform command to be executed and a set
38
- // of validations to list after the command.
39
- type List struct {
40
- Type string
41
- Name string
42
-
43
- ProviderConfigRef * ProviderConfigRef
44
- Provider addrs.Provider
45
-
46
- // File is a reference to the parent QueryFile that contains this block.
47
- File * QueryFile
48
-
49
- // Config is the main configuration body for the list block.
50
- Config hcl.Body
51
-
52
- TypeDeclRange hcl.Range
53
- ConfigDeclRange hcl.Range
54
- DeclRange hcl.Range
55
- }
56
-
57
- func (list * List ) moduleUniqueKey () string {
58
- return list .Name
59
- }
60
-
61
- func (list * List ) Addr () addrs.List {
62
- return addrs.List {
63
- Type : list .Type ,
64
- Name : list .Name ,
65
- }
66
- }
67
-
68
- // ProviderConfigAddr returns the address for the provider configuration that
69
- // should be used for this list. This function returns a default provider
70
- // config addr if an explicit "provider" argument was not provided.
71
- func (list * List ) ProviderConfigAddr () addrs.LocalProviderConfig {
72
- if list .ProviderConfigRef == nil {
73
- // all lists must have a provider config ref
74
- panic ("ProviderConfigRef is nil" )
75
- }
76
-
77
- return addrs.LocalProviderConfig {
78
- LocalName : list .ProviderConfigRef .Name ,
79
- Alias : list .ProviderConfigRef .Alias ,
80
- }
81
- }
82
-
83
30
func loadQueryFile (body hcl.Body ) (* QueryFile , hcl.Diagnostics ) {
84
31
var diags hcl.Diagnostics
85
32
file := & QueryFile {
86
33
Providers : make (map [string ]* Provider ),
87
34
}
88
35
89
- content , contentDiags := body .Content (testQueryFileSchema )
36
+ content , contentDiags := body .Content (queryFileSchema )
90
37
diags = append (diags , contentDiags ... )
91
38
92
39
listBlockNames := make (map [string ]hcl.Range )
93
40
94
41
for _ , block := range content .Blocks {
95
42
switch block .Type {
96
43
case "list" :
97
- list , listDiags := decodeQueryListBlock (block , file )
44
+ list , listDiags := decodeQueryListBlock (block )
98
45
diags = append (diags , listDiags ... )
99
46
if ! listDiags .HasErrors () {
100
- file .Lists = append (file .Lists , list )
47
+ file .ListResources = append (file .ListResources , list )
101
48
}
102
49
103
50
if rng , exists := listBlockNames [list .Name ]; exists {
@@ -140,21 +87,19 @@ func loadQueryFile(body hcl.Body) (*QueryFile, hcl.Diagnostics) {
140
87
return file , diags
141
88
}
142
89
143
- func decodeQueryListBlock (block * hcl.Block , file * QueryFile ) (* List , hcl.Diagnostics ) {
90
+ func decodeQueryListBlock (block * hcl.Block ) (* Resource , hcl.Diagnostics ) {
144
91
var diags hcl.Diagnostics
145
92
146
- content , remain , contentDiags := block .Body .PartialContent (& hcl.BodySchema {
147
- Attributes : []hcl.AttributeSchema {{Name : "provider" }},
148
- })
93
+ content , remain , contentDiags := block .Body .PartialContent (QueryListResourceBlockSchema )
149
94
diags = append (diags , contentDiags ... )
150
95
151
- r := List {
152
- File : file ,
153
- Type : block .Labels [0 ],
154
- TypeDeclRange : block .LabelRanges [0 ],
155
- Name : block .Labels [1 ],
156
- DeclRange : block .DefRange ,
157
- Config : remain ,
96
+ r := Resource {
97
+ Mode : addrs . ListResourceMode ,
98
+ Type : block .Labels [0 ],
99
+ TypeRange : block .LabelRanges [0 ],
100
+ Name : block .Labels [1 ],
101
+ DeclRange : block .DefRange ,
102
+ Config : remain ,
158
103
}
159
104
160
105
if attr , exists := content .Attributes ["provider" ]; exists {
@@ -180,10 +125,37 @@ func decodeQueryListBlock(block *hcl.Block, file *QueryFile) (*List, hcl.Diagnos
180
125
Subject : r .DeclRange .Ptr (),
181
126
})
182
127
}
128
+
129
+ if attr , exists := content .Attributes ["count" ]; exists {
130
+ r .Count = attr .Expr
131
+ }
132
+
133
+ if attr , exists := content .Attributes ["for_each" ]; exists {
134
+ r .ForEach = attr .Expr
135
+ // Cannot have count and for_each on the same resource block
136
+ if r .Count != nil {
137
+ diags = append (diags , & hcl.Diagnostic {
138
+ Severity : hcl .DiagError ,
139
+ Summary : `Invalid combination of "count" and "for_each"` ,
140
+ Detail : `The "count" and "for_each" meta-arguments are mutually-exclusive.` ,
141
+ Subject : & attr .NameRange ,
142
+ })
143
+ }
144
+ }
145
+
183
146
return & r , diags
184
147
}
185
148
186
- var testQueryFileSchema = & hcl.BodySchema {
149
+ // QueryListResourceBlockSchema is the schema for a list resource type within
150
+ // a terraform query file.
151
+ var QueryListResourceBlockSchema = & hcl.BodySchema {
152
+ Attributes : commonResourceAttributes ,
153
+ }
154
+
155
+ // queryFileSchema is the schema for a terraform query file. It defines the
156
+ // expected structure of the file, including the types of supported blocks and their
157
+ // attributes.
158
+ var queryFileSchema = & hcl.BodySchema {
187
159
Blocks : []hcl.BlockHeaderSchema {
188
160
{
189
161
Type : "list" ,
0 commit comments