|
| 1 | +/* |
| 2 | +Provides a resource to create a ssl replace_certificate |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_ssl_replace_certificate_operation" "replace_certificate" { |
| 8 | + certificate_id = "8L6JsWq2" |
| 9 | + valid_type = "DNS_AUTO" |
| 10 | + csr_type = "online" |
| 11 | +} |
| 12 | +``` |
| 13 | +
|
| 14 | +Import |
| 15 | +
|
| 16 | +ssl replace_certificate can be imported using the id, e.g. |
| 17 | +
|
| 18 | +``` |
| 19 | +terraform import tencentcloud_ssl_replace_certificate_operation.replace_certificate replace_certificate_id |
| 20 | +``` |
| 21 | +*/ |
| 22 | +package tencentcloud |
| 23 | + |
| 24 | +import ( |
| 25 | + "log" |
| 26 | + |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 29 | + ssl "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssl/v20191205" |
| 30 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 31 | +) |
| 32 | + |
| 33 | +func resourceTencentCloudSslReplaceCertificateOperation() *schema.Resource { |
| 34 | + return &schema.Resource{ |
| 35 | + Create: resourceTencentCloudSslReplaceCertificateCreate, |
| 36 | + Read: resourceTencentCloudSslReplaceCertificateRead, |
| 37 | + Delete: resourceTencentCloudSslReplaceCertificateDelete, |
| 38 | + Importer: &schema.ResourceImporter{ |
| 39 | + State: schema.ImportStatePassthrough, |
| 40 | + }, |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "certificate_id": { |
| 43 | + Required: true, |
| 44 | + ForceNew: true, |
| 45 | + Type: schema.TypeString, |
| 46 | + Description: "Certificate ID.", |
| 47 | + }, |
| 48 | + |
| 49 | + "valid_type": { |
| 50 | + Required: true, |
| 51 | + ForceNew: true, |
| 52 | + Type: schema.TypeString, |
| 53 | + Description: "Verification type: DNS_AUTO = automatic DNS verification (this verification type is only supported for domain names that are resolved by Tencent Cloud and have normal resolution status), DNS = manual DNS verification, FILE = file verification.", |
| 54 | + }, |
| 55 | + |
| 56 | + "csr_type": { |
| 57 | + Optional: true, |
| 58 | + ForceNew: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "Type, default Original. Available options: Original = original certificate CSR, Upload = manual upload, Online = online generation.", |
| 61 | + }, |
| 62 | + |
| 63 | + "csr_content": { |
| 64 | + Optional: true, |
| 65 | + ForceNew: true, |
| 66 | + Type: schema.TypeString, |
| 67 | + Description: "CSR Content.", |
| 68 | + }, |
| 69 | + |
| 70 | + "csr_key_password": { |
| 71 | + Optional: true, |
| 72 | + ForceNew: true, |
| 73 | + Type: schema.TypeString, |
| 74 | + Description: "KEY Password.", |
| 75 | + }, |
| 76 | + |
| 77 | + "reason": { |
| 78 | + Optional: true, |
| 79 | + ForceNew: true, |
| 80 | + Type: schema.TypeString, |
| 81 | + Description: "Reason for reissue.", |
| 82 | + }, |
| 83 | + |
| 84 | + "cert_csr_encrypt_algo": { |
| 85 | + Optional: true, |
| 86 | + ForceNew: true, |
| 87 | + Type: schema.TypeString, |
| 88 | + Description: "CSR encryption method, optional: RSA, ECC, SM2. (Selectable only if CsrType is Online), default is RSA.", |
| 89 | + }, |
| 90 | + |
| 91 | + "cert_csr_key_parameter": { |
| 92 | + Optional: true, |
| 93 | + ForceNew: true, |
| 94 | + Type: schema.TypeString, |
| 95 | + Description: "CSR encryption parameter, when CsrEncryptAlgo is RSA, you can choose 2048, 4096, etc., and the default is 2048; when CsrEncryptAlgo is ECC, you can choose prime256v1, secp384r1, etc., and the default is prime256v1;.", |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func resourceTencentCloudSslReplaceCertificateCreate(d *schema.ResourceData, meta interface{}) error { |
| 102 | + defer logElapsed("resource.tencentcloud_ssl_replace_certificate_operation.create")() |
| 103 | + defer inconsistentCheck(d, meta)() |
| 104 | + |
| 105 | + logId := getLogId(contextNil) |
| 106 | + |
| 107 | + var ( |
| 108 | + request = ssl.NewReplaceCertificateRequest() |
| 109 | + response = ssl.NewReplaceCertificateResponse() |
| 110 | + certificateId uint64 |
| 111 | + ) |
| 112 | + if v, ok := d.GetOk("certificate_id"); ok { |
| 113 | + request.CertificateId = helper.String(v.(string)) |
| 114 | + } |
| 115 | + |
| 116 | + if v, ok := d.GetOk("valid_type"); ok { |
| 117 | + request.ValidType = helper.String(v.(string)) |
| 118 | + } |
| 119 | + |
| 120 | + if v, ok := d.GetOk("csr_type"); ok { |
| 121 | + request.CsrType = helper.String(v.(string)) |
| 122 | + } |
| 123 | + |
| 124 | + if v, ok := d.GetOk("csr_content"); ok { |
| 125 | + request.CsrContent = helper.String(v.(string)) |
| 126 | + } |
| 127 | + |
| 128 | + if v, ok := d.GetOk("csr_key_password"); ok { |
| 129 | + request.CsrkeyPassword = helper.String(v.(string)) |
| 130 | + } |
| 131 | + |
| 132 | + if v, ok := d.GetOk("reason"); ok { |
| 133 | + request.Reason = helper.String(v.(string)) |
| 134 | + } |
| 135 | + |
| 136 | + if v, ok := d.GetOk("cert_csr_encrypt_algo"); ok { |
| 137 | + request.CertCSREncryptAlgo = helper.String(v.(string)) |
| 138 | + } |
| 139 | + |
| 140 | + if v, ok := d.GetOk("cert_csr_key_parameter"); ok { |
| 141 | + request.CertCSRKeyParameter = helper.String(v.(string)) |
| 142 | + } |
| 143 | + |
| 144 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 145 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseSSLCertificateClient().ReplaceCertificate(request) |
| 146 | + if e != nil { |
| 147 | + return retryError(e) |
| 148 | + } else { |
| 149 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 150 | + } |
| 151 | + response = result |
| 152 | + return nil |
| 153 | + }) |
| 154 | + if err != nil { |
| 155 | + log.Printf("[CRITAL]%s operate ssl replaceCertificate failed, reason:%+v", logId, err) |
| 156 | + return err |
| 157 | + } |
| 158 | + if response != nil && response.Response != nil && response.Response.CertificateId != nil { |
| 159 | + certificateId = helper.StrToUInt64(*response.Response.CertificateId) |
| 160 | + } |
| 161 | + |
| 162 | + d.SetId(helper.UInt64ToStr(certificateId)) |
| 163 | + |
| 164 | + return resourceTencentCloudSslReplaceCertificateRead(d, meta) |
| 165 | +} |
| 166 | + |
| 167 | +func resourceTencentCloudSslReplaceCertificateRead(d *schema.ResourceData, meta interface{}) error { |
| 168 | + defer logElapsed("resource.tencentcloud_ssl_replace_certificate_operation.read")() |
| 169 | + defer inconsistentCheck(d, meta)() |
| 170 | + |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +func resourceTencentCloudSslReplaceCertificateDelete(d *schema.ResourceData, meta interface{}) error { |
| 175 | + defer logElapsed("resource.tencentcloud_ssl_replace_certificate_operation.delete")() |
| 176 | + defer inconsistentCheck(d, meta)() |
| 177 | + |
| 178 | + return nil |
| 179 | +} |
0 commit comments