From 84234afc1c36dae28a8ea5c3b52c86e64e3cff7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Ga=C5=82uszka?= Date: Mon, 12 Oct 2020 13:32:57 +0200 Subject: [PATCH] add Obs bucket quota into resource (#579) --- docs/resources/obs_bucket.md | 3 ++ .../resource_huaweicloud_obs_bucket.go | 51 ++++++++++++++++++- .../resource_huaweicloud_obs_bucket_test.go | 31 +++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) diff --git a/docs/resources/obs_bucket.md b/docs/resources/obs_bucket.md index 4fe6fef4d2..cebb38d5a4 100644 --- a/docs/resources/obs_bucket.md +++ b/docs/resources/obs_bucket.md @@ -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 263 – 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). diff --git a/huaweicloud/resource_huaweicloud_obs_bucket.go b/huaweicloud/resource_huaweicloud_obs_bucket.go index bd1ca645f9..a1af733f1e 100644 --- a/huaweicloud/resource_huaweicloud_obs_bucket.go +++ b/huaweicloud/resource_huaweicloud_obs_bucket.go @@ -85,6 +85,13 @@ func resourceObsBucket() *schema.Resource { }, }, + "quota": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + ValidateFunc: validation.IntAtLeast(0), + }, + "lifecycle_rule": { Type: schema.TypeList, Optional: true, @@ -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 @@ -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 @@ -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 @@ -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{}) @@ -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) diff --git a/huaweicloud/resource_huaweicloud_obs_bucket_test.go b/huaweicloud/resource_huaweicloud_obs_bucket_test.go index 40f6c1543f..05993c47fb 100644 --- a/huaweicloud/resource_huaweicloud_obs_bucket_test.go +++ b/huaweicloud/resource_huaweicloud_obs_bucket_test.go @@ -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" @@ -380,6 +401,16 @@ resource "huaweicloud_obs_bucket" "bucket" { `, randInt, randInt) } +func testAccObsBucketConfigWithQuota(randInt int) string { + 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" {