/
utils.go
354 lines (297 loc) · 7.31 KB
/
utils.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
package iamtf
import (
"fmt"
"reflect"
"strings"
cli "github.com/atricore/josso-sdk-go"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
api "github.com/atricore/josso-api-go"
)
// This builds a space separetd string with the values of the given schema resource (TypeSet -> TypeString)
func PtrSchemaAsSpacedList(d *schema.ResourceData, p string) (bool, string) {
m := d.Get(p).(*schema.Set)
if m == nil {
return false, ""
}
ls := m.List()
if len(ls) < 1 {
return false, ""
}
var sb strings.Builder
for _, v := range ls {
sb.WriteString(v.(string))
sb.WriteString(" ")
}
r := sb.String()
return true, r[:len(r)-1]
}
func SpacedListToSet(strLs string) (bool, *schema.Set) {
if strLs == "" {
return false, nil
}
ls := strings.Split(strLs, " ")
if len(ls) < 1 {
return false, nil
}
return true, convertStringSetToInterface(ls)
}
// Gets the value of the resources property as a string pointer
func PtrSchemaStr(d *schema.ResourceData, p string) *string {
a := d.Get(p)
var v string
switch a.(type) {
case nil:
return nil
default:
v = a.(string)
}
return &v
}
func PtrSchemaStrPointer(d *schema.ResourceData, p *string) *string {
a := d.Get(*p)
var v *string
switch a.(type) {
case nil:
return nil
default:
v = a.(*string)
}
return v
}
func PtrSchemaInt(d *schema.ResourceData, p string) *int {
a := d.Get(p)
var v int
switch a.(type) {
case nil:
return nil
default:
v = a.(int)
}
return &v
}
func PtrSchemaInt32(d *schema.ResourceData, p string) *int32 {
a := d.Get(p)
var v int32
switch a.(type) {
case nil:
return nil
default:
v = int32(a.(int))
}
return &v
}
func PtrSchemaInt64(d *schema.ResourceData, p string) *int64 {
a := d.Get(p)
var v int64
switch a.(type) {
case nil:
return nil
default:
v = int64(a.(int))
}
return &v
}
func PtrSchemaBool(d *schema.ResourceData, p string) *bool {
a := d.Get(p)
var v bool
switch a.(type) {
case nil:
return &v
default:
v = a.(bool)
}
return &v
}
func PtrSchemaLocation(d *schema.ResourceData, p string) (*api.LocationDTO, error) {
l := d.Get(p).(string)
return cli.StrToLocation(l)
}
func getJossoClient(meta interface{}) *cli.IdbusApiClient {
return meta.(*Config).apiClient
}
func getLogger(meta interface{}) hclog.Logger {
return meta.(*Config).logger
}
func convertStringArrToIdLookups(str []string) []api.IdentityLookupDTO {
arr := make([]api.IdentityLookupDTO, len(str))
for i, s := range str {
name := s
arr[i] = api.IdentityLookupDTO{Name: &name}
arr[i].AdditionalProperties = make(map[string]interface{})
arr[i].AdditionalProperties["@c"] = ".IdentityLookupDTO"
}
return arr
}
func convertIdLookupsToStringArr(dtos []api.IdentityLookupDTO) []string {
arr := make([]string, len(dtos))
for i, dto := range dtos {
arr[i] = *dto.Name
}
return arr
}
// --------------------------------------------
// Takes the first element of the array and returns its value as a map[string]interface{}
func asTFMapSingle(arr interface{}) (map[string]interface{}, error) {
var result map[string]interface{}
ls, err := asTFMap(arr, 1)
if err != nil {
return result, err
}
if len(ls) == 0 {
return result, nil
}
result, ok := ls[0].(map[string]interface{}) //
if !ok {
return result, fmt.Errorf("invalid argument value type %s", reflect.TypeOf(ls[0]))
}
return result, nil
}
func asTFMapAll(arr interface{}) ([]interface{}, error) {
return asTFMap(arr, 0)
}
// when maxLen is 0, it is umbounded
func asTFMap(arr interface{}, maxLen int) ([]interface{}, error) {
ls, ok := arr.([]interface{})
if !ok {
return ls, fmt.Errorf("invalid argument type %s", reflect.TypeOf(arr))
}
if maxLen > 0 && len(ls) > maxLen {
return ls, fmt.Errorf("invalid number of elements %d", len(ls))
}
return ls, nil
}
// --------------------------------------------------------------------
func convertStringArrToFederatedConnections(idps []string) []api.FederatedConnectionDTO {
arr := make([]api.FederatedConnectionDTO, len(idps))
for i, s := range idps {
name := s
arr[i] = *api.NewFederatedConnectionDTO()
arr[i].Name = &name
arr[i].AdditionalProperties = make(map[string]interface{})
arr[i].AdditionalProperties["@c"] = ".FederatedConnectionDTO"
}
return arr
}
func convertFederatedConnectionsToStringArr(dtos []api.FederatedConnectionDTO) []string {
arr := make([]string, len(dtos))
for i, dto := range dtos {
arr[i] = *dto.Name
}
return arr
}
func convertInterfaceToStringSet(set interface{}) []string {
if set == nil {
return make([]string, 0)
}
return convertInterfaceToStringArr(set.(*schema.Set).List())
}
func convertInterfaceToStringSetNullable(set interface{}) []string {
if set == nil {
return make([]string, 0)
}
return convertInterfaceToStringArrNullable(set.(*schema.Set).List())
}
func convertInterfaceToStringArrNullable(ls interface{}) []string {
arr := convertInterfaceToStringArr(ls)
if len(arr) < 1 {
return nil
}
return arr
}
func convertInterfaceToStringArr(ls interface{}) []string {
var arr []string
lsArr, ok := ls.([]interface{})
if ok {
arr = make([]string, len(lsArr))
for i, str := range lsArr {
s := str.(string)
arr[i] = s
}
}
return arr
}
func convertStringSetToInterface(stringList []string) *schema.Set {
arr := make([]interface{}, len(stringList))
for i, str := range stringList {
arr[i] = str
}
return schema.NewSet(schema.HashString, arr)
}
// The best practices states that aggregate types should have error handling (think non-primitive). This will not attempt to set nil values.
func setNonPrimitives(d *schema.ResourceData, valueMap map[string]interface{}) error {
for k, v := range valueMap {
if v != nil {
if err := d.Set(k, v); err != nil {
return fmt.Errorf("error setting %s for resource %s: %s", k, d.Id(), err)
}
}
}
return nil
}
func GetAsBool(d *schema.ResourceData, k string, v bool) bool {
// Although deprecated, this is actually what we need. GetOK does not work when values are 'false'
r, e := d.GetOkExists(k)
if !e {
return v
}
return r.(bool)
}
func GetAsString(d *schema.ResourceData, k string, v string) string {
r, e := d.GetOk(k)
if !e {
return v
}
return r.(string)
}
func GetAsInt32(d *schema.ResourceData, k string, v int32) int32 {
r, e := d.GetOk(k)
if !e {
return v
}
var result int32
switch v := r.(type) {
case int32:
result = v
case int:
result = int32(v)
case int64:
result = int32(v)
case float32:
result = int32(v)
case float64:
result = int32(v)
}
return result
}
func GetAsIface(d *schema.ResourceData, k string, v interface{}) interface{} {
r, e := d.GetOk(k)
if !e {
return v
}
return r
}
func convertSubjectAuthnPoliciesDTOToMapArr(ap []api.SubjectAuthenticationPolicyDTO) ([]map[string]interface{}, error) {
result := make([]map[string]interface{}, 0)
for _, sa := range ap {
subjetMap := map[string]interface{}{
"name": sa.GetName(),
}
result = append(result, subjetMap)
}
return result, nil
}
func convertSubjectAuthnPoliciesMapArrToDTO(ap_arr interface{}) ([]api.SubjectAuthenticationPolicyDTO, error) {
var sap []api.SubjectAuthenticationPolicyDTO
tfMapLs, err := asTFMapSingle(ap_arr)
if err != nil {
return sap, err
}
if tfMapLs == nil {
return sap, err
}
nsap := api.NewSubjectAuthenticationPolicyDTO()
nsap.SetName(api.AsString(tfMapLs["name"], ""))
sap = append(sap, *nsap)
return sap, nil
}