-
Notifications
You must be signed in to change notification settings - Fork 5
/
jamfproapi_volume_purchasing_subscriptions.go
196 lines (153 loc) · 7.02 KB
/
jamfproapi_volume_purchasing_subscriptions.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
// jamfproapi_volume_purchasing_subscriptions.go
// Jamf Pro Api - Volume Purchasing Subscriptions
// api reference: https://developer.jamf.com/jamf-pro/reference/get_v1-volume-purchasing-subscriptions
// Jamf Pro API requires the structs to support an JSON data structure.
package jamfpro
import (
"fmt"
"github.com/mitchellh/mapstructure"
)
const uriVolumePurchasingSubscriptions = "/api/v1/volume-purchasing-subscriptions"
// List
type ResponseVolumePurchasingSubscriptionsList struct {
TotalCount *int `json:"totalCount,omitempty"`
Results []ResourceVolumePurchasingSubscription `json:"results,omitempty"`
}
// Resource
type ResourceVolumePurchasingSubscription struct {
Id string `json:"id,omitempty"`
Name string `json:"name"`
Enabled bool `json:"enabled,omitempty"`
Triggers []string `json:"triggers,omitempty"`
LocationIds []string `json:"locationIds,omitempty"`
InternalRecipients []VolumePurchasingSubscriptionSubsetInternalRecipients `json:"internalRecipients,omitempty"`
ExternalRecipients []VolumePurchasingSubscriptionSubsetExternalRecipients `json:"externalRecipients,omitempty"`
SiteId string `json:"siteId,omitempty"`
}
// Subsets
type VolumePurchasingSubscriptionSubsetInternalRecipients struct {
AccountId string `json:"accountId,omitempty"`
Frequency string `json:"frequency,omitempty"`
}
type VolumePurchasingSubscriptionSubsetExternalRecipients struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
// CRUD
// GetVolumePurchasingSubscriptions retrieves all volume purchasing subscriptions
func (c *Client) GetVolumePurchasingSubscriptions(sort_filter string) (*ResponseVolumePurchasingSubscriptionsList, error) {
resp, err := c.DoPaginatedGet(
uriVolumePurchasingSubscriptions,
maxPageSize,
startingPageNumber,
sort_filter,
)
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "volume purchasing subscriptions", err)
}
var out ResponseVolumePurchasingSubscriptionsList
out.TotalCount = &resp.Size
for _, value := range resp.Results {
var newObj ResourceVolumePurchasingSubscription
err := mapstructure.Decode(value, &newObj)
if err != nil {
return nil, fmt.Errorf(errMsgFailedMapstruct, "volume purchasing subscription", err)
}
out.Results = append(out.Results, newObj)
}
return &out, nil
}
// GetVolumePurchasingSubscriptionByID retrieves a single volume purchasing subscription by its ID
func (c *Client) GetVolumePurchasingSubscriptionByID(id string) (*ResourceVolumePurchasingSubscription, error) {
endpoint := fmt.Sprintf("%s/%s", uriVolumePurchasingSubscriptions, id)
var subscription ResourceVolumePurchasingSubscription
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &subscription)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByID, "volume purchasing subscription", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &subscription, nil
}
// GetVolumePurchasingSubscriptionByNameByID fetches a volume purchasing subscription by its display name and retrieves its details using its ID.
func (c *Client) GetVolumePurchasingSubscriptionByName(name string) (*ResourceVolumePurchasingSubscription, error) {
vpSubcriptions, err := c.GetVolumePurchasingSubscriptions("")
if err != nil {
return nil, fmt.Errorf(errMsgFailedPaginatedGet, "volume purchasing subscriptions", err)
}
for _, value := range vpSubcriptions.Results {
if value.Name == name {
return &value, nil
}
}
return nil, fmt.Errorf(errMsgFailedGetByName, "volume purchasing subscription", name, errMsgNoName)
}
// CreateVolumePurchasingSubscription creates a new volume purchasing subscription
func (c *Client) CreateVolumePurchasingSubscription(subscription *ResourceVolumePurchasingSubscription) (*ResourceVolumePurchasingSubscription, error) {
endpoint := uriVolumePurchasingSubscriptions
// Default the SiteId to "-1" if not provided
if subscription.SiteId == "" {
subscription.SiteId = "-1"
}
var createdSubscription ResourceVolumePurchasingSubscription
resp, err := c.HTTP.DoRequest("POST", endpoint, subscription, &createdSubscription)
if err != nil {
return nil, fmt.Errorf("failed to create volume purchasing subscription: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &createdSubscription, nil
}
// UpdateVolumePurchasingSubscriptionByID updates a volume purchasing subscription by its ID
func (c *Client) UpdateVolumePurchasingSubscriptionByID(id string, subscription *ResourceVolumePurchasingSubscription) (*ResourceVolumePurchasingSubscription, error) {
endpoint := fmt.Sprintf("%s/%s", uriVolumePurchasingSubscriptions, id)
var updatedSubscription ResourceVolumePurchasingSubscription
resp, err := c.HTTP.DoRequest("PUT", endpoint, subscription, &updatedSubscription)
if err != nil {
return nil, fmt.Errorf("failed to update volume purchasing subscription with ID %s: %v", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &updatedSubscription, nil
}
// UpdateVolumePurchasingSubscriptionByNameByID updates a volume purchasing subscription by its display name
func (c *Client) UpdateVolumePurchasingSubscriptionByName(name string, updateData *ResourceVolumePurchasingSubscription) (*ResourceVolumePurchasingSubscription, error) {
target, err := c.GetVolumePurchasingSubscriptionByName(name)
if err != nil {
return nil, fmt.Errorf(errMsgFailedGetByName, "volume purchasing subscription", name, err)
}
target_id := target.Id
resp, err := c.UpdateVolumePurchasingSubscriptionByID(target_id, updateData)
if err != nil {
return nil, fmt.Errorf(errMsgFailedUpdateByName, "volume purchasing subscription", name, err)
}
return resp, nil
}
// DeleteVolumePurchasingSubscriptionByID deletes a volume purchasing subscription by its ID
func (c *Client) DeleteVolumePurchasingSubscriptionByID(id string) error {
endpoint := fmt.Sprintf("%s/%s", uriVolumePurchasingSubscriptions, id)
resp, err := c.HTTP.DoRequest("DELETE", endpoint, nil, nil)
if err != nil {
return fmt.Errorf("failed to delete volume purchasing subscription with ID %s: %v", id, err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return nil
}
// DeleteVolumePurchasingSubscriptionByName finds a subscription by name and deletes it by its ID
func (c *Client) DeleteVolumePurchasingSubscriptionByName(name string) error {
target, err := c.GetVolumePurchasingSubscriptionByName(name)
if err != nil {
return fmt.Errorf(errMsgFailedGetByName, "volume purchasing subscription", name, err)
}
target_id := target.Id
err = c.DeleteVolumePurchasingSubscriptionByID(target_id)
if err != nil {
return fmt.Errorf(errMsgFailedDeleteByName, "volume purchasing subscription", name, err)
}
return nil
}