Skip to content

Commit

Permalink
lxd/api/internal: Remove force argument from internalImportFromBackup
Browse files Browse the repository at this point in the history
No longer needed.

Signed-off-by: Thomas Parrott <thomas.parrott@canonical.com>
  • Loading branch information
tomponline committed May 23, 2023
1 parent 84fe689 commit 14a5882
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions lxd/api_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -583,7 +582,7 @@ func internalSQLExec(tx *sql.Tx, query string, result *internalSQLResult) error

// internalImportFromBackup creates instance, storage pool and volume DB records from an instance's backup file.
// It expects the instance volume to be mounted so that the backup.yaml file is readable.
func internalImportFromBackup(s *state.State, projectName string, instName string, force bool, allowNameOverride bool) error {
func internalImportFromBackup(s *state.State, projectName string, instName string, allowNameOverride bool) error {
if instName == "" {
return fmt.Errorf("The name of the instance is required")
}
Expand Down Expand Up @@ -694,14 +693,10 @@ func internalImportFromBackup(s *state.State, projectName string, instName strin
return fmt.Errorf(`The storage pool's %q driver %q conflicts with the driver %q recorded in the instance's backup file`, instancePoolName, pool.Driver().Info().Name, backupConf.Pool.Driver)
}

// Check snapshots are consistent, and if not, if req.Force is true, then delete snapshots that do not exist in backup.yaml.
existingSnapshots, err := pool.CheckInstanceBackupFileSnapshots(backupConf, projectName, force, nil)
// Check snapshots are consistent.
existingSnapshots, err := pool.CheckInstanceBackupFileSnapshots(backupConf, projectName, false, nil)
if err != nil {
if errors.Is(err, storagePools.ErrBackupSnapshotsMismatch) {
return fmt.Errorf(`%s. Set "force" to discard non-existing snapshots`, err)
}

return fmt.Errorf("Checking snapshots: %w", err)
return fmt.Errorf("Failed checking snapshots: %w", err)
}

// Check if a storage volume entry for the instance already exists.
Expand All @@ -718,20 +713,18 @@ func internalImportFromBackup(s *state.State, projectName string, instName strin
return err
}

// If a storage volume entry exists only proceed if force was specified.
if dbVolume != nil && !force {
return fmt.Errorf(`Storage volume for instance %q already exists in the database. Set "force" to overwrite`, backupConf.Container.Name)
if dbVolume != nil {
return fmt.Errorf(`Storage volume for instance %q already exists in the database`, backupConf.Container.Name)
}

// Check if an entry for the instance already exists in the db.
_, instanceErr := s.DB.Cluster.GetInstanceID(projectName, backupConf.Container.Name)
if instanceErr != nil && !response.IsNotFoundError(instanceErr) {
return instanceErr
_, err = s.DB.Cluster.GetInstanceID(projectName, backupConf.Container.Name)
if err != nil && !response.IsNotFoundError(err) {
return err
}

// If a db entry exists only proceed if force was specified.
if instanceErr == nil && !force {
return fmt.Errorf(`Entry for instance %q already exists in the database. Set "force" to overwrite`, backupConf.Container.Name)
if err == nil {
return fmt.Errorf(`Entry for instance %q already exists in the database`, backupConf.Container.Name)
}

if backupConf.Volume == nil {
Expand All @@ -754,14 +747,6 @@ func internalImportFromBackup(s *state.State, projectName string, instName strin
}
}

if instanceErr == nil {
// Remove the storage volume db entry for the instance since force was specified.
err := s.DB.Cluster.DeleteInstance(projectName, backupConf.Container.Name)
if err != nil {
return err
}
}

profiles, err := s.DB.Cluster.GetProfiles(projectName, backupConf.Container.Profiles)
if err != nil {
return fmt.Errorf("Failed loading profiles for instance: %w", err)
Expand Down Expand Up @@ -818,9 +803,8 @@ func internalImportFromBackup(s *state.State, projectName string, instName strin
return snapErr
}

// If a db entry exists only proceed if force was specified.
if snapErr == nil && !force {
return fmt.Errorf(`Entry for snapshot %q already exists in the database. Set "force" to overwrite`, snapInstName)
if snapErr == nil {
return fmt.Errorf(`Entry for snapshot %q already exists in the database`, snapInstName)
}

// Check if a storage volume entry for the snapshot already exists.
Expand All @@ -838,8 +822,8 @@ func internalImportFromBackup(s *state.State, projectName string, instName strin
}

// If a storage volume entry exists only proceed if force was specified.
if dbVolume != nil && !force {
return fmt.Errorf(`Storage volume for snapshot %q already exists in the database. Set "force" to overwrite`, snapInstName)
if dbVolume != nil {
return fmt.Errorf(`Storage volume for snapshot %q already exists in the database`, snapInstName)
}

if snapErr == nil {
Expand Down

0 comments on commit 14a5882

Please sign in to comment.