Skip to content

Commit 0e701d3

Browse files
authored
Add support for provider refs in scoped data sources (#136)
1 parent 231568f commit 0e701d3

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

terraform/runner.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ func (r *Runner) GetProviderRefs() (map[string]*ProviderRef, hcl.Diagnostics) {
142142
},
143143
},
144144
},
145+
{
146+
Type: "check",
147+
LabelNames: []string{"name"},
148+
Body: &hclext.BodySchema{
149+
Blocks: []hclext.BlockSchema{
150+
{
151+
Type: "data",
152+
LabelNames: []string{"type", "name"},
153+
Body: &hclext.BodySchema{
154+
Attributes: []hclext.AttributeSchema{
155+
{Name: "provider"},
156+
},
157+
},
158+
},
159+
},
160+
},
161+
},
145162
},
146163
}, &tflint.GetModuleContentOption{ExpandMode: tflint.ExpandModeNone})
147164
if err != nil {
@@ -199,6 +216,28 @@ func (r *Runner) GetProviderRefs() (map[string]*ProviderRef, hcl.Diagnostics) {
199216
providerRefs[ref.Name] = ref
200217
}
201218
}
219+
case "check":
220+
for _, data := range block.Body.Blocks {
221+
if attr, exists := data.Body.Attributes["provider"]; exists {
222+
ref, decodeDiags := decodeProviderRef(attr.Expr, data.DefRange)
223+
diags = diags.Extend(decodeDiags)
224+
if decodeDiags.HasErrors() {
225+
continue
226+
}
227+
providerRefs[ref.Name] = ref
228+
} else {
229+
providerName := data.Labels[0]
230+
if under := strings.Index(providerName, "_"); under != -1 {
231+
providerName = providerName[:under]
232+
}
233+
providerRefs[providerName] = &ProviderRef{
234+
Name: providerName,
235+
DefRange: data.DefRange,
236+
}
237+
}
238+
}
239+
default:
240+
panic("unreachable")
202241
}
203242
}
204243

terraform/runner_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ func TestGetProviderRefs(t *testing.T) {
183183
{
184184
name: "resource",
185185
content: `
186+
resource "google_compute_instance" "main" {}`,
187+
want: map[string]*ProviderRef{
188+
"google": {Name: "google", DefRange: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 2, Column: 1}, End: hcl.Pos{Line: 2, Column: 42}}},
189+
},
190+
},
191+
{
192+
name: "resource with provider",
193+
content: `
186194
resource "google_compute_instance" "main" {
187195
provider = google.europe
188196
}`,
@@ -193,6 +201,14 @@ resource "google_compute_instance" "main" {
193201
{
194202
name: "data",
195203
content: `
204+
data "aws_ami" "main" {}`,
205+
want: map[string]*ProviderRef{
206+
"aws": {Name: "aws", DefRange: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 2, Column: 1}, End: hcl.Pos{Line: 2, Column: 22}}},
207+
},
208+
},
209+
{
210+
name: "data with provider",
211+
content: `
196212
data "aws_ami" "main" {
197213
provider = aws.west
198214
}`,
@@ -222,6 +238,28 @@ module "server" {
222238
"aws": {Name: "aws", DefRange: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 2, Column: 1}, End: hcl.Pos{Line: 2, Column: 16}}},
223239
},
224240
},
241+
{
242+
name: "scoped data",
243+
content: `
244+
check "my_check" {
245+
data "aws_ami" "main" {}
246+
}`,
247+
want: map[string]*ProviderRef{
248+
"aws": {Name: "aws", DefRange: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 3, Column: 3}, End: hcl.Pos{Line: 3, Column: 24}}},
249+
},
250+
},
251+
{
252+
name: "scoped data with provider",
253+
content: `
254+
check "my_check" {
255+
data "aws_ami" "main" {
256+
provider = aws.west
257+
}
258+
}`,
259+
want: map[string]*ProviderRef{
260+
"aws": {Name: "aws", DefRange: hcl.Range{Filename: "main.tf", Start: hcl.Pos{Line: 3, Column: 3}, End: hcl.Pos{Line: 3, Column: 24}}},
261+
},
262+
},
225263
}
226264

227265
for _, test := range tests {

0 commit comments

Comments
 (0)