mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
command: terraform.tfvars loaded by default if it exists
This commit is contained in:
parent
2ba5a6064d
commit
642fed0356
@ -12,6 +12,7 @@ IMPROVEMENTS:
|
|||||||
|
|
||||||
BUG FIXES:
|
BUG FIXES:
|
||||||
|
|
||||||
|
* core: Default variable file "terraform.tfvars" is auto-loaded. [GH-59]
|
||||||
* providers/cloudflare: Include the proper bins so the cloudflare
|
* providers/cloudflare: Include the proper bins so the cloudflare
|
||||||
provider is compiled
|
provider is compiled
|
||||||
* providers/aws: Engine version for RDS now properly set [GH-118]
|
* providers/aws: Engine version for RDS now properly set [GH-118]
|
||||||
|
@ -23,7 +23,7 @@ func (c *ApplyCommand) Run(args []string) int {
|
|||||||
var refresh bool
|
var refresh bool
|
||||||
var statePath, stateOutPath, backupPath string
|
var statePath, stateOutPath, backupPath string
|
||||||
|
|
||||||
args = c.Meta.process(args)
|
args = c.Meta.process(args, true)
|
||||||
|
|
||||||
cmdFlags := c.Meta.flagSet("apply")
|
cmdFlags := c.Meta.flagSet("apply")
|
||||||
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
cmdFlags.BoolVar(&refresh, "refresh", true, "refresh")
|
||||||
|
@ -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) {
|
func TestApply_backup(t *testing.T) {
|
||||||
originalState := &terraform.State{
|
originalState := &terraform.State{
|
||||||
Resources: map[string]*terraform.ResourceState{
|
Resources: map[string]*terraform.ResourceState{
|
||||||
|
@ -10,6 +10,9 @@ import (
|
|||||||
// DefaultStateFilename is the default filename used for the state file.
|
// DefaultStateFilename is the default filename used for the state file.
|
||||||
const DefaultStateFilename = "terraform.tfstate"
|
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
|
// DefaultBackupExtention is added to the state file to form the path
|
||||||
const DefaultBackupExtention = ".backup"
|
const DefaultBackupExtention = ".backup"
|
||||||
|
|
||||||
|
@ -116,3 +116,12 @@ func testTempFile(t *testing.T) string {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testTempDir(t *testing.T) string {
|
||||||
|
d, err := ioutil.TempDir("", "tf")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
@ -16,7 +16,7 @@ type GraphCommand struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *GraphCommand) Run(args []string) int {
|
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 := flag.NewFlagSet("graph", flag.ContinueOnError)
|
||||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||||
|
@ -134,7 +134,7 @@ func (m *Meta) flagSet(n string) *flag.FlagSet {
|
|||||||
// process will process the meta-parameters out of the arguments. This
|
// process will process the meta-parameters out of the arguments. This
|
||||||
// will potentially modify the args in-place. It will return the resulting
|
// will potentially modify the args in-place. It will return the resulting
|
||||||
// slice.
|
// 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
|
// We do this so that we retain the ability to technically call
|
||||||
// process multiple times, even if we have no plans to do so
|
// process multiple times, even if we have no plans to do so
|
||||||
if m.oldUi != nil {
|
if m.oldUi != nil {
|
||||||
@ -159,6 +159,17 @@ func (m *Meta) process(args []string) []string {
|
|||||||
Ui: m.oldUi,
|
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
|
return args
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ func TestMetaColorize(t *testing.T) {
|
|||||||
m.Color = true
|
m.Color = true
|
||||||
args = []string{"foo", "bar"}
|
args = []string{"foo", "bar"}
|
||||||
args2 = []string{"foo", "bar"}
|
args2 = []string{"foo", "bar"}
|
||||||
args = m.process(args)
|
args = m.process(args, false)
|
||||||
if !reflect.DeepEqual(args, args2) {
|
if !reflect.DeepEqual(args, args2) {
|
||||||
t.Fatalf("bad: %#v", args)
|
t.Fatalf("bad: %#v", args)
|
||||||
}
|
}
|
||||||
@ -26,7 +26,7 @@ func TestMetaColorize(t *testing.T) {
|
|||||||
m = new(Meta)
|
m = new(Meta)
|
||||||
args = []string{"foo", "bar"}
|
args = []string{"foo", "bar"}
|
||||||
args2 = []string{"foo", "bar"}
|
args2 = []string{"foo", "bar"}
|
||||||
args = m.process(args)
|
args = m.process(args, false)
|
||||||
if !reflect.DeepEqual(args, args2) {
|
if !reflect.DeepEqual(args, args2) {
|
||||||
t.Fatalf("bad: %#v", args)
|
t.Fatalf("bad: %#v", args)
|
||||||
}
|
}
|
||||||
@ -38,7 +38,7 @@ func TestMetaColorize(t *testing.T) {
|
|||||||
m = new(Meta)
|
m = new(Meta)
|
||||||
args = []string{"foo", "-no-color", "bar"}
|
args = []string{"foo", "-no-color", "bar"}
|
||||||
args2 = []string{"foo", "bar"}
|
args2 = []string{"foo", "bar"}
|
||||||
args = m.process(args)
|
args = m.process(args, false)
|
||||||
if !reflect.DeepEqual(args, args2) {
|
if !reflect.DeepEqual(args, args2) {
|
||||||
t.Fatalf("bad: %#v", args)
|
t.Fatalf("bad: %#v", args)
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ type OutputCommand struct {
|
|||||||
func (c *OutputCommand) Run(args []string) int {
|
func (c *OutputCommand) Run(args []string) int {
|
||||||
var statePath string
|
var statePath string
|
||||||
|
|
||||||
args = c.Meta.process(args)
|
args = c.Meta.process(args, false)
|
||||||
|
|
||||||
cmdFlags := flag.NewFlagSet("output", flag.ContinueOnError)
|
cmdFlags := flag.NewFlagSet("output", flag.ContinueOnError)
|
||||||
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
||||||
|
@ -19,7 +19,7 @@ func (c *PlanCommand) Run(args []string) int {
|
|||||||
var destroy, refresh bool
|
var destroy, refresh bool
|
||||||
var outPath, statePath, backupPath string
|
var outPath, statePath, backupPath string
|
||||||
|
|
||||||
args = c.Meta.process(args)
|
args = c.Meta.process(args, true)
|
||||||
|
|
||||||
cmdFlags := c.Meta.flagSet("plan")
|
cmdFlags := c.Meta.flagSet("plan")
|
||||||
cmdFlags.BoolVar(&destroy, "destroy", false, "destroy")
|
cmdFlags.BoolVar(&destroy, "destroy", false, "destroy")
|
||||||
|
@ -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) {
|
func TestPlan_backup(t *testing.T) {
|
||||||
// Write out some prior state
|
// Write out some prior state
|
||||||
tf, err := ioutil.TempFile("", "tf")
|
tf, err := ioutil.TempFile("", "tf")
|
||||||
|
@ -18,7 +18,7 @@ type RefreshCommand struct {
|
|||||||
func (c *RefreshCommand) Run(args []string) int {
|
func (c *RefreshCommand) Run(args []string) int {
|
||||||
var statePath, stateOutPath, backupPath string
|
var statePath, stateOutPath, backupPath string
|
||||||
|
|
||||||
args = c.Meta.process(args)
|
args = c.Meta.process(args, true)
|
||||||
|
|
||||||
cmdFlags := c.Meta.flagSet("refresh")
|
cmdFlags := c.Meta.flagSet("refresh")
|
||||||
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
cmdFlags.StringVar(&statePath, "state", DefaultStateFilename, "path")
|
||||||
|
@ -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) {
|
func TestRefresh_backup(t *testing.T) {
|
||||||
state := &terraform.State{
|
state := &terraform.State{
|
||||||
Resources: map[string]*terraform.ResourceState{
|
Resources: map[string]*terraform.ResourceState{
|
||||||
|
@ -16,7 +16,7 @@ type ShowCommand struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ShowCommand) Run(args []string) int {
|
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 := flag.NewFlagSet("show", flag.ContinueOnError)
|
||||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||||
|
@ -21,7 +21,7 @@ func (c *VersionCommand) Help() string {
|
|||||||
func (c *VersionCommand) Run(args []string) int {
|
func (c *VersionCommand) Run(args []string) int {
|
||||||
var versionString bytes.Buffer
|
var versionString bytes.Buffer
|
||||||
|
|
||||||
args = c.Meta.process(args)
|
args = c.Meta.process(args, false)
|
||||||
|
|
||||||
fmt.Fprintf(&versionString, "Terraform v%s", c.Version)
|
fmt.Fprintf(&versionString, "Terraform v%s", c.Version)
|
||||||
if c.VersionPrerelease != "" {
|
if c.VersionPrerelease != "" {
|
||||||
|
Loading…
Reference in New Issue
Block a user