-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagement.go
More file actions
414 lines (348 loc) · 11.8 KB
/
management.go
File metadata and controls
414 lines (348 loc) · 11.8 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package sprites
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
)
// CreateSprite creates a new sprite with the given name and optional configuration
func (c *Client) CreateSprite(ctx context.Context, name string, config *SpriteConfig) (*Sprite, error) {
return c.CreateSpriteWithOrg(ctx, name, config, nil)
}
// CreateSpriteWithOrg creates a new sprite with the given name, optional configuration, and organization information
func (c *Client) CreateSpriteWithOrg(ctx context.Context, name string, config *SpriteConfig, org *OrganizationInfo) (*Sprite, error) {
req := CreateSpriteRequest{
Name: name,
Config: config,
}
jsonData, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
// Build URL
url := fmt.Sprintf("%s/v1/sprites", c.baseURL)
// Create HTTP request
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
httpReq.Header.Set("Content-Type", "application/json")
// Make request
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("failed to create sprite: %w", err)
}
defer resp.Body.Close()
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
// Check status code
if resp.StatusCode != http.StatusCreated {
// Parse structured error for 4xx/5xx responses
if apiErr := parseAPIError(resp, body); apiErr != nil {
return nil, apiErr
}
return nil, fmt.Errorf("failed to create sprite (status %d): %s", resp.StatusCode, string(body))
}
// Parse response
var createResp CreateSpriteResponse
if err := json.Unmarshal(body, &createResp); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
// Return a sprite object with the name
sprite := &Sprite{
name: createResp.Name,
Status: "created",
client: c,
org: org,
}
return sprite, nil
}
// GetSprite retrieves information about a specific sprite
func (c *Client) GetSprite(ctx context.Context, name string) (*Sprite, error) {
return c.GetSpriteWithOrg(ctx, name, nil)
}
// GetSpriteWithOrg retrieves information about a specific sprite with organization information
func (c *Client) GetSpriteWithOrg(ctx context.Context, name string, org *OrganizationInfo) (*Sprite, error) {
// Build URL
url := fmt.Sprintf("%s/v1/sprites/%s", c.baseURL, name)
// Create HTTP request
httpReq, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
// Make request
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("failed to get sprite: %w", err)
}
defer resp.Body.Close()
// Check status code
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("sprite not found: %s", name)
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
// Parse structured error for 4xx/5xx responses
if apiErr := parseAPIError(resp, body); apiErr != nil {
return nil, apiErr
}
return nil, fmt.Errorf("failed to get sprite (status %d): %s", resp.StatusCode, string(body))
}
// Parse response
var info SpriteInfo
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
// Convert to Sprite
sprite := &Sprite{
name: info.Name,
client: c,
org: org,
ID: info.ID,
OrganizationName: info.Organization,
Status: info.Status,
Config: info.Config,
Environment: info.Environment,
CreatedAt: info.CreatedAt,
UpdatedAt: info.UpdatedAt,
BucketName: info.BucketName,
PrimaryRegion: info.PrimaryRegion,
URL: info.URL,
URLSettings: info.URLSettings,
LastRunningAt: info.LastRunningAt,
LastWarmingAt: info.LastWarmingAt,
}
return sprite, nil
}
// ListSprites retrieves a list of sprites with optional filtering
func (c *Client) ListSprites(ctx context.Context, opts *ListOptions) (*SpriteList, error) {
if opts == nil {
opts = &ListOptions{
MaxResults: 100,
}
}
// Build URL with query parameters
baseURL := fmt.Sprintf("%s/v1/sprites", c.baseURL)
u, err := url.Parse(baseURL)
if err != nil {
return nil, fmt.Errorf("failed to parse URL: %w", err)
}
q := u.Query()
if opts.MaxResults > 0 {
q.Set("max_results", strconv.Itoa(opts.MaxResults))
}
if opts.ContinuationToken != "" {
q.Set("continuation_token", opts.ContinuationToken)
}
if opts.Prefix != "" {
q.Set("prefix", opts.Prefix)
}
u.RawQuery = q.Encode()
// Create request
httpReq, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
// Make request
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("failed to list sprites: %w", err)
}
defer resp.Body.Close()
// Check status
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
// Parse structured error for 4xx/5xx responses
if apiErr := parseAPIError(resp, body); apiErr != nil {
return nil, apiErr
}
return nil, fmt.Errorf("failed to list sprites (status %d): %s", resp.StatusCode, string(body))
}
// Parse response
var listResp SpriteList
if err := json.NewDecoder(resp.Body).Decode(&listResp); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
// Note: Sprites in the list response are SpriteInfo, not Sprite objects
// They don't have a client reference
return &listResp, nil
}
// ListAllSprites retrieves all sprites, handling pagination automatically
func (c *Client) ListAllSprites(ctx context.Context, prefix string) ([]*Sprite, error) {
return c.ListAllSpritesWithOrg(ctx, prefix, nil)
}
// ListResult holds the result of listing all sprites, including aggregate org info.
type ListResult struct {
Sprites []*Sprite
Org *OrgInfo
}
// ListAllSpritesWithOrg retrieves all sprites with organization information, handling pagination automatically
func (c *Client) ListAllSpritesWithOrg(ctx context.Context, prefix string, org *OrganizationInfo) ([]*Sprite, error) {
result, err := c.ListAllSpritesResult(ctx, prefix, org)
if err != nil {
return nil, err
}
return result.Sprites, nil
}
// ListAllSpritesResult retrieves all sprites with aggregate org stats, handling pagination automatically
func (c *Client) ListAllSpritesResult(ctx context.Context, prefix string, org *OrganizationInfo) (*ListResult, error) {
result := &ListResult{}
continuationToken := ""
for {
opts := &ListOptions{
Prefix: prefix,
MaxResults: 100,
ContinuationToken: continuationToken,
}
list, err := c.ListSprites(ctx, opts)
if err != nil {
return nil, err
}
// Capture org info from the first page
if result.Org == nil && list.Org != nil {
result.Org = list.Org
}
// Convert SpriteInfo to Sprite objects
for _, info := range list.Sprites {
sprite := &Sprite{
name: info.Name,
client: c,
org: org,
ID: info.ID,
OrganizationName: info.Organization,
Status: info.Status,
Config: info.Config,
Environment: info.Environment,
CreatedAt: info.CreatedAt,
UpdatedAt: info.UpdatedAt,
BucketName: info.BucketName,
PrimaryRegion: info.PrimaryRegion,
URL: info.URL,
URLSettings: info.URLSettings,
LastRunningAt: info.LastRunningAt,
LastWarmingAt: info.LastWarmingAt,
}
result.Sprites = append(result.Sprites, sprite)
}
if !list.HasMore || list.NextContinuationToken == "" {
break
}
continuationToken = list.NextContinuationToken
}
return result, nil
}
// DeleteSprite deletes a sprite
func (c *Client) DeleteSprite(ctx context.Context, name string) error {
// Build URL
url := fmt.Sprintf("%s/v1/sprites/%s", c.baseURL, name)
// Create request
httpReq, err := http.NewRequestWithContext(ctx, "DELETE", url, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
// Make request
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return fmt.Errorf("failed to delete sprite: %w", err)
}
defer resp.Body.Close()
// Check status
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
body, _ := io.ReadAll(resp.Body)
// Parse structured error for 4xx/5xx responses
if apiErr := parseAPIError(resp, body); apiErr != nil {
return apiErr
}
return fmt.Errorf("failed to delete sprite (status %d): %s", resp.StatusCode, string(body))
}
return nil
}
// DestroySprite is an alias for DeleteSprite to match the client's naming
func (c *Client) DestroySprite(ctx context.Context, name string) error {
return c.DeleteSprite(ctx, name)
}
// Delete deletes this sprite
func (s *Sprite) Delete(ctx context.Context) error {
return s.client.DeleteSprite(ctx, s.name)
}
// UpgradeSprite upgrades a sprite to the latest version
func (c *Client) UpgradeSprite(ctx context.Context, name string) error {
// Build URL
url := fmt.Sprintf("%s/v1/sprites/%s/upgrade", c.baseURL, name)
// Create request
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
// Make request
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return fmt.Errorf("failed to upgrade sprite: %w", err)
}
defer resp.Body.Close()
// Check status
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
body, _ := io.ReadAll(resp.Body)
// Parse structured error for 4xx/5xx responses
if apiErr := parseAPIError(resp, body); apiErr != nil {
return apiErr
}
return fmt.Errorf("failed to upgrade sprite (status %d): %s", resp.StatusCode, string(body))
}
return nil
}
// Upgrade upgrades this sprite to the latest version
func (s *Sprite) Upgrade(ctx context.Context) error {
return s.client.UpgradeSprite(ctx, s.name)
}
// UpdateURLSettings updates the URL authentication settings for a sprite
func (c *Client) UpdateURLSettings(ctx context.Context, spriteName string, settings *URLSettings) error {
req := UpdateURLSettingsRequest{
URLSettings: settings,
}
jsonData, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("failed to marshal request: %w", err)
}
// Build URL
url := fmt.Sprintf("%s/v1/sprites/%s", c.baseURL, spriteName)
// Create HTTP request
httpReq, err := http.NewRequestWithContext(ctx, "PUT", url, bytes.NewReader(jsonData))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
httpReq.Header.Set("Content-Type", "application/json")
// Make request
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return fmt.Errorf("failed to update URL settings: %w", err)
}
defer resp.Body.Close()
// Check status code
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
// Parse structured error for 4xx/5xx responses
if apiErr := parseAPIError(resp, body); apiErr != nil {
return apiErr
}
return fmt.Errorf("failed to update URL settings (status %d): %s", resp.StatusCode, string(body))
}
return nil
}
// UpdateURLSettings updates the URL authentication settings for this sprite
func (s *Sprite) UpdateURLSettings(ctx context.Context, settings *URLSettings) error {
return s.client.UpdateURLSettings(ctx, s.name, settings)
}