Skip to content

Commit

Permalink
resource/alicloud_oss_bucket: Fixes apply error when field "bucket" i…
Browse files Browse the repository at this point in the history
…s omitted.

This commit fixes issue #7761. The field "bucket" is be computed, and its references are corrected.
  • Loading branch information
zcxsythenew authored and 庶幸 committed Nov 1, 2024
1 parent dca2da1 commit 86e7a1d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
12 changes: 8 additions & 4 deletions alicloud/resource_alicloud_oss_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func resourceAlicloudOssBucket() *schema.Resource {
"bucket": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: StringLenBetween(3, 63),
Default: resource.PrefixedUniqueId("tf-oss-bucket-"),
},

"acl": {
Expand Down Expand Up @@ -485,7 +485,11 @@ func resourceAlicloudOssBucket() *schema.Resource {

func resourceAlicloudOssBucketCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := map[string]string{"bucketName": d.Get("bucket").(string)}
bucketName, ok := d.Get("bucket").(string)
if !ok || bucketName == "" {
bucketName = resource.PrefixedUniqueId("tf-oss-bucket-")
}
request := map[string]string{"bucketName": bucketName}
var requestInfo *oss.Client
raw, err := client.WithOssClient(func(ossClient *oss.Client) (interface{}, error) {
requestInfo = ossClient
Expand All @@ -507,7 +511,7 @@ func resourceAlicloudOssBucketCreate(d *schema.ResourceData, meta interface{}) e
}

req := Request{
d.Get("bucket").(string),
bucketName,
oss.StorageClass(oss.StorageClassType(d.Get("storage_class").(string))),
oss.RedundancyType(oss.DataRedundancyType(d.Get("redundancy_type").(string))),
oss.ACL(oss.ACLType(d.Get("acl").(string))),
Expand Down Expand Up @@ -1715,7 +1719,7 @@ func resourceAlicloudOssBucketDelete(d *schema.ResourceData, meta interface{}) e
if IsExpectedErrors(err, []string{"BucketNotEmpty"}) {
if d.Get("force_destroy").(bool) {
raw, er := client.WithOssClient(func(ossClient *oss.Client) (interface{}, error) {
bucket, _ := ossClient.Bucket(d.Get("bucket").(string))
bucket, _ := ossClient.Bucket(d.Id())
lor, err := bucket.ListObjectVersions()
if err != nil {
return nil, WrapErrorf(err, DefaultErrorMsg, d.Id(), "ListObjectVersions", AliyunOssGoSdk)
Expand Down
43 changes: 43 additions & 0 deletions alicloud/resource_alicloud_oss_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2258,6 +2258,49 @@ func TestAccAliCloudOssBucketResourceGroup(t *testing.T) {
})
}

func TestAccAliCloudOssBucketOmitBucketName(t *testing.T) {
var v oss.GetBucketInfoResult

resourceId := "alicloud_oss_bucket.default"
ra := resourceAttrInit(resourceId, ossBucketBasicMap)

serviceFunc := func() interface{} {
return &OssService{testAccProvider.Meta().(*connectivity.AliyunClient)}
}
rc := resourceCheckInit(resourceId, &v, serviceFunc)

rac := resourceAttrCheckInit(rc, ra)

testAccCheck := rac.resourceAttrMapUpdateSet()
testAccConfig := resourceTestAccConfigFunc(resourceId, "", resourceOssBucketResourceGroupDependence)
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
// module name
IDRefreshName: resourceId,
Providers: testAccProviders,
CheckDestroy: rac.checkResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccConfig(map[string]interface{}{
"resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.groups.1.id}",
"redundancy_type": "LRS",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
"bucket": REGEXMATCH + "^tf\\-oss\\-bucket\\-.*$",
"access_monitor.#": "1",
"access_monitor.0.status": "Disabled",
"resource_group_id": CHECKSET,
"redundancy_type": "LRS",
}),
),
},
},
})
}

func resourceOssBucketConfigBasic(name string) string {
return fmt.Sprintf("")
}
Expand Down

0 comments on commit 86e7a1d

Please sign in to comment.