Skip to content

Commit

Permalink
MongoDB: Improves the invoking api method and supports refreshing cre…
Browse files Browse the repository at this point in the history
…dential automatically
  • Loading branch information
xiaozhu36 committed Feb 9, 2025
1 parent e29fc03 commit fdbce62
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 521 deletions.
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_mongodb_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

"github.com/PaesslerAG/jsonpath"
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 @@ -80,15 +79,10 @@ func dataSourceAlicloudMongodbAccountsRead(d *schema.ResourceData, meta interfac
}
var objects []map[string]interface{}
var response map[string]interface{}
conn, err := client.NewDdsClient()
if err != nil {
return WrapError(err)
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
var err error
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-12-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Dbs", "2015-12-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_mongodb_serverless_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
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 @@ -268,16 +267,11 @@ func dataSourceAlicloudMongodbServerlessInstancesRead(d *schema.ResourceData, me
}
}
var response map[string]interface{}
conn, err := client.NewDdsClient()
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-12-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Dbs", "2015-12-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

"github.com/PaesslerAG/jsonpath"
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 @@ -105,15 +104,10 @@ func dataSourceAlicloudMongodbShardingNetworkPrivateAddressesRead(d *schema.Reso
role, roleOk := d.GetOk("role")
var objects []map[string]interface{}
var response map[string]interface{}
conn, err := client.NewDdsClient()
if err != nil {
return WrapError(err)
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
var err error
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-12-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Dbs", "2015-12-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"

"github.com/PaesslerAG/jsonpath"
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 @@ -106,15 +105,10 @@ func dataSourceAlicloudMongodbShardingNetworkPublicAddressesRead(d *schema.Resou
role, roleOk := d.GetOk("role")
var response map[string]interface{}
var objects []map[string]interface{}
conn, err := client.NewDdsClient()
if err != nil {
return WrapError(err)
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
var err error
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-12-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Dbs", "2015-12-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
35 changes: 6 additions & 29 deletions alicloud/resource_alicloud_mongodb_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"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 @@ -64,15 +63,12 @@ func resourceAliCloudMongodbAccount() *schema.Resource {
func resourceAliCloudMongodbAccountCreate(d *schema.ResourceData, meta interface{}) error {

client := meta.(*connectivity.AliyunClient)
var err error
if v, ok := d.GetOk("character_type"); ok && InArray(fmt.Sprint(v), []string{"db"}) {
action := "CreateAccount"
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewDdsClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
if v, ok := d.GetOk("account_name"); ok {
request["AccountName"] = v
Expand All @@ -83,11 +79,9 @@ func resourceAliCloudMongodbAccountCreate(d *schema.ResourceData, meta interface
request["RegionId"] = client.RegionId

request["AccountPassword"] = d.Get("account_password")
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-12-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Dbs", "2015-12-01", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -125,10 +119,6 @@ func resourceAliCloudMongodbAccountCreate(d *schema.ResourceData, meta interface
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewDdsClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
if v, ok := d.GetOk("account_name"); ok {
request["AccountName"] = v
Expand All @@ -142,11 +132,9 @@ func resourceAliCloudMongodbAccountCreate(d *schema.ResourceData, meta interface
if v, ok := d.GetOk("character_type"); ok {
request["CharacterType"] = v
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 5*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-12-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Dbs", "2015-12-01", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -213,15 +201,12 @@ func resourceAliCloudMongodbAccountUpdate(d *schema.ResourceData, meta interface
var request map[string]interface{}
var response map[string]interface{}
var query map[string]interface{}
var err error
update := false
d.Partial(true)

parts := strings.Split(d.Id(), ":")
action := "ResetAccountPassword"
conn, err := client.NewDdsClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["AccountName"] = parts[1]
Expand All @@ -237,11 +222,9 @@ func resourceAliCloudMongodbAccountUpdate(d *schema.ResourceData, meta interface
}

if update {
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-12-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Dbs", "2015-12-01", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -264,10 +247,6 @@ func resourceAliCloudMongodbAccountUpdate(d *schema.ResourceData, meta interface
update = false
parts = strings.Split(d.Id(), ":")
action = "ModifyAccountDescription"
conn, err = client.NewDdsClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["AccountName"] = parts[1]
Expand All @@ -278,11 +257,9 @@ func resourceAliCloudMongodbAccountUpdate(d *schema.ResourceData, meta interface
}
request["AccountDescription"] = d.Get("account_description")
if update {
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-12-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("Dbs", "2015-12-01", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
15 changes: 4 additions & 11 deletions alicloud/resource_alicloud_mongodb_audit_policy.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 @@ -50,10 +49,7 @@ func resourceAlicloudMongodbAuditPolicyCreate(d *schema.ResourceData, meta inter
var response map[string]interface{}
action := "ModifyAuditPolicy"
request := make(map[string]interface{})
conn, err := client.NewDdsClient()
if err != nil {
return WrapError(err)
}
var err error
request["DBInstanceId"] = d.Get("db_instance_id")
request["AuditStatus"] = d.Get("audit_status")
request["ServiceType"] = "Standard"
Expand All @@ -62,7 +58,7 @@ func resourceAlicloudMongodbAuditPolicyCreate(d *schema.ResourceData, meta inter
}
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-12-01"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("Dbs", "2015-12-01", action, nil, request, false)
if err != nil {
if NeedRetry(err) || IsExpectedErrors(err, []string{"OperationDenied.DBInstanceStatus"}) {
wait()
Expand Down Expand Up @@ -105,10 +101,7 @@ func resourceAlicloudMongodbAuditPolicyRead(d *schema.ResourceData, meta interfa
}
func resourceAlicloudMongodbAuditPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
conn, err := client.NewDdsClient()
if err != nil {
return WrapError(err)
}
var err error
var response map[string]interface{}
update := false
request := map[string]interface{}{
Expand All @@ -126,7 +119,7 @@ func resourceAlicloudMongodbAuditPolicyUpdate(d *schema.ResourceData, meta inter
action := "ModifyAuditPolicy"
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-12-01"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
response, err = client.RpcPost("Dbs", "2015-12-01", action, nil, request, false)
if err != nil {
if NeedRetry(err) || IsExpectedErrors(err, []string{"OperationDenied.DBInstanceStatus"}) {
wait()
Expand Down
Loading

0 comments on commit fdbce62

Please sign in to comment.