-
Notifications
You must be signed in to change notification settings - Fork 5
/
jamfproapi_api_integrations.go
203 lines (159 loc) · 6.32 KB
/
jamfproapi_api_integrations.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
// jamfproapi_api_integrations.go
// Jamf Pro Api - API Integrations
// api reference: https://developer.jamf.com/jamf-pro/reference/getallapiintegrations
// Jamf Pro API requires the structs to support a JSON data structure.
package jamfpro
import (
"fmt"
"github.com/mitchellh/mapstructure"
)
const uriApiIntegrations = "/api/v1/api-integrations"
// List
// ResponseApiIntegrations represents the structure of the response for fetching API integrations
type ResponseApiIntegrationsList struct {
TotalCount int `json:"totalCount"`
Results []ResourceApiIntegration `json:"results"`
}
// Resource
// Integration represents the details of an individual API integration
type ResourceApiIntegration struct {
ID int `json:"id,omitempty"`
AuthorizationScopes []string `json:"authorizationScopes,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Enabled bool `json:"enabled,omitempty"`
AccessTokenLifetimeSeconds int `json:"accessTokenLifetimeSeconds,omitempty"`
AppType string `json:"appType,omitempty"`
ClientID string `json:"clientId,omitempty"`
}
// Integration represents the details of Api client credentials
type ResourceClientCredentials struct {
ClientID string `json:"clientId"`
ClientSecret string `json:"clientSecret"`
}
// CRUD
// GetApiIntegrations fetches all API integrations
func (c *Client) GetApiIntegrations(sort_filter string) (*ResponseApiIntegrationsList, error) {
endpoint := uriApiIntegrations
resp, err := c.DoPaginatedGet(endpoint, standardPageSize, 0, sort_filter)
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "api integrations", err)
}
var OutStruct ResponseApiIntegrationsList
OutStruct.TotalCount = resp.Size
for _, value := range resp.Results {
var newObj ResourceApiIntegration
err := mapstructure.Decode(value, &newObj)
if err != nil {
return nil, fmt.Errorf(errMsgFailedMapstruct, "api integrations", err)
}
OutStruct.Results = append(OutStruct.Results, newObj)
}
return &OutStruct, nil
}
// GetApiIntegrationByID fetches an API integration by its ID
func (c *Client) GetApiIntegrationByID(id int) (*ResourceApiIntegration, error) {
endpoint := fmt.Sprintf("%s/%d", uriApiIntegrations, id)
var integration ResourceApiIntegration
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &integration)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "api integration", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &integration, nil
}
// GetApiIntegrationNameByID fetches an API integration by its display name and then retrieves its details using its ID
func (c *Client) GetApiIntegrationByName(name string) (*ResourceApiIntegration, error) {
integrations, err := c.GetApiIntegrations("")
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "api integration", err)
}
for _, integration := range integrations.Results {
if integration.DisplayName == name {
return &integration, nil
}
}
return nil, fmt.Errorf(errMsgFailedGetByName, "api integration", name, errMsgNoName)
}
// CreateApiIntegration creates a new API integration
func (c *Client) CreateApiIntegration(integration *ResourceApiIntegration) (*ResourceApiIntegration, error) {
endpoint := uriApiIntegrations
var response ResourceApiIntegration
resp, err := c.HTTP.DoRequest("POST", endpoint, integration, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedCreate, "api integration", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// UpdateApiIntegrationByID updates an API integration by its ID
func (c *Client) UpdateApiIntegrationByID(id int, integrationUpdate *ResourceApiIntegration) (*ResourceApiIntegration, error) {
// Construct the URL with the provided ID
endpoint := fmt.Sprintf(uriApiIntegrations+"/%d", id)
var updatedIntegration ResourceApiIntegration
resp, err := c.HTTP.DoRequest("PUT", endpoint, integrationUpdate, &updatedIntegration)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByID, "api integration", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedIntegration, nil
}
// UpdateApiIntegrationByName updates an API integration based on its display name
func (c *Client) UpdateApiIntegrationByName(name string, integrationUpdate *ResourceApiIntegration) (*ResourceApiIntegration, error) {
target, err := c.GetApiIntegrationByName(name)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByName, "api integration", name, err)
}
target_id := target.ID
resp, err := c.UpdateApiIntegrationByID(target_id, integrationUpdate)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByName, "api integration", name, err)
}
return resp, nil
}
// DeleteApiIntegrationByID deletes an API integration by its ID
func (c *Client) DeleteApiIntegrationByID(id int) error {
// Construct the URL with the provided ID
endpoint := fmt.Sprintf(uriApiIntegrations+"/%d", id)
// Perform the DELETE request
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByID, "api integration", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// DeleteApiIntegrationByName deletes an API integration by its display name
func (c *Client) DeleteApiIntegrationByName(name string) error {
target, err := c.GetApiIntegrationByName(name)
if err != nil {
return fmt.Errorf(errMsgFailedGetByName, "api integration", name, err)
}
target_id := target.ID
err = c.DeleteApiIntegrationByID(target_id)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByName, "api integration", name, err)
}
return nil
}
// Client Credentials
// RefreshClientCredentialsByApiRoleID creates new client credentials for an API integration by its ID
func (c *Client) RefreshClientCredentialsByApiRoleID(id string) (*ResourceClientCredentials, error) {
endpoint := fmt.Sprintf(uriApiIntegrations+"/%s/client-credentials", id)
var response ResourceClientCredentials
resp, err := c.HTTP.DoRequest("POST", endpoint, nil, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedRefreshClientCreds, id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}