Skip to content

Commit

Permalink
command: terraform.tfvars loaded by default if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Aug 5, 2014
1 parent 2ba5a60 commit 642fed0
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ IMPROVEMENTS:

BUG FIXES:

* core: Default variable file "terraform.tfvars" is auto-loaded. [GH-59]
* providers/cloudflare: Include the proper bins so the cloudflare
provider is compiled
* providers/aws: Engine version for RDS now properly set [GH-118]
Expand Down
2 changes: 1 addition & 1 deletion command/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (c *ApplyCommand) Run(args []string) int {
var refresh bool
var statePath, stateOutPath, backupPath string

args = c.Meta.process(args)
args = c.Meta.process(args, true)

cmdFlags := c.Meta.flagSet("apply")
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
Expand Down
51 changes: 51 additions & 0 deletions command/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,57 @@ func TestApply_varFile(t *testing.T) {
}
}

func TestApply_varFileDefault(t *testing.T) {
varFileDir := testTempDir(t)
varFilePath := filepath.Join(varFileDir, "terraform.tfvars")
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFile), 0644); err != nil {
t.Fatalf("err: %s", err)
}

statePath := testTempFile(t)

cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(varFileDir); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)

p := testProvider()
ui := new(cli.MockUi)
c := &ApplyCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}

actual := ""
p.DiffFn = func(
s *terraform.ResourceState,
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
if v, ok := c.Config["value"]; ok {
actual = v.(string)
}

return &terraform.ResourceDiff{}, nil
}

args := []string{
"-state", statePath,
testFixturePath("apply-vars"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}

if actual != "bar" {
t.Fatal("didn't work")
}
}

