@@ -18,86 +18,36 @@ import (
18
18
type QueryFile struct {
19
19
// Providers defines a set of providers that are available to the list blocks
20
20
// within this query file.
21
- Providers map [string ]* Provider
22
-
21
+ Providers map [string ]* Provider
23
22
ProviderConfigs []* Provider
24
23
25
- Locals []* Local
26
-
24
+ Locals []* Local
27
25
Variables []* Variable
28
26
29
- // Lists defines the list of List blocks within the query file.
30
- Lists []* List
27
+ // ListResources is a slice of List blocks within the query file.
28
+ ListResources []* Resource
31
29
32
30
VariablesDeclRange hcl.Range
33
31
}
34
32
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
33
func loadQueryFile (body hcl.Body ) (* QueryFile , hcl.Diagnostics ) {
84
34
var diags hcl.Diagnostics
85
35
file := & QueryFile {
86
36
Providers : make (map [string ]* Provider ),
87
37
}
88
38
89
- content , contentDiags := body .Content (testQueryFileSchema )
39
+ content , contentDiags := body .Content (queryFileSchema )
90
40
diags = append (diags , contentDiags ... )
91
41
92
42
listBlockNames := make (map [string ]hcl.Range )
93
43
94
44
for _ , block := range content .Blocks {
95
45
switch block .Type {
96
46
case "list" :
97
- list , listDiags := decodeQueryListBlock (block , file )
47
+ list , listDiags := decodeQueryListBlock (block )
98
48
diags = append (diags , listDiags ... )
99
49
if ! listDiags .HasErrors () {
100
- file .Lists = append (file .Lists , list )
50
+ file .ListResources = append (file .ListResources , list )
101
51
}
102
52
103
53
if rng , exists := listBlockNames [list .Name ]; exists {
@@ -140,21 +90,19 @@ func loadQueryFile(body hcl.Body) (*QueryFile, hcl.Diagnostics) {
140
90
return file , diags
141
91
}
142
92
143
- func decodeQueryListBlock (block * hcl.Block , file * QueryFile ) (* List , hcl.Diagnostics ) {
93
+ func decodeQueryListBlock (block * hcl.Block ) (* Resource , hcl.Diagnostics ) {
144
94
var diags hcl.Diagnostics
145
95
146
- content , remain , contentDiags := block .Body .PartialContent (& hcl.BodySchema {
147
- Attributes : []hcl.AttributeSchema {{Name : "provider" }},
148
- })
96
+ content , remain , contentDiags := block .Body .PartialContent (QueryListResourceBlockSchema )
149
97
diags = append (diags , contentDiags ... )
150
98
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 ,
99
+ r := Resource {
100
+ Mode : addrs . ListResourceMode ,
101
+ Type : block .Labels [0 ],
102
+ TypeRange : block .LabelRanges [0 ],
103
+ Name : block .Labels [1 ],
104
+ DeclRange : block .DefRange ,
105
+ Config : remain ,
158
106
}
159
107
160
108
if attr , exists := content .Attributes ["provider" ]; exists {
@@ -180,10 +128,37 @@ func decodeQueryListBlock(block *hcl.Block, file *QueryFile) (*List, hcl.Diagnos
180
128
Subject : r .DeclRange .Ptr (),
181
129
})
182
130
}
131
+
132
+ if attr , exists := content .Attributes ["count" ]; exists {
133
+ r .Count = attr .Expr
134
+ }
135
+
136
+ if attr , exists := content .Attributes ["for_each" ]; exists {
137
+ r .ForEach = attr .Expr
138
+ // Cannot have count and for_each on the same resource block
139
+ if r .Count != nil {
140
+ diags = append (diags , & hcl.Diagnostic {
141
+ Severity : hcl .DiagError ,
142
+ Summary : `Invalid combination of "count" and "for_each"` ,
143
+ Detail : `The "count" and "for_each" meta-arguments are mutually-exclusive.` ,
144
+ Subject : & attr .NameRange ,
145
+ })
146
+ }
147
+ }
148
+
183
149
return & r , diags
184
150
}
185
151
186
- var testQueryFileSchema = & hcl.BodySchema {
152
+ // QueryListResourceBlockSchema is the schema for a list resource type within
153
+ // a terraform query file.
154
+ var QueryListResourceBlockSchema = & hcl.BodySchema {
155
+ Attributes : commonResourceAttributes ,
156
+ }
157
+
158
+ // queryFileSchema is the schema for a terraform query file. It defines the
159
+ // expected structure of the file, including the types of supported blocks and their
160
+ // attributes.
161
+ var queryFileSchema = & hcl.BodySchema {
187
162
Blocks : []hcl.BlockHeaderSchema {
188
163
{
189
164
Type : "list" ,
0 commit comments