|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of css live_domain_cert |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_css_live_domain_cert" "live_domain_cert" { |
| 8 | + domain_name = "" |
| 9 | + } |
| 10 | +``` |
| 11 | +*/ |
| 12 | +package tencentcloud |
| 13 | + |
| 14 | +import ( |
| 15 | + "context" |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 18 | + css "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live/v20180801" |
| 19 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 20 | +) |
| 21 | + |
| 22 | +func dataSourceTencentCloudCssLiveDomainCert() *schema.Resource { |
| 23 | + return &schema.Resource{ |
| 24 | + Read: dataSourceTencentCloudCssLiveDomainCertRead, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "domain_name": { |
| 27 | + Required: true, |
| 28 | + Type: schema.TypeString, |
| 29 | + Description: "Playback domain name.", |
| 30 | + }, |
| 31 | + |
| 32 | + "domain_cert_info": { |
| 33 | + Computed: true, |
| 34 | + Type: schema.TypeList, |
| 35 | + Description: "Certificate information.", |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "cert_id": { |
| 39 | + Type: schema.TypeInt, |
| 40 | + Computed: true, |
| 41 | + Description: "Certificate ID.", |
| 42 | + }, |
| 43 | + "cert_name": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + Description: "Certificate name.", |
| 47 | + }, |
| 48 | + "description": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + Description: "Description.", |
| 52 | + }, |
| 53 | + "create_time": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + Description: "The creation time in UTC format.Note: Beijing time (UTC+8) is used.", |
| 57 | + }, |
| 58 | + "https_crt": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "Certificate content.", |
| 62 | + }, |
| 63 | + "cert_type": { |
| 64 | + Type: schema.TypeInt, |
| 65 | + Computed: true, |
| 66 | + Description: "Certificate type.0: user-added certificate1: Tencent Cloud-hosted certificate.", |
| 67 | + }, |
| 68 | + "cert_expire_time": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "The certificate expiration time in UTC format.Note: Beijing time (UTC+8) is used.", |
| 72 | + }, |
| 73 | + "domain_name": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "Domain name that uses this certificate.", |
| 77 | + }, |
| 78 | + "status": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Computed: true, |
| 81 | + Description: "Certificate status.", |
| 82 | + }, |
| 83 | + "cert_domains": { |
| 84 | + Type: schema.TypeSet, |
| 85 | + Elem: &schema.Schema{ |
| 86 | + Type: schema.TypeString, |
| 87 | + }, |
| 88 | + Computed: true, |
| 89 | + Description: "List of domain names in the certificate.[*.x.com] for example.Note: this field may return `null`, indicating that no valid values can be obtained.", |
| 90 | + }, |
| 91 | + "cloud_cert_id": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Computed: true, |
| 94 | + Description: "Tencent Cloud SSL certificate ID.Note: this field may return `null`, indicating that no valid values can be obtained.", |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + |
| 100 | + "result_output_file": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Optional: true, |
| 103 | + Description: "Used to save results.", |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func dataSourceTencentCloudCssLiveDomainCertRead(d *schema.ResourceData, meta interface{}) error { |
| 110 | + defer logElapsed("data_source.tencentcloud_css_live_domain_cert.read")() |
| 111 | + defer inconsistentCheck(d, meta)() |
| 112 | + |
| 113 | + logId := getLogId(contextNil) |
| 114 | + |
| 115 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 116 | + |
| 117 | + paramMap := make(map[string]interface{}) |
| 118 | + if v, ok := d.GetOk("domain_name"); ok { |
| 119 | + paramMap["DomainName"] = helper.String(v.(string)) |
| 120 | + } |
| 121 | + |
| 122 | + service := CssService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 123 | + |
| 124 | + var domainCertInfo []*css.DomainCertInfo |
| 125 | + |
| 126 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 127 | + result, e := service.DescribeCssLiveDomainCertByFilter(ctx, paramMap) |
| 128 | + if e != nil { |
| 129 | + return retryError(e) |
| 130 | + } |
| 131 | + domainCertInfo = result |
| 132 | + return nil |
| 133 | + }) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + ids := make([]string, 0, len(domainCertInfo)) |
| 139 | + if domainCertInfo != nil { |
| 140 | + domainCertInfoMap := map[string]interface{}{} |
| 141 | + |
| 142 | + if domainCertInfo.CertId != nil { |
| 143 | + domainCertInfoMap["cert_id"] = domainCertInfo.CertId |
| 144 | + } |
| 145 | + |
| 146 | + if domainCertInfo.CertName != nil { |
| 147 | + domainCertInfoMap["cert_name"] = domainCertInfo.CertName |
| 148 | + } |
| 149 | + |
| 150 | + if domainCertInfo.Description != nil { |
| 151 | + domainCertInfoMap["description"] = domainCertInfo.Description |
| 152 | + } |
| 153 | + |
| 154 | + if domainCertInfo.CreateTime != nil { |
| 155 | + domainCertInfoMap["create_time"] = domainCertInfo.CreateTime |
| 156 | + } |
| 157 | + |
| 158 | + if domainCertInfo.HttpsCrt != nil { |
| 159 | + domainCertInfoMap["https_crt"] = domainCertInfo.HttpsCrt |
| 160 | + } |
| 161 | + |
| 162 | + if domainCertInfo.CertType != nil { |
| 163 | + domainCertInfoMap["cert_type"] = domainCertInfo.CertType |
| 164 | + } |
| 165 | + |
| 166 | + if domainCertInfo.CertExpireTime != nil { |
| 167 | + domainCertInfoMap["cert_expire_time"] = domainCertInfo.CertExpireTime |
| 168 | + } |
| 169 | + |
| 170 | + if domainCertInfo.DomainName != nil { |
| 171 | + domainCertInfoMap["domain_name"] = domainCertInfo.DomainName |
| 172 | + } |
| 173 | + |
| 174 | + if domainCertInfo.Status != nil { |
| 175 | + domainCertInfoMap["status"] = domainCertInfo.Status |
| 176 | + } |
| 177 | + |
| 178 | + if domainCertInfo.CertDomains != nil { |
| 179 | + domainCertInfoMap["cert_domains"] = domainCertInfo.CertDomains |
| 180 | + } |
| 181 | + |
| 182 | + if domainCertInfo.CloudCertId != nil { |
| 183 | + domainCertInfoMap["cloud_cert_id"] = domainCertInfo.CloudCertId |
| 184 | + } |
| 185 | + |
| 186 | + ids = append(ids, *domainCertInfo.CertId) |
| 187 | + _ = d.Set("domain_cert_info", domainCertInfoMap) |
| 188 | + } |
| 189 | + |
| 190 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 191 | + output, ok := d.GetOk("result_output_file") |
| 192 | + if ok && output.(string) != "" { |
| 193 | + if e := writeToFile(output.(string), domainCertInfoMap); e != nil { |
| 194 | + return e |
| 195 | + } |
| 196 | + } |
| 197 | + return nil |
| 198 | +} |
0 commit comments