Debian Codegraph @

fastly/version.go

raw symbols SCIP
1 package fastly
2
3 import (
4 "fmt"
5 "sort"
6 "time"
7 )
8
9 // Version represents a distinct configuration version.
10 type Version struct {
11 Number int `mapstructure:"number"`
12 Comment string `mapstructure:"comment"`
13 ServiceID string `mapstructure:"service_id"`
14 Active bool `mapstructure:"active"`
15 Locked bool `mapstructure:"locked"`
16 Deployed bool `mapstructure:"deployed"`
17 Staging bool `mapstructure:"staging"`
18 Testing bool `mapstructure:"testing"`
19 CreatedAt *time.Time `mapstructure:"created_at"`
20 UpdatedAt *time.Time `mapstructure:"updated_at"`
21 DeletedAt *time.Time `mapstructure:"deleted_at"`
22 }
23
24 // versionsByNumber is a sortable list of versions. This is used by the version
25 // `List()` function to sort the API responses.
26 type versionsByNumber []*Version
27
28 // Len, Swap, and Less implement the sortable interface.
29 func (s versionsByNumber) Len() int { return len(s) }
30 func (s versionsByNumber) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
31 func (s versionsByNumber) Less(i, j int) bool {
32 return s[i].Number < s[j].Number
33 }
34
35 // ListVersionsInput is the input to the ListVersions function.
36 type ListVersionsInput struct {
37 // Service is the ID of the service (required).
38 Service string
39 }
40
41 // ListVersions returns the full list of all versions of the given service.
42 func (c *Client) ListVersions(i *ListVersionsInput) ([]*Version, error) {
43 if i.Service == "" {
44 return nil, ErrMissingService
45 }
46
47 path := fmt.Sprintf("/service/%s/version", i.Service)
48 resp, err := c.Get(path, nil)
49 if err != nil {
50 return nil, err
51 }
52
53 var e []*Version
54 if err := decodeJSON(&e, resp.Body); err != nil {
55 return nil, err
56 }
57 sort.Sort(versionsByNumber(e))
58
59 return e, nil
60 }
61
62 // LatestVersionInput is the input to the LatestVersion function.
63 type LatestVersionInput struct {
64 // Service is the ID of the service (required).
65 Service string
66 }
67
68 // LatestVersion fetches the latest version. If there are no versions, this
69 // function will return nil (but not an error).
70 func (c *Client) LatestVersion(i *LatestVersionInput) (*Version, error) {
71 if i.Service == "" {
72 return nil, ErrMissingService
73 }
74
75 list, err := c.ListVersions(&ListVersionsInput{Service: i.Service})
76 if err != nil {
77 return nil, err
78 }
79 if len(list) < 1 {
80 return nil, nil
81 }
82
83 e := list[len(list)-1]
84 return e, nil
85 }
86
87 // CreateVersionInput is the input to the CreateVersion function.
88 type CreateVersionInput struct {
89 // Service is the ID of the service (required).
90 Service string
91
92 // A personal freeform descriptive note.
93 Comment string `form:"comment,omitempty"`
94 }
95
96 // CreateVersion constructs a new version. Note that `CloneVersion` is
97 // preferred in almost all scenarios, since `Create()` creates a _blank_
98 // configuration where `Clone()` builds off of an existing configuration.
99 func (c *Client) CreateVersion(i *CreateVersionInput) (*Version, error) {
100 if i.Service == "" {
101 return nil, ErrMissingService
102 }
103
104 path := fmt.Sprintf("/service/%s/version", i.Service)
105 resp, err := c.PostForm(path, i, nil)
106 if err != nil {
107 return nil, err
108 }
109
110 var e *Version
111 if err := decodeJSON(&e, resp.Body); err != nil {
112 return nil, err
113 }
114 return e, nil
115 }
116
117 // GetVersionInput is the input to the GetVersion function.
118 type GetVersionInput struct {
119 // Service is the ID of the service (required).
120 Service string
121
122 // Version is the version number to fetch (required).
123 Version int
124 }
125
126 // GetVersion fetches a version with the given information.
127 func (c *Client) GetVersion(i *GetVersionInput) (*Version, error) {
128 if i.Service == "" {
129 return nil, ErrMissingService
130 }
131
132 if i.Version == 0 {
133 return nil, ErrMissingVersion
134 }
135
136 path := fmt.Sprintf("/service/%s/version/%d", i.Service, i.Version)
137 resp, err := c.Get(path, nil)
138 if err != nil {
139 return nil, err
140 }
141
142 var e *Version
143 if err := decodeJSON(&e, resp.Body); err != nil {
144 return nil, err
145 }
146 return e, nil
147 }
148
149 // UpdateVersionInput is the input to the UpdateVersion function.
150 type UpdateVersionInput struct {
151 // Service is the ID of the service. Version is the specific configuration
152 // version. Both fields are required.
153 Service string
154 Version int
155
156 // A personal freeform descriptive note.
157 Comment string `form:"comment,omitempty"`
158 }
159
160 // UpdateVersion updates the given version
161 func (c *Client) UpdateVersion(i *UpdateVersionInput) (*Version, error) {
162 if i.Service == "" {
163 return nil, ErrMissingService
164 }
165
166 if i.Version == 0 {
167 return nil, ErrMissingVersion
168 }
169
170 path := fmt.Sprintf("/service/%s/version/%d", i.Service, i.Version)
171 resp, err := c.PutForm(path, i, nil)
172 if err != nil {
173 return nil, err
174 }
175
176 var e *Version
177 if err := decodeJSON(&e, resp.Body); err != nil {
178 return nil, err
179 }
180 return e, nil
181 }
182
183 // ActivateVersionInput is the input to the ActivateVersion function.
184 type ActivateVersionInput struct {
185 // Service is the ID of the service. Version is the specific configuration
186 // version. Both fields are required.
187 Service string
188 Version int
189 }
190
191 // ActivateVersion activates the given version.
192 func (c *Client) ActivateVersion(i *ActivateVersionInput) (*Version, error) {
193 if i.Service == "" {
194 return nil, ErrMissingService
195 }
196
197 if i.Version == 0 {
198 return nil, ErrMissingVersion
199 }
200
201 path := fmt.Sprintf("/service/%s/version/%d/activate", i.Service, i.Version)
202 resp, err := c.Put(path, nil)
203 if err != nil {
204 return nil, err
205 }
206
207 var e *Version
208 if err := decodeJSON(&e, resp.Body); err != nil {
209 return nil, err
210 }
211 return e, nil
212 }
213
214 // DeactivateVersionInput is the input to the DeactivateVersion function.
215 type DeactivateVersionInput struct {
216 // Service is the ID of the service. Version is the specific configuration
217 // version. Both fields are required.
218 Service string
219 Version int
220 }
221
222 // DeactivateVersion deactivates the given version.
223 func (c *Client) DeactivateVersion(i *DeactivateVersionInput) (*Version, error) {
224 if i.Service == "" {
225 return nil, ErrMissingService
226 }
227
228 if i.Version == 0 {
229 return nil, ErrMissingVersion
230 }
231
232 path := fmt.Sprintf("/service/%s/version/%d/deactivate", i.Service, i.Version)
233 resp, err := c.Put(path, nil)
234 if err != nil {
235 return nil, err
236 }
237
238 var e *Version
239 if err := decodeJSON(&e, resp.Body); err != nil {
240 return nil, err
241 }
242 return e, nil
243 }
244
245 // CloneVersionInput is the input to the CloneVersion function.
246 type CloneVersionInput struct {
247 // Service is the ID of the service. Version is the specific configuration
248 // version. Both fields are required.
249 Service string
250 Version int
251 }
252
253 // CloneVersion creates a clone of the version with and returns a new
254 // configuration version with all the same configuration options, but an
255 // incremented number.
256 func (c *Client) CloneVersion(i *CloneVersionInput) (*Version, error) {
257 if i.Service == "" {
258 return nil, ErrMissingService
259 }
260
261 if i.Version == 0 {
262 return nil, ErrMissingVersion
263 }
264
265 path := fmt.Sprintf("/service/%s/version/%d/clone", i.Service, i.Version)
266 resp, err := c.Put(path, nil)
267 if err != nil {
268 return nil, err
269 }
270
271 var e *Version
272 if err := decodeJSON(&e, resp.Body); err != nil {
273 return nil, err
274 }
275 return e, nil
276 }
277
278 // ValidateVersionInput is the input to the ValidateVersion function.
279 type ValidateVersionInput struct {
280 // Service is the ID of the service. Version is the specific configuration
281 // version. Both fields are required.
282 Service string
283 Version int
284 }
285
286 // ValidateVersion validates if the given version is okay.
287 func (c *Client) ValidateVersion(i *ValidateVersionInput) (bool, string, error) {
288 var msg string
289
290 if i.Service == "" {
291 return false, msg, ErrMissingService
292 }
293
294 if i.Version == 0 {
295 return false, msg, ErrMissingVersion
296 }
297
298 path := fmt.Sprintf("/service/%s/version/%d/validate", i.Service, i.Version)
299 resp, err := c.Get(path, nil)
300 if err != nil {
301 return false, msg, err
302 }
303
304 var r *statusResp
305 if err := decodeJSON(&r, resp.Body); err != nil {
306 return false, msg, err
307 }
308
309 msg = r.Msg
310 return r.Ok(), msg, nil
311 }
312
313 // LockVersionInput is the input to the LockVersion function.
314 type LockVersionInput struct {
315 // Service is the ID of the service. Version is the specific configuration
316 // version. Both fields are required.
317 Service string
318 Version int
319 }
320
321 // LockVersion locks the specified version.
322 func (c *Client) LockVersion(i *LockVersionInput) (*Version, error) {
323 if i.Service == "" {
324 return nil, ErrMissingService
325 }
326
327 if i.Version == 0 {
328 return nil, ErrMissingVersion
329 }
330
331 path := fmt.Sprintf("/service/%s/version/%d/lock", i.Service, i.Version)
332 resp, err := c.Put(path, nil)
333 if err != nil {
334 return nil, err
335 }
336
337 var e *Version
338 if err := decodeJSON(&e, resp.Body); err != nil {
339 return nil, err
340 }
341 return e, nil
342 }