Skip to content

Commit

Permalink
lxd/storage-volumes: Add entitlements for 'storage-volume' entities
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Mougard <[email protected]>
  • Loading branch information
gabrielmougard committed Jan 8, 2025
1 parent 92f9f13 commit 291c1f2
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lxd/storage_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,12 @@ func storagePoolVolumesGet(d *Daemon, r *http.Request) response.Response {
return response.SmartError(err)
}

// Detect if we want to also returns entitlements for each volume.
withEntitlements, err := extractEntitlementsFromQuery(r, entity.TypeStorageVolume)
if err != nil {
return response.SmartError(err)
}

// Check if current route is in /1.0/storage-volumes
allPools := poolName == ""

Expand Down Expand Up @@ -814,6 +820,7 @@ func storagePoolVolumesGet(d *Daemon, r *http.Request) response.Response {

if util.IsRecursionRequest(r) {
volumes := make([]*api.StorageVolume, 0, len(dbVolumes))
entityURLs := make([]*api.URL, 0)
for _, dbVol := range dbVolumes {
vol := &dbVol.StorageVolume

Expand All @@ -833,6 +840,18 @@ func storagePoolVolumesGet(d *Daemon, r *http.Request) response.Response {
}

volumes = append(volumes, vol)
entityURLs = append(entityURLs, entity.StorageVolumeURL(vol.Project, vol.Location, vol.Pool, vol.Type, vol.Name))
}

if len(withEntitlements) > 0 {
entitlementsMap, err := d.authorizer.GetEntitlementsToEntities(r.Context(), entity.TypeStorageVolume, entityURLs, withEntitlements)
if err != nil {
return response.SmartError(err)
}

for i, vol := range volumes {
vol.Entitlements = entitlementsMap[entityURLs[i].String()]
}
}

return response.SyncResponse(true, volumes)
Expand Down Expand Up @@ -2016,6 +2035,12 @@ func storagePoolVolumeGet(d *Daemon, r *http.Request) response.Response {
return response.SmartError(err)
}

// Detect if we want to also returns entitlements for each volume.
withEntitlements, err := extractEntitlementsFromQuery(r, entity.TypeStorageVolume)
if err != nil {
return response.SmartError(err)
}

resp := forwardedResponseIfTargetIsRemote(s, r)
if resp != nil {
return resp
Expand Down Expand Up @@ -2044,6 +2069,15 @@ func storagePoolVolumeGet(d *Daemon, r *http.Request) response.Response {

dbVolume.UsedBy = project.FilterUsedBy(s.Authorizer, r, volumeUsedBy)

if len(withEntitlements) > 0 {
entitlements, err := d.authorizer.GetEntitlements(r.Context(), entity.TypeStorageVolume, entity.StorageVolumeURL(dbVolume.Project, dbVolume.Location, dbVolume.Pool, dbVolume.Type, dbVolume.Name), withEntitlements)
if err != nil {
return response.SmartError(err)
}

dbVolume.Entitlements = entitlements
}

etag := []any{details.volumeName, dbVolume.Type, dbVolume.Config}

return response.SyncResponseETag(true, dbVolume.StorageVolume, etag)
Expand Down

0 comments on commit 291c1f2

Please sign in to comment.