cloud: Automatically select renamed current workspace

After migrating to TFC with renamed workspaces, automatically select
what was the previous current workspace on behalf of the user. We don't
need to make the user reselect.
This commit is contained in:
Chris Arcand 2021-11-03 10:59:09 -05:00
parent f80b2177c8
commit c3bf5d4512
1 changed files with 29 additions and 3 deletions

View File

@ -613,6 +613,12 @@ func (m *Meta) backendMigrateTFC(opts *backendMigrateOpts) error {
func (m *Meta) backendMigrateState_S_TFC(opts *backendMigrateOpts, sourceWorkspaces []string) error {
log.Print("[TRACE] backendMigrateState: migrating all named workspaces")
currentWorkspace, err := m.Workspace()
if err != nil {
return err
}
newCurrentWorkspace := ""
// This map is used later when doing the migration per source/destination.
// If a source has 'default', then we ask what the new name should be.
// And further down when we actually run state migration for each
@ -653,15 +659,35 @@ func (m *Meta) backendMigrateState_S_TFC(opts *backendMigrateOpts, sourceWorkspa
return fmt.Errorf(strings.TrimSpace(
errMigrateMulti), name, opts.SourceType, opts.DestinationType, err)
}
if currentWorkspace == opts.sourceWorkspace {
newCurrentWorkspace = opts.destinationWorkspace
}
}
// After migrating multiple workspaces, we want to ensure that a workspace is
// set or we prompt the user to set a workspace.
err = m.selectWorkspace(opts.Destination)
// After migrating multiple workspaces, we need to reselect the current workspace as it may
// have been renamed. Query the backend first to be sure it now exists, and if it does,
// select it.
workspaces, err := opts.Destination.Workspaces()
if err != nil {
return err
}
for _, name := range workspaces {
if name == newCurrentWorkspace {
if err = m.SetWorkspace(name); err != nil {
return err
}
return nil
}
}
// If we couldn't select the workspace automatically from the backend (maybe it was empty
// and wasn't migrated, for instance), ask the user to select one.
if err = m.selectWorkspace(opts.Destination); err != nil {
return err
}
return nil
}