// Changing a previously saved backend.
Changing a previously saved backend.
// Changing a previously saved backend .
func (m *Meta) backend_C_r_S_changed(c *configs.Backend, cHash int, sMgr *clistate.LocalState, output bool) (backend.Backend, tfdiags.Diagnostics) {
	if output {
		m.Ui.Output(m.Colorize().Color(fmt.Sprintf(
			"[reset]%s\n\n",
			strings.TrimSpace(outputBackendReconfigure))))
	}
	s := sMgr.State()
	b, configVal, diags := m.backendInitFromConfig(c)
	if diags.HasErrors() {
		return nil, diags
	}
	if s.Backend.Type != c.Type {
		m.Ui.Output(strings.TrimSpace(fmt.Sprintf(outputBackendMigrateChange, s.Backend.Type, c.Type)))
	}
	oldB, oldBDiags := m.backend_C_r_S_unchanged(c, cHash, sMgr)
	diags = diags.Append(oldBDiags)
	if oldBDiags.HasErrors() {
		return nil, diags
	}
	err := m.backendMigrateState(&backendMigrateOpts{
		OneType: s.Backend.Type,
		TwoType: c.Type,
		One:     oldB,
		Two:     b,
	})
	if err != nil {
		diags = diags.Append(err)
		return nil, diags
	}
	if m.stateLock {
		stateLocker := clistate.NewLocker(context.Background(), m.stateLockTimeout, m.Ui, m.Colorize())
		if err := stateLocker.Lock(sMgr, "backend from plan"); err != nil {
			diags = diags.Append(fmt.Errorf("Error locking state: %s", err))
			return nil, diags
		}
		defer stateLocker.Unlock(nil)
	}
	configJSON, err := ctyjson.Marshal(configVal, b.ConfigSchema().ImpliedType())
	if err != nil {
		diags = diags.Append(fmt.Errorf("Can't serialize backend configuration as JSON: %s", err))
		return nil, diags
	}
	s = sMgr.State()
	if s == nil {
		s = terraform.NewState()
	}
	s.Backend = &terraform.BackendState{
		Type:      c.Type,
		ConfigRaw: json.RawMessage(configJSON),
		Hash:      uint64(cHash),
	}
	if err := sMgr.WriteState(s); err != nil {
		diags = diags.Append(fmt.Errorf(errBackendWriteSaved, err))
		return nil, diags
	}
	if err := sMgr.PersistState(); err != nil {
		diags = diags.Append(fmt.Errorf(errBackendWriteSaved, err))
		return nil, diags
	}
	if output {
		m.Ui.Output(m.Colorize().Color(fmt.Sprintf(
			"[reset][green]\n"+strings.TrimSpace(successBackendSet), s.Backend.Type)))
	}
	return b, diags
}
func ( m * Meta ) backend_C_r_S_changed ( c * configs . Backend , cHash int , sMgr * clistate . LocalState , output bool ) ( backend . Backend , tfdiags . Diagnostics ) { if output { m . Ui . Output ( m . Colorize ( ) . Color ( fmt . Sprintf ( " \n \n " , strings . TrimSpace ( outputBackendReconfigure ) ) ) ) } s := sMgr . State ( ) b , configVal , diags := m . backendInitFromConfig ( c ) if diags . HasErrors ( ) { return nil , diags } if s . Backend . Type != c . Type { m . Ui . Output ( strings . TrimSpace ( fmt . Sprintf ( outputBackendMigrateChange , s . Backend . Type , c . Type ) ) ) } oldB , oldBDiags := m . backend_C_r_S_unchanged ( c , cHash , sMgr ) diags = diags . Append ( oldBDiags ) if oldBDiags . HasErrors ( ) { return nil , diags } err := m . backendMigrateState ( & backendMigrateOpts { OneType : s . Backend . Type , TwoType : c . Type , One : oldB , Two : b , } ) if err != nil { diags = diags . Append ( err ) return nil , diags } if m . stateLock { stateLocker := clistate . NewLocker ( context . Background ( ) , m . stateLockTimeout , m . Ui , m . Colorize ( ) ) if err := stateLocker . Lock ( sMgr , " " ) ; err != nil { diags = diags . Append ( fmt . Errorf ( " " , err ) ) return nil , diags } defer stateLocker . Unlock ( nil ) } configJSON , err := ctyjson . Marshal ( configVal , b . ConfigSchema ( ) . ImpliedType ( ) ) if err != nil { diags = diags . Append ( fmt . Errorf ( " " , err ) ) return nil , diags } s = sMgr . State ( ) if s == nil { s = terraform . NewState ( ) } s . Backend = & terraform . BackendState { Type : c . Type , ConfigRaw : json . RawMessage ( configJSON ) , Hash : uint64 ( cHash ) , } if err := sMgr . WriteState ( s ) ; err != nil { diags = diags . Append ( fmt . Errorf ( errBackendWriteSaved , err ) ) return nil , diags } if err := sMgr . PersistState ( ) ; err != nil { diags = diags . Append ( fmt . Errorf ( errBackendWriteSaved , err ) ) return nil , diags } if output { m . Ui . Output ( m . Colorize ( ) . Color ( fmt . Sprintf ( " \n " + strings . TrimSpace ( successBackendSet ) , s . Backend . Type ) ) ) } return b , diags }
Changing a previously saved backend.
Changing a previously saved backend .
// Notify the user // Get the old state // Get the backend // no need to confuse the user if the backend types are the same // Grab the existing backend // Perform the migration // Update the backend state