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

RAM: Improves the invoking ram api method and supports refreshing credential automatically #8009

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
66 changes: 66 additions & 0 deletions alicloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,12 @@
ramconn.AppendUserAgent(Module, client.config.ConfigurationSource)
ramconn.AppendUserAgent(TerraformTraceId, client.config.TerraformTraceId)
client.ramconn = ramconn
} else {
err := client.ramconn.InitWithOptions(client.config.RegionId, client.getSdkConfig(), client.config.getAuthCredential(true))
if err != nil {
return nil, fmt.Errorf("unable to initialize the RAM client: %#v", err)
}

}

return do(client.ramconn)
Expand Down Expand Up @@ -5833,7 +5839,7 @@
return conn, nil
}

type ossCredentials struct {

Check failure on line 5842 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / TestingCoverageRate

other declaration of ossCredentials

Check failure on line 5842 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

other declaration of ossCredentials

Check failure on line 5842 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Compile

other declaration of ossCredentials

Check failure on line 5842 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

other declaration of ossCredentials
client *AliyunClient
}

Expand Down Expand Up @@ -5861,7 +5867,7 @@
return defCre.client.config.SecurityToken
}

type ossCredentialsProvider struct {

Check failure on line 5870 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / TestingCoverageRate

other declaration of ossCredentialsProvider

Check failure on line 5870 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

other declaration of ossCredentialsProvider

Check failure on line 5870 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Compile

other declaration of ossCredentialsProvider

Check failure on line 5870 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

other declaration of ossCredentialsProvider
client *AliyunClient
}

Expand Down Expand Up @@ -5942,3 +5948,63 @@
}
return conn, nil
}

type ossCredentials struct {

Check failure on line 5952 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5952

ossCredentials redeclared in this block
Raw output
alicloud/connectivity/client.go:5952:6: ossCredentials redeclared in this block

Check failure on line 5952 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5952

ossCredentials redeclared in this block
Raw output
alicloud/connectivity/client.go:5952:6: ossCredentials redeclared in this block

Check failure on line 5952 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / TestingCoverageRate

ossCredentials redeclared in this block

Check failure on line 5952 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

ossCredentials redeclared in this block

Check failure on line 5952 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Compile

ossCredentials redeclared in this block

Check failure on line 5952 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

ossCredentials redeclared in this block
client *AliyunClient
}

func (defCre *ossCredentials) GetAccessKeyID() string {

Check failure on line 5956 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5956

method ossCredentials.GetAccessKeyID already declared at alicloud/connectivity/client.go:5846:31
Raw output
alicloud/connectivity/client.go:5956:31: method ossCredentials.GetAccessKeyID already declared at alicloud/connectivity/client.go:5846:31

Check failure on line 5956 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5956

method ossCredentials.GetAccessKeyID already declared at alicloud/connectivity/client.go:5846:31
Raw output
alicloud/connectivity/client.go:5956:31: method ossCredentials.GetAccessKeyID already declared at alicloud/connectivity/client.go:5846:31

Check failure on line 5956 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / TestingCoverageRate

method ossCredentials.GetAccessKeyID already declared at alicloud/connectivity/client.go:5846:31

Check failure on line 5956 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method ossCredentials.GetAccessKeyID already declared at alicloud/connectivity/client.go:5846:31

Check failure on line 5956 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Compile

method ossCredentials.GetAccessKeyID already declared at alicloud/connectivity/client.go:5846:31

Check failure on line 5956 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method ossCredentials.GetAccessKeyID already declared at alicloud/connectivity/client.go:5846:31
value, err := defCre.client.teaSdkConfig.Credential.GetAccessKeyId()
if err == nil && value != nil {
return *value
}
return defCre.client.config.AccessKey
}

func (defCre *ossCredentials) GetAccessKeySecret() string {

Check failure on line 5964 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5964

method ossCredentials.GetAccessKeySecret already declared at alicloud/connectivity/client.go:5854:31
Raw output
alicloud/connectivity/client.go:5964:31: method ossCredentials.GetAccessKeySecret already declared at alicloud/connectivity/client.go:5854:31

Check failure on line 5964 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5964

method ossCredentials.GetAccessKeySecret already declared at alicloud/connectivity/client.go:5854:31
Raw output
alicloud/connectivity/client.go:5964:31: method ossCredentials.GetAccessKeySecret already declared at alicloud/connectivity/client.go:5854:31

Check failure on line 5964 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / TestingCoverageRate

method ossCredentials.GetAccessKeySecret already declared at alicloud/connectivity/client.go:5854:31

Check failure on line 5964 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method ossCredentials.GetAccessKeySecret already declared at alicloud/connectivity/client.go:5854:31

Check failure on line 5964 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Compile

method ossCredentials.GetAccessKeySecret already declared at alicloud/connectivity/client.go:5854:31

Check failure on line 5964 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method ossCredentials.GetAccessKeySecret already declared at alicloud/connectivity/client.go:5854:31
value, err := defCre.client.teaSdkConfig.Credential.GetAccessKeySecret()
if err == nil && value != nil {
return *value
}
return defCre.client.config.SecretKey
}

