add `-force-copy` option to init command

The `-force-copy` option will suppress confirmation for copying state
data.

Modify some tests to use the option, making sure to leave coverage of
the Input code path.
This commit is contained in:
James Bardin 2017-03-21 15:05:51 -04:00
parent b7152c4405
commit 54e536cfe0
6 changed files with 117 additions and 83 deletions

View File

@ -21,11 +21,14 @@ type InitCommand struct {
func (c *InitCommand) Run(args []string) int {
var flagBackend, flagGet bool
var flagConfigExtra map[string]interface{}
args = c.Meta.process(args, false)
cmdFlags := c.flagSet("init")
cmdFlags.BoolVar(&flagBackend, "backend", true, "")
cmdFlags.Var((*variables.FlagAny)(&flagConfigExtra), "backend-config", "")
cmdFlags.BoolVar(&flagGet, "get", true, "")
cmdFlags.BoolVar(&c.forceInitCopy, "force-copy", false, "suppress prompts about copying state data")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
@ -225,6 +228,10 @@ Options:
-no-color If specified, output won't contain any color.
-force-copy Suppress prompts about copying state data. This is
equivalent to providing a "yes" to all confirmation
prompts.
`
return strings.TrimSpace(helpText)
}

View File

@ -270,9 +270,6 @@ func TestInit_backendUnset(t *testing.T) {
t.Fatalf("err: %s", err)
}
// Run it again
defer testInteractiveInput(t, []string{"yes", "yes"})()
ui := new(cli.MockUi)
c := &InitCommand{
Meta: Meta{
@ -281,7 +278,7 @@ func TestInit_backendUnset(t *testing.T) {
},
}
args := []string{}
args := []string{"-force-copy"}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}

View File

@ -87,7 +87,10 @@ type Meta struct {
//
// provider is to specify specific resource providers
//
// lockState is set to false to disable state locking
// stateLock is set to false to disable state locking
//
// forceInitCopy suppresses confirmation for copying state data during
// init.
statePath string
stateOutPath string
backupPath string
@ -95,6 +98,7 @@ type Meta struct {
shadow bool
provider string
stateLock bool
forceInitCopy bool
}
// initStatePaths is used to initialize the default values for

View File

@ -684,8 +684,11 @@ func (m *Meta) backend_c_r_S(
// Get the backend type for output
backendType := s.Backend.Type
copy := m.forceInitCopy
if !copy {
var err error
// Confirm with the user that the copy should occur
copy, err := m.confirm(&terraform.InputOpts{
copy, err = m.confirm(&terraform.InputOpts{
Id: "backend-migrate-to-local",
Query: fmt.Sprintf("Do you want to copy the state from %q?", s.Backend.Type),
Description: fmt.Sprintf(
@ -695,6 +698,7 @@ func (m *Meta) backend_c_r_S(
return nil, fmt.Errorf(
"Error asking for state copy action: %s", err)
}
}
// If we're copying, perform the migration
if copy {
@ -805,7 +809,9 @@ func (m *Meta) backend_c_R_S(
s := sMgr.State()
// Ask the user if they want to migrate their existing remote state
copy, err := m.confirm(&terraform.InputOpts{
copy := m.forceInitCopy
if !copy {
copy, err = m.confirm(&terraform.InputOpts{
Id: "backend-migrate-to-new",
Query: fmt.Sprintf(
"Do you want to copy the legacy remote state from %q?",
@ -816,6 +822,7 @@ func (m *Meta) backend_c_R_S(
return nil, fmt.Errorf(
"Error asking for state copy action: %s", err)
}
}
// If the user wants a copy, copy!
if copy {
@ -898,7 +905,9 @@ func (m *Meta) backend_C_R_s(
// Finally, ask the user if they want to copy the state from
// their old remote state location.
copy, err := m.confirm(&terraform.InputOpts{
copy := m.forceInitCopy
if !copy {
copy, err = m.confirm(&terraform.InputOpts{
Id: "backend-migrate-to-new",
Query: fmt.Sprintf(
"Do you want to copy the legacy remote state from %q?",
@ -909,6 +918,7 @@ func (m *Meta) backend_C_R_s(
return nil, fmt.Errorf(
"Error asking for state copy action: %s", err)
}
}
// If the user wants a copy, copy!
if copy {
@ -1055,7 +1065,9 @@ func (m *Meta) backend_C_r_S_changed(
}
// Check with the user if we want to migrate state
copy, err := m.confirm(&terraform.InputOpts{
copy := m.forceInitCopy
if !copy {
copy, err = m.confirm(&terraform.InputOpts{
Id: "backend-migrate-to-new",
Query: fmt.Sprintf("Do you want to copy the state from %q?", c.Type),
Description: strings.TrimSpace(fmt.Sprintf(inputBackendMigrateChange, c.Type, s.Backend.Type)),
@ -1064,6 +1076,7 @@ func (m *Meta) backend_C_r_S_changed(
return nil, fmt.Errorf(
"Error asking for state copy action: %s", err)
}
}
// If we are, then we need to initialize the old backend and
// perform the copy.
@ -1198,7 +1211,9 @@ func (m *Meta) backend_C_R_S_unchanged(
}
// Ask if the user wants to move their legacy remote state
copy, err := m.confirm(&terraform.InputOpts{
copy := m.forceInitCopy
if !copy {
copy, err = m.confirm(&terraform.InputOpts{
Id: "backend-migrate-to-new",
Query: fmt.Sprintf(
"Do you want to copy the legacy remote state from %q?",
@ -1209,6 +1224,7 @@ func (m *Meta) backend_C_R_S_unchanged(
return nil, fmt.Errorf(
"Error asking for state copy action: %s", err)
}
}
// If the user wants a copy, copy!
if copy {

View File

@ -162,8 +162,11 @@ func (m *Meta) backendMigrateState_S_S(opts *backendMigrateOpts) error {
func (m *Meta) backendMigrateState_S_s(opts *backendMigrateOpts) error {
currentEnv := m.Env()
migrate := m.forceInitCopy
if !migrate {
var err error
// Ask the user if they want to migrate their existing remote state
migrate, err := m.confirm(&terraform.InputOpts{
migrate, err = m.confirm(&terraform.InputOpts{
Id: "backend-migrate-multistate-to-single",
Query: fmt.Sprintf(
"Destination state %q doesn't support environments (named states).\n"+
@ -177,6 +180,8 @@ func (m *Meta) backendMigrateState_S_s(opts *backendMigrateOpts) error {
return fmt.Errorf(
"Error asking for state migration action: %s", err)
}
}
if !migrate {
return fmt.Errorf("Migration aborted by user.")
}
@ -295,6 +300,10 @@ func (m *Meta) backendMigrateState_s_s(opts *backendMigrateOpts) error {
}
func (m *Meta) backendMigrateEmptyConfirm(one, two state.State, opts *backendMigrateOpts) (bool, error) {
if m.forceInitCopy {
return true, nil
}
inputOpts := &terraform.InputOpts{
Id: "backend-migrate-copy-to-empty",
Query: fmt.Sprintf(
@ -357,6 +366,10 @@ func (m *Meta) backendMigrateNonEmptyConfirm(
return false, fmt.Errorf("Error saving temporary state: %s", err)
}
if m.forceInitCopy {
return true, nil
}
// Ask for confirmation
inputOpts := &terraform.InputOpts{
Id: "backend-migrate-to-backend",

View File

@ -480,11 +480,10 @@ func TestMetaBackend_configureNewWithStateExisting(t *testing.T) {
defer os.RemoveAll(td)
defer testChdir(t, td)()
// Ask input
defer testInteractiveInput(t, []string{"yes"})()
// Setup the meta
m := testMetaBackend(t, nil)
// suppress input
m.forceInitCopy = true
// Get the backend
b, err := m.Backend(&BackendOpts{Init: true})
@ -722,12 +721,12 @@ func TestMetaBackend_configureNewLegacyCopy(t *testing.T) {
defer os.RemoveAll(td)
defer testChdir(t, td)()
// Ask input
defer testInteractiveInput(t, []string{"yes", "yes"})()
// Setup the meta
m := testMetaBackend(t, nil)
// suppress input
m.forceInitCopy = true
// Get the backend
b, err := m.Backend(&BackendOpts{Init: true})
if err != nil {
@ -1593,11 +1592,9 @@ func TestMetaBackend_configuredUnchangedLegacyCopy(t *testing.T) {
defer os.RemoveAll(td)
defer testChdir(t, td)()
// Ask input
defer testInteractiveInput(t, []string{"yes", "yes"})()
// Setup the meta
m := testMetaBackend(t, nil)
m.forceInitCopy = true
// Get the backend
b, err := m.Backend(&BackendOpts{Init: true})