-
Notifications
You must be signed in to change notification settings - Fork 43
/
user_assigned_map.go
153 lines (126 loc) · 4.14 KB
/
user_assigned_map.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package identity
import (
"encoding/json"
"fmt"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var _ json.Marshaler = &UserAssignedMap{}
type UserAssignedMap struct {
Type Type `json:"type" tfschema:"type"`
IdentityIds map[string]UserAssignedIdentityDetails `json:"userAssignedIdentities"`
}
func (s *UserAssignedMap) MarshalJSON() ([]byte, error) {
// we use a custom marshal function here since we can only send the Type / UserAssignedIdentities field
identityType := TypeNone
userAssignedIdentityIds := map[string]UserAssignedIdentityDetails{}
if s != nil {
if s.Type == TypeUserAssigned {
identityType = TypeUserAssigned
userAssignedIdentityIds = s.IdentityIds
}
}
out := map[string]interface{}{
"type": string(identityType),
"userAssignedIdentities": nil,
}
if len(userAssignedIdentityIds) > 0 {
out["userAssignedIdentities"] = userAssignedIdentityIds
}
return json.Marshal(out)
}
// ExpandUserAssignedMap expands the schema input into a UserAssignedMap struct
func ExpandUserAssignedMap(input []interface{}) (*UserAssignedMap, error) {
identityType := TypeNone
identityIds := make(map[string]UserAssignedIdentityDetails, 0)
if len(input) > 0 {
raw := input[0].(map[string]interface{})
typeRaw := raw["type"].(string)
if typeRaw == string(TypeUserAssigned) {
identityType = TypeUserAssigned
}
identityIdsRaw := raw["identity_ids"].(*schema.Set).List()
for _, v := range identityIdsRaw {
identityIds[v.(string)] = UserAssignedIdentityDetails{
// intentionally empty since the expand shouldn't send these values
}
}
}
if len(identityIds) > 0 && identityType != TypeUserAssigned {
return nil, fmt.Errorf("`identity_ids` can only be specified when `type` is set to %q", string(TypeUserAssigned))
}
return &UserAssignedMap{
Type: identityType,
IdentityIds: identityIds,
}, nil
}
// FlattenUserAssignedMap turns a UserAssignedMap into a []interface{}
func FlattenUserAssignedMap(input *UserAssignedMap) (*[]interface{}, error) {
if input == nil {
return &[]interface{}{}, nil
}
input.Type = normalizeType(input.Type)
if input.Type != TypeUserAssigned {
return &[]interface{}{}, nil
}
identityIds := make([]string, 0)
for raw := range input.IdentityIds {
id, err := commonids.ParseUserAssignedIdentityIDInsensitively(raw)
if err != nil {
return nil, fmt.Errorf("parsing %q as a User Assigned Identity ID: %+v", raw, err)
}
identityIds = append(identityIds, id.ID())
}
return &[]interface{}{
map[string]interface{}{
"type": string(input.Type),
"identity_ids": identityIds,
},
}, nil
}
// ExpandUserAssignedMapFromModel expands the typed schema input into a UserAssignedMap struct
func ExpandUserAssignedMapFromModel(input []ModelUserAssigned) (*UserAssignedMap, error) {
if len(input) == 0 {
return &UserAssignedMap{
Type: TypeNone,
IdentityIds: nil,
}, nil
}
identity := input[0]
identityIds := make(map[string]UserAssignedIdentityDetails, 0)
for _, v := range identity.IdentityIds {
identityIds[v] = UserAssignedIdentityDetails{
// intentionally empty since the expand shouldn't send these values
}
}
return &UserAssignedMap{
Type: identity.Type,
IdentityIds: identityIds,
}, nil
}
// FlattenUserAssignedMapToModel turns a UserAssignedMap into a typed schema model
func FlattenUserAssignedMapToModel(input *UserAssignedMap) (*[]ModelUserAssigned, error) {
if input == nil {
return &[]ModelUserAssigned{}, nil
}
input.Type = normalizeType(input.Type)
if input.Type != TypeUserAssigned {
return &[]ModelUserAssigned{}, nil
}
identityIds := make([]string, 0)
for raw := range input.IdentityIds {
id, err := commonids.ParseUserAssignedIdentityIDInsensitively(raw)
if err != nil {
return nil, fmt.Errorf("parsing %q as a User Assigned Identity ID: %+v", raw, err)
}
identityIds = append(identityIds, id.ID())
}
return &[]ModelUserAssigned{
{
Type: input.Type,
IdentityIds: identityIds,
},
}, nil
}