github.com/minio/madmin-go@v1.7.5/quota-commands.go (about) 1 // 2 // MinIO Object Storage (c) 2021 MinIO, Inc. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 package madmin 18 19 import ( 20 "context" 21 "encoding/json" 22 "io/ioutil" 23 "net/http" 24 "net/url" 25 ) 26 27 // QuotaType represents bucket quota type 28 type QuotaType string 29 30 const ( 31 // HardQuota specifies a hard quota of usage for bucket 32 HardQuota QuotaType = "hard" 33 ) 34 35 // IsValid returns true if quota type is one of Hard 36 func (t QuotaType) IsValid() bool { 37 return t == HardQuota 38 } 39 40 // BucketQuota holds bucket quota restrictions 41 type BucketQuota struct { 42 Quota uint64 `json:"quota"` 43 Type QuotaType `json:"quotatype,omitempty"` 44 } 45 46 // IsValid returns false if quota is invalid 47 // empty quota when Quota == 0 is always true. 48 func (q BucketQuota) IsValid() bool { 49 if q.Quota > 0 { 50 return q.Type.IsValid() 51 } 52 // Empty configs are valid. 53 return true 54 } 55 56 // GetBucketQuota - get info on a user 57 func (adm *AdminClient) GetBucketQuota(ctx context.Context, bucket string) (q BucketQuota, err error) { 58 queryValues := url.Values{} 59 queryValues.Set("bucket", bucket) 60 61 reqData := requestData{ 62 relPath: adminAPIPrefix + "/get-bucket-quota", 63 queryValues: queryValues, 64 } 65 66 // Execute GET on /minio/admin/v3/get-quota 67 resp, err := adm.executeMethod(ctx, http.MethodGet, reqData) 68 69 defer closeResponse(resp) 70 if err != nil { 71 return q, err 72 } 73 74 if resp.StatusCode != http.StatusOK { 75 return q, httpRespToErrorResponse(resp) 76 } 77 78 b, err := ioutil.ReadAll(resp.Body) 79 if err != nil { 80 return q, err 81 } 82 if err = json.Unmarshal(b, &q); err != nil { 83 return q, err 84 } 85 86 return q, nil 87 } 88 89 // SetBucketQuota - sets a bucket's quota, if quota is set to '0' 90 // quota is disabled. 91 func (adm *AdminClient) SetBucketQuota(ctx context.Context, bucket string, quota *BucketQuota) error { 92 data, err := json.Marshal(quota) 93 if err != nil { 94 return err 95 } 96 97 queryValues := url.Values{} 98 queryValues.Set("bucket", bucket) 99 100 reqData := requestData{ 101 relPath: adminAPIPrefix + "/set-bucket-quota", 102 queryValues: queryValues, 103 content: data, 104 } 105 106 // Execute PUT on /minio/admin/v3/set-bucket-quota to set quota for a bucket. 107 resp, err := adm.executeMethod(ctx, http.MethodPut, reqData) 108 109 defer closeResponse(resp) 110 if err != nil { 111 return err 112 } 113 114 if resp.StatusCode != http.StatusOK { 115 return httpRespToErrorResponse(resp) 116 } 117 118 return nil 119 }