Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Obs bucket quota into resource #579

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/resources/obs_bucket.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ The following arguments are supported:
You can, however, suspend versioning on that bucket.

* `logging` - (Optional) A settings of bucket logging (documented below).

* `quota` - (Optional) Specifies bucket storage quota. Must be a positive integer in the unit of byte. The maximum storage quota is 2<sup>63</sup> – 1 bytes. The default bucket storage quota is 0, indicating that the bucket storage quota is not limited.

* `website` - (Optional) A website object (documented below).
* `cors_rule` - (Optional) A rule of Cross-Origin Resource Sharing (documented below).
* `lifecycle_rule` - (Optional) A configuration of object lifecycle management (documented below).
Expand Down
51 changes: 50 additions & 1 deletion huaweicloud/resource_huaweicloud_obs_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ func resourceObsBucket() *schema.Resource {
},
},

"quota": {
galuszkak marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeInt,
Optional: true,
Default: 0,
ValidateFunc: validation.IntAtLeast(0),
},

"lifecycle_rule": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -348,6 +355,12 @@ func resourceObsBucketUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

if d.HasChange("quota") {
if err := resourceObsBucketQuotaUpdate(obsClient, d); err != nil {
return err
}
}

if d.HasChange("lifecycle_rule") {
if err := resourceObsBucketLifecycleUpdate(obsClient, d); err != nil {
return err
Expand Down Expand Up @@ -406,11 +419,17 @@ func resourceObsBucketRead(d *schema.ResourceData, meta interface{}) error {
if err := setObsBucketVersioning(obsClient, d); err != nil {
return err
}

// Read the logging configuration
if err := setObsBucketLogging(obsClient, d); err != nil {
return err
}

// Read the quota
if err := setObsBucketQuota(obsClient, d); err != nil {
return err
}

// Read the Lifecycle configuration
if err := setObsBucketLifecycleConfiguration(obsClient, d); err != nil {
return err
Expand Down Expand Up @@ -576,7 +595,7 @@ func resourceObsBucketVersioningUpdate(obsClient *obs.ObsClient, d *schema.Resou

_, err := obsClient.SetBucketVersioning(input)
if err != nil {
return getObsError("Error setting versining status of OBS bucket", bucket, err)
return getObsError("Error setting versioning status of OBS bucket", bucket, err)
}

return nil
Expand Down Expand Up @@ -608,6 +627,22 @@ func resourceObsBucketLoggingUpdate(obsClient *obs.ObsClient, d *schema.Resource
return nil
}

func resourceObsBucketQuotaUpdate(obsClient *obs.ObsClient, d *schema.ResourceData) error {
bucket := d.Get("bucket").(string)
quota := d.Get("quota").(int)
quotaInput := &obs.SetBucketQuotaInput{}
quotaInput.Bucket = bucket
quotaInput.BucketQuota.Quota = int64(quota)

_, err := obsClient.SetBucketQuota(quotaInput)
if err != nil {
return getObsError("Error setting quota of OBS bucket", bucket, err)
}

return nil

}

func resourceObsBucketLifecycleUpdate(obsClient *obs.ObsClient, d *schema.ResourceData) error {
bucket := d.Get("bucket").(string)
lifecycleRules := d.Get("lifecycle_rule").([]interface{})
Expand Down Expand Up @@ -940,6 +975,20 @@ func setObsBucketLogging(obsClient *obs.ObsClient, d *schema.ResourceData) error
return nil
}

func setObsBucketQuota(obsClient *obs.ObsClient, d *schema.ResourceData) error {
bucket := d.Id()
output, err := obsClient.GetBucketQuota(bucket)
if err != nil {
return getObsError("Error getting quota of OBS bucket", bucket, err)
}

log.Printf("[DEBUG] getting quota of OBS bucket %s: %d", bucket, output.Quota)

d.Set("quota", output.Quota)

return nil
}

func setObsBucketLifecycleConfiguration(obsClient *obs.ObsClient, d *schema.ResourceData) error {
bucket := d.Id()
output, err := obsClient.GetBucketLifecycleConfiguration(bucket)
Expand Down
31 changes: 31 additions & 0 deletions huaweicloud/resource_huaweicloud_obs_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ func TestAccObsBucket_logging(t *testing.T) {
})
}

func TestAccObsBucket_quota(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "huaweicloud_obs_bucket.bucket"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckS3(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckObsBucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccObsBucketConfigWithQuota(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckObsBucketExists(resourceName),
resource.TestCheckResourceAttr(
resourceName, "quota", "1000000000"),
),
},
},
})
}

func TestAccObsBucket_lifecycle(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "huaweicloud_obs_bucket.bucket"
Expand Down Expand Up @@ -380,6 +401,16 @@ resource "huaweicloud_obs_bucket" "bucket" {
`, randInt, randInt)
}

func testAccObsBucketConfigWithQuota(randInt int) string {
galuszkak marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf(`
resource "huaweicloud_obs_bucket" "bucket" {
bucket = "tf-test-bucket-%d"
acl = "private"
quota = 1000000000
}
`, randInt)
}

func testAccObsBucketConfigWithLifecycle(randInt int) string {
return fmt.Sprintf(`
resource "huaweicloud_obs_bucket" "bucket" {
Expand Down