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

VpcPeer: Improves the invoking vpcpeer api method and supports refreshing credential automatically #8012

Merged
merged 1 commit into from
Dec 23, 2024
Merged
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
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
11 changes: 7 additions & 4 deletions alicloud/data_source_alicloud_vpc_peer_connections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,19 @@ variable "name" {
}
data "alicloud_account" "default" {}

data "alicloud_vpcs" "default" {
name_regex = "^default-NODELETING$"
resource "alicloud_vpc" "local" {
vpc_name = var.name
}

resource "alicloud_vpc" "peer" {
vpc_name = var.name
}
resource "alicloud_vpc_peer_connection" "default" {
peer_connection_name = var.name
vpc_id = data.alicloud_vpcs.default.ids.0
vpc_id = alicloud_vpc.local.id
accepting_ali_uid = data.alicloud_account.default.id
accepting_region_id = "%s"
accepting_vpc_id = data.alicloud_vpcs.default.ids.1
accepting_vpc_id = alicloud_vpc.peer.id
description = var.name
}

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 @@ -102,7 +95,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
Loading
Loading