From e22e706668914c195650a49984e2919967647f43 Mon Sep 17 00:00:00 2001 From: "guimin.hgm" Date: Fri, 8 Nov 2024 15:50:56 +0800 Subject: [PATCH] VpcPeer: Improves the invoking vpcpeer api method and supports refreshing credential automatically --- ...ta_source_alicloud_vpc_peer_connections.go | 10 +- .../resource_alicloud_vpc_peer_connection.go | 52 ++----- ...e_alicloud_vpc_peer_connection_accepter.go | 37 +---- ...ource_alicloud_vpc_peer_connection_test.go | 15 +- alicloud/service_alicloud_vpc_peer.go | 19 +-- alicloud/service_alicloud_vpc_peer_v2.go | 38 +---- alicloud/service_alicloud_vpc_v2.go | 141 +----------------- .../docs/d/vpc_peer_connections.html.markdown | 28 ++-- 8 files changed, 49 insertions(+), 291 deletions(-) diff --git a/alicloud/data_source_alicloud_vpc_peer_connections.go b/alicloud/data_source_alicloud_vpc_peer_connections.go index d4be5b3aad98..5369299c7df3 100644 --- a/alicloud/data_source_alicloud_vpc_peer_connections.go +++ b/alicloud/data_source_alicloud_vpc_peer_connections.go @@ -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" @@ -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() diff --git a/alicloud/resource_alicloud_vpc_peer_connection.go b/alicloud/resource_alicloud_vpc_peer_connection.go index 6a3d6c68973a..48a294772b93 100644 --- a/alicloud/resource_alicloud_vpc_peer_connection.go +++ b/alicloud/resource_alicloud_vpc_peer_connection.go @@ -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" @@ -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) @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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 { diff --git a/alicloud/resource_alicloud_vpc_peer_connection_accepter.go b/alicloud/resource_alicloud_vpc_peer_connection_accepter.go index 845744009953..7fc58978526b 100644 --- a/alicloud/resource_alicloud_vpc_peer_connection_accepter.go +++ b/alicloud/resource_alicloud_vpc_peer_connection_accepter.go @@ -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" @@ -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 @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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 { diff --git a/alicloud/resource_alicloud_vpc_peer_connection_test.go b/alicloud/resource_alicloud_vpc_peer_connection_test.go index 2fc4988a0251..6478991c0d89 100644 --- a/alicloud/resource_alicloud_vpc_peer_connection_test.go +++ b/alicloud/resource_alicloud_vpc_peer_connection_test.go @@ -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() @@ -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) } diff --git a/alicloud/service_alicloud_vpc_peer.go b/alicloud/service_alicloud_vpc_peer.go index 4e83b31f20da..88999e60eb99 100644 --- a/alicloud/service_alicloud_vpc_peer.go +++ b/alicloud/service_alicloud_vpc_peer.go @@ -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" @@ -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() @@ -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() diff --git a/alicloud/service_alicloud_vpc_peer_v2.go b/alicloud/service_alicloud_vpc_peer_v2.go index 2ec0c9b4978e..6feaad90b7d1 100644 --- a/alicloud/service_alicloud_vpc_peer_v2.go +++ b/alicloud/service_alicloud_vpc_peer_v2.go @@ -5,8 +5,6 @@ import ( "time" "github.com/PaesslerAG/jsonpath" - rpc "github.com/alibabacloud-go/tea-rpc/client" - 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" @@ -24,19 +22,13 @@ func (s *VpcPeerServiceV2) DescribeVpcPeerPeerConnection(id string) (object map[ var response map[string]interface{} var query map[string]interface{} action := "GetVpcPeerConnectionAttribute" - conn, err := client.NewVpcpeerClient() - if err != nil { - return object, WrapError(err) - } request = make(map[string]interface{}) query = make(map[string]interface{}) request["InstanceId"] = id - runtime := util.RuntimeOptions{} - runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 5*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"), query, request, &runtime) + response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true) if err != nil { if NeedRetry(err) { @@ -88,8 +80,6 @@ func (s *VpcPeerServiceV2) SetResourceTags(d *schema.ResourceData, resourceType if d.HasChange("tags") { var err error var action string - var conn *rpc.Client - client := s.client var request map[string]interface{} var response map[string]interface{} query := make(map[string]interface{}) @@ -103,10 +93,7 @@ func (s *VpcPeerServiceV2) SetResourceTags(d *schema.ResourceData, resourceType } if len(removedTagKeys) > 0 { action = "UnTagResources" - conn, err = client.NewVpcpeerClient() - if err != nil { - return WrapError(err) - } + client := s.client request = make(map[string]interface{}) query = make(map[string]interface{}) request["ResourceId.1"] = d.Id() @@ -117,11 +104,9 @@ func (s *VpcPeerServiceV2) SetResourceTags(d *schema.ResourceData, resourceType } request["ResourceType"] = resourceType - 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() @@ -140,10 +125,7 @@ func (s *VpcPeerServiceV2) SetResourceTags(d *schema.ResourceData, resourceType if len(added) > 0 { action = "TagResources" - conn, err = client.NewVpcpeerClient() - if err != nil { - return WrapError(err) - } + client := s.client request = make(map[string]interface{}) query = make(map[string]interface{}) request["ResourceId.1"] = d.Id() @@ -157,11 +139,9 @@ func (s *VpcPeerServiceV2) SetResourceTags(d *schema.ResourceData, resourceType } request["ResourceType"] = resourceType - 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() @@ -192,19 +172,13 @@ func (s *VpcPeerServiceV2) DescribeVpcPeerPeerConnectionAccepter(id string) (obj var response map[string]interface{} var query map[string]interface{} action := "GetVpcPeerConnectionAttribute" - conn, err := client.NewVpcpeerClient() - if err != nil { - return object, WrapError(err) - } request = make(map[string]interface{}) query = make(map[string]interface{}) request["InstanceId"] = id - runtime := util.RuntimeOptions{} - runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 5*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"), query, request, &runtime) + response, err = client.RpcPost("VpcPeer", "2022-01-01", action, query, request, true) if err != nil { if NeedRetry(err) { diff --git a/alicloud/service_alicloud_vpc_v2.go b/alicloud/service_alicloud_vpc_v2.go index f488b727a1ed..f3fcfc4c2766 100644 --- a/alicloud/service_alicloud_vpc_v2.go +++ b/alicloud/service_alicloud_vpc_v2.go @@ -1616,23 +1616,16 @@ func (s *VpcServiceV2) VpcDhcpOptionsSetStateRefreshFunc(id string, field string // DescribeVpcPeerConnection <<< Encapsulated get interface for Vpc PeerConnection. func (s *VpcServiceV2) DescribeVpcPeerConnection(id string) (object map[string]interface{}, err error) { + client := s.client var response map[string]interface{} action := "GetVpcPeerConnectionAttribute" - - conn, err := s.client.NewVpcpeerClient() - if err != nil { - return object, WrapError(err) - } - request := map[string]interface{}{ "InstanceId": id, } - runtime := util.RuntimeOptions{} - runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 5*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() @@ -1661,136 +1654,6 @@ func (s *VpcServiceV2) DescribeVpcPeerConnection(id string) (object map[string]i return object, nil } -func (s *VpcServiceV2) VpcPeerConnectionStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { - return func() (interface{}, string, error) { - object, err := s.DescribeVpcPeerConnection(id) - if err != nil { - if NotFoundError(err) { - return nil, "", nil - } - return nil, "", WrapError(err) - } - - v, err := jsonpath.Get(field, object) - currentStatus := fmt.Sprint(v) - - for _, failState := range failStates { - if currentStatus == failState { - return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) - } - } - return object, currentStatus, nil - } -} - -// DescribeVpcPeerConnection >>> Encapsulated. - -// SetVpcPeerResourceTags <<< Encapsulated tag function for VpcPeer. -func (s *VpcServiceV2) SetVpcPeerResourceTags(d *schema.ResourceData, resourceType string) error { - if d.HasChange("tags") { - var err error - var action string - var conn *rpc.Client - client := s.client - var request map[string]interface{} - var response map[string]interface{} - query := make(map[string]interface{}) - - added, removed := parsingTags(d) - removedTagKeys := make([]string, 0) - for _, v := range removed { - if !ignoredTags(v, "") { - removedTagKeys = append(removedTagKeys, v) - } - } - if len(removedTagKeys) > 0 { - action = "UnTagResources" - conn, err = client.NewVpcpeerClient() - if err != nil { - return WrapError(err) - } - request = make(map[string]interface{}) - query = make(map[string]interface{}) - request["ResourceId.1"] = d.Id() - request["RegionId"] = client.RegionId - request["ClientToken"] = buildClientToken(action) - for i, key := range removedTagKeys { - request[fmt.Sprintf("TagKey.%d", i+1)] = key - } - - request["ResourceType"] = resourceType - 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) - request["ClientToken"] = buildClientToken(action) - - if err != nil { - if NeedRetry(err) { - wait() - return resource.RetryableError(err) - } - return resource.NonRetryableError(err) - } - addDebug(action, response, request) - return nil - }) - if err != nil { - return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) - } - - } - - if len(added) > 0 { - action = "TagResources" - conn, err = client.NewVpcpeerClient() - if err != nil { - return WrapError(err) - } - request = make(map[string]interface{}) - query = make(map[string]interface{}) - request["ResourceId.1"] = d.Id() - request["RegionId"] = client.RegionId - request["ClientToken"] = buildClientToken(action) - count := 1 - for key, value := range added { - request[fmt.Sprintf("Tag.%d.Key", count)] = key - request[fmt.Sprintf("Tag.%d.Value", count)] = value - count++ - } - - request["ResourceType"] = resourceType - 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) - request["ClientToken"] = buildClientToken(action) - - if err != nil { - if NeedRetry(err) { - wait() - return resource.RetryableError(err) - } - return resource.NonRetryableError(err) - } - addDebug(action, response, request) - return nil - }) - if err != nil { - return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) - } - - } - d.SetPartial("tags") - } - - return nil -} - -// SetVpcPeerResourceTags >>> tag function encapsulated. - // DescribeVpcGatewayEndpoint <<< Encapsulated get interface for Vpc GatewayEndpoint. func (s *VpcServiceV2) DescribeVpcGatewayEndpoint(id string) (object map[string]interface{}, err error) { diff --git a/website/docs/d/vpc_peer_connections.html.markdown b/website/docs/d/vpc_peer_connections.html.markdown index 327727467c7d..74d749856c9e 100644 --- a/website/docs/d/vpc_peer_connections.html.markdown +++ b/website/docs/d/vpc_peer_connections.html.markdown @@ -7,11 +7,11 @@ description: |- Provides a list of Vpc Peer Connections to the user. --- -# alicloud\_vpc\_peer\_connections +# alicloud_vpc_peer_connections This data source provides the Vpc Peer Connections of the current Alibaba Cloud user. --> **NOTE:** Available in v1.186.0+. +-> **NOTE:** Available since v1.186.0. ## Example Usage @@ -42,20 +42,20 @@ The following arguments are supported: * `status` - (Optional, ForceNew) The status of the resource. Valid values: `Accepting`, `Activated`, `Creating`, `Deleted`, `Deleting`, `Expired`, `Rejected`, `Updating`. * `vpc_id` - (Optional, ForceNew) The ID of the requester VPC. -## Argument Reference +## Attributes Reference The following attributes are exported in addition to the arguments listed above: * `names` - A list of PeerConnection names. * `connections` - A list of Vpc Peer Connections. Each element contains the following attributes: - * `accepting_ali_uid` - The ID of the Alibaba Cloud account (primary account) of the receiving end of the VPC peering connection to be created. - * `accepting_region_id` - The region ID of the recipient of the VPC peering connection to be created. - * `accepting_vpc_id` - The VPC ID of the receiving end of the VPC peer connection. - * `bandwidth` - The bandwidth of the VPC peering connection to be modified. Unit: Mbps. - * `create_time` - The creation time of the resource. - * `description` - The description of the VPC peer connection to be created. - * `id` - The ID of the PeerConnection. - * `peer_connection_id` - The first ID of the resource. - * `peer_connection_name` - The name of the resource. - * `status` - The status of the resource. - * `vpc_id` - The ID of the requester VPC. \ No newline at end of file + * `accepting_ali_uid` - The ID of the Alibaba Cloud account (primary account) of the receiving end of the VPC peering connection to be created. + * `accepting_region_id` - The region ID of the recipient of the VPC peering connection to be created. + * `accepting_vpc_id` - The VPC ID of the receiving end of the VPC peer connection. + * `bandwidth` - The bandwidth of the VPC peering connection to be modified. Unit: Mbps. + * `create_time` - The creation time of the resource. + * `description` - The description of the VPC peer connection to be created. + * `id` - The ID of the PeerConnection. + * `peer_connection_id` - The first ID of the resource. + * `peer_connection_name` - The name of the resource. + * `status` - The status of the resource. + * `vpc_id` - The ID of the requester VPC. \ No newline at end of file