command_s3_bucket_quota.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "io"
  8. )
  9. func init() {
  10. Commands = append(Commands, &commandS3BucketQuota{})
  11. }
  12. type commandS3BucketQuota struct {
  13. }
  14. func (c *commandS3BucketQuota) Name() string {
  15. return "s3.bucket.quota"
  16. }
  17. func (c *commandS3BucketQuota) Help() string {
  18. return `set/remove/enable/disable quota for a bucket
  19. Example:
  20. s3.bucket.quota -name=<bucket_name> -operation=set -sizeMB=1024
  21. `
  22. }
  23. func (c *commandS3BucketQuota) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  24. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  25. bucketName := bucketCommand.String("name", "", "bucket name")
  26. operationName := bucketCommand.String("op", "set", "operation name [set|remove|enable|disable]")
  27. sizeMB := bucketCommand.Int64("sizeMB", 0, "bucket quota size in MiB")
  28. if err = bucketCommand.Parse(args); err != nil {
  29. return nil
  30. }
  31. if *bucketName == "" {
  32. return fmt.Errorf("empty bucket name")
  33. }
  34. err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  35. ctx := context.Background()
  36. resp, err := client.GetFilerConfiguration(ctx, &filer_pb.GetFilerConfigurationRequest{})
  37. if err != nil {
  38. return fmt.Errorf("get filer configuration: %v", err)
  39. }
  40. filerBucketsPath := resp.DirBuckets
  41. lookupResp, err := client.LookupDirectoryEntry(ctx, &filer_pb.LookupDirectoryEntryRequest{
  42. Directory: filerBucketsPath,
  43. Name: *bucketName,
  44. })
  45. if err != nil {
  46. return fmt.Errorf("did not find bucket %s: %v", *bucketName, err)
  47. }
  48. bucketEntry := lookupResp.Entry
  49. switch *operationName {
  50. case "set":
  51. bucketEntry.Quota = *sizeMB * 1024 * 1024
  52. case "remove":
  53. bucketEntry.Quota = 0
  54. case "enable":
  55. if bucketEntry.Quota < 0 {
  56. bucketEntry.Quota = -bucketEntry.Quota
  57. }
  58. case "disable":
  59. if bucketEntry.Quota > 0 {
  60. bucketEntry.Quota = -bucketEntry.Quota
  61. }
  62. }
  63. if err := filer_pb.UpdateEntry(client, &filer_pb.UpdateEntryRequest{
  64. Directory: filerBucketsPath,
  65. Entry: bucketEntry,
  66. }); err != nil {
  67. return err
  68. }
  69. println("updated quota for bucket", *bucketName)
  70. return nil
  71. })
  72. return err
  73. }