func (defCre *ossCredentials) GetSecurityToken() string {

Check failure on line 5972 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5972

method ossCredentials.GetSecurityToken already declared at alicloud/connectivity/client.go:5862:31
Raw output
alicloud/connectivity/client.go:5972:31: method ossCredentials.GetSecurityToken already declared at alicloud/connectivity/client.go:5862:31

Check failure on line 5972 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5972

method ossCredentials.GetSecurityToken already declared at alicloud/connectivity/client.go:5862:31
Raw output
alicloud/connectivity/client.go:5972:31: method ossCredentials.GetSecurityToken already declared at alicloud/connectivity/client.go:5862:31

Check failure on line 5972 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / TestingCoverageRate

method ossCredentials.GetSecurityToken already declared at alicloud/connectivity/client.go:5862:31

Check failure on line 5972 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method ossCredentials.GetSecurityToken already declared at alicloud/connectivity/client.go:5862:31

Check failure on line 5972 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Compile

method ossCredentials.GetSecurityToken already declared at alicloud/connectivity/client.go:5862:31

Check failure on line 5972 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method ossCredentials.GetSecurityToken already declared at alicloud/connectivity/client.go:5862:31
value, err := defCre.client.teaSdkConfig.Credential.GetSecurityToken()
if err == nil && value != nil {
return *value
}
return defCre.client.config.SecurityToken
}

type ossCredentialsProvider struct {

Check failure on line 5980 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5980

ossCredentialsProvider redeclared in this block
Raw output
alicloud/connectivity/client.go:5980:6: ossCredentialsProvider redeclared in this block

Check failure on line 5980 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5980

ossCredentialsProvider redeclared in this block
Raw output
alicloud/connectivity/client.go:5980:6: ossCredentialsProvider redeclared in this block

Check failure on line 5980 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / TestingCoverageRate

ossCredentialsProvider redeclared in this block

Check failure on line 5980 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

ossCredentialsProvider redeclared in this block

Check failure on line 5980 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Compile

ossCredentialsProvider redeclared in this block

Check failure on line 5980 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

ossCredentialsProvider redeclared in this block
client *AliyunClient
}

func (defBuild *ossCredentialsProvider) GetCredentials() oss.Credentials {

Check failure on line 5984 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5984

method ossCredentialsProvider.GetCredentials already declared at alicloud/connectivity/client.go:5874:41
Raw output
alicloud/connectivity/client.go:5984:41: method ossCredentialsProvider.GetCredentials already declared at alicloud/connectivity/client.go:5874:41

Check failure on line 5984 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5984

method ossCredentialsProvider.GetCredentials already declared at alicloud/connectivity/client.go:5874:41
Raw output
alicloud/connectivity/client.go:5984:41: method ossCredentialsProvider.GetCredentials already declared at alicloud/connectivity/client.go:5874:41

Check failure on line 5984 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / TestingCoverageRate

method ossCredentialsProvider.GetCredentials already declared at alicloud/connectivity/client.go:5874:41

Check failure on line 5984 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method ossCredentialsProvider.GetCredentials already declared at alicloud/connectivity/client.go:5874:41

Check failure on line 5984 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Compile

method ossCredentialsProvider.GetCredentials already declared at alicloud/connectivity/client.go:5874:41

Check failure on line 5984 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method ossCredentialsProvider.GetCredentials already declared at alicloud/connectivity/client.go:5874:41
return &ossCredentials{client: defBuild.client}
}

func (client *AliyunClient) GetRetryTimeout(defaultTimeout time.Duration) time.Duration {

Check failure on line 5988 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5988

method AliyunClient.GetRetryTimeout already declared at alicloud/connectivity/client.go:5878:29
Raw output
alicloud/connectivity/client.go:5988:29: method AliyunClient.GetRetryTimeout already declared at alicloud/connectivity/client.go:5878:29

Check failure on line 5988 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5988

method AliyunClient.GetRetryTimeout already declared at alicloud/connectivity/client.go:5878:29
Raw output
alicloud/connectivity/client.go:5988:29: method AliyunClient.GetRetryTimeout already declared at alicloud/connectivity/client.go:5878:29

Check failure on line 5988 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / TestingCoverageRate

method AliyunClient.GetRetryTimeout already declared at alicloud/connectivity/client.go:5878:29

Check failure on line 5988 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method AliyunClient.GetRetryTimeout already declared at alicloud/connectivity/client.go:5878:29

Check failure on line 5988 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Compile

method AliyunClient.GetRetryTimeout already declared at alicloud/connectivity/client.go:5878:29

Check failure on line 5988 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method AliyunClient.GetRetryTimeout already declared at alicloud/connectivity/client.go:5878:29

maxRetryTimeout := client.config.MaxRetryTimeout
if maxRetryTimeout != 0 {
return time.Duration(maxRetryTimeout) * time.Second
}

return defaultTimeout
}

