Skip to content

Commit

Permalink
VpnGateway: Improves the invoking api method and supports refreshing …
Browse files Browse the repository at this point in the history
…credential automatically
  • Loading branch information
xiaozhu36 committed Jan 31, 2025
1 parent 239c28b commit 9208cdc
Show file tree
Hide file tree
Showing 16 changed files with 248 additions and 663 deletions.
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_vpn_gateway_vco_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -125,16 +124,11 @@ func dataSourceAlicloudVpnGatewayVcoRoutesRead(d *schema.ResourceData, meta inte
}
status, statusOk := d.GetOk("status")
var response map[string]interface{}
conn, err := client.NewVpcClient()
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("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_vpn_gateway_vpn_attachments.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 @@ -279,16 +278,11 @@ func dataSourceAlicloudVpnGatewayVpnAttachmentsRead(d *schema.ResourceData, meta
}
status, statusOk := d.GetOk("status")
var response map[string]interface{}
conn, err := client.NewVpcClient()
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("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
11 changes: 2 additions & 9 deletions alicloud/data_source_alicloud_vpn_gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"

"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
Expand Down Expand Up @@ -228,20 +227,14 @@ func dataSourceAlicloudVpnsRead(d *schema.ResourceData, meta interface{}) error
}

var response map[string]interface{}
conn, err := client.NewVpcClient()
if err != nil {
return WrapError(err)
}

runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
var err error
wait := incrementalWait(3*time.Second, 3*time.Second)
request["PageNumber"] = 1
request["PageSize"] = PageSizeLarge

