generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 4
/
workspace_participants.go
135 lines (101 loc) · 3.9 KB
/
workspace_participants.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
package client
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
func (c *TowerClient) CreateWorkspaceParticipant(ctx context.Context, workspaceId string, memberId int64, role string) (int64, string, error) {
payload := map[string]interface{}{
"memberId": memberId,
}
res, err := c.requestWithJsonPayload(ctx, "PUT", fmt.Sprintf("/orgs/%d/workspaces/%s/participants/add", c.orgId, workspaceId), nil, payload)
participantExists := false
if err != nil {
towerErr, ok := err.(towerError)
if ok {
if towerErr.statusCode == 409 {
participantExists = true
}
} else {
return -1, "", err
}
}
var participantObj map[string]interface{}
if participantExists {
ctx = tflog.SetField(ctx, "organizationId", c.orgId)
ctx = tflog.SetField(ctx, "workspaceId", workspaceId)
ctx = tflog.SetField(ctx, "memberId", memberId)
tflog.Debug(ctx, "Member already exists, updating current state and role")
participant, err := c.GetWorkspaceParticipantByMemberId(ctx, workspaceId, memberId)
if err != nil {
return -1, "", err
}
if participant == nil {
return -1, "", fmt.Errorf("No matching participant found with member ID: %d in workspace: %s", memberId, workspaceId)
}
participantObj = map[string]interface{}{"participant": participant}
} else {
if res == nil {
return -1, "", fmt.Errorf("Empty response from server")
}
ctx = tflog.SetField(ctx, "organizationId", c.orgId)
ctx = tflog.SetField(ctx, "workspaceId", workspaceId)
ctx = tflog.SetField(ctx, "memberId", memberId)
tflog.Debug(ctx, "Member created, updating role")
participantObj = res.(map[string]interface{})
}
participant := participantObj["participant"].(map[string]interface{})
participantId := int64(participant["participantId"].(float64))
err = c.UpdateWorkspaceParticipantRole(ctx, workspaceId, participantId, role)
return participantId, participant["email"].(string), err
}
func (c *TowerClient) UpdateWorkspaceParticipantRole(ctx context.Context, workspaceId string, id int64, role string) error {
payload := map[string]interface{}{
"role": role,
}
_, err := c.requestWithJsonPayload(ctx, "PUT", fmt.Sprintf("/orgs/%d/workspaces/%s/participants/%d/role", c.orgId, workspaceId, id), nil, payload)
return err
}
func (c *TowerClient) GetWorkspaceParticipants(ctx context.Context, workspaceId string, search map[string]string) ([]interface{}, error) {
res, err := c.requestWithoutPayload(ctx, "GET", fmt.Sprintf("/orgs/%d/workspaces/%s/participants", c.orgId, workspaceId), search)
if err != nil {
return nil, err
}
participants := res.(map[string]interface{})
if int64(participants["totalSize"].(float64)) == 0 {
return nil, nil
}
return participants["participants"].([]interface{}), nil
}
func (c *TowerClient) GetWorkspaceParticipantByMemberEmail(ctx context.Context, workspaceId string, email string) (map[string]interface{}, error) {
participants, err := c.GetWorkspaceParticipants(ctx, workspaceId, map[string]string{"search": email})
if err != nil {
return nil, err
}
if participants == nil {
return nil, nil
}
return participants[0].(map[string]interface{}), nil
}
func (c *TowerClient) GetWorkspaceParticipantByMemberId(ctx context.Context, workspaceId string, memberId int64) (map[string]interface{}, error) {
participants, err := c.GetWorkspaceParticipants(ctx, workspaceId, map[string]string{})
if err != nil {
return nil, err
}
if participants == nil {
return nil, nil
}
var participant map[string]interface{}
for _, value := range participants {
p := value.(map[string]interface{})
if int64(p["memberId"].(float64)) == memberId {
participant = p
break
}
}
return participant, nil
}
func (c *TowerClient) DeleteWorkspaceParticipant(ctx context.Context, workspaceId string, id int64) error {
_, err := c.requestWithoutPayload(ctx, "DELETE", fmt.Sprintf("/orgs/%d/workspaces/%s/participants/%d", c.orgId, workspaceId, id), nil)
return err
}