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

[Fix] OBS: Fix OBS encryption disable #2771

Merged
merged 2 commits into from
Dec 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/obs"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/env"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
Expand Down Expand Up @@ -38,19 +36,23 @@ func TestAccObsBucket_basic(t *testing.T) {
{
Config: testAccObsBucketUpdate(rInt),
Check: resource.ComposeTestCheckFunc(testAccCheckObsBucketExists(resourceName),
testUploadObjectToObsBucket(rInt),
resource.TestCheckResourceAttr(resourceName, "acl", "public-read"),
resource.TestCheckResourceAttr(resourceName, "storage_class", "WARM"),
),
},
{
Config: testAccObsBucketSSE(rInt),
Check: resource.ComposeTestCheckFunc(testAccCheckObsBucketExists(resourceName),
testUploadDeleteObjectObsBucket(rInt),
resource.TestCheckResourceAttr(resourceName, "server_side_encryption.0.kms_key_id", env.OS_KMS_ID),
resource.TestCheckResourceAttr(resourceName, "server_side_encryption.0.algorithm", "kms"),
),
},
{
Config: testAccObsBucketUpdate(rInt),
Check: resource.ComposeTestCheckFunc(testAccCheckObsBucketExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "server_side_encryption.#", "0"),
),
},
},
})
}
Expand Down Expand Up @@ -351,63 +353,6 @@ func testAccCheckObsBucketExists(n string) resource.TestCheckFunc {
}
}

func testUploadObjectToObsBucket(obsNumber int) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.NewObjectStorageClient(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf("error creating OpenTelekomCloud OBS client: %s", err)
}

objectName := tools.RandomString("test-obs-", 5)

_, err = client.PutObject(&obs.PutObjectInput{
PutObjectBasicInput: obs.PutObjectBasicInput{
ObjectOperationInput: obs.ObjectOperationInput{
Bucket: fmt.Sprintf("tf-test-bucket-%d", obsNumber),
Key: objectName,
},
},
})
if err != nil {
return fmt.Errorf("error uploading object to OBS bucket: %s", err)
}
return nil
}
}

func testUploadDeleteObjectObsBucket(obsNumber int) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.NewObjectStorageClient(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf("error creating OpenTelekomCloud OBS client: %s", err)
}

objectName := tools.RandomString("test-obs-", 5)

_, err = client.PutObject(&obs.PutObjectInput{
PutObjectBasicInput: obs.PutObjectBasicInput{
ObjectOperationInput: obs.ObjectOperationInput{
Bucket: fmt.Sprintf("tf-test-bucket-%d", obsNumber),
Key: objectName,
},
},
})
if err != nil {
return fmt.Errorf("error uploading object to OBS bucket: %s", err)
}
_, err = client.DeleteObject(&obs.DeleteObjectInput{
Bucket: fmt.Sprintf("tf-test-bucket-%d", obsNumber),
Key: objectName,
})
if err != nil {
return fmt.Errorf("error deleting object from OBS bucket: %s", err)
}
return nil
}
}

func testAccCheckObsBucketLogging(name, target, prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,11 @@ type WebsiteRoutingRule struct {
}

func resourceObsBucketEncryptionUpdate(client *obs.ObsClient, d *schema.ResourceData) error {
if d.Get("server_side_encryption.#") == 0 {
if d.Get("server_side_encryption.#") == 0 && !d.IsNewResource() {
_, err := client.DeleteBucketEncryption(d.Id())
if err != nil {
return fmt.Errorf("failed to disable default encryption of OBS bucket %s", d.Id())
}
return nil
}
_, err := client.SetBucketEncryption(&obs.SetBucketEncryptionInput{
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/obs_kms_fix-85ca84ef660a3b07.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
**[OBS]** Fix encryption disable for ``resource/opentelekomcloud_obs_bucket`` (`#2771 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2771>`_)
Loading