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

resource/alicloud_oss_bucket_logging: add wait for status while create and update opertion. #8053

Merged
merged 1 commit into from
Dec 27, 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
41 changes: 29 additions & 12 deletions alicloud/resource_alicloud_oss_bucket_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package alicloud
import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
"log"
"time"

util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"time"
)

func resourceAliCloudOssBucketLogging() *schema.Resource {
Expand Down Expand Up @@ -77,24 +78,29 @@ func resourceAliCloudOssBucketLoggingCreate(d *schema.ResourceData, meta interfa
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.Execute(genXmlParam("PutBucketLogging", "PUT", "2019-05-17", action), &openapi.OpenApiRequest{Query: query, Body: body, HostMap: hostMap}, &util.RuntimeOptions{})

if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
addDebug(action, response, request)

if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_oss_bucket_logging", action, AlibabaCloudSdkGoERROR)
}

d.SetId(fmt.Sprint(*hostMap["bucket"]))

ossServiceV2 := OssServiceV2{client}
stateConf := BuildStateConf([]string{}, []string{fmt.Sprint(d.Get("target_bucket"))}, d.Timeout(schema.TimeoutCreate), 0, ossServiceV2.OssBucketLoggingStateRefreshFunc(d.Id(), "TargetBucket", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}

return resourceAliCloudOssBucketLoggingRead(d, meta)
}

Expand All @@ -112,8 +118,12 @@ func resourceAliCloudOssBucketLoggingRead(d *schema.ResourceData, meta interface
return WrapError(err)
}

d.Set("target_bucket", objectRaw["TargetBucket"])
d.Set("target_prefix", objectRaw["TargetPrefix"])
if objectRaw["TargetBucket"] != nil {
d.Set("target_bucket", objectRaw["TargetBucket"])
}
if objectRaw["TargetPrefix"] != nil {
d.Set("target_prefix", objectRaw["TargetPrefix"])
}

d.Set("bucket", d.Id())

Expand All @@ -127,6 +137,7 @@ func resourceAliCloudOssBucketLoggingUpdate(d *schema.ResourceData, meta interfa
var query map[string]*string
var body map[string]interface{}
update := false

action := fmt.Sprintf("/?logging")
conn, err := client.NewOssClient()
if err != nil {
Expand All @@ -137,6 +148,7 @@ func resourceAliCloudOssBucketLoggingUpdate(d *schema.ResourceData, meta interfa
body = make(map[string]interface{})
hostMap := make(map[string]*string)
hostMap["bucket"] = StringPointer(d.Id())

objectDataLocalMap := make(map[string]interface{})
loggingEnabled := make(map[string]interface{})
if d.HasChange("target_bucket") {
Expand All @@ -160,20 +172,24 @@ func resourceAliCloudOssBucketLoggingUpdate(d *schema.ResourceData, meta interfa
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.Execute(genXmlParam("PutBucketLogging", "PUT", "2019-05-17", action), &openapi.OpenApiRequest{Query: query, Body: body, HostMap: hostMap}, &util.RuntimeOptions{})

if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
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("target_bucket"))}, d.Timeout(schema.TimeoutUpdate), 0, ossServiceV2.OssBucketLoggingStateRefreshFunc(d.Id(), "TargetBucket", []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
}

return resourceAliCloudOssBucketLoggingRead(d, meta)
Expand All @@ -186,7 +202,6 @@ func resourceAliCloudOssBucketLoggingDelete(d *schema.ResourceData, meta interfa
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]*string)
body := make(map[string]interface{})
hostMap := make(map[string]*string)
conn, err := client.NewOssClient()
if err != nil {
Expand All @@ -195,12 +210,11 @@ func resourceAliCloudOssBucketLoggingDelete(d *schema.ResourceData, meta interfa
request = make(map[string]interface{})
hostMap["bucket"] = StringPointer(d.Id())

body = request
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.Execute(genXmlParam("DeleteBucketLogging", "DELETE", "2019-05-17", action), &openapi.OpenApiRequest{Query: query, Body: body, HostMap: hostMap}, &util.RuntimeOptions{})
response, err = conn.Execute(genXmlParam("DeleteBucketLogging", "DELETE", "2019-05-17", action), &openapi.OpenApiRequest{Query: query, Body: nil, HostMap: hostMap}, &util.RuntimeOptions{})

if err != nil {
if NeedRetry(err) {
Expand All @@ -209,11 +223,14 @@ func resourceAliCloudOssBucketLoggingDelete(d *schema.ResourceData, meta interfa
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
addDebug(action, response, request)

if err != nil {
if NotFoundError(err) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}

Expand Down
17 changes: 14 additions & 3 deletions alicloud/service_alicloud_oss_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,18 +778,22 @@ func (s *OssServiceV2) DescribeOssBucketLogging(id string) (object map[string]in
}
return resource.NonRetryableError(err)
}
addDebug(action, response, request)
return nil
})
addDebug(action, response, request)
if err != nil {
addDebug(action, response, request)
return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR)
}
response = response["body"].(map[string]interface{})

v, err := jsonpath.Get("$.BucketLoggingStatus.LoggingEnabled", response)
if err != nil {
return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.BucketLoggingStatus.LoggingEnabled", response)
return object, WrapErrorf(Error(GetNotFoundMessage("BucketLogging", id)), NotFoundMsg, response)
}

currentStatus := v.(map[string]interface{})["TargetBucket"]
if currentStatus == nil {
return object, WrapErrorf(Error(GetNotFoundMessage("BucketLogging", id)), NotFoundMsg, response)
}

return v.(map[string]interface{}), nil
Expand All @@ -808,6 +812,13 @@ func (s *OssServiceV2) OssBucketLoggingStateRefreshFunc(id string, field string,
v, err := jsonpath.Get(field, object)
currentStatus := fmt.Sprint(v)

if strings.HasPrefix(field, "#") {
v, _ := jsonpath.Get(strings.TrimPrefix(field, "#"), object)
if v != nil {
currentStatus = "#CHECKSET"
}
}

for _, failState := range failStates {
if currentStatus == failState {
return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus))
Expand Down
Loading