-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathdata_source_ibm_scc_profile.go
592 lines (558 loc) · 19.2 KB
/
data_source_ibm_scc_profile.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package scc
import (
"context"
"fmt"
"log"
"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 DataSourceIbmSccProfile() *schema.Resource {
return AddSchemaData(&schema.Resource{
ReadContext: dataSourceIbmSccProfileRead,
Schema: map[string]*schema.Schema{
"profile_id": {
Type: schema.TypeString,
Required: true,
Description: "The profile ID.",
},
"profile_name": {
Type: schema.TypeString,
Computed: true,
Description: "The profile name.",
},
"profile_description": {
Type: schema.TypeString,
Computed: true,
Description: "The profile description.",
},
"profile_type": {
Type: schema.TypeString,
Computed: true,
Description: "The profile type, such as custom or predefined.",
},
"profile_version": {
Type: schema.TypeString,
Computed: true,
Description: "The version status of the profile.",
},
"version_group_label": {
Type: schema.TypeString,
Computed: true,
Description: "The version group label of the profile.",
},
"instance_id": {
Type: schema.TypeString,
Computed: true,
Description: "The instance ID.",
},
"latest": {
Type: schema.TypeBool,
Computed: true,
Description: "The latest version of the profile.",
},
"hierarchy_enabled": {
Type: schema.TypeBool,
Computed: true,
Description: "The indication of whether hierarchy is enabled for the profile.",
},
"created_by": {
Type: schema.TypeString,
Computed: true,
Description: "The user who created the profile.",
},
"created_on": {
Type: schema.TypeString,
Computed: true,
Description: "The date when the profile was created.",
},
"updated_by": {
Type: schema.TypeString,
Computed: true,
Description: "The user who updated the profile.",
},
"updated_on": {
Type: schema.TypeString,
Computed: true,
Description: "The date when the profile was updated.",
},
"controls_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of controls for the profile.",
},
"control_parents_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of parent controls for the profile.",
},
"attachments_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of attachments related to this profile.",
},
"controls": {
Type: schema.TypeList,
Computed: true,
Description: "The array of controls that are used to create the profile.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"control_library_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the control library that contains the profile.",
},
"control_id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique ID of the control library that contains the profile.",
},
"control_library_version": {
Type: schema.TypeString,
Computed: true,
Description: "The most recent version of the control library.",
},
"control_name": {
Type: schema.TypeString,
Computed: true,
Description: "The control name.",
},
"control_description": {
Type: schema.TypeString,
Computed: true,
Description: "The control description.",
},
"control_category": {
Type: schema.TypeString,
Computed: true,
Description: "The control category.",
},
"control_parent": {
Type: schema.TypeString,
Computed: true,
Description: "The parent control.",
},
"control_requirement": {
Type: schema.TypeBool,
Computed: true,
Description: "Is this a control that can be automated or manually evaluated.",
},
"control_docs": {
Type: schema.TypeList,
Computed: true,
Description: "The control documentation.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"control_docs_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the control documentation.",
},
"control_docs_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of control documentation.",
},
},
},
},
"control_specifications_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of control specifications.",
},
"control_specifications": {
Type: schema.TypeList,
Computed: true,
Description: "The control specifications.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"control_specification_id": {
Type: schema.TypeString,
Computed: true,
Description: "The control specification ID.",
},
"responsibility": {
Type: schema.TypeString,
Computed: true,
Description: "The responsibility for managing the control.",
},
"component_id": {
Type: schema.TypeString,
Computed: true,
Description: "The component ID.",
},
"component_name": {
Type: schema.TypeString,
Computed: true,
Description: "The component name.",
},
"environment": {
Type: schema.TypeString,
Computed: true,
Description: "The control specifications environment.",
},
"control_specification_description": {
Type: schema.TypeString,
Computed: true,
Description: "The control specifications description.",
},
"assessments_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of assessments.",
},
"assessments": {
Type: schema.TypeList,
Computed: true,
Description: "The assessments.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"assessment_id": {
Type: schema.TypeString,
Computed: true,
Description: "The assessment ID.",
},
"assessment_method": {
Type: schema.TypeString,
Computed: true,
Description: "The assessment method.",
},
"assessment_type": {
Type: schema.TypeString,
Computed: true,
Description: "The assessment type.",
},
"assessment_description": {
Type: schema.TypeString,
Computed: true,
Description: "The assessment description.",
},
"parameter_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The parameter count.",
},
"parameters": {
Type: schema.TypeList,
Computed: true,
Description: "The parameters.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"parameter_name": {
Type: schema.TypeString,
Computed: true,
Description: "The parameter name.",
},
"parameter_display_name": {
Type: schema.TypeString,
Computed: true,
Description: "The parameter display name.",
},
"parameter_type": {
Type: schema.TypeString,
Computed: true,
Description: "The parameter type.",
},
},
},
},
},
},
},
},
},
},
},
},
},
"default_parameters": {
Type: schema.TypeList,
Computed: true,
Description: "The default parameters of the profile.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"assessment_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of the implementation.",
},
"assessment_id": {
Type: schema.TypeString,
Computed: true,
Description: "The implementation ID of the parameter.",
},
"parameter_name": {
Type: schema.TypeString,
Computed: true,
Description: "The parameter name.",
},
"parameter_default_value": {
Type: schema.TypeString,
Computed: true,
Description: "The default value of the parameter.",
},
"parameter_display_name": {
Type: schema.TypeString,
Computed: true,
Description: "The parameter display name.",
},
"parameter_type": {
Type: schema.TypeString,
Computed: true,
Description: "The parameter type.",
},
},
},
},
},
})
}
func dataSourceIbmSccProfileRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
securityandcompliancecenterapiClient, err := meta.(conns.ClientSession).SecurityAndComplianceCenterV3()
if err != nil {
return diag.FromErr(err)
}
getProfileOptions := &securityandcompliancecenterapiv3.GetProfileOptions{}
getProfileOptions.SetProfileID(d.Get("profile_id").(string))
getProfileOptions.SetInstanceID(d.Get("instance_id").(string))
profile, response, err := securityandcompliancecenterapiClient.GetProfileWithContext(context, getProfileOptions)
if err != nil {
log.Printf("[DEBUG] GetProfileWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetProfileWithContext failed %s\n%s", err, response))
}
d.SetId(fmt.Sprintf("%s", *getProfileOptions.ProfileID))
if err = d.Set("profile_name", profile.ProfileName); err != nil {
return diag.FromErr(fmt.Errorf("Error setting profile_name: %s", err))
}
if err = d.Set("profile_description", profile.ProfileDescription); err != nil {
return diag.FromErr(fmt.Errorf("Error setting profile_description: %s", err))
}
if err = d.Set("profile_type", profile.ProfileType); err != nil {
return diag.FromErr(fmt.Errorf("Error setting profile_type: %s", err))
}
if err = d.Set("profile_version", profile.ProfileVersion); err != nil {
return diag.FromErr(fmt.Errorf("Error setting profile_version: %s", err))
}
if err = d.Set("version_group_label", profile.VersionGroupLabel); err != nil {
return diag.FromErr(fmt.Errorf("Error setting version_group_label: %s", err))
}
if err = d.Set("latest", profile.Latest); err != nil {
return diag.FromErr(fmt.Errorf("Error setting latest: %s", err))
}
if err = d.Set("hierarchy_enabled", profile.HierarchyEnabled); err != nil {
return diag.FromErr(fmt.Errorf("Error setting hierarchy_enabled: %s", err))
}
if err = d.Set("created_by", profile.CreatedBy); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_by: %s", err))
}
if err = d.Set("created_on", flex.DateTimeToString(profile.CreatedOn)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_on: %s", err))
}
if err = d.Set("updated_by", profile.UpdatedBy); err != nil {
return diag.FromErr(fmt.Errorf("Error setting updated_by: %s", err))
}
if err = d.Set("updated_on", flex.DateTimeToString(profile.UpdatedOn)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting updated_on: %s", err))
}
if err = d.Set("controls_count", flex.IntValue(profile.ControlsCount)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting controls_count: %s", err))
}
if err = d.Set("control_parents_count", flex.IntValue(profile.ControlParentsCount)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting control_parents_count: %s", err))
}
if err = d.Set("attachments_count", flex.IntValue(profile.AttachmentsCount)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting attachments_count: %s", err))
}
controls := []map[string]interface{}{}
if profile.Controls != nil {
for _, modelItem := range profile.Controls {
modelMap, err := dataSourceIbmSccProfileProfileControlsToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
controls = append(controls, modelMap)
}
}
if err = d.Set("controls", controls); err != nil {
return diag.FromErr(fmt.Errorf("Error setting controls %s", err))
}
defaultParameters := []map[string]interface{}{}
if profile.DefaultParameters != nil {
for _, modelItem := range profile.DefaultParameters {
modelMap, err := dataSourceIbmSccProfileDefaultParametersPrototypeToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
defaultParameters = append(defaultParameters, modelMap)
}
}
if err = d.Set("default_parameters", defaultParameters); err != nil {
return diag.FromErr(fmt.Errorf("Error setting default_parameters %s", err))
}
return nil
}
func dataSourceIbmSccProfileProfileControlsToMap(model *securityandcompliancecenterapiv3.ProfileControls) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ControlLibraryID != nil {
modelMap["control_library_id"] = model.ControlLibraryID
}
if model.ControlID != nil {
modelMap["control_id"] = model.ControlID
}
if model.ControlLibraryVersion != nil {
modelMap["control_library_version"] = model.ControlLibraryVersion
}
if model.ControlName != nil {
modelMap["control_name"] = model.ControlName
}
if model.ControlDescription != nil {
modelMap["control_description"] = model.ControlDescription
}
if model.ControlCategory != nil {
modelMap["control_category"] = model.ControlCategory
}
if model.ControlParent != nil {
modelMap["control_parent"] = model.ControlParent
}
if model.ControlRequirement != nil {
modelMap["control_requirement"] = model.ControlRequirement
}
if model.ControlDocs != nil {
controlDocsMap, err := dataSourceIbmSccProfileControlDocsToMap(model.ControlDocs)
if err != nil {
return modelMap, err
}
modelMap["control_docs"] = []map[string]interface{}{controlDocsMap}
}
if model.ControlSpecificationsCount != nil {
modelMap["control_specifications_count"] = flex.IntValue(model.ControlSpecificationsCount)
}
if model.ControlSpecifications != nil {
controlSpecifications := []map[string]interface{}{}
for _, controlSpecificationsItem := range model.ControlSpecifications {
controlSpecificationsItemMap, err := dataSourceIbmSccProfileControlSpecificationsToMap(&controlSpecificationsItem)
if err != nil {
return modelMap, err
}
controlSpecifications = append(controlSpecifications, controlSpecificationsItemMap)
}
modelMap["control_specifications"] = controlSpecifications
}
return modelMap, nil
}
func dataSourceIbmSccProfileControlDocsToMap(model *securityandcompliancecenterapiv3.ControlDocs) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ControlDocsID != nil {
modelMap["control_docs_id"] = model.ControlDocsID
}
if model.ControlDocsType != nil {
modelMap["control_docs_type"] = model.ControlDocsType
}
return modelMap, nil
}
func dataSourceIbmSccProfileControlSpecificationsToMap(model *securityandcompliancecenterapiv3.ControlSpecifications) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ControlSpecificationID != nil {
modelMap["control_specification_id"] = model.ControlSpecificationID
}
if model.Responsibility != nil {
modelMap["responsibility"] = model.Responsibility
}
if model.ComponentID != nil {
modelMap["component_id"] = model.ComponentID
}
if model.ComponentName != nil {
modelMap["component_name"] = model.ComponentName
}
if model.Environment != nil {
modelMap["environment"] = model.Environment
}
if model.ControlSpecificationDescription != nil {
modelMap["control_specification_description"] = model.ControlSpecificationDescription
}
if model.AssessmentsCount != nil {
modelMap["assessments_count"] = flex.IntValue(model.AssessmentsCount)
}
if model.Assessments != nil {
assessments := []map[string]interface{}{}
for _, assessmentsItem := range model.Assessments {
assessmentsItemMap, err := dataSourceIbmSccProfileImplementationToMap(&assessmentsItem)
if err != nil {
return modelMap, err
}
assessments = append(assessments, assessmentsItemMap)
}
modelMap["assessments"] = assessments
}
return modelMap, nil
}
func dataSourceIbmSccProfileImplementationToMap(model *securityandcompliancecenterapiv3.Implementation) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.AssessmentID != nil {
modelMap["assessment_id"] = model.AssessmentID
}
if model.AssessmentMethod != nil {
modelMap["assessment_method"] = model.AssessmentMethod
}
if model.AssessmentType != nil {
modelMap["assessment_type"] = model.AssessmentType
}
if model.AssessmentDescription != nil {
modelMap["assessment_description"] = model.AssessmentDescription
}
if model.ParameterCount != nil {
modelMap["parameter_count"] = flex.IntValue(model.ParameterCount)
}
if model.Parameters != nil {
parameters := []map[string]interface{}{}
for _, parametersItem := range model.Parameters {
parametersItemMap, err := dataSourceIbmSccProfileParameterInfoToMap(¶metersItem)
if err != nil {
return modelMap, err
}
parameters = append(parameters, parametersItemMap)
}
modelMap["parameters"] = parameters
}
return modelMap, nil
}
func dataSourceIbmSccProfileParameterInfoToMap(model *securityandcompliancecenterapiv3.ParameterInfo) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ParameterName != nil {
modelMap["parameter_name"] = model.ParameterName
}
if model.ParameterDisplayName != nil {
modelMap["parameter_display_name"] = model.ParameterDisplayName
}
if model.ParameterType != nil {
modelMap["parameter_type"] = model.ParameterType
}
return modelMap, nil
}
func dataSourceIbmSccProfileDefaultParametersPrototypeToMap(model *securityandcompliancecenterapiv3.DefaultParametersPrototype) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.AssessmentType != nil {
modelMap["assessment_type"] = model.AssessmentType
}
if model.AssessmentID != nil {
modelMap["assessment_id"] = model.AssessmentID
}
if model.ParameterName != nil {
modelMap["parameter_name"] = model.ParameterName
}
if model.ParameterDefaultValue != nil {
modelMap["parameter_default_value"] = model.ParameterDefaultValue
}
if model.ParameterDisplayName != nil {
modelMap["parameter_display_name"] = model.ParameterDisplayName
}
if model.ParameterType != nil {
modelMap["parameter_type"] = model.ParameterType
}
return modelMap, nil
}