Skip to content

Commit

Permalink
Fix addr and ID usage for import
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Jul 11, 2020
1 parent c18babf commit 7961e88
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 32 deletions.
17 changes: 6 additions & 11 deletions tfexec/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ type ImportOption interface {
configureImport(*importConfig)
}

func (opt *AddrOption) configureImport(conf *importConfig) {
conf.addr = opt.addr
}

func (opt *IdOption) configureImport(conf *importConfig) {
conf.id = opt.id
}

func (opt *BackupOption) configureImport(conf *importConfig) {
conf.backup = opt.path
}
Expand Down Expand Up @@ -75,8 +67,8 @@ func (opt *VarFileOption) configureImport(conf *importConfig) {
conf.varFile = opt.path
}

func (t *Terraform) Import(ctx context.Context, opts ...ImportOption) error {
importCmd := t.ImportCmd(ctx, opts...)
func (t *Terraform) Import(ctx context.Context, address, id string, opts ...ImportOption) error {
importCmd := t.ImportCmd(ctx, address, id, opts...)

var errBuf strings.Builder
importCmd.Stderr = &errBuf
Expand All @@ -89,7 +81,7 @@ func (t *Terraform) Import(ctx context.Context, opts ...ImportOption) error {
return nil
}

func (tf *Terraform) ImportCmd(ctx context.Context, opts ...ImportOption) *exec.Cmd {
func (tf *Terraform) ImportCmd(ctx context.Context, address, id string, opts ...ImportOption) *exec.Cmd {
c := defaultImportOptions

for _, o := range opts {
Expand Down Expand Up @@ -133,5 +125,8 @@ func (tf *Terraform) ImportCmd(ctx context.Context, opts ...ImportOption) *exec.
}
}

// required args, always pass
args = append(args, address, id)

return tf.buildTerraformCmd(ctx, args...)
}
11 changes: 6 additions & 5 deletions tfexec/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ func TestImportCmd(t *testing.T) {
}

// defaults
importCmd := tf.ImportCmd(context.Background())
importCmd := tf.ImportCmd(context.Background(), "my-addr", "my-id")

actual := strings.TrimPrefix(cmdString(importCmd), importCmd.Path+" ")

expected := "import -no-color -input=false -lock-timeout=0s -lock=true"
expected := "import -no-color -input=false -lock-timeout=0s -lock=true my-addr my-id"

if actual != expected {
t.Fatalf("expected default arguments of ImportCmd:\n%s\n actual arguments:\n%s\n", expected, actual)
}

// override all defaults
importCmd = tf.ImportCmd(context.Background(),
importCmd = tf.ImportCmd(context.Background(), "my-addr2", "my-id2",
Backup("testbackup"),
LockTimeout("200s"),
State("teststate"),
Expand All @@ -37,11 +37,12 @@ func TestImportCmd(t *testing.T) {
Lock(false),
Var("var1=foo"),
Var("var2=bar"),
AllowMissingConfig(true))
AllowMissingConfig(true),
)

actual = strings.TrimPrefix(cmdString(importCmd), importCmd.Path+" ")

expected = "import -no-color -input=false -backup=testbackup -lock-timeout=200s -state=teststate -state-out=teststateout -var-file=testvarfile -lock=false -allow-missing-config -var 'var1=foo' -var 'var2=bar'"
expected = "import -no-color -input=false -backup=testbackup -lock-timeout=200s -state=teststate -state-out=teststateout -var-file=testvarfile -lock=false -allow-missing-config -var 'var1=foo' -var 'var2=bar' my-addr2 my-id2"

if actual != expected {
t.Fatalf("expected arguments of ImportCmd:\n%s\n actual arguments:\n%s\n", expected, actual)
Expand Down
16 changes: 0 additions & 16 deletions tfexec/options.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package tfexec

type AddrOption struct {
addr string
}

func Addr(addr string) *AddrOption {
return &AddrOption{addr}
}

type AllowMissingConfigOption struct {
allowMissingConfig bool
}
Expand Down Expand Up @@ -97,14 +89,6 @@ func GetPlugins(getPlugins bool) *GetPluginsOption {
return &GetPluginsOption{getPlugins}
}

type IdOption struct {
id string
}

func Id(id string) *IdOption {
return &IdOption{id}
}

type LockOption struct {
lock bool
}
Expand Down

0 comments on commit 7961e88

Please sign in to comment.