Skip to content

Commit

Permalink
Redis: Improves the invoking redis api method and supports refreshing…
Browse files Browse the repository at this point in the history
… credential automatically
  • Loading branch information
xiaozhu36 committed Dec 27, 2024
1 parent 75419fa commit 2779262
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 481 deletions.
28 changes: 10 additions & 18 deletions alicloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2285,27 +2285,14 @@ func (client *AliyunClient) WithDcdnClient(do func(*dcdn.Client) (interface{}, e
}

func (client *AliyunClient) WithRKvstoreClient(do func(*r_kvstore.Client) (interface{}, error)) (interface{}, error) {
productCode := "r_kvstore"
endpoint := ""
if client.r_kvstoreConn == nil {
if endpoint == "" {
endpoint = loadEndpoint(client.config.RegionId, RKvstoreCode)
}
if endpoint == "" {
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
endpoint = "r-kvstore.aliyuncs.com"
client.config.Endpoints.Store(productCode, endpoint)
log.Printf("[ERROR] loading %s endpoint got an error: %#v. Using the endpoint %s instead.", productCode, err, endpoint)
}
}
if v, ok := client.config.Endpoints.Load(productCode); ok && v.(string) != "" {
endpoint = v.(string)
productCode := "r_kvstore"
endpoint := ""
if v, ok := client.config.Endpoints.Load(productCode); !ok || v.(string) == "" {
if err := client.loadEndpoint(productCode); err != nil {
return nil, err
}
}
if strings.HasPrefix(endpoint, "http") {
endpoint = fmt.Sprintf("https://%s", strings.TrimPrefix(endpoint, "http://"))
}
if endpoint != "" {
endpoints.AddEndpointMapping(client.config.RegionId, "r-kvstore", endpoint)
}
Expand All @@ -2323,6 +2310,11 @@ func (client *AliyunClient) WithRKvstoreClient(do func(*r_kvstore.Client) (inter
r_kvstoreConn.AppendUserAgent(Module, client.config.ConfigurationSource)
r_kvstoreConn.AppendUserAgent(TerraformTraceId, client.config.TerraformTraceId)
client.r_kvstoreConn = r_kvstoreConn
} else {
err := client.r_kvstoreConn.InitWithOptions(client.config.RegionId, client.getSdkConfig(), client.config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the Redis client: %#v", err)
}
}
return do(client.r_kvstoreConn)
}
Expand Down
11 changes: 2 additions & 9 deletions alicloud/data_source_alicloud_kvstore_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"

"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
Expand Down Expand Up @@ -437,17 +436,11 @@ func dataSourceAlicloudKvstoreInstancesRead(d *schema.ResourceData, meta interfa
}

var response map[string]interface{}
conn, err := client.NewRedisaClient()
if err != nil {
return WrapError(err)
}

var err error
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("R-kvstore", "2015-01-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
15 changes: 4 additions & 11 deletions alicloud/resource_alicloud_kvstore_audit_log_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"time"

util "github.com/alibabacloud-go/tea-utils/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"
Expand Down Expand Up @@ -53,12 +52,9 @@ func resourceAlicloudKvstoreAuditLogConfig() *schema.Resource {
func resourceAlicloudKvstoreAuditLogConfigCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
var err error
action := "ModifyAuditLogConfig"
request := make(map[string]interface{})
conn, err := client.NewRedisaClient()
if err != nil {
return WrapError(err)
}
if v, ok := d.GetOkExists("db_audit"); ok {
request["DbAudit"] = v
}
Expand All @@ -70,7 +66,7 @@ func resourceAlicloudKvstoreAuditLogConfigCreate(d *schema.ResourceData, meta in
}
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-01"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("R-kvstore", "2015-01-01", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -123,11 +119,8 @@ func resourceAlicloudKvstoreAuditLogConfigRead(d *schema.ResourceData, meta inte
func resourceAlicloudKvstoreAuditLogConfigUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
rKvstoreService := RKvstoreService{client}
conn, err := client.NewRedisaClient()
if err != nil {
return WrapError(err)
}
var response map[string]interface{}
var err error
update := false
request := map[string]interface{}{
"InstanceId": d.Id(),
Expand All @@ -150,7 +143,7 @@ func resourceAlicloudKvstoreAuditLogConfigUpdate(d *schema.ResourceData, meta in
action := "ModifyAuditLogConfig"
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-01"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("R-kvstore", "2015-01-01", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
Loading

0 comments on commit 2779262

Please sign in to comment.