Skip to content

Commit

Permalink
set quota configuration on bucket
Browse files Browse the repository at this point in the history
Added command line parameters
1. MaxSize
2. MaxObjects
to noobaa command to set/update quota on a bucket

Signed-off-by: Vinayakswami Hariharmath <[email protected]>
(cherry picked from commit b18c8eb)
  • Loading branch information
vh05 authored and dannyzaken committed Jan 9, 2024
1 parent b0ad020 commit 6dfa9ed
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions pkg/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bucket

import (
"fmt"
"strconv"

"github.com/noobaa/noobaa-operator/v5/pkg/bucketclass"
"github.com/noobaa/noobaa-operator/v5/pkg/nb"
Expand Down Expand Up @@ -36,6 +37,10 @@ func CmdCreate() *cobra.Command {
Run: RunCreate,
}
cmd.Flags().Bool("force_md5_etag", false, "This flag enables md5 etag calculation for bucket")
cmd.Flags().String("max-objects", "",
"Set quota max objects quantity config to requested bucket")
cmd.Flags().String("max-size", "",
"Set quota max size config to requested bucket")
return cmd
}

Expand All @@ -47,6 +52,10 @@ func CmdUpdate() *cobra.Command {
Run: RunUpdate,
}
cmd.Flags().Bool("force_md5_etag", false, "This flag enables md5 etag calculation for bucket")
cmd.Flags().String("max-objects", "",
"Set quota max objects quantity config to requested bucket")
cmd.Flags().String("max-size", "",
"Set quota max size config to requested bucket")
return cmd
}

Expand Down Expand Up @@ -101,11 +110,21 @@ func RunCreate(cmd *cobra.Command, args []string) {
}

forceMd5Etag, _ := cmd.Flags().GetBool("force_md5_etag")
maxSize, _ := cmd.Flags().GetString("max-size")
maxObjects, _ := cmd.Flags().GetString("max-objects")

err = nbClient.CreateBucketAPI(nb.CreateBucketParams{Name: bucketName, Tiering: tierName, ForceMd5Etag: forceMd5Etag})
if err != nil {
log.Fatal(err)
}

quota, updateQuota := prepareQuotaConfig(maxSize, maxObjects)
if updateQuota {
err = nbClient.UpdateBucketAPI(nb.CreateBucketParams{Name: bucketName, Quota: &quota})
if err != nil {
log.Fatal(err)
}
}
}

// RunUpdate runs a CLI command
Expand All @@ -116,10 +135,17 @@ func RunUpdate(cmd *cobra.Command, args []string) {
}
bucketName := args[0]
nbClient := system.GetNBClient()

forceMd5Etag, _ := cmd.Flags().GetBool("force_md5_etag")
maxSize, _ := cmd.Flags().GetString("max-size")
maxObjects, _ := cmd.Flags().GetString("max-objects")

err := nbClient.UpdateBucketAPI(nb.CreateBucketParams{Name: bucketName, ForceMd5Etag: forceMd5Etag})
quota, updateQuota := prepareQuotaConfig(maxSize, maxObjects)
var err error
if updateQuota {
err = nbClient.UpdateBucketAPI(nb.CreateBucketParams{Name: bucketName, ForceMd5Etag: forceMd5Etag, Quota: &quota})
} else {
err = nbClient.UpdateBucketAPI(nb.CreateBucketParams{Name: bucketName, ForceMd5Etag: forceMd5Etag})
}
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -212,3 +238,26 @@ func RunList(cmd *cobra.Command, args []string) {
fmt.Print(table.String())
fmt.Printf("\n")
}

func prepareQuotaConfig(maxSize string, maxObjects string) (nb.QuotaConfig, bool) {
var bucketMaxSize, bucketMaxObjects int64
updateQuota := false

if len(maxSize) > 0 {
bucketMaxSize, _ = strconv.ParseInt(maxSize, 10, 64)
updateQuota = true
}
if len(maxObjects) > 0 {
bucketMaxObjects, _ = strconv.ParseInt(maxObjects, 10, 64)
updateQuota = true
}
quota := nb.QuotaConfig{}
if bucketMaxSize > 0 {
f, u := nb.GetBytesAndUnits(bucketMaxSize, 2)
quota.Size = &nb.SizeQuotaConfig{Value: f, Unit: u}
}
if bucketMaxObjects > 0 {
quota.Quantity = &nb.QuantityQuotaConfig{Value: int(bucketMaxObjects)}
}
return quota, updateQuota
}

0 comments on commit 6dfa9ed

Please sign in to comment.