forked from hashicorp/terraform-exec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds feature requested in hashicorp#201. This allows programatic generation of `.terraform.lock.hcl` files via this library
- Loading branch information
1 parent
43e54be
commit 5789a53
Showing
4 changed files
with
180 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 TestProvidersLock(t *testing.T) { | ||
runTestVersions(t, []string{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.ProvidersLock(context.Background()) | ||
if err != nil { | ||
t.Fatalf("error running provider lock: %s", err) | ||
} | ||
}) | ||
|
||
} |
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,76 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
) | ||
|
||
type providersLockConfig struct { | ||
fsMirror string | ||
netMirror string | ||
platforms []string | ||
providers []string | ||
} | ||
|
||
var defaultProvidersLockOptions = providersLockConfig{} | ||
|
||
type ProvidersLockOption interface { | ||
configureProvidersLock(*providersLockConfig) | ||
} | ||
|
||
func (opt *FSMirrorOption) configureProvidersLock(conf *providersLockConfig) { | ||
conf.fsMirror = opt.fsMirror | ||
} | ||
|
||
func (opt *NetMirrorOption) configureProvidersLock(conf *providersLockConfig) { | ||
conf.netMirror = opt.netMirror | ||
} | ||
|
||
func (opt *PlatformOption) configureProvidersLock(conf *providersLockConfig) { | ||
conf.platforms = append(conf.platforms, opt.platform) | ||
} | ||
|
||
func (opt *ProviderOption) configureProvidersLock(conf *providersLockConfig) { | ||
conf.providers = append(conf.providers, opt.provider) | ||
} | ||
|
||
// ProvidersLock represents the terraform providers lock | ||
func (tf *Terraform) ProvidersLock(ctx context.Context, opts ...ProvidersLockOption) error { | ||
lockCmd := tf.providersLockCmd(ctx, opts...) | ||
|
||
err := tf.runTerraformCmd(ctx, lockCmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return err | ||
} | ||
|
||
func (tf *Terraform) providersLockCmd(ctx context.Context, opts ...ProvidersLockOption) *exec.Cmd { | ||
c := defaultProvidersLockOptions | ||
|
||
for _, o := range opts { | ||
o.configureProvidersLock(&c) | ||
} | ||
args := []string{"providers", "lock"} | ||
|
||
// string options, only pass if set | ||
if c.fsMirror != "" { | ||
args = append(args, "-fs-mirror="+c.fsMirror) | ||
} | ||
|
||
if c.netMirror != "" { | ||
args = append(args, "-net-mirror="+c.netMirror) | ||
} | ||
|
||
for _, p := range c.platforms { | ||
args = append(args, "-platform="+p) | ||
} | ||
|
||
// positional providers argument | ||
for _, p := range c.providers { | ||
args = append(args, p) | ||
} | ||
|
||
return tf.buildTerraformCmd(ctx, nil, args...) | ||
} |
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,42 @@ | ||
package tfexec | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-exec/tfexec/internal/testutil" | ||
) | ||
|
||
func TestProvidersLockCmd(t *testing.T) { | ||
td := testTempDir(t) | ||
|
||
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) { | ||
lockCmd := tf.providersLockCmd(context.Background()) | ||
|
||
assertCmd(t, []string{ | ||
"providers", | ||
"lock", | ||
}, nil, lockCmd) | ||
}) | ||
|
||
t.Run("override all defaults", func(t *testing.T) { | ||
lockCmd := tf.providersLockCmd(context.Background(), FSMirror("test"), NetMirror("test"), Platform("linux_amd64"), Provider("workingdir")) | ||
|
||
assertCmd(t, []string{ | ||
"providers", | ||
"lock", | ||
"-fs-mirror=test", | ||
"-net-mirror=test", | ||
"-platform=linux_amd64", | ||
"workingdir", | ||
}, nil, lockCmd) | ||
}) | ||
} |