for {
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_vpn_ipsec_servers.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 @@ -212,16 +211,11 @@ func dataSourceAlicloudVpnIpsecServersRead(d *schema.ResourceData, meta interfac
request["IpsecServerId"] = v
}
var response map[string]interface{}
conn, err := client.NewVpcClient()
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("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
10 changes: 2 additions & 8 deletions alicloud/data_source_alicloud_vpn_pbr_route_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/PaesslerAG/jsonpath"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -96,16 +95,11 @@ func dataSourceAlicloudVpnPbrRouteEntriesRead(d *schema.ResourceData, meta inter
}
}
var response map[string]interface{}
conn, err := client.NewVpcClient()
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("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
19 changes: 4 additions & 15 deletions alicloud/resource_alicloud_vpn_gateway_vco_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

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

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 @@ -65,10 +64,7 @@ func resourceAlicloudVpnGatewayVcoRouteCreate(d *schema.ResourceData, meta inter
var response map[string]interface{}
action := "CreateVcoRouteEntry"
request := make(map[string]interface{})
conn, err := client.NewVpcClient()
if err != nil {
return WrapError(err)
}
var err error
request["RegionId"] = client.RegionId
request["Weight"] = d.Get("weight")
request["NextHop"] = d.Get("next_hop")
Expand All @@ -78,11 +74,9 @@ func resourceAlicloudVpnGatewayVcoRouteCreate(d *schema.ResourceData, meta inter
request["OverlayMode"] = v
}
request["ClientToken"] = buildClientToken("CreateVcoRouteEntry")
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutCreate)), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if IsExpectedErrors(err, []string{"VpnGateway.Configuring", "TaskConflict", "Appliance.Configuring", "VpnTask.CONFLICT", "VpnConnection.Configuring"}) || NeedRetry(err) {
wait()
Expand Down Expand Up @@ -122,10 +116,7 @@ func resourceAlicloudVpnGatewayVcoRouteRead(d *schema.ResourceData, meta interfa
}
func resourceAlicloudVpnGatewayVcoRouteDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
conn, err := client.NewVpcClient()
if err != nil {
return WrapError(err)
}
var err error
parts, err := ParseResourceId(d.Id(), 4)
if err != nil {
return WrapError(err)
Expand All @@ -141,11 +132,9 @@ func resourceAlicloudVpnGatewayVcoRouteDelete(d *schema.ResourceData, meta inter
request["Weight"] = parts[3]

request["ClientToken"] = buildClientToken("DeleteVcoRouteEntry")
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(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if IsExpectedErrors(err, []string{"VpnGateway.Configuring", "TaskConflict", "Appliance.Configuring", "VpnTask.CONFLICT", "VpnConnection.Configuring"}) || NeedRetry(err) {
wait()
Expand Down
28 changes: 6 additions & 22 deletions alicloud/resource_alicloud_vpn_gateway_vpn_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

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

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 @@ -240,10 +239,7 @@ func resourceAlicloudVpnGatewayVpnAttachmentCreate(d *schema.ResourceData, meta
var response map[string]interface{}
action := "CreateVpnAttachment"
request := make(map[string]interface{})
conn, err := client.NewVpcClient()
if err != nil {
return WrapError(err)
}
var err error
request["CustomerGatewayId"] = d.Get("customer_gateway_id")
if v, ok := d.GetOkExists("effect_immediately"); ok {
request["EffectImmediately"] = v
Expand Down Expand Up @@ -316,11 +312,9 @@ func resourceAlicloudVpnGatewayVpnAttachmentCreate(d *schema.ResourceData, meta
request["BgpConfig"] = bgpMapsString
}
request["ClientToken"] = buildClientToken("CreateVpnAttachment")
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("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -429,10 +423,7 @@ func resourceAlicloudVpnGatewayVpnAttachmentRead(d *schema.ResourceData, meta in
func resourceAlicloudVpnGatewayVpnAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
vpcService := VpcService{client}
conn, err := client.NewVpcClient()
if err != nil {
return WrapError(err)
}
var err error
var response map[string]interface{}
update := false
request := map[string]interface{}{
Expand Down Expand Up @@ -547,11 +538,9 @@ func resourceAlicloudVpnGatewayVpnAttachmentUpdate(d *schema.ResourceData, meta
if update {
action := "ModifyVpnAttachmentAttribute"
request["ClientToken"] = buildClientToken("ModifyVpnAttachmentAttribute")
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("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand All @@ -576,20 +565,15 @@ func resourceAlicloudVpnGatewayVpnAttachmentDelete(d *schema.ResourceData, meta
client := meta.(*connectivity.AliyunClient)
action := "DeleteVpnAttachment"
var response map[string]interface{}
conn, err := client.NewVpcClient()
if err != nil {
return WrapError(err)
}
var err error
request := map[string]interface{}{}

request["RegionId"] = client.RegionId
request["VpnConnectionId"] = d.Id()
request["ClientToken"] = buildClientToken("DeleteVpnAttachment")
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(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down
17 changes: 5 additions & 12 deletions alicloud/resource_alicloud_vpn_gateway_vpn_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,23 @@ func testSweepVpnGatewayVpnAttachment(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 := "DescribeVpnConnections"
request := map[string]interface{}{}
request["RegionId"] = aliyunClient.RegionId
request["RegionId"] = client.RegionId

request["PageSize"] = PageSizeLarge
request["PageNumber"] = 1

var response map[string]interface{}
conn, err := aliyunClient.NewVpcClient()
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("2016-04-28"), StringPointer("AK"), nil, request, &runtime)
response, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, true)
if err != nil {
if NeedRetry(err) {
wait()
Expand Down Expand Up @@ -103,9 +96,9 @@ func testSweepVpnGatewayVpnAttachment(region string) error {
action := "DeleteVpnAttachment"
request := map[string]interface{}{
"VpnConnectionId": item["VpnConnectionId"],
"RegionId": aliyunClient.RegionId,
"RegionId": client.RegionId,
}
_, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
_, err = client.RpcPost("Vpc", "2016-04-28", action, nil, request, false)
if err != nil {
log.Printf("[ERROR] Failed to delete Vpn Gateway Vpn Attachment (%s): %s", name, err)
}
Expand Down
Loading

0 comments on commit 9208cdc

Please sign in to comment.