-
Notifications
You must be signed in to change notification settings - Fork 149
/
custom_image.go
332 lines (283 loc) · 9.78 KB
/
custom_image.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
package instance
import (
"context"
"reflect"
"sort"
"strconv"
"time"
"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/human"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
const (
imageActionTimeout = 60 * time.Minute
)
//
// Marshalers
//
// imageStateMarshalSpecs allows to override the displayed instance.ImageState.
var (
imageStateMarshalSpecs = human.EnumMarshalSpecs{
instance.ImageStateCreating: &human.EnumMarshalSpec{Attribute: color.FgBlue},
instance.ImageStateAvailable: &human.EnumMarshalSpec{Attribute: color.FgGreen},
instance.ImageStateError: &human.EnumMarshalSpec{Attribute: color.FgRed},
}
)
func imagesMarshalerFunc(i interface{}, _ *human.MarshalOpt) (string, error) {
type humanImage struct {
ID string
Name string
State instance.ImageState
Public bool
Zone scw.Zone
Volumes []scw.Size
ServerName string
ServerID string
Arch instance.Arch
OrganizationID string
ProjectID string
CreationDate *time.Time
ModificationDate *time.Time
}
images := i.([]*imageListItem)
humanImages := []*humanImage(nil)
for _, image := range images {
// For each image we want to display a list of volume size sepatated with `,`
// e.g: 10 GB, 20 GB
volumes := []scw.Size{
image.RootVolume.Size,
}
// We must sort map key to make sure volume size are in the correct order.
extraVolumeKeys := []string(nil)
for key := range image.ExtraVolumes {
extraVolumeKeys = append(extraVolumeKeys, key)
}
sort.Strings(extraVolumeKeys)
for _, key := range extraVolumeKeys {
volumes = append(volumes, image.ExtraVolumes[key].Size)
}
humanImages = append(humanImages, &humanImage{
ID: image.ID,
Name: image.Name,
State: image.State,
Public: image.Public,
Zone: image.Zone,
Volumes: volumes,
ServerName: image.ServerName,
ServerID: image.ServerID,
Arch: image.Arch,
OrganizationID: image.Organization,
ProjectID: image.Project,
CreationDate: image.CreationDate,
ModificationDate: image.ModificationDate,
})
}
return human.Marshal(humanImages, nil)
}
//
// Builders
//
// imageCreateBuilder overrides 'instance image create' to
// - rename extra-volumes arguments into additional-volumes
// - rename the argument 'root-volume' into 'snapshot-id'
func imageCreateBuilder(c *core.Command) *core.Command {
type customCreateImageRequest struct {
*instance.CreateImageRequest
AdditionalVolumes []*instance.VolumeTemplate
SnapshotID string
OrganizationID *string
ProjectID *string
}
c.ArgSpecs.GetByName("extra-volumes.{key}.id").Short = "UUID of the snapshot to add"
c.ArgSpecs.GetByName("extra-volumes.{key}.id").Name = "additional-volumes.{index}.id"
c.ArgSpecs.GetByName("extra-volumes.{key}.name").Short = "Name of the additional snapshot"
c.ArgSpecs.GetByName("extra-volumes.{key}.name").Name = "additional-volumes.{index}.name"
c.ArgSpecs.GetByName("extra-volumes.{key}.size").Short = "Size of the additional snapshot"
c.ArgSpecs.GetByName("extra-volumes.{key}.size").Name = "additional-volumes.{index}.size"
c.ArgSpecs.GetByName("extra-volumes.{key}.volume-type").Short = "Underlying volume type of the additional snapshot"
c.ArgSpecs.GetByName("extra-volumes.{key}.volume-type").Name = "additional-volumes.{index}.volume-type"
c.ArgSpecs.GetByName("extra-volumes.{key}.organization").Short = "Organization ID that own the additional snapshot"
c.ArgSpecs.GetByName("extra-volumes.{key}.organization").Name = "additional-volumes.{index}.organization-id"
c.ArgSpecs.GetByName("extra-volumes.{key}.project").Short = "Project ID that own the additional snapshot"
c.ArgSpecs.GetByName("extra-volumes.{key}.project").Name = "additional-volumes.{index}.project-id"
c.ArgSpecs.GetByName("root-volume").Short = "UUID of the snapshot that will be used as root volume in the image"
c.ArgSpecs.GetByName("root-volume").Name = "snapshot-id"
renameOrganizationIDArgSpec(c.ArgSpecs)
renameProjectIDArgSpec(c.ArgSpecs)
c.ArgsType = reflect.TypeOf(customCreateImageRequest{})
c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) {
args := argsI.(*customCreateImageRequest)
request := args.CreateImageRequest
request.RootVolume = args.SnapshotID
request.ExtraVolumes = make(map[string]*instance.VolumeTemplate)
request.Organization = args.OrganizationID
request.Project = args.ProjectID
// Extra volumes need to start at volumeIndex 1.
volumeIndex := 1
for _, volume := range args.AdditionalVolumes {
request.ExtraVolumes[strconv.Itoa(volumeIndex)] = volume
volumeIndex++
}
return runner(ctx, request)
})
return c
}
// customImage is based on instance.Image, with additional information about the server
type imageListItem struct {
*instance.Image
// Replace Image.FromServer
ServerID string `json:"server_id"`
ServerName string `json:"server_name"`
}
// imageListBuilder list the images for a given organization/project.
// A call to GetServer(..) with the ID contained in Image.FromServer retrieves more information about the server.
func imageListBuilder(c *core.Command) *core.Command {
type customListImageRequest struct {
*instance.ListImagesRequest
OrganizationID *string
ProjectID *string
}
renameOrganizationIDArgSpec(c.ArgSpecs)
renameProjectIDArgSpec(c.ArgSpecs)
c.ArgsType = reflect.TypeOf(customListImageRequest{})
c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
// Get images
args := argsI.(*customListImageRequest)
if args.ListImagesRequest == nil {
args.ListImagesRequest = &instance.ListImagesRequest{}
}
req := args.ListImagesRequest
req.Organization = args.OrganizationID
req.Project = args.ProjectID
req.Public = scw.BoolPtr(false)
client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
listImagesResponse, err := api.ListImages(req, scw.WithAllPages())
if err != nil {
return nil, err
}
images := listImagesResponse.Images
// Builds customImages
customImages := []*imageListItem(nil)
for _, image := range images {
newCustomImage := &imageListItem{
Image: image,
}
customImages = append(customImages, newCustomImage)
if image.FromServer == "" {
continue
}
serverReq := instance.GetServerRequest{
Zone: req.Zone,
ServerID: image.FromServer,
}
getServerResponse, err := api.GetServer(&serverReq)
if _, ok := err.(*scw.ResourceNotFoundError); ok {
newCustomImage.ServerName = "-"
continue
}
if err != nil {
return nil, err
}
newCustomImage.ServerID = getServerResponse.Server.ID
newCustomImage.ServerName = getServerResponse.Server.Name
}
return customImages, nil
}
return c
}
// imageDeleteBuilder override delete command to:
// - add a with-snapshots parameter
func imageDeleteBuilder(c *core.Command) *core.Command {
type customDeleteImageRequest struct {
*instance.DeleteImageRequest
WithSnapshots bool
}
c.ArgsType = reflect.TypeOf(customDeleteImageRequest{})
c.ArgSpecs.AddBefore("zone", &core.ArgSpec{
Name: "with-snapshots",
Short: "Delete the snapshots attached to this image",
})
c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) {
args := argsI.(*customDeleteImageRequest)
api := instance.NewAPI(core.ExtractClient(ctx))
// If we want to delete snapshot we must GET image before we delete it
image := (*instance.Image)(nil)
if args.WithSnapshots {
res, err := api.GetImage(&instance.GetImageRequest{
Zone: args.Zone,
ImageID: args.ImageID,
})
if err != nil {
return nil, err
}
image = res.Image
}
// Call the generated delete
runnerRes, err := runner(ctx, args.DeleteImageRequest)
if err != nil {
return nil, err
}
// Once the image is deleted we can delete snapshots.
if args.WithSnapshots {
snapshotIDs := []string{
image.RootVolume.ID,
}
for _, snapshot := range image.ExtraVolumes {
snapshotIDs = append(snapshotIDs, snapshot.ID)
}
for _, snapshotID := range snapshotIDs {
err := api.DeleteSnapshot(&instance.DeleteSnapshotRequest{
Zone: args.Zone,
SnapshotID: snapshotID,
})
if err != nil {
return nil, err
}
}
}
return runnerRes, nil
})
return c
}
//
// Commands
//
func imageWaitCommand() *core.Command {
return &core.Command{
Short: `Wait for image to reach a stable state`,
Long: `Wait for image to reach a stable state. This is similar to using --wait flag on other action commands, but without requiring a new action on the image.`,
Namespace: "instance",
Resource: "image",
Verb: "wait",
Groups: []string{"workflow"},
ArgsType: reflect.TypeOf(instance.WaitForImageRequest{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
api := instance.NewAPI(core.ExtractClient(ctx))
return api.WaitForImage(&instance.WaitForImageRequest{
Zone: argsI.(*instance.WaitForImageRequest).Zone,
ImageID: argsI.(*instance.WaitForImageRequest).ImageID,
Timeout: argsI.(*instance.WaitForImageRequest).Timeout,
RetryInterval: core.DefaultRetryInterval,
})
},
ArgSpecs: core.ArgSpecs{
{
Name: "image-id",
Short: `ID of the image.`,
Required: true,
Positional: true,
},
core.ZoneArgSpec(),
core.WaitTimeoutArgSpec(imageActionTimeout),
},
Examples: []*core.Example{
{
Short: "Wait for a image to reach a stable state",
ArgsJSON: `{"image_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}