Skip to content
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

terraform_unused_required_providers: check child modules for required_providers #1477

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 40 additions & 12 deletions rules/terraformrules/terraform_unused_required_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,45 @@ func (r *TerraformUnusedRequiredProvidersRule) Check(runner *tflint.Runner) erro
return diags
}

moduleRunners, err := tflint.NewModuleRunners(runner)
if err != nil {
return err
}

requiredProviders, diags := getRequiredProviders(runner)
if diags.HasErrors() {
return diags
}

RequiredProvidersLoop:
for key, required := range requiredProviders {
if _, exists := providerRefs[required.Name]; !exists {
for _, runner := range moduleRunners {
moduleRequiredProviders, diags := getRequiredProviders(runner)
if diags.HasErrors() {
return diags
}

if _, exists := moduleRequiredProviders[key]; exists {
continue RequiredProvidersLoop
}
}

runner.EmitIssue(
r,
fmt.Sprintf("provider '%s' is declared in required_providers but not used by the module", required.Name),
required.Range,
)
}
}

return nil
}

func getRequiredProviders(runner *tflint.Runner) (hcl.Attributes, hcl.Diagnostics) {
requiredProviders := hcl.Attributes{}
diags := hcl.Diagnostics{}

for _, file := range runner.Files() {
content, _, schemaDiags := file.Body.PartialContent(&hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{{Type: "terraform"}},
Expand Down Expand Up @@ -80,18 +118,8 @@ func (r *TerraformUnusedRequiredProvidersRule) Check(runner *tflint.Runner) erro
}
}
if diags.HasErrors() {
return diags
}

for _, required := range requiredProviders {
if _, exists := providerRefs[required.Name]; !exists {
runner.EmitIssue(
r,
fmt.Sprintf("provider '%s' is declared in required_providers but not used by the module", required.Name),
required.Range,
)
}
return nil, diags
}

return nil
return requiredProviders, nil
}