|
| 1 | +/* |
| 2 | +Provides a resource to create a vpc vpn_gateway_ssl_client_cert |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_vpn_gateway_ssl_client_cert" "vpn_gateway_ssl_client_cert" { |
| 8 | + ssl_vpn_client_id = "vpnc-123456" |
| 9 | + switch = "off" |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +Import |
| 14 | +
|
| 15 | +vpc vpn_gateway_ssl_client_cert can be imported using the id, e.g. |
| 16 | +
|
| 17 | +``` |
| 18 | +terraform import tencentcloud_vpn_gateway_ssl_client_cert.vpn_gateway_ssl_client_cert ssl_client_id |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "log" |
| 26 | + |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 29 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 30 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 31 | +) |
| 32 | + |
| 33 | +func resourceTencentCloudVpnGatewaySslClientCert() *schema.Resource { |
| 34 | + return &schema.Resource{ |
| 35 | + Create: resourceTencentCloudVpnGatewaySslClientCertCreate, |
| 36 | + Read: resourceTencentCloudVpnGatewaySslClientCertRead, |
| 37 | + Update: resourceTencentCloudVpnGatewaySslClientCertUpdate, |
| 38 | + Delete: resourceTencentCloudVpnGatewaySslClientCertDelete, |
| 39 | + Importer: &schema.ResourceImporter{ |
| 40 | + State: schema.ImportStatePassthrough, |
| 41 | + }, |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "ssl_vpn_client_id": { |
| 44 | + Required: true, |
| 45 | + Type: schema.TypeString, |
| 46 | + Description: "SSL-VPN-CLIENT Instance ID.", |
| 47 | + }, |
| 48 | + |
| 49 | + "switch": { |
| 50 | + Optional: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Default: "on", |
| 53 | + ValidateFunc: validateAllowedStringValue([]string{"on", "off"}), |
| 54 | + Description: "`on`: Enable, `off`: Disable.", |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func resourceTencentCloudVpnGatewaySslClientCertCreate(d *schema.ResourceData, meta interface{}) error { |
| 61 | + defer logElapsed("resource.tencentcloud_vpn_gateway_ssl_client_cert.create")() |
| 62 | + defer inconsistentCheck(d, meta)() |
| 63 | + |
| 64 | + sslVpnClientId := d.Get("ssl_vpn_client_id").(string) |
| 65 | + d.SetId(sslVpnClientId) |
| 66 | + |
| 67 | + return resourceTencentCloudVpnGatewaySslClientCertUpdate(d, meta) |
| 68 | +} |
| 69 | + |
| 70 | +func resourceTencentCloudVpnGatewaySslClientCertRead(d *schema.ResourceData, meta interface{}) error { |
| 71 | + defer logElapsed("resource.tencentcloud_vpn_gateway_ssl_client_cert.read")() |
| 72 | + defer inconsistentCheck(d, meta)() |
| 73 | + |
| 74 | + logId := getLogId(contextNil) |
| 75 | + |
| 76 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 77 | + |
| 78 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 79 | + |
| 80 | + sslVpnClientId := d.Id() |
| 81 | + |
| 82 | + _, vpnGatewaySslClientCert, err := service.DescribeVpnSslClientById(ctx, sslVpnClientId) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + if vpnGatewaySslClientCert == nil { |
| 88 | + d.SetId("") |
| 89 | + log.Printf("[WARN]%s resource `VpnGatewaySslClientCert` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 90 | + return nil |
| 91 | + } |
| 92 | + |
| 93 | + if vpnGatewaySslClientCert.SslVpnClientId != nil { |
| 94 | + _ = d.Set("ssl_vpn_client_id", vpnGatewaySslClientCert.SslVpnClientId) |
| 95 | + } |
| 96 | + |
| 97 | + if vpnGatewaySslClientCert.CertStatus != nil { |
| 98 | + if *vpnGatewaySslClientCert.CertStatus == 1 { |
| 99 | + _ = d.Set("switch", "on") |
| 100 | + } |
| 101 | + if *vpnGatewaySslClientCert.CertStatus == 2 { |
| 102 | + _ = d.Set("switch", "off") |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func resourceTencentCloudVpnGatewaySslClientCertUpdate(d *schema.ResourceData, meta interface{}) error { |
| 110 | + defer logElapsed("resource.tencentcloud_vpn_gateway_ssl_client_cert.update")() |
| 111 | + defer inconsistentCheck(d, meta)() |
| 112 | + |
| 113 | + var taskId *uint64 |
| 114 | + |
| 115 | + logId := getLogId(contextNil) |
| 116 | + |
| 117 | + sslVpnClientId := d.Id() |
| 118 | + |
| 119 | + certSwitch := d.Get("switch").(string) |
| 120 | + |
| 121 | + if certSwitch == "on" { |
| 122 | + |
| 123 | + var ( |
| 124 | + request = vpc.NewEnableVpnGatewaySslClientCertRequest() |
| 125 | + ) |
| 126 | + |
| 127 | + request.SslVpnClientId = &sslVpnClientId |
| 128 | + |
| 129 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 130 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().EnableVpnGatewaySslClientCert(request) |
| 131 | + if e != nil { |
| 132 | + return retryError(e) |
| 133 | + } else { |
| 134 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 135 | + } |
| 136 | + taskId = result.Response.TaskId |
| 137 | + return nil |
| 138 | + }) |
| 139 | + if err != nil { |
| 140 | + log.Printf("[CRITAL]%s enable vpc vpnGatewaySslClientCert failed, reason:%+v", logId, err) |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + } else { |
| 145 | + |
| 146 | + var ( |
| 147 | + request = vpc.NewDisableVpnGatewaySslClientCertRequest() |
| 148 | + ) |
| 149 | + |
| 150 | + request.SslVpnClientId = &sslVpnClientId |
| 151 | + |
| 152 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 153 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().DisableVpnGatewaySslClientCert(request) |
| 154 | + if e != nil { |
| 155 | + return retryError(e) |
| 156 | + } else { |
| 157 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 158 | + } |
| 159 | + taskId = result.Response.TaskId |
| 160 | + return nil |
| 161 | + }) |
| 162 | + if err != nil { |
| 163 | + log.Printf("[CRITAL]%s disable vpc vpnGatewaySslClientCert failed, reason:%+v", logId, err) |
| 164 | + return err |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 169 | + service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 170 | + |
| 171 | + err := service.DescribeVpcTaskResult(ctx, helper.String(helper.UInt64ToStr(*taskId))) |
| 172 | + if err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + |
| 176 | + return resourceTencentCloudVpnGatewaySslClientCertRead(d, meta) |
| 177 | +} |
| 178 | + |
| 179 | +func resourceTencentCloudVpnGatewaySslClientCertDelete(d *schema.ResourceData, meta interface{}) error { |
| 180 | + defer logElapsed("resource.tencentcloud_vpn_gateway_ssl_client_cert.delete")() |
| 181 | + defer inconsistentCheck(d, meta)() |
| 182 | + |
| 183 | + return nil |
| 184 | +} |
0 commit comments