/
group.go
176 lines (145 loc) · 3.81 KB
/
group.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
package tableau
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
)
type GroupImport struct {
DomainName *string `json:"domainName"`
MinimumSiteRole *string `json:"siteRole"`
GrantLicenseMode *string `json:"grantLicenseMode"`
}
type Group struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
MinimumSiteRole string `json:"minimumSiteRole"`
Import *GroupImport `json:"import,omitempty"`
}
type GroupRequest struct {
Group Group `json:"group"`
}
type GroupResponse struct {
Group Group `json:"group"`
}
type GroupsResponse struct {
Groups []Group `json:"group"`
}
type GroupListResponse struct {
GroupsResponse GroupsResponse `json:"groups"`
Pagination PaginationDetails `json:"pagination"`
}
func (c *Client) GetGroup(groupID string) (*Group, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/groups", c.ApiUrl), nil)
if err != nil {
return nil, err
}
body, err := c.doRequest(req)
if err != nil {
return nil, err
}
groupListResponse := GroupListResponse{}
err = json.Unmarshal(body, &groupListResponse)
if err != nil {
return nil, err
}
// TODO: Generalise pagination handling and use elsewhere
pageNumber, totalPageCount, err := GetPaginationNumbers(groupListResponse.Pagination)
if err != nil {
return nil, err
}
for i, group := range groupListResponse.GroupsResponse.Groups {
if group.ID == groupID {
return &groupListResponse.GroupsResponse.Groups[i], nil
}
}
for page := pageNumber + 1; page <= totalPageCount; page++ {
fmt.Printf("Searching page %d", page)
req, err = http.NewRequest("GET", fmt.Sprintf("%s/groups?pageNumber=%s", c.ApiUrl, strconv.Itoa(page)), nil)
if err != nil {
return nil, err
}
body, err = c.doRequest(req)
if err != nil {
return nil, err
}
groupListResponse = GroupListResponse{}
err = json.Unmarshal(body, &groupListResponse)
if err != nil {
return nil, err
}
// check if we found the group in this page
for i, group := range groupListResponse.GroupsResponse.Groups {
if group.ID == groupID {
return &groupListResponse.GroupsResponse.Groups[i], nil
}
}
}
return nil, fmt.Errorf("Did not find group ID %s", groupID)
}
func (c *Client) CreateGroup(name, minimumSiteRole string) (*Group, error) {
newGroup := Group{
Name: name,
MinimumSiteRole: minimumSiteRole,
}
groupRequest := GroupRequest{
Group: newGroup,
}
newGroupJson, err := json.Marshal(groupRequest)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/groups", c.ApiUrl), strings.NewReader(string(newGroupJson)))
if err != nil {
return nil, err
}
body, err := c.doRequest(req)
if err != nil {
return nil, err
}
groupResponse := GroupResponse{}
err = json.Unmarshal(body, &groupResponse)
if err != nil {
return nil, err
}
return &groupResponse.Group, nil
}
func (c *Client) UpdateGroup(groupID, name, minimumSiteRole string) (*Group, error) {
group := Group{
Name: name,
MinimumSiteRole: minimumSiteRole,
}
groupRequest := GroupRequest{
Group: group,
}
updateGroupJson, err := json.Marshal(groupRequest)
if err != nil {
return nil, err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("%s/groups/%s", c.ApiUrl, groupID), strings.NewReader(string(updateGroupJson)))
if err != nil {
return nil, err
}
body, err := c.doRequest(req)
if err != nil {
return nil, err
}
groupResponse := GroupResponse{}
err = json.Unmarshal(body, &groupResponse)
if err != nil {
return nil, err
}
return &groupResponse.Group, nil
}
func (c *Client) DeleteGroup(groupID string) error {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/groups/%s", c.ApiUrl, groupID), nil)
if err != nil {
return err
}
_, err = c.doRequest(req)
if err != nil {
return err
}
return nil
}