func (client *AliyunClient) GenRoaParam(action, method, version, path string) *openapi.Params {

Check failure on line 5998 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5998

method AliyunClient.GenRoaParam already declared at alicloud/connectivity/client.go:5888:29 (typecheck)
Raw output
alicloud/connectivity/client.go:5998:29: method AliyunClient.GenRoaParam already declared at alicloud/connectivity/client.go:5888:29 (typecheck)
package connectivity

Check failure on line 5998 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / errcheck

[errcheck] alicloud/connectivity/client.go#L5998

method AliyunClient.GenRoaParam already declared at alicloud/connectivity/client.go:5888:29) (typecheck)
Raw output
alicloud/connectivity/client.go:5998:29: method AliyunClient.GenRoaParam already declared at alicloud/connectivity/client.go:5888:29) (typecheck)
	"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
	^

Check failure on line 5998 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / TestingCoverageRate

method AliyunClient.GenRoaParam already declared at alicloud/connectivity/client.go:5888:29

Check failure on line 5998 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method AliyunClient.GenRoaParam already declared at alicloud/connectivity/client.go:5888:29

Check failure on line 5998 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Compile

method AliyunClient.GenRoaParam already declared at alicloud/connectivity/client.go:5888:29

Check failure on line 5998 in alicloud/connectivity/client.go

View workflow job for this annotation

GitHub Actions / Consistency

method AliyunClient.GenRoaParam already declared at alicloud/connectivity/client.go:5888:29
return &openapi.Params{
Action: tea.String(action),
Version: tea.String(version),
Protocol: tea.String(client.config.Protocol),
Pathname: tea.String(path),
Method: tea.String(method),
AuthType: tea.String("AK"),
Style: tea.String("ROA"),
ReqBodyType: tea.String("formData"),
BodyType: tea.String("json"),
}
}
26 changes: 6 additions & 20 deletions alicloud/data_source_alicloud_ram_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 @@ -167,22 +166,17 @@ func dataSourceAlicloudRamPoliciesRead(d *schema.ResourceData, meta interface{})
}
}
var response map[string]interface{}
conn, err := client.NewRamClient()
if err != nil {
return WrapError(err)
}
var err error
// policies for user
if userNameOk {
userAction := "ListPoliciesForUser"
userRequest := map[string]interface{}{
"UserName": userName,
}

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(userAction), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, userRequest, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", userAction, nil, userRequest, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -214,11 +208,9 @@ func dataSourceAlicloudRamPoliciesRead(d *schema.ResourceData, meta interface{})
"GroupName": groupName,
}

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(groupAction), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, groupRequest, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", groupAction, nil, groupRequest, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -249,11 +241,9 @@ func dataSourceAlicloudRamPoliciesRead(d *schema.ResourceData, meta interface{})
"RoleName": roleName,
}

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(roleAction), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, roleRequest, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", roleAction, nil, roleRequest, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -278,11 +268,9 @@ func dataSourceAlicloudRamPoliciesRead(d *schema.ResourceData, meta interface{})
}

