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 23, 2024
1 parent d505bad commit 052743a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 330 deletions.
5 changes: 5 additions & 0 deletions alicloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,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
78 changes: 11 additions & 67 deletions alicloud/resource_alicloud_kvstore_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"strings"
"time"

util "github.com/alibabacloud-go/tea-utils/service"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
r_kvstore "github.com/aliyun/alibaba-cloud-sdk-go/services/r-kvstore"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
Expand Down Expand Up @@ -434,12 +432,9 @@ func resourceAliCloudKvstoreInstanceCreate(d *schema.ResourceData, meta interfac
client := meta.(*connectivity.AliyunClient)
r_kvstoreService := R_kvstoreService{client}
var response map[string]interface{}
var err error
action := "CreateInstance"
request := make(map[string]interface{})
conn, err := client.NewRedisClient()
if err != nil {
return WrapError(err)
}

request["RegionId"] = client.RegionId
request["NetworkType"] = "CLASSIC"
Expand Down Expand Up @@ -587,11 +582,9 @@ func resourceAliCloudKvstoreInstanceCreate(d *schema.ResourceData, meta interfac
}
}

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutCreate)), 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 NoCodeRegexRetry(err) {
wait()
Expand Down Expand Up @@ -797,11 +790,8 @@ func resourceAliCloudKvstoreInstanceRead(d *schema.ResourceData, meta interface{
func resourceAliCloudKvstoreInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
r_kvstoreService := R_kvstoreService{client}
conn, err := client.NewRedisaClient()
if err != nil {
return WrapError(err)
}
var response map[string]interface{}
var err error
d.Partial(true)

if d.HasChange("tags") {
Expand Down Expand Up @@ -904,16 +894,9 @@ func resourceAliCloudKvstoreInstanceUpdate(d *schema.ResourceData, meta interfac

if update {
action := "TransformInstanceChargeType"
conn, err := client.NewRedisClient()
if err != nil {
return WrapError(err)
}

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutUpdate)), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-01"), StringPointer("AK"), nil, transformInstanceChargeTypeReq, &runtime)
response, err = client.RpcPost("R-kvstore", "2015-01-01", action, nil, transformInstanceChargeTypeReq, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -1441,7 +1424,7 @@ func resourceAliCloudKvstoreInstanceUpdate(d *schema.ResourceData, meta interfac
action := "ModifyInstanceTDE"
wait := incrementalWait(3*time.Second, 3*time.Second)
err := resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutUpdate)), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-01"), StringPointer("AK"), nil, modifyInstanceTDERequest, &util.RuntimeOptions{})
response, err = client.RpcPost("R-kvstore", "2015-01-01", action, nil, modifyInstanceTDERequest, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -1480,18 +1463,12 @@ func resourceAliCloudKvstoreInstanceUpdate(d *schema.ResourceData, meta interfac

if removed > 0 {
action := "DeleteShardingNode"
conn, err = client.NewRedisClient()
if err != nil {
return WrapError(err)
}
request := make(map[string]interface{})
request["InstanceId"] = d.Id()
request["ShardCount"] = removed
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*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, &runtime)
response, err = client.RpcPost("R-kvstore", "2015-01-01", action, nil, request, false)

if err != nil {
if NeedRetry(err) {
Expand Down Expand Up @@ -1522,19 +1499,13 @@ func resourceAliCloudKvstoreInstanceUpdate(d *schema.ResourceData, meta interfac

if added > 0 {
action := "AddShardingNode"
conn, err = client.NewRedisClient()
if err != nil {
return WrapError(err)
}
request := make(map[string]interface{})
request["InstanceId"] = d.Id()
request["ClientToken"] = buildClientToken(action)
request["ShardCount"] = added
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*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, &runtime)
response, err = client.RpcPost("R-kvstore", "2015-01-01", action, nil, request, true)
request["ClientToken"] = buildClientToken(action)
if err != nil {
if NeedRetry(err) {
Expand Down Expand Up @@ -1607,16 +1578,9 @@ func resourceAliCloudKvstoreInstanceUpdate(d *schema.ResourceData, meta interfac

if update {
action := "ModifyInstanceSpec"
conn, err := client.NewRedisClient()
if err != nil {
return WrapError(err)
}

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutUpdate)), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-01"), StringPointer("AK"), nil, modifySlaveReadOnlyCountReq, &runtime)
response, err = client.RpcPost("R-kvstore", "2015-01-01", action, nil, modifySlaveReadOnlyCountReq, false)
if err != nil {
if IsExpectedErrors(err, []string{"MissingRedisUsedmemoryUnsupportPerfItem", "Task.Conflict"}) || NeedRetry(err) {
wait()
Expand Down Expand Up @@ -1659,16 +1623,9 @@ func resourceAliCloudKvstoreInstanceUpdate(d *schema.ResourceData, meta interfac

if update {
action := "ModifyDBInstanceAutoUpgrade"
conn, err := client.NewRedisClient()
if err != nil {
return WrapError(err)
}

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutUpdate)), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-01"), StringPointer("AK"), nil, modifyDBInstanceAutoUpgradeReq, &runtime)
response, err = client.RpcPost("R-kvstore", "2015-01-01", action, nil, modifyDBInstanceAutoUpgradeReq, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -1702,16 +1659,9 @@ func resourceAliCloudKvstoreInstanceUpdate(d *schema.ResourceData, meta interfac

if update {
action := "ModifyInstanceBandwidth"
conn, err := client.NewRedisClient()
if err != nil {
return WrapError(err)
}

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutUpdate)), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-01"), StringPointer("AK"), nil, modifyInstanceBandwidthReq, &runtime)
response, err = client.RpcPost("R-kvstore", "2015-01-01", action, nil, modifyInstanceBandwidthReq, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -1763,10 +1713,6 @@ func resourceAliCloudKvstoreInstanceDelete(d *schema.ResourceData, meta interfac
}

var response map[string]interface{}
conn, err := client.NewRedisClient()
if err != nil {
return WrapError(err)
}
action := "DeleteInstance"
request := map[string]interface{}{
"InstanceId": d.Id(),
Expand All @@ -1775,11 +1721,9 @@ func resourceAliCloudKvstoreInstanceDelete(d *schema.ResourceData, meta interfac
if v, ok := d.GetOk("global_instance_id"); ok {
request["GlobalInstanceId"] = v.(string)
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutDelete)), 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 NoCodeRegexRetry(err) || NeedRetry(err) {
wait()
Expand Down
16 changes: 3 additions & 13 deletions alicloud/resource_alicloud_kvstore_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"testing"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"

"strings"
Expand All @@ -31,12 +29,6 @@ func testSweepKVStoreInstances(region string) error {
return fmt.Errorf("error getting Alicloud client: %s", err)
}
client := rawClient.(*connectivity.AliyunClient)

conn, err := client.NewRedisaClient()
if err != nil {
return WrapError(err)
}

prefixes := []string{
"tf-testAcc",
"tf_testAcc",
Expand All @@ -53,9 +45,7 @@ func testSweepKVStoreInstances(region string) error {
for _, instanceType := range []string{string(KVStoreRedis), string(KVStoreMemcache)} {
request["InstanceType"] = instanceType
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
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 {
log.Printf("[ERROR] Failed to retrieve VPC in service list: %s", err)
return nil
Expand Down Expand Up @@ -98,7 +88,7 @@ func testSweepKVStoreInstances(region string) error {
}
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-01"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
_, err = client.RpcPost("R-kvstore", "2015-01-01", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -117,7 +107,7 @@ func testSweepKVStoreInstances(region string) error {
}
wait = incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-01-01"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
_, 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 052743a

Please sign in to comment.