-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_scc_profiles.go
199 lines (189 loc) · 6.2 KB
/
data_source_ibm_scc_profiles.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
// 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/validate"
"github.com/IBM/scc-go-sdk/v5/securityandcompliancecenterapiv3"
)
func DataSourceIbmSccProfiles() *schema.Resource {
return AddSchemaData(&schema.Resource{
ReadContext: dataSourceIbmSccProfilesRead,
Schema: map[string]*schema.Schema{
"profile_type": {
Type: schema.TypeString,
Optional: true,
},
"profiles": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The list of profiles found.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: 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,
ValidateFunc: validate.InvokeValidator("ibm_scc_profile", "profile_type"),
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.",
},
"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.",
},
},
},
},
},
})
}
func dataSourceIbmSccProfilesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
securityandcompliancecenterapiClient, err := meta.(conns.ClientSession).SecurityAndComplianceCenterV3()
if err != nil {
return diag.FromErr(err)
}
listProfilesOptions := &securityandcompliancecenterapiv3.ListProfilesOptions{}
listProfilesOptions.SetInstanceID(d.Get("instance_id").(string))
if val, ok := d.GetOk("profile_type"); ok && val != nil {
listProfilesOptions.SetProfileType(d.Get("profile_type").(string))
}
pager, err := securityandcompliancecenterapiClient.NewProfilesPager(listProfilesOptions)
if err != nil {
log.Printf("[DEBUG] ListProfilesWithContext failed %s", err)
return diag.FromErr(fmt.Errorf("ListProfilesWithContext failed %s", err))
}
profileList, err := pager.GetAll()
if err != nil {
log.Printf("[DEBUG] ListProfilesWithContext failed %s", err)
return diag.FromErr(fmt.Errorf("ListProfilesWithContext failed %s", err))
}
d.SetId(fmt.Sprintf("%s/profiles", d.Get("instance_id").(string)))
if err = d.Set("instance_id", d.Get("instance_id")); err != nil {
return diag.FromErr(fmt.Errorf("Error setting instance_id %s", err))
}
profiles := []map[string]interface{}{}
for _, profile := range profileList {
modelMap, err := dataSourceIbmSccProfileToMap(&profile)
if err != nil {
return diag.FromErr(fmt.Errorf("Error setting profile:%v\n%s", profile, err))
}
profiles = append(profiles, modelMap)
}
if err = d.Set("profiles", profiles); err != nil {
return diag.FromErr(fmt.Errorf("Error setting profiles: %s", err))
}
return nil
}
func dataSourceIbmSccProfileToMap(profile *securityandcompliancecenterapiv3.ProfileItem) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if profile.ID != nil {
modelMap["id"] = profile.ID
}
if profile.ProfileName != nil {
modelMap["profile_name"] = profile.ProfileName
}
if profile.ProfileDescription != nil {
modelMap["profile_description"] = profile.ProfileDescription
}
if profile.ProfileType != nil {
modelMap["profile_type"] = profile.ProfileType
}
if profile.ProfileVersion != nil {
modelMap["profile_version"] = profile.ProfileVersion
}
if profile.VersionGroupLabel != nil {
modelMap["version_group_label"] = profile.VersionGroupLabel
}
if profile.Latest != nil {
modelMap["latest"] = profile.Latest
}
if profile.CreatedBy != nil {
modelMap["created_by"] = profile.CreatedBy
}
if profile.CreatedOn != nil {
modelMap["created_on"] = profile.CreatedOn.String()
}
if profile.UpdatedBy != nil {
modelMap["updated_by"] = profile.UpdatedBy
}
if profile.UpdatedOn != nil {
modelMap["updated_on"] = profile.UpdatedOn.String()
}
if profile.ControlsCount != nil {
modelMap["controls_count"] = profile.ControlsCount
}
if profile.AttachmentsCount != nil {
modelMap["attachments_count"] = profile.AttachmentsCount
}
return modelMap, nil
}