-
Notifications
You must be signed in to change notification settings - Fork 5
/
jamfproapi_volume_purchasing_locations.go
220 lines (178 loc) · 8.1 KB
/
jamfproapi_volume_purchasing_locations.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// jamfproapi_volume_purchasing_locations.go
// Jamf Pro Api - Volume Purchasing Locations
// api reference: https://developer.jamf.com/jamf-pro/reference/get_v1-volume-purchasing-locations
// Jamf Pro API requires the structs to support an JSON data structure.
package jamfpro
import (
"fmt"
"net/url"
"strconv"
"github.com/mitchellh/mapstructure"
)
const uriVolumePurchasingLocations = "/api/v1/volume-purchasing-locations"
// List
// ResponseVolumePurchasingList represents the paginated response for volume purchasing locations.
type ResponseVolumePurchasingList struct {
TotalCount int `json:"totalCount"`
Results []ResourceVolumePurchasingLocation `json:"results"`
}
type ResponseVolumePurchasingContentList struct {
TotalCount int `json:"totalCount"`
Results []VolumePurchasingSubsetContent `json:"results"`
}
// VolumePurchasingLocationCreateResponse represents the response for creating a volume purchasing location.
type ResponseVolumePurchasingLocationCreate struct {
ID string `json:"id"`
Href string `json:"href"`
}
// ResponseVolumePurchasingLocation represents the response structure for a single volume purchasing location.
type ResourceVolumePurchasingLocation struct {
VolumePurchasingLocationSubsetBody // I don't think this works. See exmaples/volume_purchase_locations/CreateVolumePurchasingLocation.go
Content []VolumePurchasingSubsetContent `json:"content"`
}
// Subsets
// VolumePurchasingLocation represents an individual volume purchasing location.
type VolumePurchasingLocationSubsetBody struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
AppleID string `json:"appleId,omitempty"`
OrganizationName string `json:"organizationName,omitempty"`
TokenExpiration string `json:"tokenExpiration,omitempty"`
CountryCode string `json:"countryCode,omitempty"`
LocationName string `json:"locationName,omitempty"`
ClientContextMismatch bool `json:"clientContextMismatch,omitempty"`
AutomaticallyPopulatePurchasedContent bool `json:"automaticallyPopulatePurchasedContent,omitempty"`
SendNotificationWhenNoLongerAssigned bool `json:"sendNotificationWhenNoLongerAssigned,omitempty"`
AutoRegisterManagedUsers bool `json:"autoRegisterManagedUsers,omitempty"`
SiteID string `json:"siteId,omitempty"`
LastSyncTime string `json:"lastSyncTime,omitempty"`
TotalPurchasedLicenses int `json:"totalPurchasedLicenses,omitempty"`
TotalUsedLicenses int `json:"totalUsedLicenses,omitempty"`
ServiceToken string `json:"serviceToken,omitempty"`
}
// VolumePurchasingContent represents the content associated with a volume purchasing location.
type VolumePurchasingSubsetContent struct {
Name string `json:"name"`
LicenseCountTotal int `json:"licenseCountTotal"`
LicenseCountInUse int `json:"licenseCountInUse"`
LicenseCountReported int `json:"licenseCountReported"`
IconURL string `json:"iconUrl"`
DeviceTypes []string `json:"deviceTypes"`
ContentType string `json:"contentType"`
PricingParam string `json:"pricingParam"`
AdamId string `json:"adamId"`
}
// CRUD
// VPP Locations
// GetVolumePurchaseLocations retrieves all volume purchasing locations with optional sorting and filtering.
func (c *Client) GetVolumePurchaseLocations(sort_filter string) (*ResponseVolumePurchasingList, error) {
resp, err := c.DoPaginatedGet(uriVolumePurchasingLocations, standardPageSize, startingPageNumber, sort_filter)
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "vpp locations", err)
}
var out ResponseVolumePurchasingList
out.TotalCount = resp.Size
for _, value := range resp.Results {
var newObj ResourceVolumePurchasingLocation
err := mapstructure.Decode(value, &newObj)
if err != nil {
return nil, fmt.Errorf(errMsgFailedMapstruct, "vpp location", err)
}
out.Results = append(out.Results, newObj)
}
return &out, nil
}
// GetVolumePurchasingLocationByID retrieves a specific volume purchasing location by its ID.
func (c *Client) GetVolumePurchasingLocationByID(id string) (*ResourceVolumePurchasingLocation, error) {
endpoint := fmt.Sprintf("%s/%s", uriVolumePurchasingLocations, id)
var responseLocation ResourceVolumePurchasingLocation
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &responseLocation)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "vpp locations", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &responseLocation, nil
}
// CreateVolumePurchasingLocation creates a new volume purchasing location.
func (c *Client) CreateVolumePurchasingLocation(request *ResourceVolumePurchasingLocation) (*ResponseVolumePurchasingLocationCreate, error) {
endpoint := uriVolumePurchasingLocations
var response ResponseVolumePurchasingLocationCreate
resp, err := c.HTTP.DoRequest("POST", endpoint, request, &response)
if err != nil {
return nil, fmt.Errorf("failed to create volume purchasing location: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &response, nil
}
// UpdateVolumePurchasingLocationByID updates a specific volume purchasing location by its ID.
func (c *Client) UpdateVolumePurchasingLocationByID(id string) (*ResourceVolumePurchasingLocation, error) {
endpoint := fmt.Sprintf("%s/%s", uriVolumePurchasingLocations, id)
var responseLocation ResourceVolumePurchasingLocation
resp, err := c.HTTP.DoRequest("PATCH", endpoint, nil, &responseLocation)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "vpp locations", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &responseLocation, nil
}
// DeleteVolumePurchasingLocationByID deletes a specific volume purchasing location by its ID.
func (c *Client) DeleteVolumePurchasingLocationByID(id string) error {
endpoint := uriVolumePurchasingLocations
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByID, "vpp location", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// QUERY do we need the stuff below here?
// GetVolumePurchasingContentForLocationByID retrieves the content for a specific volume purchasing location by its ID.
func (c *Client) GetVolumePurchasingContentForLocationByID(id string, sort []string, filter string) (*ResponseVolumePurchasingContentList, error) {
var allContent []VolumePurchasingSubsetContent
page := 1
for {
params := url.Values{
"page": []string{strconv.Itoa(page)},
"page-size": []string{"100"}, // Adjust the page size as per requirement
}
// Append sort parameters
for _, s := range sort {
params.Add("sort", s)
}
// Append filter parameter if provided
if filter != "" {
params.Add("filter", filter)
}
endpointWithParams := fmt.Sprintf("%s/%s/content?%s", uriVolumePurchasingLocations, id, params.Encode())
// Fetch the content for the current page
var responseContent ResponseVolumePurchasingContentList
resp, err := c.HTTP.DoRequest("GET", endpointWithParams, nil, &responseContent)
if err != nil {
return nil, fmt.Errorf("failed to fetch volume purchasing content for location ID %s: %v", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
// Add the fetched content to the total list
allContent = append(allContent, responseContent.Results...)
// Check if all content has been fetched
if len(allContent) >= responseContent.TotalCount {
break
}
// Increment page number for the next iteration
page++
}
// Return the combined list of all content for the given volume purchasing location
return &ResponseVolumePurchasingContentList{
TotalCount: len(allContent),
Results: allContent,
}, nil
}