/
data_source_ibm_iam_access_group_template_assignment.go
296 lines (274 loc) · 9.52 KB
/
data_source_ibm_iam_access_group_template_assignment.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
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package iamaccessgroup
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/platform-services-go-sdk/iamaccessgroupsv2"
)
func DataSourceIBMIAMAccessGroupTemplateAssignment() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMIAMAccessGroupTemplateAssignmentRead,
Schema: map[string]*schema.Schema{
"account_id": {
Type: schema.TypeString,
Computed: true,
Description: "Enterprise account ID.",
},
"template_id": {
Type: schema.TypeString,
Optional: true,
Description: "Filter results by Template Id.",
},
"template_version": {
Type: schema.TypeString,
Optional: true,
Description: "Filter results by Template Version.",
},
"target": {
Type: schema.TypeString,
Optional: true,
Description: "Filter results by the assignment target.",
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: "Filter results by the assignment status.",
},
"transaction_id": {
Type: schema.TypeString,
Optional: true,
Description: "An optional transaction id for the request.",
},
"limit": {
Type: schema.TypeInt,
Computed: true,
Description: "Maximum number of items returned in the response.",
},
"offset": {
Type: schema.TypeInt,
Computed: true,
Description: "Index of the first item returned in the response.",
},
"total_count": {
Type: schema.TypeInt,
Computed: true,
Description: "Total number of items matching the query.",
},
"first": {
Type: schema.TypeList,
Computed: true,
Description: "A link object.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "A string containing the link’s URL.",
},
},
},
},
"last": {
Type: schema.TypeList,
Computed: true,
Description: "A link object.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "A string containing the link’s URL.",
},
},
},
},
"assignments": {
Type: schema.TypeList,
Computed: true,
Description: "List of template assignments.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the assignment.",
},
"account_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the account that the assignment belongs to.",
},
"template_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the template that the assignment is based on.",
},
"template_version": {
Type: schema.TypeString,
Computed: true,
Description: "The version of the template that the assignment is based on.",
},
"target_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of the entity that the assignment applies to.",
},
"target": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the entity that the assignment applies to.",
},
"operation": {
Type: schema.TypeString,
Computed: true,
Description: "The operation that the assignment applies to (e.g. 'assign', 'update', 'remove').",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of the assignment (e.g. 'accepted', 'in_progress', 'succeeded', 'failed', 'superseded').",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the assignment resource.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time when the assignment was created.",
},
"created_by_id": {
Type: schema.TypeString,
Computed: true,
Description: "The user or system that created the assignment.",
},
"last_modified_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time when the assignment was last updated.",
},
"last_modified_by_id": {
Type: schema.TypeString,
Computed: true,
Description: "The user or system that last updated the assignment.",
},
},
},
},
},
}
}
func dataSourceIBMIAMAccessGroupTemplateAssignmentRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
iamAccessGroupsClient, err := meta.(conns.ClientSession).IAMAccessGroupsV2()
if err != nil {
return diag.FromErr(err)
}
listAssignmentsOptions := &iamaccessgroupsv2.ListAssignmentsOptions{}
userDetails, err := meta.(conns.ClientSession).BluemixUserDetails()
if err != nil {
return diag.FromErr(err)
}
listAssignmentsOptions.SetAccountID(userDetails.UserAccount)
if _, ok := d.GetOk("template_id"); ok {
listAssignmentsOptions.SetTemplateID(d.Get("template_id").(string))
}
if _, ok := d.GetOk("template_version"); ok {
listAssignmentsOptions.SetTemplateVersion(d.Get("template_version").(string))
}
if _, ok := d.GetOk("target"); ok {
listAssignmentsOptions.SetTarget(d.Get("target").(string))
}
if _, ok := d.GetOk("status"); ok {
listAssignmentsOptions.SetStatus(d.Get("status").(string))
}
if _, ok := d.GetOk("transaction_id"); ok {
listAssignmentsOptions.SetTransactionID(d.Get("transaction_id").(string))
}
listTemplateAssignmentResponse, response, err := iamAccessGroupsClient.ListAssignmentsWithContext(context, listAssignmentsOptions)
if err != nil {
log.Printf("[DEBUG] ListAssignmentsWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListAssignmentsWithContext failed %s\n%s", err, response))
}
d.SetId(dataSourceIBMIAMAccessGroupTemplateAssignmentID(d))
if err = d.Set("limit", flex.IntValue(listTemplateAssignmentResponse.Limit)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting limit: %s", err))
}
if err = d.Set("offset", flex.IntValue(listTemplateAssignmentResponse.Offset)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting offset: %s", err))
}
if err = d.Set("total_count", flex.IntValue(listTemplateAssignmentResponse.TotalCount)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting total_count: %s", err))
}
first := []map[string]interface{}{}
if listTemplateAssignmentResponse.First != nil {
modelMap, err := dataSourceIBMIAMAccessGroupTemplateAssignmentHrefStructToMap(listTemplateAssignmentResponse.First)
if err != nil {
return diag.FromErr(err)
}
first = append(first, modelMap)
}
if err = d.Set("first", first); err != nil {
return diag.FromErr(fmt.Errorf("Error setting first %s", err))
}
last := []map[string]interface{}{}
if listTemplateAssignmentResponse.Last != nil {
modelMap, err := dataSourceIBMIAMAccessGroupTemplateAssignmentHrefStructToMap(listTemplateAssignmentResponse.Last)
if err != nil {
return diag.FromErr(err)
}
last = append(last, modelMap)
}
if err = d.Set("last", last); err != nil {
return diag.FromErr(fmt.Errorf("Error setting last %s", err))
}
assignments := []map[string]interface{}{}
if listTemplateAssignmentResponse.Assignments != nil {
for _, modelItem := range listTemplateAssignmentResponse.Assignments {
modelMap, err := dataSourceIBMIAMAccessGroupTemplateAssignmentTemplateAssignmentResponseToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
assignments = append(assignments, modelMap)
}
}
if err = d.Set("assignments", assignments); err != nil {
return diag.FromErr(fmt.Errorf("Error setting assignments %s", err))
}
return nil
}
// dataSourceIBMIAMAccessGroupTemplateAssignmentID returns a reasonable ID for the list.
func dataSourceIBMIAMAccessGroupTemplateAssignmentID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceIBMIAMAccessGroupTemplateAssignmentHrefStructToMap(model *iamaccessgroupsv2.HrefStruct) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Href != nil {
modelMap["href"] = model.Href
}
return modelMap, nil
}
func dataSourceIBMIAMAccessGroupTemplateAssignmentTemplateAssignmentResponseToMap(model *iamaccessgroupsv2.TemplateAssignmentResponse) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["id"] = model.ID
modelMap["account_id"] = model.AccountID
modelMap["template_id"] = model.TemplateID
modelMap["template_version"] = model.TemplateVersion
modelMap["target_type"] = model.TargetType
modelMap["target"] = model.Target
modelMap["operation"] = model.Operation
modelMap["status"] = model.Status
modelMap["href"] = model.Href
modelMap["created_at"] = model.CreatedAt.String()
modelMap["created_by_id"] = model.CreatedByID
modelMap["last_modified_at"] = model.LastModifiedAt.String()
modelMap["last_modified_by_id"] = model.LastModifiedByID
return modelMap, nil
}