-
Notifications
You must be signed in to change notification settings - Fork 5
/
classicapi_vpp_accounts.go
155 lines (121 loc) · 4.19 KB
/
classicapi_vpp_accounts.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
// classicapi_vpp_accounts.go
// Jamf Pro Classic Api - VPP Accounts
// api reference: https://developer.jamf.com/jamf-pro/reference/vppaccounts
// Jamf Pro Classic Api requires the structs to support an XML data structure.
/*
Shared Resources in this Endpoint:
- SharedResourceSite
*/
package jamfpro
import (
"encoding/xml"
"fmt"
)
const uriVPPAccounts = "/JSSResource/vppaccounts"
// List
// Structs for VPP Accounts Response
type ResponseVPPAccountsList struct {
Size int `xml:"size"`
Accounts []VPPAccountsListItem `xml:"vpp_account"`
}
type VPPAccountsListItem struct {
ID int `xml:"id"`
Name string `xml:"name"`
}
// Resource
// Struct for individual VPP Account
type ResourceVPPAccount struct {
ID int `xml:"id"`
Name string `xml:"name"`
Contact string `xml:"contact"`
ServiceToken string `xml:"service_token"`
AccountName string `xml:"account_name"`
ExpirationDate string `xml:"expiration_date"`
Country string `xml:"country"`
AppleID string `xml:"apple_id"`
Site SharedResourceSite `xml:"site"`
PopulateCatalogFromVPPContent bool `xml:"populate_catalog_from_vpp_content"`
NotifyDisassociation bool `xml:"notify_disassociation"`
AutoRegisterManagedUsers bool `xml:"auto_register_managed_users"`
}
// CRUD
// GetVPPAccounts retrieves a list of all VPP accounts.
func (c *Client) GetVPPAccounts() (*ResponseVPPAccountsList, error) {
endpoint := uriVPPAccounts
var response ResponseVPPAccountsList
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGet, "vpp accounts", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// GetVPPAccountByID retrieves a specific VPP account by its ID.
func (c *Client) GetVPPAccountByID(id int) (*ResourceVPPAccount, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriVPPAccounts, id)
var response ResourceVPPAccount
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "vpp account", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// CreateVPPAccount creates a new VPP account.
func (c *Client) CreateVPPAccount(account *ResourceVPPAccount) (*ResourceVPPAccount, error) {
endpoint := fmt.Sprintf("%s/id/0", uriVPPAccounts)
if account.Site.ID == 0 && account.Site.Name == "" {
account.Site.ID = -1
account.Site.Name = "none"
}
requestBody := struct {
XMLName xml.Name `xml:"vpp_account"`
*ResourceVPPAccount
}{
ResourceVPPAccount: account,
}
var response ResourceVPPAccount
resp, err := c.HTTP.DoRequest("POST", endpoint, &requestBody, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedCreate, "vpp account", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// UpdateVPPAccount updates an existing VPP account.
func (c *Client) UpdateVPPAccountByID(id int, account *ResourceVPPAccount) (*ResourceVPPAccount, error) {
endpoint := fmt.Sprintf("%s/id/%d", uriVPPAccounts, id)
requestBody := struct {
XMLName xml.Name `xml:"vpp_account"`
*ResourceVPPAccount
}{
ResourceVPPAccount: account,
}
var response ResourceVPPAccount
resp, err := c.HTTP.DoRequest("PUT", endpoint, &requestBody, &response)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByID, "vpp account", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// DeleteVPPAccountByID deletes a specific VPP account by its ID.
func (c *Client) DeleteVPPAccountByID(id int) error {
endpoint := fmt.Sprintf("%s/id/%d", uriVPPAccounts, id)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByID, "vpp account", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}