-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplications.go
163 lines (142 loc) · 4.76 KB
/
applications.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
package jumpcloud
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strconv"
)
// GetAllApplications returns all applications
func (c *Client) GetAllApplications() (allApplications AllApps, err error) {
// Prepare request
totalApps := 0
c.HostURL.Path = "/api/v2/applications/"
c.HostURL.RawQuery = "limit=100&skip=0"
req, err := http.NewRequest(http.MethodGet, c.HostURL.String(), nil)
req.Header = c.Headers
// Send request
response, _ := c.HTTPClient.Do(req)
defer response.Body.Close()
// Parse response
// totalApps is the total number of records
totalApps, err = strconv.Atoi(response.Header.Get("x-total-count"))
body, _ := io.ReadAll(response.Body)
err = json.Unmarshal(body, &allApplications)
if err != nil {
return nil, err
}
// While all groups is less than the total number of records
if len(allApplications) < totalApps {
var tempData AllApps // Create a temp slice to hold the data
// Get step and skip values
stepValue, err := strconv.Atoi(c.HostURL.Query().Get("limit"))
currentSkip, err := strconv.Atoi(c.HostURL.Query().Get("skip"))
if err != nil {
return nil, err
}
// Calculate new step value and add it to params
newStep := strconv.Itoa(currentSkip + stepValue)
c.HostURL.RawQuery = "limit=100&skip=" + newStep
req.URL = c.HostURL // Update URL to include new params
// Send Request
response, err := c.HTTPClient.Do(req)
// Parse response, add results to tempData
body, err := io.ReadAll(response.Body)
err = json.Unmarshal(body, &tempData)
// Add tempData to allApplications
allApplications = append(allApplications, tempData...)
if err != nil {
return nil, err
}
}
return allApplications, err
}
// GetApplication returns a single application
func (c *Client) GetApplication(appId string) (application App, err error) {
// Prepare request
c.HostURL.Path = "/api/v2/applications/" + appId
req, err := http.NewRequest(http.MethodGet, c.HostURL.String(), nil)
req.Header = c.Headers
// Send request
response, _ := c.HTTPClient.Do(req)
// Parse response
body, _ := io.ReadAll(response.Body)
err = json.Unmarshal(body, &application)
return application, err
}
// GetAppAssociations returns a single application
// groupType can be either "user_group" or "user"
func (c *Client) GetAppAssociations(appId string, groupType string) (appAssociations AppAssociations, err error) {
// Prepare request
totalAssociations := 0
c.HostURL.Path = "/api/v2/applications/" + appId + "/associations"
c.HostURL.RawQuery = "targets=" + groupType + "&limit=100&skip=0"
req, err := http.NewRequest(http.MethodGet, c.HostURL.String(), nil)
req.Header = c.Headers
// Send request
response, _ := c.HTTPClient.Do(req)
body, _ := io.ReadAll(response.Body)
err = json.Unmarshal(body, &appAssociations)
totalAssociations, err = strconv.Atoi(response.Header.Get("x-total-count"))
// While all groups is less than the total number of records...
if len(appAssociations) < totalAssociations {
var tempData AppAssociations // Create a temp slice to hold the data
stepValue, err := strconv.Atoi(c.HostURL.Query().Get("limit"))
currentSkip, err := strconv.Atoi(c.HostURL.Query().Get("skip"))
if err != nil {
return nil, err
}
newStep := strconv.Itoa(currentSkip + stepValue)
c.HostURL.RawQuery = "targets=user_group&limit=100&skip=" + newStep
req.URL = c.HostURL
response, err := c.HTTPClient.Do(req)
body, err := io.ReadAll(response.Body)
err = json.Unmarshal(body, &tempData)
appAssociations = append(appAssociations, tempData...)
if err != nil {
return nil, err
}
}
return appAssociations, err
}
// AssociateGroupWithApp associates a group with an application
func (c *Client) AssociateGroupWithApp(appId string, groupId string) (err error) {
// Prepare request
c.HostURL.Path = "/api/v2/applications/" + appId + "/associations"
j, _ := json.Marshal(AppAssociationModifier{
ID: groupId,
OP: "add",
Type: "user_group",
})
bodyReader := bytes.NewReader(j)
req, err := http.NewRequest(http.MethodPost, c.HostURL.String(), bodyReader)
req.Header = c.Headers
// Send request
response, err := c.HTTPClient.Do(req)
if response.StatusCode == 204 {
// 204 = OK
return nil
}
return err
}
// RemoveGroupFromApp removes a group from an application
func (c *Client) RemoveGroupFromApp(appId string, groupId string) (err error) {
// Prepare request
c.HostURL.Path = "/api/v2/applications/" + appId + "/associations"
j, _ := json.Marshal(map[string]string{
"id": groupId,
"op": "remove",
"type": "user_group",
})
bodyReader := bytes.NewReader(j)
req, err := http.NewRequest(http.MethodPost, c.HostURL.String(), bodyReader)
req.Header = c.Headers
// Send request
response, err := c.HTTPClient.Do(req)
defer response.Body.Close()
if response.StatusCode == 204 {
// 204 -- OK
return nil
}
return err
}