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

(TF-19616) feat: Support provider defined functions in stacks #1804

Merged
merged 3 commits into from
Aug 26, 2024
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
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20240822-123409.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: Support provider defined functions in stacks configuration
time: 2024-08-22T12:34:09.71462+02:00
custom:
Issue: "1804"
Repository: terraform-ls
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/hashicorp/terraform-exec v0.21.0
github.com/hashicorp/terraform-json v0.22.1
github.com/hashicorp/terraform-registry-address v0.2.3
github.com/hashicorp/terraform-schema v0.0.0-20240822075240-1800e4ce193c
github.com/hashicorp/terraform-schema v0.0.0-20240826132342-4f99bab76318
github.com/mcuadros/go-defaults v1.2.0
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ github.com/hashicorp/terraform-json v0.22.1 h1:xft84GZR0QzjPVWs4lRUwvTcPnegqlyS7
github.com/hashicorp/terraform-json v0.22.1/go.mod h1:JbWSQCLFSXFFhg42T7l9iJwdGXBYV8fmmD6o/ML4p3A=
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
github.com/hashicorp/terraform-schema v0.0.0-20240822075240-1800e4ce193c h1:yyO3IXjZd0R1muo2yDp5x/9oYkv4w5OviH3QK/9H5F8=
github.com/hashicorp/terraform-schema v0.0.0-20240822075240-1800e4ce193c/go.mod h1:Tc8mlcXI3ulpnC1/Ho4O5DeivcXGfezj0U+igIDE3iA=
github.com/hashicorp/terraform-schema v0.0.0-20240826132342-4f99bab76318 h1:uvRBiaf+0qM3c6u/AjOmKsvdtEsSKlmty8PJ9ukEnsY=
github.com/hashicorp/terraform-schema v0.0.0-20240826132342-4f99bab76318/go.mod h1:Tc8mlcXI3ulpnC1/Ho4O5DeivcXGfezj0U+igIDE3iA=
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
github.com/hexops/autogold v1.3.1 h1:YgxF9OHWbEIUjhDbpnLhgVsjUDsiHDTyDfy2lrfdlzo=
Expand Down
7 changes: 6 additions & 1 deletion internal/features/stacks/decoder/path_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ func stackPathContext(record *state.StackRecord, stateReader CombinedReader) (*d
return nil, err
}

functions, err := functionsForStack(record, version, stateReader)
if err != nil {
return nil, err
}

pathCtx := &decoder.PathContext{
Schema: mergedSchema,
ReferenceOrigins: make(reference.Origins, 0),
ReferenceTargets: make(reference.Targets, 0),
Files: make(map[string]*hcl.File, 0),
Functions: mustFunctionsForVersion(version),
Functions: functions,
Validators: stackValidators,
}

Expand Down
35 changes: 35 additions & 0 deletions internal/features/stacks/decoder/stack_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package decoder

import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/terraform-ls/internal/features/stacks/state"
tfaddr "github.com/hashicorp/terraform-registry-address"
tfmod "github.com/hashicorp/terraform-schema/module"
tfschema "github.com/hashicorp/terraform-schema/schema"
)

func functionsForStack(record *state.StackRecord, version *version.Version, stateReader CombinedReader) (map[string]schema.FunctionSignature, error) {
fm := tfschema.NewFunctionsMerger(mustFunctionsForVersion(version))
fm.SetTerraformVersion(version)
fm.SetStateReader(stateReader)

// We have to create the provider requirements and references based on the types the functions merger expects
providerRequirements := make(tfmod.ProviderRequirements, len(record.Meta.ProviderRequirements))
providerReferences := make(map[tfmod.ProviderRef]tfaddr.Provider, len(record.Meta.ProviderRequirements))
for localName, req := range record.Meta.ProviderRequirements {
providerRequirements[req.Source] = req.VersionConstraints
providerReferences[tfmod.ProviderRef{LocalName: localName}] = req.Source
}

mMeta := &tfmod.Meta{
Path: record.Path(),
ProviderRequirements: providerRequirements,
ProviderReferences: providerReferences,
}

return fm.FunctionsForModule(mMeta)
}