diff --git a/alicloud/resource_alicloud_oss_bucket.go b/alicloud/resource_alicloud_oss_bucket.go index 1d8d9d22e92d..742b08bdcaf4 100644 --- a/alicloud/resource_alicloud_oss_bucket.go +++ b/alicloud/resource_alicloud_oss_bucket.go @@ -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": { @@ -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 @@ -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))), @@ -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) diff --git a/alicloud/resource_alicloud_oss_bucket_test.go b/alicloud/resource_alicloud_oss_bucket_test.go index 2ad97d146fc6..5586e99b2eb9 100644 --- a/alicloud/resource_alicloud_oss_bucket_test.go +++ b/alicloud/resource_alicloud_oss_bucket_test.go @@ -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("") }