RenameCustomVolume renames a custom volume and its snapshots.
(projectName string, volName string, newVolName string, op *operations.Operation)
| 5522 | |
| 5523 | // RenameCustomVolume renames a custom volume and its snapshots. |
| 5524 | func (b *backend) RenameCustomVolume(projectName string, volName string, newVolName string, op *operations.Operation) error { |
| 5525 | l := b.logger.AddContext(logger.Ctx{"project": projectName, "volName": volName, "newVolName": newVolName}) |
| 5526 | l.Debug("RenameCustomVolume started") |
| 5527 | defer l.Debug("RenameCustomVolume finished") |
| 5528 | |
| 5529 | if internalInstance.IsSnapshot(volName) { |
| 5530 | return errors.New("Volume name cannot be a snapshot") |
| 5531 | } |
| 5532 | |
| 5533 | if internalInstance.IsSnapshot(newVolName) { |
| 5534 | return errors.New("New volume name cannot be a snapshot") |
| 5535 | } |
| 5536 | |
| 5537 | reverter := revert.New() |
| 5538 | defer reverter.Fail() |
| 5539 | |
| 5540 | volume, err := VolumeDBGet(b, projectName, volName, drivers.VolumeTypeCustom) |
| 5541 | if err != nil { |
| 5542 | return err |
| 5543 | } |
| 5544 | |
| 5545 | // Rename each snapshot to have the new parent volume prefix. |
| 5546 | snapshots, err := VolumeDBSnapshotsGet(b, projectName, volName, drivers.VolumeTypeCustom) |
| 5547 | if err != nil { |
| 5548 | return err |
| 5549 | } |
| 5550 | |
| 5551 | for _, srcSnapshot := range snapshots { |
| 5552 | _, snapName, _ := api.GetParentAndSnapshotName(srcSnapshot.Name) |
| 5553 | newSnapVolName := drivers.GetSnapshotVolumeName(newVolName, snapName) |
| 5554 | |
| 5555 | err = b.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error { |
| 5556 | return tx.RenameStoragePoolVolume(ctx, projectName, srcSnapshot.Name, newSnapVolName, db.StoragePoolVolumeTypeCustom, b.ID()) |
| 5557 | }) |
| 5558 | if err != nil { |
| 5559 | return err |
| 5560 | } |
| 5561 | |
| 5562 | reverter.Add(func() { |
| 5563 | _ = b.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error { |
| 5564 | return tx.RenameStoragePoolVolume(ctx, projectName, newSnapVolName, srcSnapshot.Name, db.StoragePoolVolumeTypeCustom, b.ID()) |
| 5565 | }) |
| 5566 | }) |
| 5567 | } |
| 5568 | |
| 5569 | var backups []db.StoragePoolVolumeBackup |
| 5570 | |
| 5571 | // Rename each backup to have the new parent volume prefix. |
| 5572 | err = b.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error { |
| 5573 | var err error |
| 5574 | backups, err = tx.GetStoragePoolVolumeBackups(ctx, projectName, volName, b.ID()) |
| 5575 | return err |
| 5576 | }) |
| 5577 | if err != nil { |
| 5578 | return err |
| 5579 | } |
| 5580 | |
| 5581 | for _, br := range backups { |
nothing calls this directly
no test coverage detected