Skip to content

required_providers: support ephemeral resources #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions rules/terraform_required_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ resource "random_string" "foo" {
},
},
},
{
Name: "implicit provider - resource",
Content: `
ephemeral "random_string" "foo" {
length = 16
}
`,
Expected: helper.Issues{
{
Rule: NewTerraformRequiredProvidersRule(),
Message: "Missing version constraint for provider \"random\" in `required_providers`",
Range: hcl.Range{
Filename: "module.tf",
Start: hcl.Pos{
Line: 2,
Column: 1,
},
End: hcl.Pos{
Line: 2,
Column: 32,
},
},
},
},
},
{
Name: "implicit provider - data source",
Content: `
Expand Down Expand Up @@ -96,11 +121,11 @@ terraform {
required_providers {
template = {
source = "hashicorp/template"
version = "~> 2"
version = "~> 2"
}
}
}
provider "template" {}
provider "template" {}
`,
Expected: helper.Issues{},
},
Expand All @@ -112,7 +137,7 @@ terraform {
template = "~> 2"
}
}
provider "template" {}
provider "template" {}
`,
Expected: helper.Issues{
{
Expand Down Expand Up @@ -154,7 +179,7 @@ terraform {
}
}

provider "template" {}
provider "template" {}
`,
Expected: helper.Issues{
{
Expand Down Expand Up @@ -185,7 +210,7 @@ terraform {
}
}

provider "template" {}
provider "template" {}
`,
Config: `
rule "terraform_required_providers" {
Expand All @@ -207,7 +232,7 @@ terraform {
}
}

provider "template" {}
provider "template" {}
`,
Expected: helper.Issues{
{
Expand Down Expand Up @@ -250,7 +275,7 @@ terraform {
}
}

provider "template" {}
provider "template" {}
`,
Config: `
rule "terraform_required_providers" {
Expand All @@ -270,7 +295,7 @@ terraform {
}
}

provider "template" {}
provider "template" {}
`,
Expected: helper.Issues{
{
Expand Down Expand Up @@ -355,7 +380,7 @@ terraform {

provider "template" {
version = "~> 2"
}
}
`,
Expected: helper.Issues{
{
Expand Down Expand Up @@ -409,7 +434,7 @@ terraform {
provider "template" {
alias = "foo"
version = "~> 2"
}
}
`,
Expected: helper.Issues{
{
Expand Down
21 changes: 18 additions & 3 deletions rules/terraform_unused_required_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ func Test_TerraformUnusedRequiredProvidersRule(t *testing.T) {
`,
Expected: helper.Issues{},
},
{
Name: "used - ephemeral resource",
Content: `
terraform {
required_providers {
null = {
source = "hashicorp/null"
}
}
}

ephemeral "null_resource" "foo" {}
`,
Expected: helper.Issues{},
},
{
Name: "used - data source",
Content: `
Expand All @@ -42,7 +57,7 @@ func Test_TerraformUnusedRequiredProvidersRule(t *testing.T) {
}
}
}
resource "null_data_source" "foo" {}
data "null_data_source" "foo" {}
`,
Expected: helper.Issues{},
},
Expand All @@ -56,7 +71,7 @@ func Test_TerraformUnusedRequiredProvidersRule(t *testing.T) {
}
}
}
resource "null_resource" "foo" {
data "null_resource" "foo" {
provider = custom-null
}
`,
Expand All @@ -72,7 +87,7 @@ func Test_TerraformUnusedRequiredProvidersRule(t *testing.T) {
}
}
}
resource "null_data_source" "foo" {
data "null_data_source" "foo" {
provider = custom-null
}
`,
Expand Down
13 changes: 10 additions & 3 deletions terraform/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ func (r *Runner) GetProviderRefs() (map[string]*ProviderRef, hcl.Diagnostics) {
},
},
},
{
Type: "ephemeral",
LabelNames: []string{"type", "name"},
Body: &hclext.BodySchema{
Attributes: []hclext.AttributeSchema{
{Name: "provider"},
},
},
},
{
Type: "data",
LabelNames: []string{"type", "name"},
Expand Down Expand Up @@ -177,9 +186,7 @@ func (r *Runner) GetProviderRefs() (map[string]*ProviderRef, hcl.Diagnostics) {
var diags hcl.Diagnostics
for _, block := range body.Blocks {
switch block.Type {
case "resource":
fallthrough
case "data":
case "resource", "ephemeral", "data":
if attr, exists := block.Body.Attributes["provider"]; exists {
ref, decodeDiags := decodeProviderRef(attr.Expr, block.DefRange)
diags = diags.Extend(decodeDiags)
Expand Down