From 7199100d238b886f9c1b4e30fbd149ca02fa75bd Mon Sep 17 00:00:00 2001 From: chenhanzhang Date: Fri, 27 Dec 2024 10:34:04 +0800 Subject: [PATCH] resource/alicloud_oss_bucket_access_monitor: add wait for status while create and update opertion. --- ...urce_alicloud_oss_bucket_access_monitor.go | 23 ++++++++++++++++--- alicloud/service_alicloud_oss_v2.go | 6 +++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/alicloud/resource_alicloud_oss_bucket_access_monitor.go b/alicloud/resource_alicloud_oss_bucket_access_monitor.go index a4752daa7934..a3cb629d9a09 100644 --- a/alicloud/resource_alicloud_oss_bucket_access_monitor.go +++ b/alicloud/resource_alicloud_oss_bucket_access_monitor.go @@ -59,6 +59,7 @@ func resourceAliCloudOssBucketAccessMonitorCreate(d *schema.ResourceData, meta i hostMap["bucket"] = StringPointer(d.Get("bucket").(string)) objectDataLocalMap := make(map[string]interface{}) + if v := d.Get("status"); v != nil { objectDataLocalMap["Status"] = v request["AccessMonitorConfiguration"] = objectDataLocalMap @@ -77,9 +78,9 @@ func resourceAliCloudOssBucketAccessMonitorCreate(d *schema.ResourceData, meta i } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { return WrapErrorf(err, DefaultErrorMsg, "alicloud_oss_bucket_access_monitor", action, AlibabaCloudSdkGoERROR) @@ -87,6 +88,12 @@ func resourceAliCloudOssBucketAccessMonitorCreate(d *schema.ResourceData, meta i d.SetId(fmt.Sprint(*hostMap["bucket"])) + ossServiceV2 := OssServiceV2{client} + stateConf := BuildStateConf([]string{}, []string{fmt.Sprint(d.Get("status"))}, d.Timeout(schema.TimeoutCreate), 0, ossServiceV2.OssBucketAccessMonitorStateRefreshFunc(d.Id(), "Status", []string{})) + if _, err := stateConf.WaitForState(); err != nil { + return WrapErrorf(err, IdMsg, d.Id()) + } + return resourceAliCloudOssBucketAccessMonitorRead(d, meta) } @@ -104,7 +111,9 @@ func resourceAliCloudOssBucketAccessMonitorRead(d *schema.ResourceData, meta int return WrapError(err) } - d.Set("status", objectRaw["Status"]) + if objectRaw["Status"] != nil { + d.Set("status", objectRaw["Status"]) + } d.Set("bucket", d.Id()) @@ -118,6 +127,7 @@ func resourceAliCloudOssBucketAccessMonitorUpdate(d *schema.ResourceData, meta i var query map[string]*string var body map[string]interface{} update := false + action := fmt.Sprintf("/?accessmonitor") conn, err := client.NewOssClient() if err != nil { @@ -128,10 +138,12 @@ func resourceAliCloudOssBucketAccessMonitorUpdate(d *schema.ResourceData, meta i body = make(map[string]interface{}) hostMap := make(map[string]*string) hostMap["bucket"] = StringPointer(d.Id()) + if d.HasChange("status") { update = true } objectDataLocalMap := make(map[string]interface{}) + if v := d.Get("status"); v != nil { objectDataLocalMap["Status"] = d.Get("status") request["AccessMonitorConfiguration"] = objectDataLocalMap @@ -151,12 +163,17 @@ func resourceAliCloudOssBucketAccessMonitorUpdate(d *schema.ResourceData, meta i } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) } + ossServiceV2 := OssServiceV2{client} + stateConf := BuildStateConf([]string{}, []string{fmt.Sprint(d.Get("status"))}, d.Timeout(schema.TimeoutUpdate), 0, ossServiceV2.OssBucketAccessMonitorStateRefreshFunc(d.Id(), "Status", []string{})) + if _, err := stateConf.WaitForState(); err != nil { + return WrapErrorf(err, IdMsg, d.Id()) + } } return resourceAliCloudOssBucketAccessMonitorRead(d, meta) diff --git a/alicloud/service_alicloud_oss_v2.go b/alicloud/service_alicloud_oss_v2.go index 5e5978c210b5..f96a35d304be 100644 --- a/alicloud/service_alicloud_oss_v2.go +++ b/alicloud/service_alicloud_oss_v2.go @@ -705,11 +705,13 @@ func (s *OssServiceV2) DescribeOssBucketAccessMonitor(id string) (object map[str } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { - addDebug(action, response, request) + if IsExpectedErrors(err, []string{"NoSuchBucket"}) { + return object, WrapErrorf(Error(GetNotFoundMessage("BucketAccessMonitor", id)), NotFoundMsg, response) + } return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) } response = response["body"].(map[string]interface{})