Skip to content

Commit

Permalink
Merge pull request #12717 from gabrielmougard/fix/snapshot.pattern-st…
Browse files Browse the repository at this point in the history
…orage-volume

Apply the `snapshots.pattern` option for manual custom volume snapshot
  • Loading branch information
tomponline authored Jan 18, 2024
2 parents 3761862 + 4541c0a commit de3f21e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
45 changes: 32 additions & 13 deletions lxd/storage_volumes_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,6 @@ func storagePoolVolumeSnapshotsTypePost(d *Daemon, r *http.Request) response.Res
return response.BadRequest(err)
}

// Get a snapshot name.
if req.Name == "" {
i := s.DB.Cluster.GetNextStorageVolumeSnapshotIndex(poolName, volumeName, volumeType, "snap%d")
req.Name = fmt.Sprintf("snap%d", i)
}

// Check that this isn't a restricted volume
used, err := storagePools.VolumeUsedByDaemon(s, poolName, volumeName)
if err != nil {
Expand All @@ -187,13 +181,44 @@ func storagePoolVolumeSnapshotsTypePost(d *Daemon, r *http.Request) response.Res
return response.SmartError(err)
}

var parentDBVolume *db.StorageVolume
var parentVolumeArgs db.StorageVolumeArgs
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
// Get the parent volume so we can get the config.
parentDBVolume, err = tx.GetStoragePoolVolume(ctx, pool.ID(), projectName, volumeType, volumeName, true)
if err != nil {
return err
}

// We will need the parent volume config to determine the snapshot name.
if req.Name == "" {
parentVolumeArgs, err = tx.GetStoragePoolVolumeWithID(ctx, int(parentDBVolume.ID))
if err != nil {
return err
}
}

return nil
})
if err != nil {
return response.SmartError(err)
}

if req.Name == "" {
snapName, err := volumeDetermineNextSnapshotName(s, parentVolumeArgs, "snap%d")
if err != nil {
return response.SmartError(err)
}

req.Name = snapName
}

// Validate the snapshot name using same rule as pool name.
err = pool.ValidateName(req.Name)
if err != nil {
return response.BadRequest(err)
}

var parentDBVolume *db.StorageVolume
err = s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
// Ensure that the snapshot doesn't already exist.
snapDBVolume, err := tx.GetStoragePoolVolume(ctx, pool.ID(), projectName, volumeType, fmt.Sprintf("%s/%s", volumeName, req.Name), true)
Expand All @@ -203,12 +228,6 @@ func storagePoolVolumeSnapshotsTypePost(d *Daemon, r *http.Request) response.Res
return api.StatusErrorf(http.StatusConflict, "Snapshot %q already in use", req.Name)
}

// Get the parent volume so we can get the config.
parentDBVolume, err = tx.GetStoragePoolVolume(ctx, pool.ID(), projectName, volumeType, volumeName, true)
if err != nil {
return err
}

return nil
})
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions test/suites/storage_snapshots.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ test_storage_volume_snapshots() {
lxc storage volume show "${storage_pool}" "${storage_volume}/snap0" | grep 'name: snap0'
lxc storage volume show "${storage_pool}" "${storage_volume}/snap0" | grep 'expires_at: 0001-01-01T00:00:00Z'

# Use the 'snapshots.pattern' option to change the snapshot name
lxc storage volume set "${storage_pool}" "${storage_volume}" snapshots.pattern='test%d'
# This will create a snapshot named 'test0' and 'test1'
lxc storage volume snapshot "${storage_pool}" "${storage_volume}"
lxc storage volume snapshot "${storage_pool}" "${storage_volume}"
lxc storage volume list "${storage_pool}" | grep "${storage_volume}/test0"
lxc storage volume list "${storage_pool}" | grep "${storage_volume}/test1"
lxc storage volume rm "${storage_pool}" "${storage_volume}/test0"
lxc storage volume rm "${storage_pool}" "${storage_volume}/test1"
lxc storage volume unset "${storage_pool}" "${storage_volume}" snapshots.pattern

# edit volume snapshot description
lxc storage volume show "${storage_pool}" "${storage_volume}/snap0" | sed 's/^description:.*/description: foo/' | lxc storage volume edit "${storage_pool}" "${storage_volume}/snap0"
lxc storage volume show "${storage_pool}" "${storage_volume}/snap0" | grep -q 'description: foo'
Expand Down

0 comments on commit de3f21e

Please sign in to comment.