|
| 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