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 e76e373 commit aa4487b
Show file tree
Hide file tree
Showing 18 changed files with 270 additions and 670 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
30 changes: 19 additions & 11 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 All @@ -20,7 +19,7 @@ import (

func dataSourceAlicloudVpnGateways() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudVpnsRead,
Read: dataSourceAlicloudVpnGatewaysRead,

Schema: map[string]*schema.Schema{
"ids": {
Expand Down Expand Up @@ -70,6 +69,12 @@ func dataSourceAlicloudVpnGateways() *schema.Resource {
ForceNew: true,
Deprecated: "Field 'enable_ipsec' has been deprecated from provider version 1.193.0 and it will be removed in the future version.",
},
"ssl_vpn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"enable", "disable"}, false),
},
"include_reservation_data": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -138,6 +143,10 @@ func dataSourceAlicloudVpnGateways() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"ssl_vpn": {
Type: schema.TypeString,
Computed: true,
},
"ssl_connections": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -185,7 +194,7 @@ func dataSourceAlicloudVpnGateways() *schema.Resource {
}
}

func dataSourceAlicloudVpnsRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceAlicloudVpnGatewaysRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)

action := "DescribeVpnGateways"
Expand Down Expand Up @@ -228,20 +237,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 Expand Up @@ -281,10 +284,14 @@ func dataSourceAlicloudVpnsRead(d *schema.ResourceData, meta interface{}) error
request["PageNumber"] = request["PageNumber"].(int) + 1
}

sslVpn := d.Get("ssl_vpn").(string)
ids := make([]string, 0)
names := make([]interface{}, 0)
s := make([]map[string]interface{}, 0)
for _, object := range objects {
if sslVpn != "" && sslVpn != fmt.Sprint(object["SslVpn"]) {
continue
}
mapping := map[string]interface{}{
"id": object["VpnGatewayId"],
"vpc_id": object["VpcId"],
Expand All @@ -297,6 +304,7 @@ func dataSourceAlicloudVpnsRead(d *schema.ResourceData, meta interface{}) error
"instance_charge_type": convertChargeType(object["ChargeType"].(string)),
"enable_ipsec": object["IpsecVpn"],
"enable_ssl": object["SslVpn"],
"ssl_vpn": object["SslVpn"],
"ssl_connections": object["SslMaxConnections"],
"network_type": object["NetworkType"],
"disaster_recovery_vswitch_id": object["DisasterRecoveryVSwitchId"],
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
2 changes: 1 addition & 1 deletion alicloud/data_source_alicloud_vpn_ipsec_servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ locals {
data "alicloud_vpn_gateways" "default" {
vpc_id = data.alicloud_vpcs.default.ids.0
enable_ipsec = true
ssl_vpn = "enable"
}
locals {
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 aa4487b

Please sign in to comment.