|
| 1 | +/* |
| 2 | +Provides a resource to check or create a cdn Domain Verify Record |
| 3 | +
|
| 4 | +~> **NOTE:** |
| 5 | +
|
| 6 | +Example Usage |
| 7 | +
|
| 8 | +```hcl |
| 9 | +data "tencentcloud_cdn_domain_verifier" "vr" { |
| 10 | + domain = "www.examplexxx123.com" |
| 11 | + auto_verify = true # auto create record if not verified |
| 12 | + freeze_record = true # once been freeze and verified, it will never be changed again |
| 13 | +} |
| 14 | +
|
| 15 | +locals { |
| 16 | + recordValue = data.tencentcloud_cdn_domain_verifier.record |
| 17 | + recordType = data.tencentcloud_cdn_domain_verifier.record_type |
| 18 | +} |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "log" |
| 26 | + |
| 27 | + "github.com/hashicorp/go-multierror" |
| 28 | + sdkError "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" |
| 29 | + |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 31 | + cdn "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606" |
| 32 | +) |
| 33 | + |
| 34 | +func dataSourceTencentCloudCdnDomainVerifyRecord() *schema.Resource { |
| 35 | + return &schema.Resource{ |
| 36 | + Read: resourceTencentCloudCdnDomainVerifyRecordRead, |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "domain": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Required: true, |
| 41 | + Description: "Specify domain name, e.g. `www.examplexxx123.com`.", |
| 42 | + }, |
| 43 | + "verify_type": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Optional: true, |
| 46 | + Description: "Specify verify type, values: `dns` (default), `file`.", |
| 47 | + }, |
| 48 | + "auto_verify": { |
| 49 | + Type: schema.TypeBool, |
| 50 | + Optional: true, |
| 51 | + Description: "Specify whether to keep first create result instead of re-create again.", |
| 52 | + }, |
| 53 | + "freeze_record": { |
| 54 | + Type: schema.TypeBool, |
| 55 | + Optional: true, |
| 56 | + Description: "Specify whether the verification record needs to be freeze instead of refresh every 8 hours, this used for domain verification.", |
| 57 | + }, |
| 58 | + "verify_result": { |
| 59 | + Type: schema.TypeBool, |
| 60 | + Computed: true, |
| 61 | + Description: "Verify result.", |
| 62 | + }, |
| 63 | + "failed_reason": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Optional: true, |
| 66 | + Description: "Indicates failed reason of verification.", |
| 67 | + }, |
| 68 | + "sub_domain": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "Sub-domain resolution.", |
| 72 | + }, |
| 73 | + "record": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "Resolution record value.", |
| 77 | + }, |
| 78 | + "record_type": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: "Type of resolution.", |
| 82 | + }, |
| 83 | + "file_verify_url": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Computed: true, |
| 86 | + Description: "File verify URL guidance.", |
| 87 | + }, |
| 88 | + "file_verify_domains": { |
| 89 | + Type: schema.TypeList, |
| 90 | + Computed: true, |
| 91 | + Description: "List of file verified domains.", |
| 92 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 93 | + }, |
| 94 | + "file_verify_name": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + Description: "Name of file verifications.", |
| 98 | + }, |
| 99 | + "result_output_file": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Optional: true, |
| 102 | + Description: "Used for save result json.", |
| 103 | + }, |
| 104 | + }, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func resourceTencentCloudCdnDomainVerifyRecordRead(d *schema.ResourceData, meta interface{}) error { |
| 109 | + defer logElapsed("resource.tencentcloud_cdn_domain_verifier.read")() |
| 110 | + defer inconsistentCheck(d, meta)() |
| 111 | + |
| 112 | + logId := getLogId(contextNil) |
| 113 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 114 | + |
| 115 | + service := CdnService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 116 | + |
| 117 | + domainName := d.Get("domain").(string) |
| 118 | + freezeRecord := d.Get("freeze_record").(bool) |
| 119 | + autoVerify := d.Get("auto_verify").(bool) |
| 120 | + |
| 121 | + verified, err := service.VerifyDomainRecord(ctx, domainName) |
| 122 | + |
| 123 | + if err != nil { |
| 124 | + canContinue, reason := checkCdnDomainVerifyErrReason(err) |
| 125 | + if !canContinue { |
| 126 | + d.SetId("") |
| 127 | + return err |
| 128 | + } |
| 129 | + _ = d.Set("failed_reason", reason) |
| 130 | + } |
| 131 | + |
| 132 | + _ = d.Set("verify_result", verified) |
| 133 | + d.SetId(domainName) |
| 134 | + |
| 135 | + if !autoVerify || (verified && freezeRecord) { |
| 136 | + return nil |
| 137 | + } |
| 138 | + response, err := service.CreateVerifyRecord(ctx, domainName) |
| 139 | + |
| 140 | + if err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + var errResults *multierror.Error |
| 145 | + |
| 146 | + errResults = multierror.Append(errResults, d.Set("record", response.Record)) |
| 147 | + errResults = multierror.Append(errResults, d.Set("record_type", response.RecordType)) |
| 148 | + errResults = multierror.Append(errResults, d.Set("sub_domain", response.SubDomain)) |
| 149 | + if len(response.FileVerifyDomains) > 0 { |
| 150 | + errResults = multierror.Append(errResults, d.Set("file_verify_domains", response.FileVerifyDomains)) |
| 151 | + } |
| 152 | + errResults = multierror.Append(errResults, d.Set("file_verify_name", response.FileVerifyName)) |
| 153 | + errResults = multierror.Append(errResults, d.Set("file_verify_url", response.FileVerifyUrl)) |
| 154 | + |
| 155 | + if e := errResults.ErrorOrNil(); e != nil { |
| 156 | + return e |
| 157 | + } |
| 158 | + |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + if output, ok := d.GetOk("result_output_file"); ok && output.(string) != "" { |
| 164 | + result := map[string]interface{}{ |
| 165 | + "verify_result": verified, |
| 166 | + "failed_reason": d.Get("failed_reason"), |
| 167 | + "sub_domain": response.SubDomain, |
| 168 | + "record": response.Record, |
| 169 | + "record_type": response.RecordType, |
| 170 | + "file_verify_url": response.FileVerifyUrl, |
| 171 | + "file_verify_domains": response.FileVerifyDomains, |
| 172 | + "file_verify_name": response.FileVerifyName, |
| 173 | + } |
| 174 | + if err := writeToFile(output.(string), result); err != nil { |
| 175 | + log.Printf("[CRITAL]%s output file[%s] fail, reason[%v]", |
| 176 | + logId, output.(string), err) |
| 177 | + return err |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +func checkCdnDomainVerifyErrReason(err error) (canContinue bool, code string) { |
| 185 | + sdkErr, ok := err.(*sdkError.TencentCloudSDKError) |
| 186 | + if !ok { |
| 187 | + return |
| 188 | + } |
| 189 | + errCode := sdkErr.Code |
| 190 | + if errCode == cdn.UNAUTHORIZEDOPERATION_CDNDOMAINRECORDNOTVERIFIED || |
| 191 | + errCode == cdn.UNAUTHORIZEDOPERATION_CDNTXTRECORDVALUENOTMATCH { |
| 192 | + canContinue = true |
| 193 | + code = errCode |
| 194 | + } |
| 195 | + return |
| 196 | +} |
0 commit comments