-
Notifications
You must be signed in to change notification settings - Fork 5
/
jamfproapi_volume_purchasing_subscriptions.go
176 lines (143 loc) · 6.6 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
// 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"
"strconv"
)
const uriVolumePurchasingSubscriptions = "/api/v1/volume-purchasing-subscriptions"
type ResponseVolumePurchasingSubscriptionsList struct {
TotalCount *int `json:"totalCount,omitempty"`
Results []VolumePurchasingSubscription `json:"results,omitempty"`
}
type VolumePurchasingSubscription 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 []struct {
AccountId string `json:"accountId,omitempty"`
Frequency string `json:"frequency,omitempty"`
} `json:"internalRecipients,omitempty"`
ExternalRecipients []struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
} `json:"externalRecipients,omitempty"`
SiteId string `json:"siteId,omitempty"`
}
// GetVolumePurchasingSubscriptions retrieves all volume purchasing subscriptions
func (c *Client) GetVolumePurchasingSubscriptions() (*ResponseVolumePurchasingSubscriptionsList, error) {
var subscriptionsList ResponseVolumePurchasingSubscriptionsList
resp, err := c.HTTP.DoRequest("GET", uriVolumePurchasingSubscriptions, nil, &subscriptionsList)
if err != nil {
return nil, fmt.Errorf("failed to fetch volume purchasing subscriptions: %v", err)
}
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
return &subscriptionsList, nil
}
// GetVolumePurchasingSubscriptionByID retrieves a single volume purchasing subscription by its ID
func (c *Client) GetVolumePurchasingSubscriptionByID(id string) (*VolumePurchasingSubscription, error) {
// Construct the URL with the provided ID
endpoint := fmt.Sprintf("%s/%s", uriVolumePurchasingSubscriptions, id)
var subscription VolumePurchasingSubscription
resp, err := c.HTTP.DoRequest("GET", endpoint, nil, &subscription)
if err != nil {
return nil, fmt.Errorf("failed to fetch volume purchasing subscription with ID %s: %v", 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) GetVolumePurchasingSubscriptionByNameByID(name string) (*VolumePurchasingSubscription, error) {
subscriptionsList, err := c.GetVolumePurchasingSubscriptions()
if err != nil {
return nil, fmt.Errorf("failed to fetch all volume purchasing subscriptions: %v", err)
}
// Search for the subscription with the given name
for _, subscription := range subscriptionsList.Results {
if subscription.Name == name {
// Assuming the ID in the struct is a string and needs to be converted to int
subscriptionID, convErr := strconv.Atoi(subscription.Id)
if convErr != nil {
return nil, fmt.Errorf("failed to convert subscription ID '%s' to integer: %v", subscription.Id, convErr)
}
return c.GetVolumePurchasingSubscriptionByID(strconv.Itoa(subscriptionID))
}
}
return nil, fmt.Errorf("no volume purchasing subscription found with the name '%s'", name)
}
// CreateVolumePurchasingSubscription creates a new volume purchasing subscription
func (c *Client) CreateVolumePurchasingSubscription(subscription *VolumePurchasingSubscription) (*VolumePurchasingSubscription, error) {
endpoint := uriVolumePurchasingSubscriptions
// Default the SiteId to "-1" if not provided
if subscription.SiteId == "" {
subscription.SiteId = "-1"
}
var createdSubscription VolumePurchasingSubscription
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 *VolumePurchasingSubscription) (*VolumePurchasingSubscription, error) {
endpoint := fmt.Sprintf("%s/%s", uriVolumePurchasingSubscriptions, id)
var updatedSubscription VolumePurchasingSubscription
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) UpdateVolumePurchasingSubscriptionByNameByID(name string, updateData *VolumePurchasingSubscription) (*VolumePurchasingSubscription, error) {
subscriptionsList, err := c.GetVolumePurchasingSubscriptions()
if err != nil {
return nil, fmt.Errorf("failed to fetch all volume purchasing subscriptions: %v", err)
}
for _, subscription := range subscriptionsList.Results {
if subscription.Name == name {
return c.UpdateVolumePurchasingSubscriptionByID(subscription.Id, updateData)
}
}
return nil, fmt.Errorf("no volume purchasing subscription found with the name '%s'", name)
}
// 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 {
subscriptionsList, err := c.GetVolumePurchasingSubscriptions()
if err != nil {
return fmt.Errorf("failed to fetch all volume purchasing subscriptions: %v", err)
}
for _, subscription := range subscriptionsList.Results {
if subscription.Name == name {
return c.DeleteVolumePurchasingSubscriptionByID(subscription.Id)
}
}
return fmt.Errorf("no volume purchasing subscription found with the name '%s'", name)
}