From 1e857571f9aa577def23df02a8adf0783c097dc2 Mon Sep 17 00:00:00 2001 From: Rambabu Duddukuri Date: Wed, 5 Jan 2022 11:17:23 -0800 Subject: [PATCH] Support terraform providers mirror subcommand --- .../internal/e2etest/providers_mirror_test.go | 26 ++++++++++ tfexec/providers_mirror.go | 50 +++++++++++++++++++ tfexec/providers_mirror_test.go | 41 +++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 tfexec/internal/e2etest/providers_mirror_test.go create mode 100644 tfexec/providers_mirror.go create mode 100644 tfexec/providers_mirror_test.go diff --git a/tfexec/internal/e2etest/providers_mirror_test.go b/tfexec/internal/e2etest/providers_mirror_test.go new file mode 100644 index 00000000..d92aea26 --- /dev/null +++ b/tfexec/internal/e2etest/providers_mirror_test.go @@ -0,0 +1,26 @@ +package e2etest + +import ( + "context" + "testing" + + "github.com/hashicorp/go-version" + + "github.com/hashicorp/terraform-exec/tfexec" + "github.com/hashicorp/terraform-exec/tfexec/internal/testutil" +) + +func TestProvidersMirror(t *testing.T) { + runTestVersions(t, []string{testutil.Latest013, testutil.Latest014, testutil.Latest015, testutil.Latest_v1}, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + err := tf.Init(context.Background()) + if err != nil { + t.Fatalf("error running Init in test directory: %s", err) + } + + err = tf.ProvidersMirror(context.Background(), "targetDir") + if err != nil { + t.Fatalf("error running provider mirror: %s", err) + } + }) + +} diff --git a/tfexec/providers_mirror.go b/tfexec/providers_mirror.go new file mode 100644 index 00000000..43bbcb22 --- /dev/null +++ b/tfexec/providers_mirror.go @@ -0,0 +1,50 @@ +package tfexec + +import ( + "context" + "fmt" + "os/exec" +) + +type providersMirrorConfig struct { + platforms []string +} + +var defaultProviderMirrorOptions = providersMirrorConfig{} + +// ProvidersMirrorCmdOption represents options used in the mirror method. +type ProvidersMirrorCmdOption interface { + configureProvidersMirror(*providersMirrorConfig) +} + +func (opt *PlatformOption) configureProvidersMirror(conf *providersMirrorConfig) { + conf.platforms = append(conf.platforms, opt.platform) +} + +// ProvidersMirror represents the terraform providers mirror subcommand. +func (tf *Terraform) ProvidersMirror(ctx context.Context, targetDir string, opts ...ProvidersMirrorCmdOption) error { + err := tf.compatible(ctx, tf0_13_0, nil) + if err != nil { + return fmt.Errorf("mirror was first introduced in Terraform 0.13.0: %w", err) + } + + mirrorCmd := tf.providersMirrorCmd(ctx, targetDir, opts...) + + return tf.runTerraformCmd(ctx, mirrorCmd) +} + +func (tf *Terraform) providersMirrorCmd(ctx context.Context, targetDir string, opts ...ProvidersMirrorCmdOption) *exec.Cmd { + c := defaultProviderMirrorOptions + + for _, o := range opts { + o.configureProvidersMirror(&c) + } + + args := []string{"providers", "mirror", targetDir} + + for _, p := range c.platforms { + args = append(args, "-platform="+p) + } + + return tf.buildTerraformCmd(ctx, nil, args...) +} diff --git a/tfexec/providers_mirror_test.go b/tfexec/providers_mirror_test.go new file mode 100644 index 00000000..5fe97b68 --- /dev/null +++ b/tfexec/providers_mirror_test.go @@ -0,0 +1,41 @@ +package tfexec + +import ( + "context" + "testing" + + "github.com/hashicorp/terraform-exec/tfexec/internal/testutil" +) + +func TestProvidersMirrorCmd(t *testing.T) { + td := t.TempDir() + + tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012)) + if err != nil { + t.Fatal(err) + } + + // empty env, to avoid environ mismatch in testing + tf.SetEnv(map[string]string{}) + + t.Run("defaults", func(t *testing.T) { + mirrorCmd := tf.providersMirrorCmd(context.Background(), "foo") + + assertCmd(t, []string{ + "providers", + "mirror", + "foo", + }, nil, mirrorCmd) + }) + + t.Run("override all defaults", func(t *testing.T) { + mirrorCmd := tf.providersMirrorCmd(context.Background(), "foo", Platform("linux_amd64")) + + assertCmd(t, []string{ + "providers", + "mirror", + "foo", + "-platform=linux_amd64", + }, nil, mirrorCmd) + }) +}