-
Notifications
You must be signed in to change notification settings - Fork 3
/
custom_images.go
110 lines (90 loc) · 3.17 KB
/
custom_images.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
package cloudscale
import (
"context"
"fmt"
"net/http"
"time"
)
const customImagesBasePath = "v1/custom-images"
const UserDataHandlingPassThrough = "pass-through"
const UserDataHandlingExtendCloudConfig = "extend-cloud-config"
type CustomImage struct {
ZonalResource
TaggedResource
// Just use omitempty everywhere. This makes it easy to use restful. Errors
// will be coming from the API if something is disabled.
HREF string `json:"href,omitempty"`
UUID string `json:"uuid,omitempty"`
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
SizeGB int `json:"size_gb,omitempty"`
Checksums map[string]string `json:"checksums,omitempty"`
UserDataHandling string `json:"user_data_handling,omitempty"`
FirmwareType string `json:"firmware_type,omitempty"`
Zones []Zone `json:"zones"`
CreatedAt time.Time `json:"created_at"`
}
type CustomImageRequest struct {
TaggedResourceRequest
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
UserDataHandling string `json:"user_data_handling,omitempty"`
}
type CustomImageService interface {
Get(ctx context.Context, CustomImageID string) (*CustomImage, error)
List(ctx context.Context, modifiers ...ListRequestModifier) ([]CustomImage, error)
Update(ctx context.Context, customImageID string, updateRequest *CustomImageRequest) error
Delete(ctx context.Context, customImageID string) error
}
type CustomImageServiceOperations struct {
client *Client
}
func (s CustomImageServiceOperations) Get(ctx context.Context, customImageID string) (*CustomImage, error) {
path := fmt.Sprintf("%s/%s", customImagesBasePath, customImageID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
customImage := new(CustomImage)
err = s.client.Do(ctx, req, customImage)
if err != nil {
return nil, err
}
return customImage, nil
}
func (s CustomImageServiceOperations) List(ctx context.Context, modifiers ...ListRequestModifier) ([]CustomImage, error) {
path := customImagesBasePath
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
for _, modifier := range modifiers {
modifier(req)
}
customImages := []CustomImage{}
err = s.client.Do(ctx, req, &customImages)
if err != nil {
return nil, err
}
return customImages, nil
}
func (s CustomImageServiceOperations) Update(ctx context.Context, customImageID string, updateRequest *CustomImageRequest) error {
path := fmt.Sprintf("%s/%s", customImagesBasePath, customImageID)
req, err := s.client.NewRequest(ctx, http.MethodPatch, path, updateRequest)
if err != nil {
return err
}
err = s.client.Do(ctx, req, nil)
if err != nil {
return err
}
return nil
}
func (s CustomImageServiceOperations) Delete(ctx context.Context, customImageID string) error {
path := fmt.Sprintf("%s/%s", customImagesBasePath, customImageID)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return err
}
return s.client.Do(ctx, req, nil)
}