-
Notifications
You must be signed in to change notification settings - Fork 19
/
images.go
268 lines (234 loc) · 7.13 KB
/
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
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
//
// Copyright 2020 Joyent, Inc. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
package compute
import (
"context"
"encoding/json"
"net/http"
"net/url"
"path"
"time"
"github.com/joyent/triton-go/v2/client"
"github.com/pkg/errors"
)
type ImagesClient struct {
client *client.Client
}
type ImageFile struct {
Compression string `json:"compression"`
SHA1 string `json:"sha1"`
Size int64 `json:"size"`
}
type Image struct {
ID string `json:"id"`
Name string `json:"name"`
OS string `json:"os"`
Description string `json:"description"`
Version string `json:"version"`
Type string `json:"type"`
Requirements map[string]interface{} `json:"requirements"`
Homepage string `json:"homepage"`
Files []*ImageFile `json:"files"`
PublishedAt time.Time `json:"published_at"`
Owner string `json:"owner"`
Public bool `json:"public"`
State string `json:"state"`
Tags map[string]string `json:"tags"`
EULA string `json:"eula"`
ACL []string `json:"acl"`
}
type ListImagesInput struct {
Name string
OS string
Version string
Public bool
State string
Owner string
Type string
}
func (c *ImagesClient) List(ctx context.Context, input *ListImagesInput) ([]*Image, error) {
fullPath := path.Join("/", c.client.AccountName, "images")
query := &url.Values{}
if input.Name != "" {
query.Set("name", input.Name)
}
if input.OS != "" {
query.Set("os", input.OS)
}
if input.Version != "" {
query.Set("version", input.Version)
}
if input.Public {
query.Set("public", "true")
}
if input.State != "" {
query.Set("state", input.State)
}
if input.Owner != "" {
query.Set("owner", input.Owner)
}
if input.Type != "" {
query.Set("type", input.Type)
}
reqInputs := client.RequestInput{
Method: http.MethodGet,
Path: fullPath,
Query: query,
}
respReader, err := c.client.ExecuteRequestURIParams(ctx, reqInputs)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return nil, errors.Wrap(err, "unable to list images")
}
var result []*Image
decoder := json.NewDecoder(respReader)
if err = decoder.Decode(&result); err != nil {
return nil, errors.Wrap(err, "unable to decode list images response")
}
return result, nil
}
type GetImageInput struct {
ImageID string
}
func (c *ImagesClient) Get(ctx context.Context, input *GetImageInput) (*Image, error) {
fullPath := path.Join("/", c.client.AccountName, "images", input.ImageID)
reqInputs := client.RequestInput{
Method: http.MethodGet,
Path: fullPath,
}
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return nil, errors.Wrap(err, "unable to get image")
}
var result *Image
decoder := json.NewDecoder(respReader)
if err = decoder.Decode(&result); err != nil {
return nil, errors.Wrap(err, "unable to decode get image response")
}
return result, nil
}
type DeleteImageInput struct {
ImageID string
}
func (c *ImagesClient) Delete(ctx context.Context, input *DeleteImageInput) error {
fullPath := path.Join("/", c.client.AccountName, "images", input.ImageID)
reqInputs := client.RequestInput{
Method: http.MethodDelete,
Path: fullPath,
}
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return errors.Wrap(err, "unable to delete image")
}
return nil
}
type ExportImageInput struct {
ImageID string
MantaPath string
}
type MantaLocation struct {
MantaURL string `json:"manta_url"`
ImagePath string `json:"image_path"`
ManifestPath string `json:"manifest_path"`
}
func (c *ImagesClient) Export(ctx context.Context, input *ExportImageInput) (*MantaLocation, error) {
fullPath := path.Join("/", c.client.AccountName, "images", input.ImageID)
query := &url.Values{}
query.Set("action", "export")
reqInputs := client.RequestInput{
Method: http.MethodPost,
Path: fullPath,
Query: query,
}
respReader, err := c.client.ExecuteRequestURIParams(ctx, reqInputs)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return nil, errors.Wrap(err, "unable to export image")
}
var result *MantaLocation
decoder := json.NewDecoder(respReader)
if err = decoder.Decode(&result); err != nil {
return nil, errors.Wrap(err, "unable to decode export image response")
}
return result, nil
}
type CreateImageFromMachineInput struct {
MachineID string `json:"machine"`
Name string `json:"name"`
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
HomePage string `json:"homepage,omitempty"`
EULA string `json:"eula,omitempty"`
ACL []string `json:"acl,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
}
func (c *ImagesClient) CreateFromMachine(ctx context.Context, input *CreateImageFromMachineInput) (*Image, error) {
fullPath := path.Join("/", c.client.AccountName, "images")
reqInputs := client.RequestInput{
Method: http.MethodPost,
Path: fullPath,
Body: input,
}
respReader, err := c.client.ExecuteRequest(ctx, reqInputs)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return nil, errors.Wrap(err, "unable to create machine from image")
}
var result *Image
decoder := json.NewDecoder(respReader)
if err = decoder.Decode(&result); err != nil {
return nil, errors.Wrap(err, "unable to decode create machine from image response")
}
return result, nil
}
type UpdateImageInput struct {
ImageID string `json:"-"`
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
HomePage string `json:"homepage,omitempty"`
EULA string `json:"eula,omitempty"`
ACL []string `json:"acl,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
}
func (c *ImagesClient) Update(ctx context.Context, input *UpdateImageInput) (*Image, error) {
fullPath := path.Join("/", c.client.AccountName, "images", input.ImageID)
query := &url.Values{}
query.Set("action", "update")
reqInputs := client.RequestInput{
Method: http.MethodPost,
Path: fullPath,
Query: query,
Body: input,
}
respReader, err := c.client.ExecuteRequestURIParams(ctx, reqInputs)
if respReader != nil {
defer respReader.Close()
}
if err != nil {
return nil, errors.Wrap(err, "unable to update image")
}
var result *Image
decoder := json.NewDecoder(respReader)
if err = decoder.Decode(&result); err != nil {
return nil, errors.Wrap(err, "unable to decode update image response")
}
return result, nil
}