-
Notifications
You must be signed in to change notification settings - Fork 43
/
system_or_user_assigned_map.go
171 lines (144 loc) · 5.18 KB
/
system_or_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// 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 = &SystemOrUserAssignedMap{}
type SystemOrUserAssignedMap struct {
Type Type `json:"type" tfschema:"type"`
PrincipalId string `json:"principalId" tfschema:"principal_id"`
TenantId string `json:"tenantId" tfschema:"tenant_id"`
IdentityIds map[string]UserAssignedIdentityDetails `json:"userAssignedIdentities"`
}
func (s *SystemOrUserAssignedMap) 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 == TypeSystemAssigned {
identityType = TypeSystemAssigned
}
if s.Type == TypeUserAssigned {
identityType = TypeUserAssigned
}
if identityType != TypeNone {
userAssignedIdentityIds = s.IdentityIds
}
}
out := map[string]interface{}{
"type": string(identityType),
"userAssignedIdentities": nil,
}
if len(userAssignedIdentityIds) > 0 {
out["userAssignedIdentities"] = userAssignedIdentityIds
}
return json.Marshal(out)
}
// ExpandSystemOrUserAssignedMap expands the schema input into a SystemOrUserAssignedMap struct
func ExpandSystemOrUserAssignedMap(input []interface{}) (*SystemOrUserAssignedMap, 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(TypeSystemAssigned) {
identityType = TypeSystemAssigned
}
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))
}
identity := &SystemOrUserAssignedMap{
Type: identityType,
IdentityIds: identityIds,
}
return identity, nil
}
// FlattenSystemOrUserAssignedMap turns a SystemOrUserAssignedMap into a []interface{}
func FlattenSystemOrUserAssignedMap(input *SystemOrUserAssignedMap) (*[]interface{}, error) {
if input == nil {
return &[]interface{}{}, nil
}
input.Type = normalizeType(input.Type)
if input.Type != TypeSystemAssigned && 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,
"principal_id": input.PrincipalId,
"tenant_id": input.TenantId,
},
}, nil
}
// ExpandSystemOrUserAssignedMapFromModel expands the typed schema input into a SystemOrUserAssignedMap struct
func ExpandSystemOrUserAssignedMapFromModel(input []ModelSystemAssignedUserAssigned) (*SystemOrUserAssignedMap, error) {
if len(input) == 0 {
return &SystemOrUserAssignedMap{
Type: TypeNone,
IdentityIds: nil,
}, nil
}
identity := input[0]
identityIds := make(map[string]UserAssignedIdentityDetails, len(identity.IdentityIds))
for _, v := range identity.IdentityIds {
identityIds[v] = UserAssignedIdentityDetails{
// intentionally empty since the expand shouldn't send these values
}
}
if len(identityIds) > 0 && identity.Type != TypeUserAssigned {
return nil, fmt.Errorf("`identity_ids` can only be specified when `type` is set to %q", TypeUserAssigned)
}
return &SystemOrUserAssignedMap{
Type: identity.Type,
IdentityIds: identityIds,
}, nil
}
// FlattenSystemOrUserAssignedMapToModel turns a SystemOrUserAssignedMap into a typed schema model
func FlattenSystemOrUserAssignedMapToModel(input *SystemOrUserAssignedMap) (*[]ModelSystemAssignedUserAssigned, error) {
if input == nil {
return &[]ModelSystemAssignedUserAssigned{}, nil
}
input.Type = normalizeType(input.Type)
if input.Type != TypeSystemAssigned && input.Type != TypeUserAssigned {
return &[]ModelSystemAssignedUserAssigned{}, 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 &[]ModelSystemAssignedUserAssigned{
{
Type: input.Type,
IdentityIds: identityIds,
PrincipalId: input.PrincipalId,
TenantId: input.TenantId,
},
}, nil
}