github.com/minio/console@v1.4.1/api/user_bucket_quota.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"fmt"
    23  
    24  	"github.com/go-openapi/runtime/middleware"
    25  	"github.com/minio/console/api/operations"
    26  	bucektApi "github.com/minio/console/api/operations/bucket"
    27  
    28  	"github.com/minio/madmin-go/v3"
    29  
    30  	"github.com/minio/console/models"
    31  )
    32  
    33  func registerBucketQuotaHandlers(api *operations.ConsoleAPI) {
    34  	// set bucket quota
    35  	api.BucketSetBucketQuotaHandler = bucektApi.SetBucketQuotaHandlerFunc(func(params bucektApi.SetBucketQuotaParams, session *models.Principal) middleware.Responder {
    36  		err := setBucketQuotaResponse(session, params)
    37  		if err != nil {
    38  			return bucektApi.NewSetBucketQuotaDefault(err.Code).WithPayload(err.APIError)
    39  		}
    40  		return bucektApi.NewSetBucketQuotaOK()
    41  	})
    42  
    43  	// get bucket quota
    44  	api.BucketGetBucketQuotaHandler = bucektApi.GetBucketQuotaHandlerFunc(func(params bucektApi.GetBucketQuotaParams, session *models.Principal) middleware.Responder {
    45  		resp, err := getBucketQuotaResponse(session, params)
    46  		if err != nil {
    47  			return bucektApi.NewGetBucketQuotaDefault(err.Code).WithPayload(err.APIError)
    48  		}
    49  		return bucektApi.NewGetBucketQuotaOK().WithPayload(resp)
    50  	})
    51  }
    52  
    53  func setBucketQuotaResponse(session *models.Principal, params bucektApi.SetBucketQuotaParams) *CodedAPIError {
    54  	ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
    55  	defer cancel()
    56  	mAdmin, err := NewMinioAdminClient(params.HTTPRequest.Context(), session)
    57  	if err != nil {
    58  		return ErrorWithContext(ctx, err)
    59  	}
    60  	// create a minioClient interface implementation
    61  	// defining the client to be used
    62  	adminClient := AdminClient{Client: mAdmin}
    63  	if err := setBucketQuota(ctx, &adminClient, &params.Name, params.Body); err != nil {
    64  		return ErrorWithContext(ctx, err)
    65  	}
    66  	return nil
    67  }
    68  
    69  func setBucketQuota(ctx context.Context, ac *AdminClient, bucket *string, bucketQuota *models.SetBucketQuota) error {
    70  	if bucketQuota == nil {
    71  		return errors.New("nil bucket quota was provided")
    72  	}
    73  	if *bucketQuota.Enabled {
    74  		var quotaType madmin.QuotaType
    75  		switch bucketQuota.QuotaType {
    76  		case models.SetBucketQuotaQuotaTypeHard:
    77  			quotaType = madmin.HardQuota
    78  		default:
    79  			return fmt.Errorf("unsupported quota type %s", bucketQuota.QuotaType)
    80  		}
    81  		if err := ac.setBucketQuota(ctx, *bucket, &madmin.BucketQuota{
    82  			Quota: uint64(bucketQuota.Amount),
    83  			Type:  quotaType,
    84  		}); err != nil {
    85  			return err
    86  		}
    87  	} else {
    88  		if err := ac.Client.SetBucketQuota(ctx, *bucket, &madmin.BucketQuota{}); err != nil {
    89  			return err
    90  		}
    91  	}
    92  	return nil
    93  }
    94  
    95  func getBucketQuotaResponse(session *models.Principal, params bucektApi.GetBucketQuotaParams) (*models.BucketQuota, *CodedAPIError) {
    96  	ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
    97  	defer cancel()
    98  	mAdmin, err := NewMinioAdminClient(params.HTTPRequest.Context(), session)
    99  	if err != nil {
   100  		return nil, ErrorWithContext(ctx, err)
   101  	}
   102  
   103  	// create a minioClient interface implementation
   104  	// defining the client to be used
   105  	adminClient := AdminClient{Client: mAdmin}
   106  	quota, err := getBucketQuota(ctx, &adminClient, &params.Name)
   107  	if err != nil {
   108  		return nil, ErrorWithContext(ctx, err)
   109  	}
   110  	return quota, nil
   111  }
   112  
   113  func getBucketQuota(ctx context.Context, ac *AdminClient, bucket *string) (*models.BucketQuota, error) {
   114  	quota, err := ac.getBucketQuota(ctx, *bucket)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	return &models.BucketQuota{
   119  		Quota: int64(quota.Quota),
   120  		Type:  string(quota.Type),
   121  	}, nil
   122  }