Skip to content

Commit

Permalink
Add ability to set bucket quota on bucket resource (#311)
Browse files Browse the repository at this point in the history
* Allows to set the bucket's quota
* Generate bucket docs
* Closes #257
  • Loading branch information
yardenshoham authored Aug 16, 2022
1 parent d269926 commit 426c0f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/resources/s3_bucket.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ output "minio_url" {
- **bucket_prefix** (String)
- **force_destroy** (Boolean)
- **id** (String) The ID of this resource.
- **quota** (Number) The limit of the amount of data in the bucket (bytes).

### Read-Only

Expand Down
40 changes: 38 additions & 2 deletions minio/resource_minio_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/minio/minio-go/v7"

"github.com/minio/madmin-go"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -62,6 +64,10 @@ func resourceMinioBucket() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"quota": {
Type: schema.TypeInt,
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -95,7 +101,7 @@ func minioCreateBucket(ctx context.Context, d *schema.ResourceData, meta interfa
if e, err := bucketConfig.MinioClient.BucketExists(ctx, bucket); err != nil {
return NewResourceError("unable to check bucket", bucket, err)
} else if e {
return NewResourceError("ucket already exists!", bucket, err)
return NewResourceError("bucket already exists!", bucket, err)
}

err := bucketConfig.MinioClient.MakeBucket(ctx, bucket, minio.MakeBucketOptions{
Expand Down Expand Up @@ -161,6 +167,22 @@ func minioUpdateBucket(ctx context.Context, d *schema.ResourceData, meta interfa
log.Printf("[DEBUG] Bucket [%s] updated!", bucketConfig.MinioBucket)
_ = d.Set("acl", bucketConfig.MinioACL)
}

if d.HasChange("quota") {
log.Printf("[DEBUG] Updating bucket, quota changed. Bucket: [%s], Region: [%s]",
bucketConfig.MinioBucket, bucketConfig.MinioRegion)

bucketQuota := madmin.BucketQuota{Quota: uint64(d.Get("quota").(int)), Type: madmin.HardQuota}

if err := minioSetBucketQuota(ctx, bucketConfig, &bucketQuota); err != nil {
log.Printf("%s", NewResourceErrorStr("unable to update bucket", bucketConfig.MinioBucket, err))
return NewResourceError("[Quota] Unable to update bucket", bucketConfig.MinioBucket, err)
}

log.Printf("[DEBUG] Bucket [%s] updated!", bucketConfig.MinioBucket)
_ = d.Set("quota", bucketQuota.Quota)
}

return minioReadBucket(ctx, d, meta)
}

Expand Down Expand Up @@ -231,7 +253,7 @@ func minioSetBucketACL(ctx context.Context, bucketConfig *S3MinioBucket) diag.Di
policyString, policyExists := defaultPolicies[bucketConfig.MinioACL]

if !policyExists {
return NewResourceError("nsupported ACL", bucketConfig.MinioACL, errors.New("(valid acl: private, public-write, public-read, public-read-write, public)"))
return NewResourceError("unsupported ACL", bucketConfig.MinioACL, errors.New("(valid acl: private, public-write, public-read, public-read-write, public)"))
}

if policyString != "" {
Expand All @@ -244,6 +266,20 @@ func minioSetBucketACL(ctx context.Context, bucketConfig *S3MinioBucket) diag.Di
return nil
}

func minioSetBucketQuota(ctx context.Context, bucketConfig *S3MinioBucket, bucketQuota *madmin.BucketQuota) diag.Diagnostics {

if !bucketQuota.IsValid() {
return NewResourceError("invalid quota", fmt.Sprint(bucketQuota.Quota), errors.New("quota must be larger than 0"))
}

if err := bucketConfig.MinioAdmin.SetBucketQuota(ctx, bucketConfig.MinioBucket, bucketQuota); err != nil {
log.Printf("%s", NewResourceErrorStr("unable to set bucket quota", bucketConfig.MinioBucket, err))
return NewResourceError("unable to set bucket quota", bucketConfig.MinioBucket, err)
}

return nil
}

func exportPolicyString(policyStruct BucketPolicy, bucketName string) string {
policyJSON, err := json.Marshal(policyStruct)
if err != nil {
Expand Down

0 comments on commit 426c0f4

Please sign in to comment.