command_s3_bucket_quota.go 2.3 KB

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