github.com/minio/madmin-go/v3@v3.0.51/quota-commands.go (about) 1 // 2 // Copyright (c) 2015-2022 MinIO, Inc. 3 // 4 // This file is part of MinIO Object Storage stack 5 // 6 // This program is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU Affero General Public License as 8 // published by the Free Software Foundation, either version 3 of the 9 // License, or (at your option) any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU Affero General Public License for more details. 15 // 16 // You should have received a copy of the GNU Affero General Public License 17 // along with this program. If not, see <http://www.gnu.org/licenses/>. 18 // 19 20 package madmin 21 22 import ( 23 "context" 24 "encoding/json" 25 "io" 26 "net/http" 27 "net/url" 28 ) 29 30 // QuotaType represents bucket quota type 31 type QuotaType string 32 33 const ( 34 // HardQuota specifies a hard quota of usage for bucket 35 HardQuota QuotaType = "hard" 36 ) 37 38 // IsValid returns true if quota type is one of Hard 39 func (t QuotaType) IsValid() bool { 40 return t == HardQuota 41 } 42 43 // BucketQuota holds bucket quota restrictions 44 type BucketQuota struct { 45 Quota uint64 `json:"quota"` // Deprecated Aug 2023 46 Size uint64 `json:"size"` // Indicates maximum size allowed per bucket 47 Rate uint64 `json:"rate"` // Indicates bandwidth rate allocated per bucket 48 Requests uint64 `json:"requests"` // Indicates number of requests allocated per bucket 49 Type QuotaType `json:"quotatype,omitempty"` 50 } 51 52 // IsValid returns false if quota is invalid 53 // empty quota when Quota == 0 is always true. 54 func (q BucketQuota) IsValid() bool { 55 if q.Quota > 0 { 56 return q.Type.IsValid() 57 } 58 // Empty configs are valid. 59 return true 60 } 61 62 // GetBucketQuota - get info on a user 63 func (adm *AdminClient) GetBucketQuota(ctx context.Context, bucket string) (q BucketQuota, err error) { 64 queryValues := url.Values{} 65 queryValues.Set("bucket", bucket) 66 67 reqData := requestData{ 68 relPath: adminAPIPrefix + "/get-bucket-quota", 69 queryValues: queryValues, 70 } 71 72 // Execute GET on /minio/admin/v3/get-quota 73 resp, err := adm.executeMethod(ctx, http.MethodGet, reqData) 74 75 defer closeResponse(resp) 76 if err != nil { 77 return q, err 78 } 79 80 if resp.StatusCode != http.StatusOK { 81 return q, httpRespToErrorResponse(resp) 82 } 83 84 b, err := io.ReadAll(resp.Body) 85 if err != nil { 86 return q, err 87 } 88 if err = json.Unmarshal(b, &q); err != nil { 89 return q, err 90 } 91 92 return q, nil 93 } 94 95 // SetBucketQuota - sets a bucket's quota, if quota is set to '0' 96 // quota is disabled. 97 func (adm *AdminClient) SetBucketQuota(ctx context.Context, bucket string, quota *BucketQuota) error { 98 data, err := json.Marshal(quota) 99 if err != nil { 100 return err 101 } 102 103 queryValues := url.Values{} 104 queryValues.Set("bucket", bucket) 105 106 reqData := requestData{ 107 relPath: adminAPIPrefix + "/set-bucket-quota", 108 queryValues: queryValues, 109 content: data, 110 } 111 112 // Execute PUT on /minio/admin/v3/set-bucket-quota to set quota for a bucket. 113 resp, err := adm.executeMethod(ctx, http.MethodPut, reqData) 114 115 defer closeResponse(resp) 116 if err != nil { 117 return err 118 } 119 120 if resp.StatusCode != http.StatusOK { 121 return httpRespToErrorResponse(resp) 122 } 123 124 return nil 125 }