@@ -17,7 +17,9 @@ import (
17
17
"log"
18
18
"strconv"
19
19
20
+ "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
20
21
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
22
+ ssl "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ssl/v20191205"
21
23
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22
24
)
23
25
@@ -144,46 +146,42 @@ func dataSourceTencentCloudSslCertificatesRead(d *schema.ResourceData, m interfa
144
146
id = helper .String (raw .(string ))
145
147
}
146
148
147
- service := SslService {client : m .(* TencentCloudClient ).apiV3Conn }
148
-
149
- respCertificates , err := service .DescribeCertificates (ctx , id , name , certType )
149
+ sslService := SSLService {client : m .(* TencentCloudClient ).apiV3Conn }
150
+ certificateList , err := GetCertificateList (ctx , sslService , id , name , certType )
150
151
if err != nil {
151
152
return err
152
153
}
153
154
154
- certificates := make ([]map [string ]interface {}, 0 , len (respCertificates ))
155
- ids := make ([]string , 0 , len (respCertificates ))
156
-
157
- for _ , certificate := range respCertificates {
155
+ certificates := make ([]map [string ]interface {}, 0 , len (certificateList ))
156
+ ids := make ([]string , 0 , len (certificateList ))
157
+ for _ , certificate := range certificateList {
158
158
if nilNames := CheckNil (certificate , map [string ]string {
159
- "Id" : "id" ,
160
- "Alias" : "name" ,
161
- "CertType" : "type" ,
162
- "ProjectId" : "project id" ,
163
- "Cert" : "cert" ,
164
- "ProductZhName" : "product zh name" ,
165
- "Domain" : "domain" ,
166
- "Status" : "status" ,
167
- "CertBeginTime" : "begin time" ,
168
- "CertEndTime" : "end time" ,
169
- "InsertTime" : "create time" ,
159
+ "CertificateId" : "id" ,
160
+ "Alias" : "name" ,
161
+ "CertificateType" : "type" ,
162
+ "ProjectId" : "project id" ,
163
+ "ProductZhName" : "product zh name" ,
164
+ "Domain" : "domain" ,
165
+ "Status" : "status" ,
166
+ "CertBeginTime" : "begin time" ,
167
+ "CertEndTime" : "end time" ,
168
+ "InsertTime" : "create time" ,
170
169
}); len (nilNames ) > 0 {
171
170
return fmt .Errorf ("certificate %v are nil" , nilNames )
172
171
}
173
172
174
- ids = append (ids , * certificate .Id )
173
+ ids = append (ids , * certificate .CertificateId )
175
174
176
175
projectId , err := strconv .Atoi (* certificate .ProjectId )
177
176
if err != nil {
178
177
return err
179
178
}
180
179
181
180
m := map [string ]interface {}{
182
- "id" : * certificate .Id ,
181
+ "id" : * certificate .CertificateId ,
183
182
"name" : * certificate .Alias ,
184
- "type" : * certificate .CertType ,
183
+ "type" : * certificate .CertificateType ,
185
184
"project_id" : projectId ,
186
- "cert" : * certificate .Cert ,
187
185
"product_zh_name" : * certificate .ProductZhName ,
188
186
"domain" : * certificate .Domain ,
189
187
"status" : * certificate .Status ,
@@ -200,6 +198,26 @@ func dataSourceTencentCloudSslCertificatesRead(d *schema.ResourceData, m interfa
200
198
m ["subject_names" ] = subjectAltNames
201
199
}
202
200
201
+ describeRequest := ssl .NewDescribeCertificateDetailRequest ()
202
+ describeRequest .CertificateId = certificate .CertificateId
203
+ var outErr , inErr error
204
+ var describeResponse * ssl.DescribeCertificateDetailResponse
205
+ outErr = resource .Retry (readRetryTimeout , func () * resource.RetryError {
206
+ describeResponse , inErr = sslService .DescribeCertificateDetail (ctx , describeRequest )
207
+ if inErr != nil {
208
+ return retryError (inErr )
209
+ }
210
+ return nil
211
+ })
212
+ if outErr != nil {
213
+ log .Printf ("[CRITAL]%s read certificate failed, reason: %v" , logId , outErr )
214
+ return outErr
215
+ }
216
+
217
+ if describeResponse != nil && describeResponse .Response != nil {
218
+ m ["cert" ] = * describeResponse .Response .CertificatePublicKey
219
+ }
220
+
203
221
certificates = append (certificates , m )
204
222
}
205
223
@@ -216,3 +234,90 @@ func dataSourceTencentCloudSslCertificatesRead(d *schema.ResourceData, m interfa
216
234
217
235
return nil
218
236
}
237
+
238
+ func GetCertificateList (ctx context.Context , sslService SSLService , id , name , certType * string ) (certificateList []* ssl.Certificates , errRet error ) {
239
+ logId := getLogId (contextNil )
240
+
241
+ var (
242
+ outErr , inErr error
243
+ certificatesById , certificatesByName []* ssl.Certificates
244
+ )
245
+
246
+ if id == nil && name == nil {
247
+ describeRequest := ssl .NewDescribeCertificatesRequest ()
248
+ describeRequest .CertificateType = certType
249
+ outErr = resource .Retry (readRetryTimeout , func () * resource.RetryError {
250
+ certificateList , inErr = sslService .DescribeCertificates (ctx , describeRequest )
251
+ if inErr != nil {
252
+ return retryError (inErr )
253
+ }
254
+ return nil
255
+ })
256
+ if outErr != nil {
257
+ log .Printf ("[CRITAL]%s read certificates failed, reason: %v" , logId , outErr )
258
+ errRet = outErr
259
+ return
260
+ }
261
+ return
262
+ }
263
+
264
+ if id != nil {
265
+ describeRequest := ssl .NewDescribeCertificatesRequest ()
266
+ describeRequest .CertificateType = certType
267
+ describeRequest .SearchKey = id
268
+ outErr = resource .Retry (readRetryTimeout , func () * resource.RetryError {
269
+ certificatesById , inErr = sslService .DescribeCertificates (ctx , describeRequest )
270
+ if inErr != nil {
271
+ return retryError (inErr )
272
+ }
273
+ return nil
274
+ })
275
+ if outErr != nil {
276
+ log .Printf ("[CRITAL]%s read certificates failed, reason: %v" , logId , outErr )
277
+ errRet = outErr
278
+ return
279
+ }
280
+ }
281
+ if name != nil {
282
+ describeRequest := ssl .NewDescribeCertificatesRequest ()
283
+ describeRequest .CertificateType = certType
284
+ describeRequest .SearchKey = name
285
+ outErr = resource .Retry (readRetryTimeout , func () * resource.RetryError {
286
+ certificatesByName , inErr = sslService .DescribeCertificates (ctx , describeRequest )
287
+ if inErr != nil {
288
+ return retryError (inErr )
289
+ }
290
+ return nil
291
+ })
292
+ if outErr != nil {
293
+ log .Printf ("[CRITAL]%s read certificates failed, reason: %v" , logId , outErr )
294
+ errRet = outErr
295
+ return
296
+ }
297
+ }
298
+
299
+ certificateList = GetCommonCertificates (certificatesById , certificatesByName )
300
+ return
301
+ }
302
+
303
+ func GetCommonCertificates (certificatesById , certificatesByName []* ssl.Certificates ) (result []* ssl.Certificates ) {
304
+ if len (certificatesById ) == 0 {
305
+ return certificatesByName
306
+ } else if len (certificatesByName ) == 0 {
307
+ return certificatesById
308
+ }
309
+ certificateMap := make (map [string ]bool )
310
+ for _ , certificate := range certificatesById {
311
+ if _ , ok := certificateMap [* certificate .CertificateId ]; ok {
312
+ continue
313
+ }
314
+ certificateMap [* certificate .CertificateId ] = true
315
+ }
316
+
317
+ for _ , certificate := range certificatesByName {
318
+ if _ , ok := certificateMap [* certificate .CertificateId ]; ok {
319
+ result = append (result , certificate )
320
+ }
321
+ }
322
+ return
323
+ }
0 commit comments