-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Report usage of write-only attributes for public providers (#1926)
* feat: Report usage of write-only attributes (only public providers) * Bump terraform-schema to `3c49914` * Bump hcl-lang to `66cdc97` * chore: add changie entry
- Loading branch information
Showing
11 changed files
with
209 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: ENHANCEMENTS | ||
body: Report usage of write-only attributes for public providers | ||
time: 2025-01-17T16:45:15.722924+01:00 | ||
custom: | ||
Issue: "1926" | ||
Repository: terraform-ls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
internal/features/modules/jobs/write_only_attributes.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package jobs | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/hcl-lang/decoder" | ||
"github.com/hashicorp/hcl-lang/lang" | ||
idecoder "github.com/hashicorp/terraform-ls/internal/decoder" | ||
"github.com/hashicorp/terraform-ls/internal/document" | ||
fdecoder "github.com/hashicorp/terraform-ls/internal/features/modules/decoder" | ||
"github.com/hashicorp/terraform-ls/internal/features/modules/state" | ||
"github.com/hashicorp/terraform-ls/internal/job" | ||
ilsp "github.com/hashicorp/terraform-ls/internal/lsp" | ||
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation" | ||
tfaddr "github.com/hashicorp/terraform-registry-address" | ||
tfschema "github.com/hashicorp/terraform-schema/schema" | ||
) | ||
|
||
// DecodeWriteOnlyAttributes collects usages of write only attributes, | ||
// using previously parsed AST (via [ParseModuleConfiguration]), | ||
// core schema of appropriate version (as obtained via [GetTerraformVersion]) | ||
// and provider schemas ([PreloadEmbeddedSchema] or [ObtainSchema]). | ||
func DecodeWriteOnlyAttributes(ctx context.Context, modStore *state.ModuleStore, rootFeature fdecoder.RootReader, modPath string) error { | ||
mod, err := modStore.ModuleRecordByPath(modPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// TODO: Avoid collection if upstream jobs reported no changes | ||
|
||
// Avoid collection if it is already in progress or already done | ||
if mod.WriteOnlyAttributesState != op.OpStateUnknown && !job.IgnoreState(ctx) { | ||
return job.StateNotChangedErr{Dir: document.DirHandleFromPath(modPath)} | ||
} | ||
|
||
err = modStore.SetWriteOnlyAttributesState(modPath, op.OpStateLoading) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d := decoder.NewDecoder(&fdecoder.PathReader{ | ||
StateReader: modStore, | ||
RootReader: rootFeature, | ||
}) | ||
d.SetContext(idecoder.DecoderContext(ctx)) | ||
|
||
pd, err := d.Path(lang.Path{ | ||
Path: modPath, | ||
LanguageID: ilsp.Terraform.String(), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// input list of write only attributes | ||
woAttrs, rErr := pd.CollectWriteOnlyAttributes() | ||
|
||
if rErr != nil { | ||
return rErr | ||
} | ||
|
||
findProviderAddr := func(resourceName string) *tfaddr.Provider { | ||
for localRef, addr := range mod.Meta.ProviderReferences { | ||
if tfschema.TypeBelongsToProvider(resourceName, localRef) { | ||
return &addr | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// output counts of write only attributes aggregated by provider, resource and attribute | ||
woAttrsMap := make(state.WriteOnlyAttributes) | ||
|
||
// count usages and resolve provider | ||
for _, attr := range woAttrs { | ||
providerAddr := findProviderAddr(attr.Resource) | ||
if providerAddr == nil { | ||
continue | ||
} | ||
|
||
if _, ok := woAttrsMap[*providerAddr]; !ok { | ||
woAttrsMap[*providerAddr] = make(map[state.ResourceName]map[state.AttributeName]int) | ||
} | ||
|
||
if _, ok := woAttrsMap[*providerAddr][attr.Resource]; !ok { | ||
woAttrsMap[*providerAddr][attr.Resource] = make(map[state.AttributeName]int) | ||
} | ||
|
||
woAttrsMap[*providerAddr][attr.Resource][attr.Name]++ | ||
} | ||
|
||
sErr := modStore.UpdateWriteOnlyAttributes(modPath, woAttrsMap, rErr) | ||
if sErr != nil { | ||
return sErr | ||
} | ||
|
||
return rErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package state | ||
|
||
import ( | ||
tfaddr "github.com/hashicorp/terraform-registry-address" | ||
) | ||
|
||
type ResourceName = string | ||
type AttributeName = string | ||
|
||
type WriteOnlyAttributes map[tfaddr.Provider]map[ResourceName]map[AttributeName]int |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters