From 90a75422fb593bae6e9cb7436fb59528c33b89e1 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 20 Mar 2018 12:44:12 -0400 Subject: [PATCH] unlock state in console, import, graph, and push The state locking improvements for the regular command had the side effect of locking the state in the console, import, graph and push commands. Those commands had been updated to get a state via the Backend.Context method, which locks the state whenever possible, and now need to call Unlock directly. Add Unlock calls to all commands that call Context directly. --- command/console.go | 7 +++++++ command/graph.go | 7 +++++++ command/import.go | 7 +++++++ command/import_test.go | 7 +++++++ command/push.go | 7 +++++++ 5 files changed, 35 insertions(+) diff --git a/command/console.go b/command/console.go index cf7e15f61..f8537f3fc 100644 --- a/command/console.go +++ b/command/console.go @@ -81,6 +81,13 @@ func (c *ConsoleCommand) Run(args []string) int { return 1 } + defer func() { + err := opReq.StateLocker.Unlock(nil) + if err != nil { + c.Ui.Error(err.Error()) + } + }() + // Setup the UI so we can output directly to stdout ui := &cli.BasicUi{ Writer: wrappedstreams.Stdout(), diff --git a/command/graph.go b/command/graph.go index 7723043e8..6b9a0c524 100644 --- a/command/graph.go +++ b/command/graph.go @@ -112,6 +112,13 @@ func (c *GraphCommand) Run(args []string) int { return 1 } + defer func() { + err := opReq.StateLocker.Unlock(nil) + if err != nil { + c.Ui.Error(err.Error()) + } + }() + // Determine the graph type graphType := terraform.GraphTypePlan if plan != nil { diff --git a/command/import.go b/command/import.go index cbaeec5f4..b19fa3293 100644 --- a/command/import.go +++ b/command/import.go @@ -184,6 +184,13 @@ func (c *ImportCommand) Run(args []string) int { return 1 } + defer func() { + err := opReq.StateLocker.Unlock(nil) + if err != nil { + c.Ui.Error(err.Error()) + } + }() + // Perform the import. Note that as you can see it is possible for this // API to import more than one resource at once. For now, we only allow // one while we stabilize this feature. diff --git a/command/import_test.go b/command/import_test.go index 587c4393f..9becdcd33 100644 --- a/command/import_test.go +++ b/command/import_test.go @@ -4,6 +4,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" "strings" "testing" @@ -175,11 +176,17 @@ func TestImport_remoteState(t *testing.T) { "test_instance.foo", "bar", } + if code := c.Run(args); code != 0 { fmt.Println(ui.OutputWriter) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) } + // verify that the local state was unlocked after import + if _, err := os.Stat(filepath.Join(td, fmt.Sprintf(".%s.lock.info", statePath))); !os.IsNotExist(err) { + t.Fatal("state left locked after import") + } + // Verify that we were called if !configured { t.Fatal("Configure should be called") diff --git a/command/push.go b/command/push.go index 039696fd3..a73689d4a 100644 --- a/command/push.go +++ b/command/push.go @@ -146,6 +146,13 @@ func (c *PushCommand) Run(args []string) int { return 1 } + defer func() { + err := opReq.StateLocker.Unlock(nil) + if err != nil { + c.Ui.Error(err.Error()) + } + }() + // Get the configuration config := ctx.Module().Config() if name == "" {