Skip to content

Commit

Permalink
VpcPeer: Improves the invoking vpcpeer api method and supports refres…
Browse files Browse the repository at this point in the history
…hing credential automatically
  • Loading branch information
xiaozhu36 committed Nov 8, 2024
1 parent cf67c06 commit e22e706
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 291 deletions.
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_vpc_peer_connections.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 @@ -145,16 +144,11 @@ func dataSourceAlicloudVpcPeerConnectionsRead(d *schema.ResourceData, meta inter
}
status, statusOk := d.GetOk("status")
var response map[string]interface{}
conn, err := client.NewVpcpeerClient()
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("2022-01-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
52 changes: 9 additions & 43 deletions alicloud/resource_alicloud_vpc_peer_connection.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/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 @@ -99,10 +98,7 @@ func resourceAliCloudVpcPeerPeerConnectionCreate(d *schema.ResourceData, meta in
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewVpcpeerClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query["RegionId"] = client.RegionId
request["ClientToken"] = buildClientToken(action)
Expand All @@ -126,11 +122,9 @@ func resourceAliCloudVpcPeerPeerConnectionCreate(d *schema.ResourceData, meta in
if v, ok := d.GetOkExists("dry_run"); ok {
request["DryRun"] = 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("2022-01-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -228,10 +222,7 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
update := false
d.Partial(true)
action := "ModifyVpcPeerConnection"
conn, err := client.NewVpcpeerClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query = make(map[string]interface{})
request["InstanceId"] = d.Id()
Expand All @@ -256,11 +247,9 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
request["DryRun"] = v
}
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("2022-01-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -282,10 +271,6 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
}
update = false
action = "MoveResourceGroup"
conn, err = client.NewVpcpeerClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["ResourceId"] = d.Id()
Expand All @@ -296,11 +281,9 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
}
request["NewResourceGroupId"] = d.Get("resource_group_id")
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("2022-01-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -327,10 +310,6 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
if object["Status"].(string) != target {
if target == "Activated" {
action := "AcceptVpcPeerConnection"
conn, err := client.NewVpcpeerClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["InstanceId"] = d.Id()
Expand All @@ -342,11 +321,9 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
if v, ok := d.GetOkExists("dry_run"); ok {
request["DryRun"] = v
}
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("2022-01-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -369,10 +346,6 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
}
if target == "Rejected" {
action := "RejectVpcPeerConnection"
conn, err := client.NewVpcpeerClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
request["InstanceId"] = d.Id()
Expand All @@ -381,11 +354,9 @@ func resourceAliCloudVpcPeerPeerConnectionUpdate(d *schema.ResourceData, meta in
if v, ok := d.GetOkExists("dry_run"); ok {
request["DryRun"] = v
}
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("2022-01-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -426,10 +397,7 @@ func resourceAliCloudVpcPeerPeerConnectionDelete(d *schema.ResourceData, meta in
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewVpcpeerClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
request["InstanceId"] = d.Id()

Expand All @@ -441,11 +409,9 @@ func resourceAliCloudVpcPeerPeerConnectionDelete(d *schema.ResourceData, meta in
if v, ok := d.GetOkExists("force_delete"); ok {
request["Force"] = v
}
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("2022-01-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
request["ClientToken"] = buildClientToken(action)

if err != nil {
Expand Down
37 changes: 8 additions & 29 deletions alicloud/resource_alicloud_vpc_peer_connection_accepter.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/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 @@ -98,10 +97,7 @@ func resourceAliCloudVpcPeerPeerConnectionAccepterCreate(d *schema.ResourceData,
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewVpcpeerClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
if v, ok := d.GetOk("instance_id"); ok {
request["InstanceId"] = v
Expand All @@ -115,11 +111,10 @@ func resourceAliCloudVpcPeerPeerConnectionAccepterCreate(d *schema.ResourceData,
if v, ok := d.GetOkExists("dry_run"); ok {
request["DryRun"] = 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("2022-01-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -221,10 +216,7 @@ func resourceAliCloudVpcPeerPeerConnectionAccepterUpdate(d *schema.ResourceData,
update := false
d.Partial(true)
action := "ModifyVpcPeerConnection"
conn, err := client.NewVpcpeerClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
query = make(map[string]interface{})
request["InstanceId"] = d.Id()
Expand All @@ -249,11 +241,9 @@ func resourceAliCloudVpcPeerPeerConnectionAccepterUpdate(d *schema.ResourceData,
request["DryRun"] = v
}
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("2022-01-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
if err != nil {
if IsExpectedErrors(err, []string{"IncorrectStatus.VpcPeer"}) || NeedRetry(err) {
wait()
Expand All @@ -275,10 +265,6 @@ func resourceAliCloudVpcPeerPeerConnectionAccepterUpdate(d *schema.ResourceData,
}
update = false
action = "MoveResourceGroup"
conn, err = client.NewVpcpeerClient()
if err != nil {
return WrapError(err)
}
request = make(map[string]interface{})
query = make(map[string]interface{})
query["ResourceId"] = d.Id()
Expand All @@ -289,11 +275,9 @@ func resourceAliCloudVpcPeerPeerConnectionAccepterUpdate(d *schema.ResourceData,
request["NewResourceGroupId"] = d.Get("resource_group_id")
request["ResourceType"] = "PeerConnection"
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("2022-01-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, false)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -320,10 +304,7 @@ func resourceAliCloudVpcPeerPeerConnectionAccepterDelete(d *schema.ResourceData,
var request map[string]interface{}
var response map[string]interface{}
query := make(map[string]interface{})
conn, err := client.NewVpcpeerClient()
if err != nil {
return WrapError(err)
}
var err error
request = make(map[string]interface{})
request["InstanceId"] = d.Id()

Expand All @@ -335,11 +316,9 @@ func resourceAliCloudVpcPeerPeerConnectionAccepterDelete(d *schema.ResourceData,
if v, ok := d.GetOkExists("force_delete"); ok {
request["Force"] = v
}
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("2022-01-01"), StringPointer("AK"), query, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true)
request["ClientToken"] = buildClientToken(action)

if err != nil {
Expand Down
15 changes: 4 additions & 11 deletions alicloud/resource_alicloud_vpc_peer_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,22 @@ func testSweepVpcPeerConnection(region string) error {
if err != nil {
return fmt.Errorf("error getting AliCloud client: %s", err)
}
aliyunClient := rawClient.(*connectivity.AliyunClient)
client := rawClient.(*connectivity.AliyunClient)
prefixes := []string{
"tf-testAcc",
"tf_testAcc",
}
action := "ListVpcPeerConnections"
request := map[string]interface{}{}
request["RegionId"] = aliyunClient.RegionId
request["RegionId"] = client.RegionId

request["MaxResults"] = PageSizeLarge

var response map[string]interface{}
conn, err := aliyunClient.NewVpcpeerClient()
if err != nil {
log.Printf("[ERROR] %s get an error: %#v", action, err)
return nil
}
for {
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2022-01-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -99,7 +92,7 @@ func testSweepVpcPeerConnection(region string) error {
request := map[string]interface{}{
"InstanceId": item["InstanceId"],
}
_, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2022-01-01"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
_, err = client.RpcPost("VpcPeer", "2022-01-01", action, nil, request, true)
if err != nil {
log.Printf("[ERROR] Failed to delete Vpc Peer Connection (%s): %s", item["Name"].(string), err)
}
Expand Down
19 changes: 4 additions & 15 deletions alicloud/service_alicloud_vpc_peer.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 All @@ -17,19 +16,14 @@ type VpcPeerService struct {

func (s *VpcPeerService) DescribeVpcPeerConnection(id string) (object map[string]interface{}, err error) {
var response map[string]interface{}
conn, err := s.client.NewVpcpeerClient()
if err != nil {
return nil, WrapError(err)
}
client := s.client
action := "GetVpcPeerConnectionAttribute"
request := map[string]interface{}{
"InstanceId": id,
}
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("2022-01-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -75,19 +69,14 @@ func (s *VpcPeerService) VpcPeerConnectionStateRefreshFunc(id string, failStates
}
func (s *VpcPeerService) DescribeVpcPeerConnectionAccepter(id string) (object map[string]interface{}, err error) {
var response map[string]interface{}
conn, err := s.client.NewVpcpeerClient()
if err != nil {
return nil, WrapError(err)
}
client := s.client
action := "GetVpcPeerConnectionAttribute"
request := map[string]interface{}{
"InstanceId": id,
}
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("2022-01-01"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("VpcPeer", "2022-01-01", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
Loading

0 comments on commit e22e706

Please sign in to comment.