command/push: don't allow pushing with local backend

This commit is contained in:
Mitchell Hashimoto 2017-03-16 10:47:48 -07:00
parent f1b0c57de3
commit 8b208a597d
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 51 additions and 18 deletions

View File

@ -138,6 +138,20 @@ func (m *Meta) Backend(opts *BackendOpts) (backend.Enhanced, error) {
return local, nil
}
// IsLocalBackend returns true if the backend is a local backend. We use this
// for some checks that require a remote backend.
func (m *Meta) IsLocalBackend(b backend.Backend) bool {
// Is it a local backend?
bLocal, ok := b.(*backendlocal.Local)
// If it is, does it not have an alternate state backend?
if ok {
ok = bLocal.Backend == nil
}
return ok
}
// Operation initializes a new backend.Operation struct.
//
// This prepares the operation. After calling this, the caller is expected

View File

@ -71,24 +71,6 @@ func (c *PushCommand) Run(args []string) int {
return 1
}
/*
// Verify the state is remote, we can't push without a remote state
s, err := c.State()
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to read state: %s", err))
return 1
}
if !s.State().IsRemote() {
c.Ui.Error(
"Remote state is not enabled. For Atlas to run Terraform\n" +
"for you, remote state must be used and configured. Remote\n" +
"state via any backend is accepted, not just Atlas. To\n" +
"configure remote state, use the `terraform remote config`\n" +
"command.")
return 1
}
*/
// Check if the path is a plan
plan, err := c.Plan(configPath)
if err != nil {
@ -125,6 +107,17 @@ func (c *PushCommand) Run(args []string) int {
return 1
}
// We require a non-local backend
if c.IsLocalBackend(b) {
c.Ui.Error(
"Remote state is not enabled. For Atlas to run Terraform\n" +
"for you, remote state must be used and configured. Remote\n" +
"state via any backend is accepted, not just Atlas. To\n" +
"configure remote state, use the `terraform remote config`\n" +
"command.")
return 1
}
// We require a local backend
local, ok := b.(backend.Local)
if !ok {

View File

@ -9,9 +9,11 @@ import (
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
atlas "github.com/hashicorp/atlas-go/v1"
"github.com/hashicorp/terraform/helper/copy"
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
)
@ -662,6 +664,12 @@ func TestPush_noState(t *testing.T) {
}
func TestPush_noRemoteState(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
copy.CopyDir(testFixturePath("push-no-remote"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
state := &terraform.State{
Modules: []*terraform.ModuleState{
&terraform.ModuleState{
@ -679,19 +687,32 @@ func TestPush_noRemoteState(t *testing.T) {
}
statePath := testStateFile(t, state)
// Path where the archive will be "uploaded" to
archivePath := testTempFile(t)
defer os.Remove(archivePath)
client := &mockPushClient{File: archivePath}
ui := new(cli.MockUi)
c := &PushCommand{
Meta: Meta{
Ui: ui,
},
client: client,
}
args := []string{
"-vcs=false",
"-state", statePath,
td,
}
if code := c.Run(args); code != 1 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
errStr := ui.ErrorWriter.String()
if !strings.Contains(errStr, "Remote state") {
t.Fatalf("bad: %s", errStr)
}
}
func TestPush_plan(t *testing.T) {

View File

@ -0,0 +1,5 @@
resource "aws_instance" "foo" {}
atlas {
name = "foo"
}