storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/quota-commands.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 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  
    18  package madmin
    19  
    20  import (
    21  	"context"
    22  	"encoding/json"
    23  	"io/ioutil"
    24  	"net/http"
    25  	"net/url"
    26  )
    27  
    28  // QuotaType represents bucket quota type
    29  type QuotaType string
    30  
    31  const (
    32  	// HardQuota specifies a hard quota of usage for bucket
    33  	HardQuota QuotaType = "hard"
    34  	// FIFOQuota specifies a quota limit beyond which older files are deleted from bucket
    35  	FIFOQuota QuotaType = "fifo"
    36  )
    37  
    38  // IsValid returns true if quota type is one of FIFO or Hard
    39  func (t QuotaType) IsValid() bool {
    40  	return t == HardQuota || t == FIFOQuota
    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  }