func TestApply_backup(t *testing.T) {
originalState := &terraform.State{
Resources: map[string]*terraform.ResourceState{
Expand Down
3 changes: 3 additions & 0 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
// DefaultStateFilename is the default filename used for the state file.
const DefaultStateFilename = "terraform.tfstate"

// DefaultVarsFilename is the default filename used for vars
const DefaultVarsFilename = "terraform.tfvars"

// DefaultBackupExtention is added to the state file to form the path
const DefaultBackupExtention = ".backup"

Expand Down
9 changes: 9 additions & 0 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,12 @@ func testTempFile(t *testing.T) string {

return result
}

func testTempDir(t *testing.T) string {
d, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}

return d
}
2 changes: 1 addition & 1 deletion command/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type GraphCommand struct {
}

func (c *GraphCommand) Run(args []string) int {
args = c.Meta.process(args)
args = c.Meta.process(args, false)

cmdFlags := flag.NewFlagSet("graph", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
Expand Down
13 changes: 12 additions & 1 deletion command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (m *Meta) flagSet(n string) *flag.FlagSet {
// process will process the meta-parameters out of the arguments. This
// will potentially modify the args in-place. It will return the resulting
// slice.
func (m *Meta) process(args []string) []string {
func (m *Meta) process(args []string, vars bool) []string {
// We do this so that we retain the ability to technically call
// process multiple times, even if we have no plans to do so
if m.oldUi != nil {
Expand All @@ -159,6 +159,17 @@ func (m *Meta) process(args []string) []string {
Ui: m.oldUi,
}

// If we support vars and the default var file exists, add it to
// the args...
if vars {
if _, err := os.Stat(DefaultVarsFilename); err == nil {
args = append(args, "", "")
copy(args[2:], args[0:])
args[0] = "-var-file"
args[1] = DefaultVarsFilename
}
}

return args
}

Expand Down
6 changes: 3 additions & 3 deletions command/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestMetaColorize(t *testing.T) {
m.Color = true
args = []string{"foo", "bar"}
args2 = []string{"foo", "bar"}
args = m.process(args)
args = m.process(args, false)
if !reflect.DeepEqual(args, args2) {
t.Fatalf("bad: %#v", args)
}
Expand All @@ -26,7 +26,7 @@ func TestMetaColorize(t *testing.T) {
m = new(Meta)
args = []string{"foo", "bar"}
args2 = []string{"foo", "bar"}
args = m.process(args)
args = m.process(args, false)
if !reflect.DeepEqual(args, args2) {
t.Fatalf("bad: %#v", args)
}
Expand All @@ -38,7 +38,7 @@ func TestMetaColorize(t *testing.T) {
m = new(Meta)
args = []string{"foo", "-no-color", "bar"}
args2 = []string{"foo", "bar"}
args = m.process(args)
args = m.process(args, false)
if !reflect.DeepEqual(args, args2) {
t.Fatalf("bad: %#v", args)
}
Expand Down
2 changes: 1 addition & 1 deletion command/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type OutputCommand struct {
func (c *OutputCommand) Run(args []string) int {
var statePath string

args = c.Meta.process(args)
args = c.Meta.process(args, false)

cmdFlags := flag.NewFlagSet("output", flag.ContinueOnError)
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
Expand Down
2 changes: 1 addition & 1 deletion command/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (c *PlanCommand) Run(args []string) int {
var destroy, refresh bool
var outPath, statePath, backupPath string

args = c.Meta.process(args)
args = c.Meta.process(args, true)

cmdFlags := c.Meta.flagSet("plan")
cmdFlags.BoolVar(&destroy, "destroy", false, "destroy")
Expand Down
48 changes: 48 additions & 0 deletions command/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,54 @@ func TestPlan_varFile(t *testing.T) {
}
}

func TestPlan_varFileDefault(t *testing.T) {
varFileDir := testTempDir(t)
varFilePath := filepath.Join(varFileDir, "terraform.tfvars")
if err := ioutil.WriteFile(varFilePath, []byte(planVarFile), 0644); err != nil {
t.Fatalf("err: %s", err)
}

cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(varFileDir); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)

p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}

actual := ""
p.DiffFn = func(
s *terraform.ResourceState,
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
if v, ok := c.Config["value"]; ok {
actual = v.(string)
}

return nil, nil
}

args := []string{
testFixturePath("plan-vars"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}

if actual != "bar" {
t.Fatal("didn't work")
}
}

func TestPlan_backup(t *testing.T) {
// Write out some prior state
tf, err := ioutil.TempFile("", "tf")
Expand Down
2 changes: 1 addition & 1 deletion command/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type RefreshCommand struct {
func (c *RefreshCommand) Run(args []string) int {
var statePath, stateOutPath, backupPath string

args = c.Meta.process(args)
args = c.Meta.process(args, true)

cmdFlags := c.Meta.flagSet("refresh")
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
Expand Down
51 changes: 51 additions & 0 deletions command/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,57 @@ func TestRefresh_varFile(t *testing.T) {
}
}

func TestRefresh_varFileDefault(t *testing.T) {
state := &terraform.State{
Resources: map[string]*terraform.ResourceState{
"test_instance.foo": &terraform.ResourceState{
ID: "bar",
Type: "test_instance",
},
},
}
statePath := testStateFile(t, state)

p := testProvider()
ui := new(cli.MockUi)
c := &RefreshCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}

varFileDir := testTempDir(t)
varFilePath := filepath.Join(varFileDir, "terraform.tfvars")
if err := ioutil.WriteFile(varFilePath, []byte(refreshVarFile), 0644); err != nil {
t.Fatalf("err: %s", err)
}

cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(varFileDir); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)

args := []string{
"-state", statePath,
testFixturePath("refresh-var"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}

if !p.ConfigureCalled {
t.Fatal("configure should be called")
}
if p.ConfigureConfig.Config["value"].(string) != "bar" {
t.Fatalf("bad: %#v", p.ConfigureConfig.Config)
}
}

func TestRefresh_backup(t *testing.T) {
state := &terraform.State{
Resources: map[string]*terraform.ResourceState{
Expand Down
2 changes: 1 addition & 1 deletion command/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ShowCommand struct {
}

func (c *ShowCommand) Run(args []string) int {
args = c.Meta.process(args)
args = c.Meta.process(args, false)

cmdFlags := flag.NewFlagSet("show", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
Expand Down
2 changes: 1 addition & 1 deletion command/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (c *VersionCommand) Help() string {
func (c *VersionCommand) Run(args []string) int {
var versionString bytes.Buffer

args = c.Meta.process(args)
args = c.Meta.process(args, false)

fmt.Fprintf(&versionString, "Terraform v%s", c.Version)
if c.VersionPrerelease != "" {
Expand Down

1 comment on commit 642fed0

@mitchellh
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.