for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -369,11 +357,9 @@ func dataSourceAlicloudRamPoliciesRead(d *schema.ResourceData, meta interface{})
"PolicyName": id,
"PolicyType": object["PolicyType"],
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
52 changes: 12 additions & 40 deletions alicloud/resource_alicloud_ram_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 @@ -141,12 +140,9 @@ func resourceAlicloudRamPolicy() *schema.Resource {
func resourceAlicloudRamPolicyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
var err error
action := "CreatePolicy"
request := make(map[string]interface{})
conn, err := client.NewRamClient()
if err != nil {
return WrapError(err)
}
if v, ok := d.GetOk("description"); ok {
request["Description"] = v
}
Expand All @@ -173,11 +169,9 @@ func resourceAlicloudRamPolicyCreate(d *schema.ResourceData, meta interface{}) e
request["PolicyName"] = v
}

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
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-05-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -230,6 +224,7 @@ func resourceAlicloudRamPolicyRead(d *schema.ResourceData, meta interface{}) err
func resourceAlicloudRamPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
var err error
update := false
request := map[string]interface{}{
"PolicyName": d.Id(),
Expand All @@ -256,15 +251,9 @@ func resourceAlicloudRamPolicyUpdate(d *schema.ResourceData, meta interface{}) e
request["RotateStrategy"] = d.Get("rotate_strategy")
}
action := "CreatePolicyVersion"
conn, err := client.NewRamClient()
if err != nil {
return WrapError(err)
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
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-05-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", action, nil, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -285,10 +274,7 @@ func resourceAlicloudRamPolicyDelete(d *schema.ResourceData, meta interface{}) e
client := meta.(*connectivity.AliyunClient)
action := "DeletePolicy"
var response map[string]interface{}
conn, err := client.NewRamClient()
if err != nil {
return WrapError(err)
}
var err error
request := map[string]interface{}{
"PolicyName": d.Id(),
}
Expand All @@ -299,11 +285,9 @@ func resourceAlicloudRamPolicyDelete(d *schema.ResourceData, meta interface{}) e
"PolicyType": "Custom",
}
listAction := "ListEntitiesForPolicy"
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(listAction), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, listRequest, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", listAction, nil, listRequest, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -330,11 +314,9 @@ func resourceAlicloudRamPolicyDelete(d *schema.ResourceData, meta interface{}) e
"UserName": v.(map[string]interface{})["UserName"],
"PolicyType": "Custom",
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(userAction), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, userRequest, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", userAction, nil, userRequest, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -365,11 +347,9 @@ func resourceAlicloudRamPolicyDelete(d *schema.ResourceData, meta interface{}) e
"GroupName": v.(map[string]interface{})["GroupName"],
"PolicyType": "Custom",
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(groupAction), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, groupRequest, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", groupAction, nil, groupRequest, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -400,11 +380,9 @@ func resourceAlicloudRamPolicyDelete(d *schema.ResourceData, meta interface{}) e
"RoleName": v.(map[string]interface{})["RoleName"],
"PolicyType": "Custom",
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(roleAction), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, roleRequest, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", roleAction, nil, roleRequest, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -429,11 +407,9 @@ func resourceAlicloudRamPolicyDelete(d *schema.ResourceData, meta interface{}) e
"PolicyType": "Custom",
}
listVersionsAction := "ListPolicyVersions"
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(listVersionsAction), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, listVersionsRequest, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", listVersionsAction, nil, listVersionsRequest, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -462,11 +438,9 @@ func resourceAlicloudRamPolicyDelete(d *schema.ResourceData, meta interface{}) e
"PolicyName": d.Id(),
"VersionId": v.(map[string]interface{})["VersionId"],
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(versionAction), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, versionRequest, &util.RuntimeOptions{})
response, err = client.RpcPost("Ram", "2015-05-01", versionAction, nil, versionRequest, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -482,11 +456,9 @@ func resourceAlicloudRamPolicyDelete(d *schema.ResourceData, meta interface{}) e
}
}

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.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", action, nil, request, false)
if err != nil {
if IsExpectedErrors(err, []string{"DeleteConflict.Policy.Group", "DeleteConflict.Policy.User", "DeleteConflict.Policy.Version", "DeleteConflict.Role.Policy"}) || NeedRetry(err) {
wait()
Expand Down
20 changes: 3 additions & 17 deletions alicloud/resource_alicloud_ram_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"time"

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

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

"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
Expand All @@ -35,10 +33,6 @@ func testSweepRamPolicies(region string) error {
return WrapError(err)
}
client := rawClient.(*connectivity.AliyunClient)
conn, err := client.NewRamClient()
if err != nil {
return WrapError(err)
}
action := "ListPolicies"
request := map[string]interface{}{
"PolicyType": "Custom",
Expand All @@ -52,9 +46,7 @@ func testSweepRamPolicies(region string) error {
var response map[string]interface{}
sweeped := false
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Ram", "2015-05-01", action, nil, request, true)
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_ram_policies", action, AlibabaCloudSdkGoERROR)
}
Expand Down Expand Up @@ -88,7 +80,7 @@ func testSweepRamPolicies(region string) error {
request := map[string]interface{}{
"PolicyName": name,
}
_, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
_, err = client.RpcPost("Ram", "2015-05-01", action, nil, request, true)
if err != nil {
log.Printf("[ERROR] Failed to delete Ram Policy (%s): %s", name, err)
}
Expand Down Expand Up @@ -369,18 +361,12 @@ func testAccCheckRamPolicyDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*connectivity.AliyunClient)

// Try to find the policy
conn, err := client.NewRamClient()
if err != nil {
return WrapError(err)
}
action := "GetPolicy"
request := map[string]interface{}{
"PolicyName": rs.Primary.ID,
"PolicyType": "Custom",
}
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
_, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2015-05-01"), StringPointer("AK"), nil, request, &runtime)
_, err := client.RpcPost("Ram", "2015-05-01", action, nil, request, true)
if err != nil && !IsExpectedErrors(err, []string{"EntityNotExist.Policy"}) {
return WrapError(err)
}
Expand Down
Loading
Loading