|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of tse gateway_certificates |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +
|
| 8 | +data "tencentcloud_tse_gateway_certificates" "gateway_certificates" { |
| 9 | + gateway_id = "gateway-ddbb709b" |
| 10 | + filters { |
| 11 | + key = "BindDomain" |
| 12 | + value = "example.com" |
| 13 | + } |
| 14 | +} |
| 15 | +``` |
| 16 | +*/ |
| 17 | +package tencentcloud |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 24 | + tse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse/v20201207" |
| 25 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 26 | +) |
| 27 | + |
| 28 | +func dataSourceTencentCloudTseGatewayCertificates() *schema.Resource { |
| 29 | + return &schema.Resource{ |
| 30 | + Read: dataSourceTencentCloudTseGatewayCertificatesRead, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "gateway_id": { |
| 33 | + Required: true, |
| 34 | + Type: schema.TypeString, |
| 35 | + Description: "Gateway ID.", |
| 36 | + }, |
| 37 | + |
| 38 | + "filters": { |
| 39 | + Optional: true, |
| 40 | + Type: schema.TypeList, |
| 41 | + Description: "Filter conditions, valid value: `BindDomain`, `Name`.", |
| 42 | + Elem: &schema.Resource{ |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + "key": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + Description: "Filter name.", |
| 48 | + }, |
| 49 | + "value": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Optional: true, |
| 52 | + Description: "Filter value.", |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + |
| 58 | + "result": { |
| 59 | + Computed: true, |
| 60 | + Type: schema.TypeList, |
| 61 | + Description: "Result.", |
| 62 | + Elem: &schema.Resource{ |
| 63 | + Schema: map[string]*schema.Schema{ |
| 64 | + "total": { |
| 65 | + Type: schema.TypeInt, |
| 66 | + Computed: true, |
| 67 | + Description: "Total count. Note: This field may return null, indicating that a valid value is not available.", |
| 68 | + }, |
| 69 | + "certificates_list": { |
| 70 | + Type: schema.TypeList, |
| 71 | + Computed: true, |
| 72 | + Description: "Certificate list of gateway. Note: This field may return null, indicating that a valid value is not available.", |
| 73 | + Elem: &schema.Resource{ |
| 74 | + Schema: map[string]*schema.Schema{ |
| 75 | + "name": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "Certificate name. Note: This field may return null, indicating that a valid value is not available.", |
| 79 | + }, |
| 80 | + "id": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + Description: "Certificate ID. Note: This field may return null, indicating that a valid value is not available.", |
| 84 | + }, |
| 85 | + "bind_domains": { |
| 86 | + Type: schema.TypeSet, |
| 87 | + Elem: &schema.Schema{ |
| 88 | + Type: schema.TypeString, |
| 89 | + }, |
| 90 | + Computed: true, |
| 91 | + Description: "Domains of the binding. Note: This field may return null, indicating that a valid value is not available.", |
| 92 | + }, |
| 93 | + "status": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + Description: "Status of certificate. Reference value:- expired- active. Note: This field may return null, indicating that a valid value is not available.", |
| 97 | + }, |
| 98 | + "crt": { |
| 99 | + Type: schema.TypeString, |
| 100 | + Computed: true, |
| 101 | + Description: "Pem format of certificate. Note: This field may return null, indicating that a valid value is not available.", |
| 102 | + }, |
| 103 | + "key": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + Description: "Private key of certificate. Note: This field may return null, indicating that a valid value is not available.", |
| 107 | + }, |
| 108 | + "expire_time": { |
| 109 | + Type: schema.TypeString, |
| 110 | + Computed: true, |
| 111 | + Description: "Expiration time of certificate. Note: This field may return null, indicating that a valid value is not available.", |
| 112 | + }, |
| 113 | + "create_time": { |
| 114 | + Type: schema.TypeString, |
| 115 | + Computed: true, |
| 116 | + Description: "Upload time of certificate. Note: This field may return null, indicating that a valid value is not available.", |
| 117 | + }, |
| 118 | + "issue_time": { |
| 119 | + Type: schema.TypeString, |
| 120 | + Computed: true, |
| 121 | + Description: "Issuance time of certificateNote: This field may return null, indicating that a valid value is not available.", |
| 122 | + }, |
| 123 | + "cert_source": { |
| 124 | + Type: schema.TypeString, |
| 125 | + Computed: true, |
| 126 | + Description: "Source of certificate. Reference value:- native. Source: konga- ssl. Source: ssl platform. Note: This field may return null, indicating that a valid value is not available.", |
| 127 | + }, |
| 128 | + "cert_id": { |
| 129 | + Type: schema.TypeString, |
| 130 | + Computed: true, |
| 131 | + Description: "Certificate ID of ssl platform. Note: This field may return null, indicating that a valid value is not available.", |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + |
| 140 | + "result_output_file": { |
| 141 | + Type: schema.TypeString, |
| 142 | + Optional: true, |
| 143 | + Description: "Used to save results.", |
| 144 | + }, |
| 145 | + }, |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func dataSourceTencentCloudTseGatewayCertificatesRead(d *schema.ResourceData, meta interface{}) error { |
| 150 | + defer logElapsed("data_source.tencentcloud_tse_gateway_certificates.read")() |
| 151 | + defer inconsistentCheck(d, meta)() |
| 152 | + |
| 153 | + logId := getLogId(contextNil) |
| 154 | + |
| 155 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 156 | + |
| 157 | + paramMap := make(map[string]interface{}) |
| 158 | + if v, ok := d.GetOk("gateway_id"); ok { |
| 159 | + paramMap["GatewayId"] = helper.String(v.(string)) |
| 160 | + } |
| 161 | + |
| 162 | + if v, ok := d.GetOk("filters"); ok { |
| 163 | + filtersSet := v.([]interface{}) |
| 164 | + tmpSet := make([]*tse.ListFilter, 0, len(filtersSet)) |
| 165 | + |
| 166 | + for _, item := range filtersSet { |
| 167 | + listFilter := tse.ListFilter{} |
| 168 | + listFilterMap := item.(map[string]interface{}) |
| 169 | + |
| 170 | + if v, ok := listFilterMap["key"]; ok { |
| 171 | + listFilter.Key = helper.String(v.(string)) |
| 172 | + } |
| 173 | + if v, ok := listFilterMap["value"]; ok { |
| 174 | + listFilter.Value = helper.String(v.(string)) |
| 175 | + } |
| 176 | + tmpSet = append(tmpSet, &listFilter) |
| 177 | + } |
| 178 | + paramMap["filters"] = tmpSet |
| 179 | + } |
| 180 | + |
| 181 | + service := TseService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 182 | + |
| 183 | + var result *tse.KongCertificatesList |
| 184 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 185 | + response, e := service.DescribeTseGatewayCertificatesByFilter(ctx, paramMap) |
| 186 | + if e != nil { |
| 187 | + return retryError(e) |
| 188 | + } |
| 189 | + result = response |
| 190 | + return nil |
| 191 | + }) |
| 192 | + if err != nil { |
| 193 | + return err |
| 194 | + } |
| 195 | + |
| 196 | + var ids []string |
| 197 | + kongCertificatesListMap := map[string]interface{}{} |
| 198 | + if result != nil { |
| 199 | + ids = make([]string, 0, *result.Total) |
| 200 | + if result.Total != nil { |
| 201 | + kongCertificatesListMap["total"] = result.Total |
| 202 | + } |
| 203 | + |
| 204 | + if result.CertificatesList != nil { |
| 205 | + certificatesListList := []interface{}{} |
| 206 | + for _, certificatesList := range result.CertificatesList { |
| 207 | + certificatesListMap := map[string]interface{}{} |
| 208 | + |
| 209 | + if certificatesList.Name != nil { |
| 210 | + certificatesListMap["name"] = certificatesList.Name |
| 211 | + } |
| 212 | + |
| 213 | + if certificatesList.Id != nil { |
| 214 | + certificatesListMap["id"] = certificatesList.Id |
| 215 | + } |
| 216 | + |
| 217 | + if certificatesList.BindDomains != nil { |
| 218 | + certificatesListMap["bind_domains"] = certificatesList.BindDomains |
| 219 | + } |
| 220 | + |
| 221 | + if certificatesList.Status != nil { |
| 222 | + certificatesListMap["status"] = certificatesList.Status |
| 223 | + } |
| 224 | + |
| 225 | + if certificatesList.Crt != nil { |
| 226 | + certificatesListMap["crt"] = certificatesList.Crt |
| 227 | + } |
| 228 | + |
| 229 | + if certificatesList.Key != nil { |
| 230 | + certificatesListMap["key"] = certificatesList.Key |
| 231 | + } |
| 232 | + |
| 233 | + if certificatesList.ExpireTime != nil { |
| 234 | + certificatesListMap["expire_time"] = certificatesList.ExpireTime |
| 235 | + } |
| 236 | + |
| 237 | + if certificatesList.CreateTime != nil { |
| 238 | + certificatesListMap["create_time"] = certificatesList.CreateTime |
| 239 | + } |
| 240 | + |
| 241 | + if certificatesList.IssueTime != nil { |
| 242 | + certificatesListMap["issue_time"] = certificatesList.IssueTime |
| 243 | + } |
| 244 | + |
| 245 | + if certificatesList.CertSource != nil { |
| 246 | + certificatesListMap["cert_source"] = certificatesList.CertSource |
| 247 | + } |
| 248 | + |
| 249 | + if certificatesList.CertId != nil { |
| 250 | + certificatesListMap["cert_id"] = certificatesList.CertId |
| 251 | + } |
| 252 | + |
| 253 | + certificatesListList = append(certificatesListList, certificatesListMap) |
| 254 | + ids = append(ids, *certificatesList.Id) |
| 255 | + } |
| 256 | + |
| 257 | + kongCertificatesListMap["certificates_list"] = certificatesListList |
| 258 | + } |
| 259 | + |
| 260 | + _ = d.Set("result", []interface{}{kongCertificatesListMap}) |
| 261 | + } |
| 262 | + |
| 263 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 264 | + output, ok := d.GetOk("result_output_file") |
| 265 | + if ok && output.(string) != "" { |
| 266 | + if e := writeToFile(output.(string), kongCertificatesListMap); e != nil { |
| 267 | + return e |
| 268 | + } |
| 269 | + } |
| 270 | + return nil |
| 271 | +} |
0 commit comments