Skip to content

Commit c6077b7

Browse files
author
hellertang
authored
add vpngw ssl server (#896)
1 parent b44c0ab commit c6077b7

File tree

13 files changed

+9868
-290
lines changed

13 files changed

+9868
-290
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ require (
5656
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq v1.0.268
5757
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.302
5858
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199
59-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.199
59+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.357
6060
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199
6161
github.com/tencentyun/cos-go-sdk-v5 v0.7.33
6262
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199 h1:6Yt74l4
562562
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199/go.mod h1:Yw6OQ33z3s4k0HVYTNSffB12qOzEJ2Zf1Vj4+5S3sRs=
563563
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.199 h1:UDZ59pvaqjDy2QIsMsv9hxm0BEJLmPIbHF1ms0MqaRk=
564564
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.199/go.mod h1:SKgeSsIfPEM6BeoIFiGHsWG9UsEXzkK0SkWx51H/OS8=
565+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.357 h1:fltPXAHX1pQRXX94LEzJbWaDzNsculh70XA9NN+9DFY=
566+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.357/go.mod h1:vuWreRMZo2kArdDtpXIjTf//ckEMRSot1msuSsmtQ0I=
565567
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199 h1:hMBLtiJPnZ9GvA677cTB6ELBR6B68wCR2QY1sNoGQc4=
566568
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199/go.mod h1:nnY91/H3j/Gu7V/oCA6Zeg8T5D3q36EUdBh4EjmHwqY=
567569
github.com/tencentyun/cos-go-sdk-v5 v0.7.31-0.20210902132439-360bc9b1be6b h1:rLl5sAeLt382023Kd3X4TaOEaT2hdgXWwTGyKiy16Zo=

tencentcloud/data_source_tc_vpn_gateway_routes_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ data "tencentcloud_vpn_gateways" "foo" {
3434
name = "Default-VPC"
3535
}
3636
37-
data "tencentcloud_vpn_gateway_connections" "conns" {
37+
data "tencentcloud_vpn_connections" "conns" {
3838
}
3939
4040
resource "tencentcloud_vpn_gateway_route" "route1" {
4141
vpn_gateway_id = data.tencentcloud_vpn_gateways.foo.gateway_list.0.id
4242
destination_cidr_block = "10.0.0.0/16"
4343
instance_type = "VPNCONN"
44-
instance_id = data.tencentcloud_vpn_gateway_connections.conns.connection_list.0.id
44+
instance_id = data.tencentcloud_vpn_connection.conns.connection_list.0.id
4545
priority = "100"
4646
status = "ENABLE"
4747
}

tencentcloud/extension_vpc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ const (
124124
VPN_DESCRIBE_LIMIT = 100
125125
)
126126

127+
const (
128+
VPN_TASK_STATUS_SUCCESS = "SUCCESS"
129+
VPN_TASK_STATUS_RUNNING = "RUNNING"
130+
VPN_TASK_STATUS_FAILED = "FAILED"
131+
)
132+
127133
const (
128134
VPN_STATE_PENDING = "PENDING"
129135
VPN_STATE_DELETING = "DELETING"

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ VPN
575575
tencentcloud_vpn_gateway
576576
tencentcloud_vpn_gateway_route
577577
tencentcloud_vpn_connection
578+
tencentcloud_vpn_ssl_server
578579
579580
EMR
580581
Data Source
@@ -915,6 +916,7 @@ func Provider() terraform.ResourceProvider {
915916
"tencentcloud_vpn_gateway": resourceTencentCloudVpnGateway(),
916917
"tencentcloud_vpn_gateway_route": resourceTencentCloudVpnGatewayRoute(),
917918
"tencentcloud_vpn_connection": resourceTencentCloudVpnConnection(),
919+
"tencentcloud_vpn_ssl_server": resourceTencentCloudVpnSslServer(),
918920
"tencentcloud_ha_vip": resourceTencentCloudHaVip(),
919921
"tencentcloud_ha_vip_eip_attachment": resourceTencentCloudHaVipEipAttachment(),
920922
"tencentcloud_security_group": resourceTencentCloudSecurityGroup(),
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
/*
2+
Provide a resource to create a VPN SSL Server.
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_vpn_ssl_server" "server" {
8+
local_address = [
9+
"10.0.0.0/17",
10+
]
11+
remote_address = "11.0.0.0/16"
12+
ssl_vpn_server_name = "helloworld"
13+
vpn_gateway_id = "vpngw-335lwf7d"
14+
ssl_vpn_protocol = "UDP"
15+
ssl_vpn_port = 1194
16+
integrity_algorithm = "MD5"
17+
encrypt_algorithm = "AES-128-CBC"
18+
compress = true
19+
}
20+
```
21+
22+
Import
23+
24+
VPN SSL Server can be imported, e.g.
25+
26+
```
27+
$ terraform import tencentcloud_vpn_ssl_server.server vpn-server-id
28+
```
29+
*/
30+
package tencentcloud
31+
32+
import (
33+
"context"
34+
"fmt"
35+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
36+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
37+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit"
38+
"log"
39+
"time"
40+
41+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
42+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
43+
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
44+
)
45+
46+
func resourceTencentCloudVpnSslServer() *schema.Resource {
47+
return &schema.Resource{
48+
Create: resourceTencentCloudVpnSslServerCreate,
49+
Read: resourceTencentCloudVpnSslServerRead,
50+
Delete: resourceTencentCloudVpnSslServerDelete,
51+
Importer: &schema.ResourceImporter{
52+
State: schema.ImportStatePassthrough,
53+
},
54+
55+
Schema: map[string]*schema.Schema{
56+
"vpn_gateway_id": {
57+
Type: schema.TypeString,
58+
Required: true,
59+
ForceNew: true,
60+
Description: "VPN gateway ID.",
61+
},
62+
"ssl_vpn_server_name": {
63+
Type: schema.TypeString,
64+
Required: true,
65+
ForceNew: true,
66+
Description: "The name of ssl vpn server to be created.",
67+
},
68+
"local_address": {
69+
Type: schema.TypeList,
70+
Required: true,
71+
ForceNew: true,
72+
Description: "List of local CIDR.",
73+
Elem: &schema.Schema{
74+
Type: schema.TypeString,
75+
},
76+
},
77+
"remote_address": {
78+
Type: schema.TypeString,
79+
Required: true,
80+
ForceNew: true,
81+
Description: "Remote CIDR for client.",
82+
},
83+
"ssl_vpn_protocol": {
84+
Type: schema.TypeString,
85+
Optional: true,
86+
ForceNew: true,
87+
Description: "The protocol of ssl vpn. Default value: UDP.",
88+
},
89+
"ssl_vpn_port": {
90+
Type: schema.TypeInt,
91+
Optional: true,
92+
ForceNew: true,
93+
Description: "The port of ssl vpn. Default value: 1194.",
94+
},
95+
"integrity_algorithm": {
96+
Type: schema.TypeString,
97+
Optional: true,
98+
ForceNew: true,
99+
Description: "The integrity algorithm. Valid values: SHA1, MD5 and NONE. Default value: NONE.",
100+
},
101+
"encrypt_algorithm": {
102+
Type: schema.TypeString,
103+
Optional: true,
104+
ForceNew: true,
105+
Description: "The encrypt algorithm. Valid values: AES-128-CBC, AES-192-CBC, AES-256-CBC, NONE." +
106+
"Default value: NONE.",
107+
},
108+
"compress": {
109+
Type: schema.TypeBool,
110+
Optional: true,
111+
ForceNew: true,
112+
Default: FALSE,
113+
Description: "need compressed. Default value: False.",
114+
},
115+
},
116+
}
117+
}
118+
119+
func resourceTencentCloudVpnSslServerCreate(d *schema.ResourceData, meta interface{}) error {
120+
defer logElapsed("resource.tencentcloud_vpn_ssl_server.create")()
121+
logId := getLogId(contextNil)
122+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
123+
124+
var (
125+
vpcService = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
126+
request = vpc.NewCreateVpnGatewaySslServerRequest()
127+
vpnGatewayId string
128+
)
129+
130+
if v, ok := d.GetOk("vpn_gateway_id"); ok {
131+
vpnGatewayId = v.(string)
132+
request.VpnGatewayId = helper.String(vpnGatewayId)
133+
}
134+
if v, ok := d.GetOk("ssl_vpn_server_name"); ok {
135+
request.SslVpnServerName = helper.String(v.(string))
136+
}
137+
138+
if v, ok := d.GetOk("local_address"); ok {
139+
address := v.([]interface{})
140+
request.LocalAddress = helper.InterfacesStringsPoint(address)
141+
}
142+
143+
if v, ok := d.GetOk("remote_address"); ok {
144+
request.RemoteAddress = helper.String(v.(string))
145+
}
146+
147+
if v, ok := d.GetOk("ssl_vpn_protocol"); ok {
148+
request.SslVpnProtocol = helper.String(v.(string))
149+
}
150+
if v, ok := d.GetOk("ssl_vpn_port"); ok {
151+
request.SslVpnPort = helper.IntInt64(v.(int))
152+
}
153+
if v, ok := d.GetOk("integrity_algorithm"); ok {
154+
request.IntegrityAlgorithm = helper.String(v.(string))
155+
}
156+
if v, ok := d.GetOk("encrypt_algorithm"); ok {
157+
request.EncryptAlgorithm = helper.String(v.(string))
158+
}
159+
if v, ok := d.GetOk("compress"); ok {
160+
request.Compress = helper.Bool(v.(bool))
161+
}
162+
163+
var taskId *int64
164+
if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
165+
ratelimit.Check(request.GetAction())
166+
response, err := vpcService.client.UseVpcClient().CreateVpnGatewaySslServer(request)
167+
if err != nil {
168+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
169+
logId, request.GetAction(), request.ToJsonString(), err.Error())
170+
return retryError(err, InternalError)
171+
}
172+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
173+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
174+
taskId = response.Response.TaskId
175+
return nil
176+
}); err != nil {
177+
return err
178+
}
179+
180+
err := vpcService.DescribeTaskResult(ctx, helper.Int64Uint64(*taskId))
181+
if err != nil {
182+
return err
183+
}
184+
185+
// add protect
186+
time.Sleep(3)
187+
188+
filter := make(map[string]string)
189+
filter["vpn-gateway-id"] = vpnGatewayId
190+
191+
instances, err := vpcService.DescribeVpnGwSslServerByFilter(ctx, filter)
192+
193+
if err != nil {
194+
return fmt.Errorf("get instance list error: %s", err.Error())
195+
}
196+
197+
sslServer := instances[0]
198+
d.SetId(*sslServer.SslVpnServerId)
199+
200+
return resourceTencentCloudVpnSslServerRead(d, meta)
201+
}
202+
203+
func resourceTencentCloudVpnSslServerRead(d *schema.ResourceData, meta interface{}) error {
204+
defer logElapsed("resource.tencentcloud_vpn_ssl_server.read")()
205+
defer inconsistentCheck(d, meta)()
206+
207+
logId := getLogId(contextNil)
208+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
209+
210+
sslServerId := d.Id()
211+
vpcService := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
212+
213+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
214+
has, info, e := vpcService.DescribeVpnSslServerById(ctx, sslServerId)
215+
if e != nil {
216+
return retryError(e)
217+
}
218+
if !has {
219+
d.SetId("")
220+
return nil
221+
}
222+
223+
_ = d.Set("vpn_gateway_id", info.VpnGatewayId)
224+
_ = d.Set("ssl_vpn_server_name", info.SslVpnServerName)
225+
_ = d.Set("local_address", helper.StringsInterfaces(info.LocalAddress))
226+
_ = d.Set("remote_address", info.RemoteAddress)
227+
if _, ok := d.GetOk("ssl_vpn_protocol"); ok {
228+
_ = d.Set("ssl_vpn_protocol", info.SslVpnProtocol)
229+
}
230+
if _, ok := d.GetOk("ssl_vpn_port"); ok {
231+
_ = d.Set("ssl_vpn_port", info.SslVpnPort)
232+
}
233+
if _, ok := d.GetOk("integrity_algorithm"); ok {
234+
_ = d.Set("integrity_algorithm", info.IntegrityAlgorithm)
235+
}
236+
if _, ok := d.GetOk("encrypt_algorithm"); ok {
237+
_ = d.Set("encrypt_algorithm", info.EncryptAlgorithm)
238+
}
239+
if _, ok := d.GetOk("compress"); ok {
240+
_ = d.Set("compress", info.Compress)
241+
}
242+
return nil
243+
})
244+
if err != nil {
245+
return err
246+
}
247+
return nil
248+
}
249+
250+
func resourceTencentCloudVpnSslServerDelete(d *schema.ResourceData, meta interface{}) error {
251+
defer logElapsed("resource.tencentcloud_vpn_ssl_server.delete")()
252+
253+
logId := getLogId(contextNil)
254+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
255+
256+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
257+
258+
serverId := d.Id()
259+
260+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
261+
if err := service.DeleteVpnGatewaySslServer(ctx, serverId); err != nil {
262+
if sdkErr, ok := err.(*errors.TencentCloudSDKError); ok {
263+
if sdkErr.Code == VPCNotFound {
264+
return nil
265+
}
266+
}
267+
return resource.RetryableError(err)
268+
}
269+
return nil
270+
})
271+
272+
return err
273+
}

0 commit comments

Comments
 (0)