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

Add support for metadata functions -json command #358

Merged
merged 6 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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 .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
go.work*
23 changes: 23 additions & 0 deletions tfexec/internal/e2etest/metadata_functions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package e2etest

import (
"context"
"testing"

"github.com/hashicorp/go-version"

"github.com/hashicorp/terraform-exec/tfexec"
)

func TestMetadataFunctions(t *testing.T) {
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
if tfv.LessThan(metadataFunctionsMinVersion) {
t.Skip("metadata functions command is not available in this Terraform version")
}

_, err := tf.MetadataFunctions(context.Background())
if err != nil {
t.Fatalf("error running MetadataFunctions: %s", err)
}
})
}
2 changes: 2 additions & 0 deletions tfexec/internal/e2etest/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var (
showMinVersion = version.Must(version.NewVersion("0.12.0"))

providerAddressMinVersion = version.Must(version.NewVersion("0.13.0"))

metadataFunctionsMinVersion = version.Must(version.NewVersion("1.4.0"))
)

func runTest(t *testing.T, fixtureName string, cb func(t *testing.T, tfVersion *version.Version, tf *tfexec.Terraform)) {
Expand Down
39 changes: 39 additions & 0 deletions tfexec/metadata_functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tfexec

import (
"context"
"fmt"
"os/exec"

tfjson "github.com/hashicorp/terraform-json"
)

// MetadataFunctions represents the terraform metadata functions -json subcommand.
func (tf *Terraform) MetadataFunctions(ctx context.Context) (*tfjson.MetadataFunctions, error) {
err := tf.compatible(ctx, tf1_4_0, nil)
if err != nil {
return nil, fmt.Errorf("terraform metadata functions was added in 1.4.0: %w", err)
}

functionsCmd := tf.metadataFunctionsCmd(ctx)

var ret tfjson.MetadataFunctions
err = tf.runTerraformCmdJSON(ctx, functionsCmd, &ret)
if err != nil {
return nil, err
}

err = ret.Validate()
if err != nil {
return nil, err
}
dbanck marked this conversation as resolved.
Show resolved Hide resolved

return &ret, nil
}

func (tf *Terraform) metadataFunctionsCmd(ctx context.Context, args ...string) *exec.Cmd {
allArgs := []string{"metadata", "functions", "-json"}
allArgs = append(allArgs, args...)

return tf.buildTerraformCmd(ctx, nil, allArgs...)
}
29 changes: 29 additions & 0 deletions tfexec/metadata_functions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tfexec

import (
"context"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

func TestMetadataFunctionsCmd(t *testing.T) {
td := t.TempDir()

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest_v1_1))
// tf, err := NewTerraform(td, tfVersion(t, testutil.Latest_v1_4)) // TODO! enable after 1.4 release
if err != nil {
t.Fatal(err)
}

// empty env, to avoid environ mismatch in testing
tf.SetEnv(map[string]string{})

functionsCmd := tf.metadataFunctionsCmd(context.Background())

assertCmd(t, []string{
"metadata",
"functions",
"-json",
}, nil, functionsCmd)
}
1 change: 1 addition & 0 deletions tfexec/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
tf0_15_2 = version.Must(version.NewVersion("0.15.2"))
tf0_15_3 = version.Must(version.NewVersion("0.15.3"))
tf1_1_0 = version.Must(version.NewVersion("1.1.0"))
tf1_4_0 = version.Must(version.NewVersion("1.4.0"))
)

// Version returns structured output from the terraform version command including both the Terraform CLI version
Expand Down