-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable early validation for Terraform Stacks
This change enables early validation for Terraform Stacks in tfstack. hcl and tfdeploy.hcl files. Available validations: - Missing required attributes - Deprecated attributes - Deprecated blocks - Unexpected attributes - Unexpected blocks - Maximum number of blocks - Minimum number of blocks - Block labels length This adds a new job to validate the schema of the these files. The job is enqueued when the `EnableEnhancedValidation` option is set to true.
- Loading branch information
Showing
6 changed files
with
208 additions
and
0 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
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
70 changes: 70 additions & 0 deletions
70
internal/features/stacks/decoder/validations/missing_required_attribute.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,70 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package validations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/hcl-lang/schema" | ||
"github.com/hashicorp/hcl-lang/schemacontext" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclsyntax" | ||
) | ||
|
||
type MissingRequiredAttribute struct{} | ||
|
||
func (mra MissingRequiredAttribute) Visit(ctx context.Context, node hclsyntax.Node, nodeSchema schema.Schema) (context.Context, hcl.Diagnostics) { | ||
var diags hcl.Diagnostics | ||
if HasUnknownRequiredAttributes(ctx) { | ||
return ctx, diags | ||
} | ||
|
||
switch nodeType := node.(type) { | ||
case *hclsyntax.Block: | ||
// Providers are excluded from the validation for the time being | ||
// due to complexity around required attributes with dynamic defaults | ||
// See https://github.com/hashicorp/vscode-terraform/issues/1616 | ||
nestingLvl, nestingOk := schemacontext.BlockNestingLevel(ctx) | ||
if nodeType.Type == "provider" && (nestingOk && nestingLvl == 0) { | ||
ctx = WithUnknownRequiredAttributes(ctx) | ||
} | ||
case *hclsyntax.Body: | ||
if nodeSchema == nil { | ||
return ctx, diags | ||
} | ||
|
||
bodySchema := nodeSchema.(*schema.BodySchema) | ||
if bodySchema.Attributes == nil { | ||
return ctx, diags | ||
} | ||
|
||
for name, attr := range bodySchema.Attributes { | ||
if attr.IsRequired { | ||
_, ok := nodeType.Attributes[name] | ||
if !ok { | ||
diags = append(diags, &hcl.Diagnostic{ | ||
Severity: hcl.DiagError, | ||
Summary: fmt.Sprintf("Required attribute %q not specified", name), | ||
Detail: fmt.Sprintf("An attribute named %q is required here", name), | ||
Subject: nodeType.SrcRange.Ptr(), | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return ctx, diags | ||
} | ||
|
||
type unknownRequiredAttrsCtxKey struct{} | ||
|
||
func HasUnknownRequiredAttributes(ctx context.Context) bool { | ||
_, ok := ctx.Value(unknownRequiredAttrsCtxKey{}).(bool) | ||
return ok | ||
} | ||
|
||
func WithUnknownRequiredAttributes(ctx context.Context) context.Context { | ||
return context.WithValue(ctx, unknownRequiredAttrsCtxKey{}, true) | ||
} |
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,20 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package decoder | ||
|
||
import ( | ||
"github.com/hashicorp/hcl-lang/validator" | ||
"github.com/hashicorp/terraform-ls/internal/features/stacks/decoder/validations" | ||
) | ||
|
||
var stackValidators = []validator.Validator{ | ||
validator.BlockLabelsLength{}, | ||
validator.DeprecatedAttribute{}, | ||
validator.DeprecatedBlock{}, | ||
validator.MaxBlocks{}, | ||
validator.MinBlocks{}, | ||
validations.MissingRequiredAttribute{}, | ||
validator.UnexpectedAttribute{}, | ||
validator.UnexpectedBlock{}, | ||
} |
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,86 @@ | ||
package jobs | ||
|
||
import ( | ||
"context" | ||
"path" | ||
|
||
"github.com/hashicorp/hcl-lang/decoder" | ||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl/v2" | ||
lsctx "github.com/hashicorp/terraform-ls/internal/context" | ||
idecoder "github.com/hashicorp/terraform-ls/internal/decoder" | ||
"github.com/hashicorp/terraform-ls/internal/document" | ||
"github.com/hashicorp/terraform-ls/internal/features/stacks/ast" | ||
sdecoder "github.com/hashicorp/terraform-ls/internal/features/stacks/decoder" | ||
"github.com/hashicorp/terraform-ls/internal/features/stacks/state" | ||
"github.com/hashicorp/terraform-ls/internal/job" | ||
lsp "github.com/hashicorp/terraform-ls/internal/lsp" | ||
globalAst "github.com/hashicorp/terraform-ls/internal/terraform/ast" | ||
"github.com/hashicorp/terraform-ls/internal/terraform/module/operation" | ||
) | ||
|
||
func SchemaModuleValidation(ctx context.Context, stackStore *state.StackStore, stackPath string) error { | ||
rpcContext := lsctx.DocumentContext(ctx) | ||
isMatchingLanguageId := rpcContext.LanguageID == lsp.Stacks.String() || rpcContext.LanguageID == lsp.Deploy.String() | ||
if !isMatchingLanguageId { | ||
return nil | ||
} | ||
|
||
record, err := stackStore.StackRecordByPath(stackPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Avoid validation if it is already in progress or already finished | ||
if record.DiagnosticsState[globalAst.SchemaValidationSource] != operation.OpStateUnknown && !job.IgnoreState(ctx) { | ||
return job.StateNotChangedErr{Dir: document.DirHandleFromPath(stackPath)} | ||
} | ||
|
||
err = stackStore.SetDiagnosticsState(stackPath, globalAst.SchemaValidationSource, operation.OpStateLoading) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d := decoder.NewDecoder(&sdecoder.PathReader{ | ||
StateReader: stackStore, | ||
}) | ||
d.SetContext(idecoder.DecoderContext(ctx)) | ||
|
||
decoder, err := d.Path(lang.Path{ | ||
Path: stackPath, | ||
LanguageID: rpcContext.LanguageID, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var rErr error | ||
if rpcContext.Method == "textDocument/didChange" { | ||
filename := path.Base(rpcContext.URI) | ||
|
||
var fileDiags hcl.Diagnostics | ||
fileDiags, rErr = decoder.ValidateFile(ctx, filename) | ||
|
||
diags, ok := record.Diagnostics[globalAst.SchemaValidationSource] | ||
if !ok { | ||
diags = make(ast.Diagnostics) | ||
} | ||
diags[ast.FilenameFromName(filename)] = fileDiags | ||
|
||
sErr := stackStore.UpdateDiagnostics(stackPath, globalAst.SchemaValidationSource, diags) | ||
if sErr != nil { | ||
return sErr | ||
} | ||
} else { | ||
// We validate the whole stack | ||
var diags lang.DiagnosticsMap | ||
diags, rErr = decoder.Validate(ctx) | ||
|
||
sErr := stackStore.UpdateDiagnostics(stackPath, globalAst.SchemaValidationSource, ast.DiagnosticsFromMap(diags)) | ||
if sErr != nil { | ||
return sErr | ||
} | ||
} | ||
|
||
return rErr | ||
} |