|
| 1 | +package ssl |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + ssl "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssl/v20191205" |
| 12 | + |
| 13 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 14 | +) |
| 15 | + |
| 16 | +func ResourceTencentCloudSslCheckCertificateDomainVerificationOperation() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Create: resourceTencentCloudSslCheckCertificateDomainVerificationCreate, |
| 19 | + Read: resourceTencentCloudSslCheckCertificateDomainVerificationRead, |
| 20 | + Delete: resourceTencentCloudSslCheckCertificateDomainVerificationDelete, |
| 21 | + |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "certificate_id": { |
| 24 | + Required: true, |
| 25 | + ForceNew: true, |
| 26 | + Type: schema.TypeString, |
| 27 | + Description: "The certificate ID.", |
| 28 | + }, |
| 29 | + // computed |
| 30 | + "verification_results": { |
| 31 | + Computed: true, |
| 32 | + Type: schema.TypeList, |
| 33 | + Description: "Domain name verification results.", |
| 34 | + Elem: &schema.Resource{ |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "domain": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + Description: "Domain name.", |
| 40 | + }, |
| 41 | + "verify_type": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + Description: "Domain Verify Type.", |
| 45 | + }, |
| 46 | + "local_check": { |
| 47 | + Type: schema.TypeInt, |
| 48 | + Computed: true, |
| 49 | + Description: "Local inspection results.", |
| 50 | + }, |
| 51 | + "ca_check": { |
| 52 | + Type: schema.TypeInt, |
| 53 | + Computed: true, |
| 54 | + Description: "CA inspection results.", |
| 55 | + }, |
| 56 | + "local_check_fail_reason": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: "Check the reason for the failure.", |
| 60 | + }, |
| 61 | + "check_value": { |
| 62 | + Type: schema.TypeList, |
| 63 | + Computed: true, |
| 64 | + Description: "Detected values.", |
| 65 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 66 | + }, |
| 67 | + "frequently": { |
| 68 | + Type: schema.TypeBool, |
| 69 | + Computed: true, |
| 70 | + Description: "Whether frequent requests.", |
| 71 | + }, |
| 72 | + "issued": { |
| 73 | + Type: schema.TypeBool, |
| 74 | + Computed: true, |
| 75 | + Description: "Whether issued.", |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func resourceTencentCloudSslCheckCertificateDomainVerificationCreate(d *schema.ResourceData, meta interface{}) error { |
| 85 | + defer tccommon.LogElapsed("resource.tencentcloud_ssl_check_certificate_domain_verification_operation.create")() |
| 86 | + defer tccommon.InconsistentCheck(d, meta)() |
| 87 | + |
| 88 | + var ( |
| 89 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 90 | + request = ssl.NewCheckCertificateDomainVerificationRequest() |
| 91 | + response = []*ssl.DomainValidationResult{} |
| 92 | + certificateId string |
| 93 | + ) |
| 94 | + |
| 95 | + if v, ok := d.GetOk("certificate_id"); ok { |
| 96 | + request.CertificateId = helper.String(v.(string)) |
| 97 | + certificateId = v.(string) |
| 98 | + } |
| 99 | + |
| 100 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 101 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseSSLCertificateClient().CheckCertificateDomainVerification(request) |
| 102 | + if e != nil { |
| 103 | + return tccommon.RetryError(e) |
| 104 | + } |
| 105 | + |
| 106 | + if result == nil || result.Response == nil { |
| 107 | + err := fmt.Errorf("[DEBUG]%s Check certificate domain verification failed.\n", logId) |
| 108 | + return tccommon.RetryError(err) |
| 109 | + } |
| 110 | + |
| 111 | + response = result.Response.VerificationResults |
| 112 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 113 | + return nil |
| 114 | + }) |
| 115 | + |
| 116 | + if err != nil { |
| 117 | + log.Printf("[CRITAL]%s Check certificate domain verification failed, reason: %+v", logId, err) |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + tmpList := make([]map[string]interface{}, 0, len(response)) |
| 122 | + for _, item := range response { |
| 123 | + tmpObject := make(map[string]interface{}) |
| 124 | + if item.Domain != nil { |
| 125 | + tmpObject["domain"] = item.Domain |
| 126 | + } |
| 127 | + |
| 128 | + if item.VerifyType != nil { |
| 129 | + tmpObject["verify_type"] = item.VerifyType |
| 130 | + } |
| 131 | + |
| 132 | + if item.LocalCheck != nil { |
| 133 | + tmpObject["local_check"] = item.LocalCheck |
| 134 | + } |
| 135 | + |
| 136 | + if item.CaCheck != nil { |
| 137 | + tmpObject["ca_check"] = item.CaCheck |
| 138 | + } |
| 139 | + |
| 140 | + if item.LocalCheckFailReason != nil { |
| 141 | + tmpObject["local_check_fail_reason"] = item.LocalCheckFailReason |
| 142 | + } |
| 143 | + |
| 144 | + if item.CheckValue != nil { |
| 145 | + tmpValueList := make([]string, 0, len(item.CheckValue)) |
| 146 | + for _, v := range item.CheckValue { |
| 147 | + tmpValueList = append(tmpValueList, *v) |
| 148 | + } |
| 149 | + |
| 150 | + tmpObject["check_value"] = tmpValueList |
| 151 | + } |
| 152 | + |
| 153 | + if item.Frequently != nil { |
| 154 | + tmpObject["frequently"] = item.Frequently |
| 155 | + } |
| 156 | + |
| 157 | + if item.Issued != nil { |
| 158 | + tmpObject["issued"] = item.Issued |
| 159 | + } |
| 160 | + |
| 161 | + tmpList = append(tmpList, tmpObject) |
| 162 | + } |
| 163 | + |
| 164 | + _ = d.Set("verification_results", tmpList) |
| 165 | + |
| 166 | + d.SetId(certificateId) |
| 167 | + |
| 168 | + return resourceTencentCloudSslCheckCertificateDomainVerificationRead(d, meta) |
| 169 | +} |
| 170 | + |
| 171 | +func resourceTencentCloudSslCheckCertificateDomainVerificationRead(d *schema.ResourceData, meta interface{}) error { |
| 172 | + defer tccommon.LogElapsed("resource.tencentcloud_ssl_check_certificate_domain_verification_operation.read")() |
| 173 | + defer tccommon.InconsistentCheck(d, meta)() |
| 174 | + |
| 175 | + return nil |
| 176 | +} |
| 177 | + |
| 178 | +func resourceTencentCloudSslCheckCertificateDomainVerificationDelete(d *schema.ResourceData, meta interface{}) error { |
| 179 | + defer tccommon.LogElapsed("resource.tencentcloud_ssl_check_certificate_domain_verification_operation.delete")() |
| 180 | + defer tccommon.InconsistentCheck(d, meta)() |
| 181 | + |
| 182 | + return nil |
| 183 | +} |
0 commit comments