Skip to content

Commit 2e34eeb

Browse files
author
hellertang
authored
add vpngw ssl client (#897)
1 parent 08883ec commit 2e34eeb

File tree

5 files changed

+327
-0
lines changed

5 files changed

+327
-0
lines changed

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ VPN
576576
tencentcloud_vpn_gateway_route
577577
tencentcloud_vpn_connection
578578
tencentcloud_vpn_ssl_server
579+
tencentcloud_vpn_ssl_client
579580
580581
EMR
581582
Data Source
@@ -919,6 +920,7 @@ func Provider() terraform.ResourceProvider {
919920
"tencentcloud_vpn_gateway_route": resourceTencentCloudVpnGatewayRoute(),
920921
"tencentcloud_vpn_connection": resourceTencentCloudVpnConnection(),
921922
"tencentcloud_vpn_ssl_server": resourceTencentCloudVpnSslServer(),
923+
"tencentcloud_vpn_ssl_client": resourceTencentCloudVpnSslClient(),
922924
"tencentcloud_ha_vip": resourceTencentCloudHaVip(),
923925
"tencentcloud_ha_vip_eip_attachment": resourceTencentCloudHaVipEipAttachment(),
924926
"tencentcloud_security_group": resourceTencentCloudSecurityGroup(),
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
Provide a resource to create a VPN SSL Client.
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_vpn_ssl_client" "client" {
8+
ssl_vpn_server_id = "vpns-aog5xcjj"
9+
ssl_vpn_client_name = "hello"
10+
}
11+
12+
```
13+
14+
Import
15+
16+
VPN SSL Client can be imported, e.g.
17+
18+
```
19+
$ terraform import tencentcloud_vpn_ssl_client.client vpn-client-id
20+
```
21+
*/
22+
package tencentcloud
23+
24+
import (
25+
"context"
26+
"fmt"
27+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
28+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
29+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit"
30+
"log"
31+
"time"
32+
33+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
34+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
35+
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
36+
)
37+
38+
func resourceTencentCloudVpnSslClient() *schema.Resource {
39+
return &schema.Resource{
40+
Create: resourceTencentCloudVpnSslClientCreate,
41+
Read: resourceTencentCloudVpnSslClientRead,
42+
Delete: resourceTencentCloudVpnSslClientDelete,
43+
Importer: &schema.ResourceImporter{
44+
State: schema.ImportStatePassthrough,
45+
},
46+
47+
Schema: map[string]*schema.Schema{
48+
"ssl_vpn_server_id": {
49+
Type: schema.TypeString,
50+
Required: true,
51+
ForceNew: true,
52+
Description: "VPN ssl server id.",
53+
},
54+
"ssl_vpn_client_name": {
55+
Type: schema.TypeString,
56+
Required: true,
57+
ForceNew: true,
58+
Description: "The name of ssl vpn client to be created.",
59+
},
60+
},
61+
}
62+
}
63+
64+
func resourceTencentCloudVpnSslClientCreate(d *schema.ResourceData, meta interface{}) error {
65+
defer logElapsed("resource.tencentcloud_vpn_ssl_client.create")()
66+
logId := getLogId(contextNil)
67+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
68+
69+
var (
70+
vpcService = VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
71+
request = vpc.NewCreateVpnGatewaySslClientRequest()
72+
sslVpnServerId string
73+
)
74+
75+
if v, ok := d.GetOk("ssl_vpn_server_id"); ok {
76+
sslVpnServerId = v.(string)
77+
request.SslVpnServerId = helper.String(sslVpnServerId)
78+
}
79+
if v, ok := d.GetOk("ssl_vpn_client_name"); ok {
80+
request.SslVpnClientName = helper.String(v.(string))
81+
}
82+
83+
var taskId *uint64
84+
if err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
85+
ratelimit.Check(request.GetAction())
86+
response, err := vpcService.client.UseVpcClient().CreateVpnGatewaySslClient(request)
87+
if err != nil {
88+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
89+
logId, request.GetAction(), request.ToJsonString(), err.Error())
90+
return retryError(err, InternalError)
91+
}
92+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
93+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
94+
taskId = response.Response.TaskId
95+
return nil
96+
}); err != nil {
97+
return err
98+
}
99+
100+
err := vpcService.DescribeTaskResult(ctx, helper.Uint64(*taskId))
101+
if err != nil {
102+
return err
103+
}
104+
105+
// add protect
106+
time.Sleep(3)
107+
108+
filter := make(map[string]string)
109+
filter["ssl-vpn-server-id"] = sslVpnServerId
110+
111+
instances, err := vpcService.DescribeVpnGwSslClientByFilter(ctx, filter)
112+
113+
if err != nil {
114+
return fmt.Errorf("get instance list error: %s", err.Error())
115+
}
116+
117+
sslClient := instances[0]
118+
d.SetId(*sslClient.SslVpnClientId)
119+
120+
return resourceTencentCloudVpnSslClientRead(d, meta)
121+
}
122+
123+
func resourceTencentCloudVpnSslClientRead(d *schema.ResourceData, meta interface{}) error {
124+
defer logElapsed("resource.tencentcloud_vpn_ssl_client.read")()
125+
defer inconsistentCheck(d, meta)()
126+
127+
logId := getLogId(contextNil)
128+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
129+
130+
sslClientId := d.Id()
131+
vpcService := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
132+
133+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
134+
has, info, e := vpcService.DescribeVpnSslClientById(ctx, sslClientId)
135+
if e != nil {
136+
return retryError(e)
137+
}
138+
if !has {
139+
d.SetId("")
140+
return nil
141+
}
142+
143+
_ = d.Set("ssl_vpn_server_id", info.SslVpnServerId)
144+
_ = d.Set("ssl_vpn_client_name", info.Name)
145+
return nil
146+
})
147+
if err != nil {
148+
return err
149+
}
150+
return nil
151+
}
152+
153+
func resourceTencentCloudVpnSslClientDelete(d *schema.ResourceData, meta interface{}) error {
154+
defer logElapsed("resource.tencentcloud_vpn_ssl_client.delete")()
155+
156+
logId := getLogId(contextNil)
157+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
158+
159+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
160+
161+
sslClientId := d.Id()
162+
163+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
164+
if err := service.DeleteVpnGatewaySslClient(ctx, sslClientId); err != nil {
165+
if sdkErr, ok := err.(*errors.TencentCloudSDKError); ok {
166+
if sdkErr.Code == VPCNotFound {
167+
return nil
168+
}
169+
}
170+
return resource.RetryableError(err)
171+
}
172+
return nil
173+
})
174+
175+
return err
176+
}

tencentcloud/service_tencentcloud_vpc.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4346,6 +4346,107 @@ func (me *VpcService) DeleteVpnGatewaySslServer(ctx context.Context, SslServerId
43464346
return
43474347
}
43484348

4349+
func (me *VpcService) DescribeVpnSslClientById(ctx context.Context, sslId string) (has bool, gateway *vpc.SslVpnClient, err error) {
4350+
var (
4351+
logId = getLogId(ctx)
4352+
request = vpc.NewDescribeVpnGatewaySslClientsRequest()
4353+
response *vpc.DescribeVpnGatewaySslClientsResponse
4354+
)
4355+
request.SslVpnClientIds = []*string{&sslId}
4356+
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
4357+
response, err = me.client.UseVpcClient().DescribeVpnGatewaySslClients(request)
4358+
if err != nil {
4359+
ee, ok := err.(*sdkErrors.TencentCloudSDKError)
4360+
if !ok {
4361+
return retryError(err)
4362+
}
4363+
if ee.Code == VPCNotFound {
4364+
return nil
4365+
} else {
4366+
return retryError(err)
4367+
}
4368+
}
4369+
return nil
4370+
})
4371+
if err != nil {
4372+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%v]", logId, request.GetAction(), request.ToJsonString(), err)
4373+
return
4374+
}
4375+
if response == nil || response.Response == nil || len(response.Response.SslVpnClientSet) < 1 {
4376+
has = false
4377+
return
4378+
}
4379+
4380+
gateway = response.Response.SslVpnClientSet[0]
4381+
has = true
4382+
return
4383+
}
4384+
4385+
func (me *VpcService) DescribeVpnGwSslClientByFilter(ctx context.Context, filters map[string]string) (instances []*vpc.SslVpnClient, errRet error) {
4386+
var (
4387+
logId = getLogId(ctx)
4388+
request = vpc.NewDescribeVpnGatewaySslClientsRequest()
4389+
)
4390+
request.Filters = make([]*vpc.Filter, 0, len(filters))
4391+
for k, v := range filters {
4392+
filter := vpc.Filter{
4393+
Name: helper.String(k),
4394+
Values: []*string{helper.String(v)},
4395+
}
4396+
request.Filters = append(request.Filters, &filter)
4397+
}
4398+
4399+
var offset uint64 = 0
4400+
var pageSize uint64 = 100
4401+
instances = make([]*vpc.SslVpnClient, 0)
4402+
4403+
for {
4404+
request.Offset = &offset
4405+
request.Limit = &pageSize
4406+
ratelimit.Check(request.GetAction())
4407+
response, err := me.client.UseVpcClient().DescribeVpnGatewaySslClients(request)
4408+
if err != nil {
4409+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
4410+
logId, request.GetAction(), request.ToJsonString(), err.Error())
4411+
errRet = err
4412+
return
4413+
}
4414+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
4415+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
4416+
4417+
if response == nil || len(response.Response.SslVpnClientSet) < 1 {
4418+
break
4419+
}
4420+
instances = append(instances, response.Response.SslVpnClientSet...)
4421+
if len(response.Response.SslVpnClientSet) < int(pageSize) {
4422+
break
4423+
}
4424+
offset += pageSize
4425+
}
4426+
return
4427+
}
4428+
4429+
func (me *VpcService) DeleteVpnGatewaySslClient(ctx context.Context, SslClientId string) (errRet error) {
4430+
logId := getLogId(ctx)
4431+
request := vpc.NewDeleteVpnGatewaySslClientRequest()
4432+
defer func() {
4433+
if errRet != nil {
4434+
log.Printf("[CRITAL]%s api[%s] fail,reason[%s]", logId, request.GetAction(), errRet.Error())
4435+
}
4436+
}()
4437+
request.SslVpnClientId = &SslClientId
4438+
4439+
errRet = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
4440+
ratelimit.Check(request.GetAction())
4441+
_, errRet = me.client.UseVpcClient().DeleteVpnGatewaySslClient(request)
4442+
if errRet != nil {
4443+
return retryError(errRet, InternalError)
4444+
}
4445+
return nil
4446+
})
4447+
return
4448+
}
4449+
43494450
func (me *VpcService) CreateNatGatewaySnat(ctx context.Context, natGatewayId string, snat *vpc.SourceIpTranslationNatRule) (errRet error) {
43504451
logId := getLogId(ctx)
43514452
request := vpc.NewCreateNatGatewaySourceIpTranslationNatRuleRequest()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
subcategory: "VPN"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_vpn_ssl_client"
5+
sidebar_current: "docs-tencentcloud-resource-vpn_ssl_client"
6+
description: |-
7+
Provide a resource to create a VPN SSL Client.
8+
---
9+
10+
# tencentcloud_vpn_ssl_client
11+
12+
Provide a resource to create a VPN SSL Client.
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_vpn_ssl_client" "client" {
18+
ssl_vpn_server_id = "vpns-aog5xcjj"
19+
ssl_vpn_client_name = "hello"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `ssl_vpn_client_name` - (Required, ForceNew) The name of ssl vpn client to be created.
28+
* `ssl_vpn_server_id` - (Required, ForceNew) VPN ssl server id.
29+
30+
## Attributes Reference
31+
32+
In addition to all arguments above, the following attributes are exported:
33+
34+
* `id` - ID of the resource.
35+
36+
37+
38+
## Import
39+
40+
VPN SSL Client can be imported, e.g.
41+
42+
```
43+
$ terraform import tencentcloud_vpn_ssl_client.client vpn-client-id
44+
```
45+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,9 @@
14871487
<li>
14881488
<a href="/docs/providers/tencentcloud/r/vpn_gateway_route.html">tencentcloud_vpn_gateway_route</a>
14891489
</li>
1490+
<li>
1491+
<a href="/docs/providers/tencentcloud/r/vpn_ssl_client.html">tencentcloud_vpn_ssl_client</a>
1492+
</li>
14901493
<li>
14911494
<a href="/docs/providers/tencentcloud/r/vpn_ssl_server.html">tencentcloud_vpn_ssl_server</a>
14921495
</li>

0 commit comments

Comments
 (0)