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

Only merge provider-defined functions for Terraform >= 1.8 #336

Merged
merged 3 commits into from
Apr 3, 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
1 change: 1 addition & 0 deletions schema/core_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
v1_5 = version.Must(version.NewVersion("1.5"))
v1_6 = version.Must(version.NewVersion("1.6"))
v1_7 = version.Must(version.NewVersion("1.7"))
v1_8 = version.Must(version.NewVersion("1.8"))
)

// CoreModuleSchemaForVersion finds a module schema which is relevant
Expand Down
14 changes: 12 additions & 2 deletions schema/functions_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ package schema
import (
"fmt"

"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/schema"
tfmod "github.com/hashicorp/terraform-schema/module"
)

type FunctionsMerger struct {
coreFunctions map[string]schema.FunctionSignature
schemaReader SchemaReader
coreFunctions map[string]schema.FunctionSignature
terraformVersion *version.Version
schemaReader SchemaReader
}

func NewFunctionsMerger(coreFunctions map[string]schema.FunctionSignature) *FunctionsMerger {
Expand All @@ -25,6 +27,10 @@ func (m *FunctionsMerger) SetSchemaReader(sr SchemaReader) {
m.schemaReader = sr
}

func (m *FunctionsMerger) SetTerraformVersion(v *version.Version) {
m.terraformVersion = v
}

func (m *FunctionsMerger) FunctionsForModule(meta *tfmod.Meta) (map[string]schema.FunctionSignature, error) {
if m.coreFunctions == nil {
return nil, coreFunctionsRequiredErr{}
Expand All @@ -34,6 +40,10 @@ func (m *FunctionsMerger) FunctionsForModule(meta *tfmod.Meta) (map[string]schem
return m.coreFunctions, nil
}

if m.terraformVersion.LessThan(v1_8) {
return m.coreFunctions, nil
}

mergedFunctions := make(map[string]schema.FunctionSignature, len(m.coreFunctions))
for fName, fSig := range m.coreFunctions {
mergedFunctions[fName] = *fSig.Copy()
Expand Down
93 changes: 62 additions & 31 deletions schema/functions_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,32 @@ func TestFunctionsMerger_FunctionsForModule_noMeta(t *testing.T) {
}
}

func TestFunctionsMerger_FunctionsForModule(t *testing.T) {
var providerSchemaWithFunctions = map[string]*tfjson.ProviderSchema{
"registry.terraform.io/hashicorp/test": {
ConfigSchema: &tfjson.Schema{},
DataSourceSchemas: map[string]*tfjson.Schema{},
ResourceSchemas: map[string]*tfjson.Schema{},
Functions: map[string]*tfjson.FunctionSignature{
"bar": {
Parameters: []*tfjson.FunctionParameter{
{Name: "baz", Type: cty.String, Description: "baz param"},
},
Description: "bar function",
ReturnType: cty.String,
},
"alleven": {
VariadicParameter: &tfjson.FunctionParameter{
Name: "numbers",
Type: cty.List(cty.Number),
},
Description: "Returns true if all passed arguments are even numbers",
ReturnType: cty.Bool,
},
},
},
}

func TestFunctionsMerger_FunctionsForModule_18(t *testing.T) {
coreFunctions := map[string]schema.FunctionSignature{
"foo": {
Params: []function.Parameter{
Expand All @@ -71,40 +96,13 @@ func TestFunctionsMerger_FunctionsForModule(t *testing.T) {
fm.SetSchemaReader(&testJsonSchemaReader{
ps: &tfjson.ProviderSchemas{
FormatVersion: "1.0",
Schemas: map[string]*tfjson.ProviderSchema{
"registry.terraform.io/hashicorp/test": {
ConfigSchema: &tfjson.Schema{},
DataSourceSchemas: map[string]*tfjson.Schema{},
ResourceSchemas: map[string]*tfjson.Schema{},
Functions: map[string]*tfjson.FunctionSignature{
"bar": {
Parameters: []*tfjson.FunctionParameter{
{Name: "baz", Type: cty.String, Description: "baz param"},
},
Description: "bar function",
ReturnType: cty.String,
},
"alleven": {
VariadicParameter: &tfjson.FunctionParameter{
Name: "numbers",
Type: cty.List(cty.Number),
},
Description: "Returns true if all passed arguments are even numbers",
ReturnType: cty.Bool,
},
},
},
},
Schemas: providerSchemaWithFunctions,
},
})
fm.SetTerraformVersion(version.Must(version.NewVersion("1.8")))

testProvider := addr.NewDefaultProvider("test")
versionConstraints, err := version.NewConstraint("1.0.0")

if err != nil {
t.Fatalf("unexpected error: %#v", err)
}

versionConstraints := version.MustConstraints(version.NewConstraint("1.0.0"))
meta := &tfmod.Meta{
ProviderReferences: map[tfmod.ProviderRef]tfaddr.Provider{
{LocalName: "localtest"}: testProvider,
Expand Down Expand Up @@ -149,3 +147,36 @@ func TestFunctionsMerger_FunctionsForModule(t *testing.T) {
t.Fatalf("functions mismatch: %s", diff)
}
}

func TestFunctionsMerger_FunctionsForModule_17(t *testing.T) {
fm := NewFunctionsMerger(map[string]schema.FunctionSignature{})
fm.SetSchemaReader(&testJsonSchemaReader{
ps: &tfjson.ProviderSchemas{
FormatVersion: "1.0",
Schemas: providerSchemaWithFunctions,
},
})
fm.SetTerraformVersion(version.Must(version.NewVersion("1.7")))

testProvider := addr.NewDefaultProvider("test")
versionConstraints := version.MustConstraints(version.NewConstraint("1.0.0"))
meta := &tfmod.Meta{
ProviderReferences: map[tfmod.ProviderRef]tfaddr.Provider{
{LocalName: "localtest"}: testProvider,
},
ProviderRequirements: tfmod.ProviderRequirements{
testProvider: versionConstraints,
},
}

expectedFunctions := map[string]schema.FunctionSignature{}

givenFunctions, err := fm.FunctionsForModule(meta)
if err != nil {
t.Fatalf("unexpected error: %#v", err)
}

if diff := cmp.Diff(expectedFunctions, givenFunctions, ctydebug.CmpOptions); diff != "" {
t.Fatalf("functions mismatch: %s", diff)
}
}
Loading