forked from akamai/AkamaiOPEN-edgegrid-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.go
334 lines (281 loc) · 8.62 KB
/
versions.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package papi
import (
"errors"
"fmt"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
"time"
)
// Versions contains a collection of Property Versions
type Versions struct {
client.Resource
PropertyID string `json:"propertyId"`
PropertyName string `json:"propertyName"`
AccountID string `json:"accountId"`
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
Versions struct {
Items []*Version `json:"items"`
} `json:"versions"`
RuleFormat string `json:"ruleFormat,omitempty"`
}
// NewVersions creates a new Versions
func NewVersions() *Versions {
version := &Versions{}
version.Init()
return version
}
// PostUnmarshalJSON is called after JSON unmarshaling into EdgeHostnames
//
// See: jsonhooks-v1/jsonhooks.Unmarshal()
func (versions *Versions) PostUnmarshalJSON() error {
versions.Init()
for key := range versions.Versions.Items {
versions.Versions.Items[key].parent = versions
}
versions.Complete <- true
return nil
}
// AddVersion adds or replaces a version within the collection
func (versions *Versions) AddVersion(version *Version) {
if version.PropertyVersion != 0 {
for key, v := range versions.Versions.Items {
if v.PropertyVersion == version.PropertyVersion {
versions.Versions.Items[key] = version
return
}
}
}
versions.Versions.Items = append(versions.Versions.Items, version)
}
// GetVersions retrieves all versions for a a given property
//
// See: Property.GetVersions()
// API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listversions
// Endpoint: GET /papi/v1/properties/{propertyId}/versions/{?contractId,groupId}
func (versions *Versions) GetVersions(property *Property) error {
if property == nil {
return errors.New("You must provide a property")
}
req, err := client.NewRequest(
Config,
"GET",
fmt.Sprintf(
"/papi/v1/properties/%s/versions",
property.PropertyID,
),
nil,
)
if err != nil {
return err
}
res, err := client.Do(Config, req)
if err != nil {
return err
}
if err = client.BodyJSON(res, versions); err != nil {
return err
}
return nil
}
// GetLatestVersion retrieves the latest Version for a property
//
// See: Property.GetLatestVersion()
// API Docs: https://developer.akamai.com/api/luna/papi/resources.html#getthelatestversion
// Endpoint: GET /papi/v1/properties/{propertyId}/versions/latest{?contractId,groupId,activatedOn}
func (versions *Versions) GetLatestVersion(activatedOn NetworkValue) (*Version, error) {
if activatedOn != "" {
activatedOn = "?activatedOn=" + activatedOn
}
req, err := client.NewRequest(
Config,
"GET",
fmt.Sprintf(
"/papi/v1/properties/%s/versions/latest%s",
versions.PropertyID,
activatedOn,
),
nil,
)
if err != nil {
return nil, err
}
res, err := client.Do(Config, req)
if err != nil {
return nil, err
}
if client.IsError(res) {
return nil, client.NewAPIError(res)
}
newVersions := NewVersions()
if err := client.BodyJSON(res, newVersions); err != nil {
return nil, err
}
return newVersions.Versions.Items[0], nil
}
// NewVersion creates a new version associated with the Versions collection
func (versions *Versions) NewVersion(createFromVersion *Version, useEtagStrict bool) *Version {
if createFromVersion == nil {
var err error
createFromVersion, err = versions.GetLatestVersion("")
if err != nil {
return nil
}
}
version := NewVersion(versions)
version.CreateFromVersion = createFromVersion.PropertyVersion
versions.Versions.Items = append(versions.Versions.Items, version)
if useEtagStrict {
version.CreateFromVersionEtag = createFromVersion.Etag
}
return version
}
// Version represents a Property Version
type Version struct {
client.Resource
parent *Versions
PropertyVersion int `json:"propertyVersion,omitempty"`
UpdatedByUser string `json:"updatedByUser,omitempty"`
UpdatedDate time.Time `json:"updatedDate,omitempty"`
ProductionStatus StatusValue `json:"productionStatus,omitempty"`
StagingStatus StatusValue `json:"stagingStatus,omitempty"`
Etag string `json:"etag,omitempty"`
ProductID string `json:"productId,omitempty"`
Note string `json:"note,omitempty"`
CreateFromVersion int `json:"createFromVersion,omitempty"`
CreateFromVersionEtag string `json:"createFromVersionEtag,omitempty"`
RuleFormat string `json:"ruleFormat,omitempty"`
}
// NewVersion creates a new Version
func NewVersion(parent *Versions) *Version {
version := &Version{parent: parent}
version.Init()
return version
}
// GetVersion populates a Version
//
// Api Docs: https://developer.akamai.com/api/luna/papi/resources.html#getaversion
// Endpoint: /papi/v1/properties/{propertyId}/versions/{propertyVersion}{?contractId,groupId}
func (version *Version) GetVersion(property *Property, getVersion int) error {
if getVersion == 0 {
getVersion = property.LatestVersion
}
req, err := client.NewRequest(
Config,
"GET",
fmt.Sprintf(
"/papi/v1/properties/%s/versions/%d",
property.PropertyID,
getVersion,
),
nil,
)
if err != nil {
return err
}
res, err := client.Do(Config, req)
if err != nil {
return err
}
if client.IsError(res) {
return client.NewAPIError(res)
}
newVersions := NewVersions()
if err := client.BodyJSON(res, newVersions); err != nil {
return err
}
version.PropertyVersion = newVersions.Versions.Items[0].PropertyVersion
version.UpdatedByUser = newVersions.Versions.Items[0].UpdatedByUser
version.UpdatedDate = newVersions.Versions.Items[0].UpdatedDate
version.ProductionStatus = newVersions.Versions.Items[0].ProductionStatus
version.StagingStatus = newVersions.Versions.Items[0].StagingStatus
version.Etag = newVersions.Versions.Items[0].Etag
version.ProductID = newVersions.Versions.Items[0].ProductID
version.Note = newVersions.Versions.Items[0].Note
version.CreateFromVersion = newVersions.Versions.Items[0].CreateFromVersion
version.CreateFromVersionEtag = newVersions.Versions.Items[0].CreateFromVersionEtag
return nil
}
// HasBeenActivated determines if a given version has been activated, optionally on a specific network
func (version *Version) HasBeenActivated(activatedOn NetworkValue) (bool, error) {
properties := NewProperties()
property := NewProperty(properties)
property.PropertyID = version.parent.PropertyID
property.Group = NewGroup(NewGroups())
property.Group.GroupID = version.parent.GroupID
property.Contract = NewContract(NewContracts())
property.Contract.ContractID = version.parent.ContractID
activations, err := property.GetActivations()
if err != nil {
return false, err
}
for _, activation := range activations.Activations.Items {
if activation.PropertyVersion == version.PropertyVersion && (activatedOn == "" || activation.Network == activatedOn) {
return true, nil
}
}
return false, nil
}
// Save creates a new version
//
// API Docs: https://developer.akamai.com/api/luna/papi/resources.html#createanewversion
// Endpoint: POST /papi/v1/properties/{propertyId}/versions/{?contractId,groupId}
func (version *Version) Save() error {
if version.PropertyVersion != 0 {
return fmt.Errorf("version (%d) already exists", version.PropertyVersion)
}
req, err := client.NewJSONRequest(
Config,
"POST",
fmt.Sprintf(
"/papi/v1/properties/%s/versions",
version.parent.PropertyID,
),
version,
)
if err != nil {
return err
}
res, err := client.Do(Config, req)
if err != nil {
return err
}
if client.IsError(res) {
return client.NewAPIError(res)
}
var location client.JSONBody
if err = client.BodyJSON(res, &location); err != nil {
return err
}
req, err = client.NewRequest(
Config,
"GET",
location["versionLink"].(string),
nil,
)
if err != nil {
return err
}
res, err = client.Do(Config, req)
if err != nil {
return err
}
if client.IsError(res) {
return client.NewAPIError(res)
}
versions := NewVersions()
if err = client.BodyJSON(res, versions); err != nil {
return err
}
version.PropertyVersion = versions.Versions.Items[0].PropertyVersion
version.UpdatedByUser = versions.Versions.Items[0].UpdatedByUser
version.UpdatedDate = versions.Versions.Items[0].UpdatedDate
version.ProductionStatus = versions.Versions.Items[0].ProductionStatus
version.StagingStatus = versions.Versions.Items[0].StagingStatus
version.Etag = versions.Versions.Items[0].Etag
version.ProductID = versions.Versions.Items[0].ProductID
version.Note = versions.Versions.Items[0].Note
version.CreateFromVersion = versions.Versions.Items[0].CreateFromVersion
version.CreateFromVersionEtag = versions.Versions.Items[0].CreateFromVersionEtag
version.parent.AddVersion(version)
return nil
}