github.com/minio/madmin-go/v2@v2.2.1/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/ioutil" 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"` 46 Type QuotaType `json:"quotatype,omitempty"` 47 } 48 49 // IsValid returns false if quota is invalid 50 // empty quota when Quota == 0 is always true. 51 func (q BucketQuota) IsValid() bool { 52 if q.Quota > 0 { 53 return q.Type.IsValid() 54 } 55 // Empty configs are valid. 56 return true 57 } 58 59 // GetBucketQuota - get info on a user 60 func (adm *AdminClient) GetBucketQuota(ctx context.Context, bucket string) (q BucketQuota, err error) { 61 queryValues := url.Values{} 62 queryValues.Set("bucket", bucket) 63 64 reqData := requestData{ 65 relPath: adminAPIPrefix + "/get-bucket-quota", 66 queryValues: queryValues, 67 } 68 69 // Execute GET on /minio/admin/v3/get-quota 70 resp, err := adm.executeMethod(ctx, http.MethodGet, reqData) 71 72 defer closeResponse(resp) 73 if err != nil { 74 return q, err 75 } 76 77 if resp.StatusCode != http.StatusOK { 78 return q, httpRespToErrorResponse(resp) 79 } 80 81 b, err := ioutil.ReadAll(resp.Body) 82 if err != nil { 83 return q, err 84 } 85 if err = json.Unmarshal(b, &q); err != nil { 86 return q, err 87 } 88 89 return q, nil 90 } 91 92 // SetBucketQuota - sets a bucket's quota, if quota is set to '0' 93 // quota is disabled. 94 func (adm *AdminClient) SetBucketQuota(ctx context.Context, bucket string, quota *BucketQuota) error { 95 data, err := json.Marshal(quota) 96 if err != nil { 97 return err 98 } 99 100 queryValues := url.Values{} 101 queryValues.Set("bucket", bucket) 102 103 reqData := requestData{ 104 relPath: adminAPIPrefix + "/set-bucket-quota", 105 queryValues: queryValues, 106 content: data, 107 } 108 109 // Execute PUT on /minio/admin/v3/set-bucket-quota to set quota for a bucket. 110 resp, err := adm.executeMethod(ctx, http.MethodPut, reqData) 111 112 defer closeResponse(resp) 113 if err != nil { 114 return err 115 } 116 117 if resp.StatusCode != http.StatusOK { 118 return httpRespToErrorResponse(resp) 119 } 120 121 return nil 122 }