Skip to content

Commit e69ddc6

Browse files
Linyushlinyus
andauthored
feat: add vpn_gateway_route resource and data_source (#644)
Co-authored-by: linyus <[email protected]>
1 parent eccbca2 commit e69ddc6

8 files changed

+737
-4
lines changed

examples/tencentcloud-vpn/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ data "tencentcloud_vpn_connections" "example" {
6868
# vpn tunnel in the usual way.
6969
resource tencentcloud_vpn_gateway ccn_vpngw_example {
7070
name = "ccn-vpngw-example"
71+
vpc_id = data.tencentcloud_vpc_instances.example.instance_list.0.vpc_id
7172
bandwidth = 5
7273
zone = var.availability_zone
7374
type = "CCN"
@@ -76,3 +77,16 @@ resource tencentcloud_vpn_gateway ccn_vpngw_example {
7677
test = "ccn-vpngw-example"
7778
}
7879
}
80+
81+
resource "tencentcloud_vpn_gateway_route" "example" {
82+
vpn_gateway_id = tencentcloud_vpn_gateway.example.id
83+
destination_cidr_block = "10.0.0.0/16"
84+
instance_type = "VPNCONN"
85+
instance_id = "vpnx-5b5dmao3"
86+
priority = "100"
87+
status = "DISABLE"
88+
}
89+
90+
data "tencentcloud_vpn_gateway_routes" "example" {
91+
vpn_gateway_id = tencentcloud_vpn_gateway.example.id
92+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Use this data source to query detailed information of VPN gateways.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_vpn_gateways" "foo" {
8+
vpn_gateway_id = "main"
9+
destination_cidr_block = "vpngw-8ccsnclt"
10+
instance_type = "1.1.1.1"
11+
instance_id = "ap-guangzhou-3"
12+
tags = {
13+
test = "tf"
14+
}
15+
}a
16+
```
17+
*/
18+
package tencentcloud
19+
20+
import (
21+
"context"
22+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
23+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
24+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
25+
"log"
26+
)
27+
28+
func dataSourceTencentCloudVpnGatewayRoutes() *schema.Resource {
29+
return &schema.Resource{
30+
Read: dataSourceTencentCloudVpnGatewayRoutesRead,
31+
32+
Schema: map[string]*schema.Schema{
33+
"vpn_gateway_id": {
34+
Type: schema.TypeString,
35+
Required: true,
36+
Description: "VPN gateway ID.",
37+
},
38+
"destination_cidr": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
Description: "Destination IDC IP range.",
42+
},
43+
"instance_type": {
44+
Type: schema.TypeString,
45+
Optional: true,
46+
Description: "Next hop type (type of the associated instance). Valid values: VPNCONN (VPN tunnel) and CCN (CCN instance).",
47+
},
48+
"instance_id": {
49+
Type: schema.TypeString,
50+
Optional: true,
51+
Description: "Instance ID of the next hop.",
52+
},
53+
"result_output_file": {
54+
Type: schema.TypeString,
55+
Optional: true,
56+
Description: "Used to save results.",
57+
},
58+
59+
// Computed values
60+
"vpn_gateway_route_list": {
61+
Type: schema.TypeList,
62+
Computed: true,
63+
Description: "Information list of the vpn gateway routes.",
64+
Elem: &schema.Resource{
65+
Schema: VpnGatewayRoutePara(),
66+
},
67+
},
68+
},
69+
}
70+
}
71+
72+
func dataSourceTencentCloudVpnGatewayRoutesRead(d *schema.ResourceData, meta interface{}) error {
73+
defer logElapsed("data_source.tencentcloud_vpn_gateway_routes.read")()
74+
75+
var (
76+
logId = getLogId(contextNil)
77+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
78+
vpnGatewayId = d.Get("vpn_gateway_id").(string)
79+
vpcService = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
80+
)
81+
82+
params := make(map[string]string)
83+
if v, ok := d.GetOk("destination_cidr"); ok {
84+
params["DestinationCidr"] = v.(string)
85+
}
86+
if v, ok := d.GetOk("instance_id"); ok {
87+
params["InstanceId"] = v.(string)
88+
}
89+
if v, ok := d.GetOk("instance_type"); ok {
90+
params["InstanceType"] = v.(string)
91+
}
92+
93+
filters := make([]*vpc.Filter, 0, len(params))
94+
for k, v := range params {
95+
filter := &vpc.Filter{
96+
Name: helper.String(k),
97+
Values: []*string{helper.String(v)},
98+
}
99+
filters = append(filters, filter)
100+
}
101+
err, result := vpcService.DescribeVpnGatewayRoutes(ctx, vpnGatewayId, filters)
102+
if err != nil {
103+
log.Printf("[CRITAL]%s read VPN gateway routes failed, reason:%s\n ", logId, err.Error())
104+
return err
105+
}
106+
ids := make([]string, 0, len(result))
107+
routeList := make([]map[string]interface{}, 0, len(result))
108+
for _, route := range result {
109+
routeList = append(routeList, ConverterVpnGatewayRouteToMap(vpnGatewayId, route))
110+
ids = append(ids, *route.RouteId)
111+
}
112+
d.SetId(helper.DataResourceIdsHash(ids))
113+
if e := d.Set("vpn_gateway_route_list", routeList); e != nil {
114+
log.Printf("[CRITAL]%s provider set vpn gateway route list fail, reason:%s\n ", logId, e.Error())
115+
return e
116+
}
117+
118+
output, ok := d.GetOk("result_output_file")
119+
if ok && output.(string) != "" {
120+
if e := writeToFile(output.(string), routeList); e != nil {
121+
return e
122+
}
123+
}
124+
125+
return nil
126+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudVpnGatewayRoutesDataSource(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() { testAccPreCheck(t) },
12+
Providers: testAccProviders,
13+
Steps: []resource.TestStep{
14+
{
15+
Config: testAccTencentCloudVpnGatewayRoutesDataSourceConfig_basic,
16+
Check: resource.ComposeTestCheckFunc(
17+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_vpn_gateway_routes.routes"),
18+
resource.TestCheckResourceAttr("data.tencentcloud_vpn_gateway_routes.routes", "vpn_gateway_route_list.#", "1"),
19+
resource.TestCheckResourceAttr("data.tencentcloud_vpn_gateway_routes.routes", "vpn_gateway_route_list.0.destination_cidr_block", "10.0.0.0/16"),
20+
resource.TestCheckResourceAttr("data.tencentcloud_vpn_gateway_routes.routes", "vpn_gateway_route_list.0.instance_type", "VPNCONN"),
21+
resource.TestCheckResourceAttr("data.tencentcloud_vpn_gateway_routes.routes", "vpn_gateway_route_list.0.priority", "100"),
22+
resource.TestCheckResourceAttr("data.tencentcloud_vpn_gateway_routes.routes", "vpn_gateway_route_list.0.status", "ENABLE"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_vpn_gateway_routes.routes", "vpn_gateway_route_list.0.type"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_vpn_gateway_routes.routes", "vpn_gateway_route_list.0.route_id"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
const testAccTencentCloudVpnGatewayRoutesDataSourceConfig_basic = `
32+
data "tencentcloud_vpn_gateways" "foo" {
33+
name = "Default-VPC"
34+
}
35+
36+
data "tencentcloud_vpn_gateway_connections" "conns" {
37+
}
38+
39+
resource "tencentcloud_vpn_gateway_route" "route1" {
40+
vpn_gateway_id = data.tencentcloud_vpn_gateways.foo.gateway_list.0.id
41+
destination_cidr_block = "10.0.0.0/16"
42+
instance_type = "VPNCONN"
43+
instance_id = data.tencentcloud_vpn_gateway_connections.conns.connection_list.0.id
44+
priority = "100"
45+
status = "ENABLE"
46+
}
47+
48+
data "tencentcloud_vpn_gateway_routes" "routes" {
49+
vpn_gateway_id = data.tencentcloud_vpn_gateways.foo.gateway_list.0.id
50+
}
51+
`

tencentcloud/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,12 @@ VPN
521521
tencentcloud_vpn_connections
522522
tencentcloud_vpn_customer_gateways
523523
tencentcloud_vpn_gateways
524+
tencentcloud_vpn_gateway_routes
524525
525526
Resource
526527
tencentcloud_vpn_customer_gateway
527528
tencentcloud_vpn_gateway
529+
tencentcloud_vpn_gateway_route
528530
tencentcloud_vpn_connection
529531
*/
530532
package tencentcloud
@@ -664,6 +666,7 @@ func Provider() terraform.ResourceProvider {
664666
"tencentcloud_nat_gateways": dataSourceTencentCloudNatGateways(),
665667
"tencentcloud_vpn_customer_gateways": dataSourceTencentCloudVpnCustomerGateways(),
666668
"tencentcloud_vpn_gateways": dataSourceTencentCloudVpnGateways(),
669+
"tencentcloud_vpn_gateway_routes": dataSourceTencentCloudVpnGatewayRoutes(),
667670
"tencentcloud_vpn_connections": dataSourceTencentCloudVpnConnections(),
668671
"tencentcloud_ha_vips": dataSourceTencentCloudHaVips(),
669672
"tencentcloud_ha_vip_eip_attachments": dataSourceTencentCloudHaVipEipAttachments(),
@@ -824,6 +827,7 @@ func Provider() terraform.ResourceProvider {
824827
"tencentcloud_dc_gateway_ccn_route": resourceTencentCloudDcGatewayCcnRouteInstance(),
825828
"tencentcloud_vpn_customer_gateway": resourceTencentCloudVpnCustomerGateway(),
826829
"tencentcloud_vpn_gateway": resourceTencentCloudVpnGateway(),
830+
"tencentcloud_vpn_gateway_route": resourceTencentCloudVpnGatewayRoute(),
827831
"tencentcloud_vpn_connection": resourceTencentCloudVpnConnection(),
828832
"tencentcloud_ha_vip": resourceTencentCloudHaVip(),
829833
"tencentcloud_ha_vip_eip_attachment": resourceTencentCloudHaVipEipAttachment(),

0 commit comments

Comments
 (0)