-
Notifications
You must be signed in to change notification settings - Fork 658
/
data_source_ibm_scc_latest_reports.go
577 lines (550 loc) · 18.2 KB
/
data_source_ibm_scc_latest_reports.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package scc
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/scc-go-sdk/v5/securityandcompliancecenterapiv3"
)
func DataSourceIbmSccLatestReports() *schema.Resource {
return AddSchemaData(&schema.Resource{
ReadContext: dataSourceIbmSccLatestReportsRead,
Schema: map[string]*schema.Schema{
"sort": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "This field sorts results by using a valid sort field. To learn more, see [Sorting](https://cloud.ibm.com/docs/api-handbook?topic=api-handbook-sorting).",
},
"home_account_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The ID of the home account.",
},
"controls_summary": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The compliance stats.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The allowed values of an aggregated status for controls, specifications, assessments, and resources.",
},
"total_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The total number of checks.",
},
"compliant_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of compliant checks.",
},
"not_compliant_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of checks that are not compliant.",
},
"unable_to_perform_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of checks that are unable to perform.",
},
"user_evaluation_required_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of checks that require a user evaluation.",
},
},
},
},
"evaluations_summary": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The evaluation stats.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The allowed values of an aggregated status for controls, specifications, assessments, and resources.",
},
"total_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The total number of evaluations.",
},
"pass_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of passed evaluations.",
},
"failure_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of failed evaluations.",
},
"error_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of evaluations that started, but did not finish, and ended with errors.",
},
"completed_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The total number of completed evaluations.",
},
},
},
},
"score": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The compliance score.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"passed": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of successful evaluations.",
},
"total_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The total number of evaluations.",
},
"percent": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The percentage of successful evaluations.",
},
},
},
},
"reports": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The list of reports.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The ID of the report.",
},
"group_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The group ID that is associated with the report. The group ID combines profile, scope, and attachment IDs.",
},
"created_on": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date when the report was created.",
},
"scan_time": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The date when the scan was run.",
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The type of the scan.",
},
"cos_object": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The Cloud Object Storage object that is associated with the report.",
},
"instance_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Instance ID.",
},
"account": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The account that is associated with a report.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The account ID.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The account name.",
},
"type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The account type.",
},
},
},
},
"profile": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The profile information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The profile ID.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The profile name.",
},
"version": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The profile version.",
},
},
},
},
"attachment": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The attachment that is associated with a report.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The attachment ID.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of the attachment.",
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The description of the attachment.",
},
"schedule": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The attachment schedule.",
},
"scope": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The scope of the attachment.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this scope.",
},
"environment": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The environment that relates to this scope.",
},
"properties": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The properties that are supported for scoping by this environment.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The property name.",
},
"value": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The property value.",
},
},
},
},
},
},
},
},
},
},
},
},
},
},
})
}
func dataSourceIbmSccLatestReportsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
resultsClient, err := meta.(conns.ClientSession).SecurityAndComplianceCenterV3()
if err != nil {
return diag.FromErr(err)
}
getLatestReportsOptions := &securityandcompliancecenterapiv3.GetLatestReportsOptions{}
getLatestReportsOptions.SetInstanceID(d.Get("instance_id").(string))
if _, ok := d.GetOk("sort"); ok {
getLatestReportsOptions.SetSort(d.Get("sort").(string))
}
reportLatest, response, err := resultsClient.GetLatestReportsWithContext(context, getLatestReportsOptions)
if err != nil {
log.Printf("[DEBUG] GetLatestReportsWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetLatestReportsWithContext failed %s\n%s", err, response))
}
d.SetId(dataSourceIbmSccLatestReportsID(d))
if err = d.Set("home_account_id", reportLatest.HomeAccountID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting home_account_id: %s", err))
}
controlsSummary := []map[string]interface{}{}
if reportLatest.ControlsSummary != nil {
modelMap, err := dataSourceIbmSccLatestReportsComplianceStatsToMap(reportLatest.ControlsSummary)
if err != nil {
return diag.FromErr(err)
}
controlsSummary = append(controlsSummary, modelMap)
}
if err = d.Set("controls_summary", controlsSummary); err != nil {
return diag.FromErr(fmt.Errorf("Error setting controls_summary %s", err))
}
evaluationsSummary := []map[string]interface{}{}
if reportLatest.EvaluationsSummary != nil {
modelMap, err := dataSourceIbmSccLatestReportsEvalStatsToMap(reportLatest.EvaluationsSummary)
if err != nil {
return diag.FromErr(err)
}
evaluationsSummary = append(evaluationsSummary, modelMap)
}
if err = d.Set("evaluations_summary", evaluationsSummary); err != nil {
return diag.FromErr(fmt.Errorf("Error setting evaluations_summary %s", err))
}
score := []map[string]interface{}{}
if reportLatest.Score != nil {
modelMap, err := dataSourceIbmSccLatestReportsComplianceScoreToMap(reportLatest.Score)
if err != nil {
return diag.FromErr(err)
}
score = append(score, modelMap)
}
if err = d.Set("score", score); err != nil {
return diag.FromErr(fmt.Errorf("Error setting score %s", err))
}
reports := []map[string]interface{}{}
if reportLatest.Reports != nil {
for _, modelItem := range reportLatest.Reports {
modelMap, err := dataSourceIbmSccLatestReportsReportToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
reports = append(reports, modelMap)
}
}
if err = d.Set("reports", reports); err != nil {
return diag.FromErr(fmt.Errorf("Error setting reports %s", err))
}
return nil
}
// dataSourceIbmSccLatestReportsID returns a reasonable ID for the list.
func dataSourceIbmSccLatestReportsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceIbmSccLatestReportsComplianceStatsToMap(model *securityandcompliancecenterapiv3.ComplianceStats) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Status != nil {
modelMap["status"] = model.Status
}
if model.TotalCount != nil {
modelMap["total_count"] = flex.IntValue(model.TotalCount)
}
if model.CompliantCount != nil {
modelMap["compliant_count"] = flex.IntValue(model.CompliantCount)
}
if model.NotCompliantCount != nil {
modelMap["not_compliant_count"] = flex.IntValue(model.NotCompliantCount)
}
if model.UnableToPerformCount != nil {
modelMap["unable_to_perform_count"] = flex.IntValue(model.UnableToPerformCount)
}
if model.UserEvaluationRequiredCount != nil {
modelMap["user_evaluation_required_count"] = flex.IntValue(model.UserEvaluationRequiredCount)
}
return modelMap, nil
}
func dataSourceIbmSccLatestReportsEvalStatsToMap(model *securityandcompliancecenterapiv3.EvalStats) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Status != nil {
modelMap["status"] = model.Status
}
if model.TotalCount != nil {
modelMap["total_count"] = flex.IntValue(model.TotalCount)
}
if model.PassCount != nil {
modelMap["pass_count"] = flex.IntValue(model.PassCount)
}
if model.FailureCount != nil {
modelMap["failure_count"] = flex.IntValue(model.FailureCount)
}
if model.ErrorCount != nil {
modelMap["error_count"] = flex.IntValue(model.ErrorCount)
}
if model.CompletedCount != nil {
modelMap["completed_count"] = flex.IntValue(model.CompletedCount)
}
return modelMap, nil
}
func dataSourceIbmSccLatestReportsComplianceScoreToMap(model *securityandcompliancecenterapiv3.ComplianceScore) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Passed != nil {
modelMap["passed"] = flex.IntValue(model.Passed)
}
if model.TotalCount != nil {
modelMap["total_count"] = flex.IntValue(model.TotalCount)
}
if model.Percent != nil {
modelMap["percent"] = flex.IntValue(model.Percent)
}
return modelMap, nil
}
func dataSourceIbmSccLatestReportsReportToMap(model *securityandcompliancecenterapiv3.Report) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ID != nil {
modelMap["id"] = model.ID
}
if model.GroupID != nil {
modelMap["group_id"] = model.GroupID
}
if model.CreatedOn != nil {
modelMap["created_on"] = model.CreatedOn
}
if model.ScanTime != nil {
modelMap["scan_time"] = model.ScanTime
}
if model.Type != nil {
modelMap["type"] = model.Type
}
if model.CosObject != nil {
modelMap["cos_object"] = model.CosObject
}
if model.InstanceID != nil {
modelMap["instance_id"] = model.InstanceID
}
if model.Account != nil {
accountMap, err := dataSourceIbmSccLatestReportsAccountToMap(model.Account)
if err != nil {
return modelMap, err
}
modelMap["account"] = []map[string]interface{}{accountMap}
}
if model.Profile != nil {
profileMap, err := dataSourceIbmSccLatestReportsProfileInfoToMap(model.Profile)
if err != nil {
return modelMap, err
}
modelMap["profile"] = []map[string]interface{}{profileMap}
}
if model.Attachment != nil {
attachmentMap, err := dataSourceIbmSccLatestReportsAttachmentToMap(model.Attachment)
if err != nil {
return modelMap, err
}
modelMap["attachment"] = []map[string]interface{}{attachmentMap}
}
return modelMap, nil
}
func dataSourceIbmSccLatestReportsAccountToMap(model *securityandcompliancecenterapiv3.Account) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ID != nil {
modelMap["id"] = model.ID
}
if model.Name != nil {
modelMap["name"] = model.Name
}
if model.Type != nil {
modelMap["type"] = model.Type
}
return modelMap, nil
}
func dataSourceIbmSccLatestReportsProfileInfoToMap(model *securityandcompliancecenterapiv3.ProfileInfo) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ID != nil {
modelMap["id"] = model.ID
}
if model.Name != nil {
modelMap["name"] = model.Name
}
if model.Version != nil {
modelMap["version"] = model.Version
}
return modelMap, nil
}
func dataSourceIbmSccLatestReportsAttachmentToMap(model *securityandcompliancecenterapiv3.Attachment) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ID != nil {
modelMap["id"] = model.ID
}
if model.Name != nil {
modelMap["name"] = model.Name
}
if model.Description != nil {
modelMap["description"] = model.Description
}
if model.Schedule != nil {
modelMap["schedule"] = model.Schedule
}
if model.Scope != nil {
scope := []map[string]interface{}{}
for _, scopeItem := range model.Scope {
scopeItemMap, err := dataSourceIbmSccLatestReportsAttachmentScopeToMap(&scopeItem)
if err != nil {
return modelMap, err
}
scope = append(scope, scopeItemMap)
}
modelMap["scope"] = scope
}
return modelMap, nil
}
func dataSourceIbmSccLatestReportsAttachmentScopeToMap(model *securityandcompliancecenterapiv3.AttachmentScope) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ID != nil {
modelMap["id"] = model.ID
}
if model.Environment != nil {
modelMap["environment"] = model.Environment
}
if model.Properties != nil {
properties := []map[string]interface{}{}
for _, propertiesItem := range model.Properties {
propertiesItemMap, err := dataSourceIbmSccLatestReportsScopePropertyToMap(&propertiesItem)
if err != nil {
return modelMap, err
}
properties = append(properties, propertiesItemMap)
}
modelMap["properties"] = properties
}
return modelMap, nil
}
func dataSourceIbmSccLatestReportsScopePropertyToMap(model *securityandcompliancecenterapiv3.ScopeProperty) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Name != nil {
modelMap["name"] = model.Name
}
if model.Value != nil {
modelMap["value"] = model.Value
}
return modelMap, nil
}