-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresource_team.go
301 lines (244 loc) · 7.17 KB
/
resource_team.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
package main
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/pactflow/terraform/broker"
"github.com/pactflow/terraform/client"
)
func team() *schema.Resource {
return &schema.Resource{
Create: teamCreate,
Update: teamUpdate,
Read: teamRead,
Delete: teamDelete,
Importer: &schema.ResourceImporter{State: schema.ImportStatePassthrough},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the Team",
},
"uuid": {
Type: schema.TypeString,
Computed: true,
Description: "The UUID of team",
},
"pacticipants": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"users": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"administrators": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func getTeamFromResourceData(d *schema.ResourceData) broker.Team {
name := d.Get("name").(string)
uuid := d.Id()
team := broker.Team{
UUID: uuid,
Name: name,
}
pacticipants := ExpandStringSet(d.Get("pacticipants").(*schema.Set))
log.Println("[DEBUG] resource_team.go pacticipants?", pacticipants)
if len(pacticipants) > 0 {
log.Println("[DEBUG] resource_team.go have pacticipants", len(pacticipants), pacticipants)
items := make([]broker.Pacticipant, len(pacticipants))
for i, p := range pacticipants {
items[i] = broker.Pacticipant{
Name: p,
}
}
team.Embedded.Pacticipants = items
}
administrators := ExpandStringSet(d.Get("administrators").(*schema.Set))
log.Println("[DEBUG] resource_team.go administrators?", administrators)
if len(administrators) > 0 {
log.Println("[DEBUG] resource_team.go have administrators", len(administrators), administrators)
items := make([]broker.TeamUser, len(administrators))
for i, p := range administrators {
items[i] = broker.TeamUser{
UUID: p,
}
}
team.Embedded.Administrators = items
}
return team
}
// Removes any users from the team that shouldn't be there, and adds those that should
func assignTeamUsers(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
uuid := d.Id()
log.Println("[DEBUG] assigning users to team", uuid)
if d.HasChange("users") {
old, new := d.GetChange("users")
log.Println("[DEBUG] teamAssignmentCreate - change. old:", old, "new:", new)
usersToAdd := ExpandStringSet(new.(*schema.Set))
log.Println("[DEBUG] teamAssignmentCreate - setting users:", usersToAdd)
req := broker.TeamsAssignmentRequest{
UUID: uuid,
Users: usersToAdd,
}
res, err := client.UpdateTeamAssignments(req)
if err != nil {
return err
}
return setTeamAssignmentState(d, res)
}
return nil
}
func teamToCRUDRequest(t broker.Team) broker.TeamCreateOrUpdateRequest {
pacticipants := make([]string, len(t.Embedded.Pacticipants))
for i, a := range t.Embedded.Pacticipants {
pacticipants[i] = a.Name
}
administrators := make([]string, len(t.Embedded.Administrators))
for i, a := range t.Embedded.Administrators {
administrators[i] = a.UUID
}
return broker.TeamCreateOrUpdateRequest{
Name: t.Name,
UUID: t.UUID,
PacticipantNames: pacticipants,
AdministratorUUIDs: administrators,
}
}
func teamCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
team := getTeamFromResourceData(d)
create := teamToCRUDRequest(team)
log.Println("[DEBUG] creating team", team)
created, err := client.CreateTeam(create)
if err != nil {
return fmt.Errorf("error creating team: %w", err)
}
team.UUID = created.UUID
d.SetId(created.UUID)
setTeamState(d, *created)
err = assignTeamUsers(d, meta)
if err != nil {
d.Partial(true)
log.Printf("\n\n[DEBUG] error assigning team users: %v \n\n", err)
return fmt.Errorf("error assigning team users: %w", err)
}
return err
}
func teamUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
team := getTeamFromResourceData(d)
update := teamToCRUDRequest(team)
log.Println("[DEBUG] updating team", team)
updated, err := client.UpdateTeam(update)
if err == nil {
setTeamState(d, *updated)
}
err = assignTeamUsers(d, meta)
if err != nil {
d.Partial(true)
return fmt.Errorf("error assigning team users: %w", err)
}
return err
}
func teamRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
read := getTeamFromResourceData(d)
log.Println("[DEBUG] reading team", read)
team, err := client.ReadTeam(read)
log.Println("[DEBUG] have team for READ", team)
if err == nil {
d.SetId(team.UUID)
setTeamState(d, *team)
}
return err
}
func teamDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*client.Client)
uuid := d.Id()
team := broker.Team{
UUID: uuid,
}
log.Println("[DEBUG] deleting team", team)
err := client.DeleteTeam(team)
if err != nil {
d.SetId("")
}
return err
}
func setTeamState(d *schema.ResourceData, team broker.Team) error {
log.Printf("[DEBUG] setting team state: %+v \n", team)
if err := d.Set("name", team.Name); err != nil {
log.Println("[ERROR] error setting key 'name'", err)
return err
}
if err := d.Set("uuid", team.UUID); err != nil {
log.Println("[ERROR] error setting key 'uuid'", err)
return err
}
if len(team.Embedded.Pacticipants) > 0 {
pacticipants := make([]string, len(team.Embedded.Pacticipants))
for i, p := range team.Embedded.Pacticipants {
pacticipants[i] = p.Name
}
if err := d.Set("pacticipants", pacticipants); err != nil {
log.Println("[ERROR] error setting key 'pacticipants'", err)
return err
}
}
if len(team.Embedded.Members) > 0 {
members := make([]string, len(team.Embedded.Members))
for i, m := range team.Embedded.Members {
log.Println("[DEBUG] adding team member with UUID", m.UUID)
members[i] = m.UUID
}
if err := d.Set("users", members); err != nil {
log.Println("[ERROR] error setting key 'users'", err)
return err
}
}
if len(team.Embedded.Administrators) > 0 {
administrators := make([]string, len(team.Embedded.Administrators))
for i, a := range team.Embedded.Administrators {
log.Println("[DEBUG] adding administrator with UUID", a.UUID)
administrators[i] = a.UUID
}
if err := d.Set("administrators", administrators); err != nil {
log.Println("[ERROR] error setting key 'administrators'", err)
return err
}
}
return nil
}
func setTeamAssignmentState(d *schema.ResourceData, team *broker.TeamsAssignmentResponse) error {
log.Printf("[DEBUG] setting team assignment state: %+v \n", team)
if team != nil {
if err := d.Set("users", extractUsersFromAPIResponse(team)); err != nil {
log.Println("[ERROR] error setting key 'users'", err)
return err
}
}
return nil
}
// Use this to find the delta, and delete them from the team
func extractUsersFromAPIResponse(response *broker.TeamsAssignmentResponse) []string {
users := make([]string, len(response.Embedded.Users))
for i, u := range response.Embedded.Users {
users[i] = u.UUID
}
return users
}