-
Notifications
You must be signed in to change notification settings - Fork 150
/
custom_snapshot.go
144 lines (121 loc) · 4.25 KB
/
custom_snapshot.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
package instance
import (
"context"
"reflect"
"time"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
const (
snapshotActionTimeout = 60 * time.Minute
)
// Builders
func snapshotCreateBuilder(c *core.Command) *core.Command {
type customCreateSnapshotRequest struct {
*instance.CreateSnapshotRequest
OrganizationID *string
ProjectID *string
Unified bool
}
renameOrganizationIDArgSpec(c.ArgSpecs)
renameProjectIDArgSpec(c.ArgSpecs)
c.ArgSpecs.DeleteByName("volume-type")
c.ArgSpecs.AddBefore("tags.{index}", &core.ArgSpec{
Name: "unified",
Short: "Whether a snapshot is unified or not.",
})
c.ArgsType = reflect.TypeOf(customCreateSnapshotRequest{})
c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) {
args := argsI.(*customCreateSnapshotRequest)
if args.CreateSnapshotRequest == nil {
args.CreateSnapshotRequest = &instance.CreateSnapshotRequest{}
}
request := args.CreateSnapshotRequest
request.Organization = args.OrganizationID
request.Project = args.ProjectID
client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
if args.Unified {
request.VolumeType = instance.SnapshotVolumeTypeUnified
} else if request.VolumeID != nil {
// If the snapshot is not unified, we need to set the snapshot volume type to the same type as the volume we target.
// Done only when creating snapshot from volume
volume, err := api.GetVolume(&instance.GetVolumeRequest{
VolumeID: *args.VolumeID,
Zone: args.Zone,
})
if err != nil {
return nil, err
}
request.VolumeType = instance.SnapshotVolumeType(volume.Volume.VolumeType)
}
return runner(ctx, request)
})
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
api := instance.NewAPI(core.ExtractClient(ctx))
return api.WaitForSnapshot(&instance.WaitForSnapshotRequest{
SnapshotID: respI.(*instance.CreateSnapshotResponse).Snapshot.ID,
Zone: argsI.(*customCreateSnapshotRequest).Zone,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
}
return c
}
func snapshotListBuilder(c *core.Command) *core.Command {
type customListSnapshotsRequest struct {
*instance.ListSnapshotsRequest
OrganizationID *string
ProjectID *string
}
renameOrganizationIDArgSpec(c.ArgSpecs)
renameProjectIDArgSpec(c.ArgSpecs)
c.ArgsType = reflect.TypeOf(customListSnapshotsRequest{})
c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) {
args := argsI.(*customListSnapshotsRequest)
if args.ListSnapshotsRequest == nil {
args.ListSnapshotsRequest = &instance.ListSnapshotsRequest{}
}
request := args.ListSnapshotsRequest
request.Organization = args.OrganizationID
request.Project = args.ProjectID
return runner(ctx, request)
})
return c
}
func snapshotWaitCommand() *core.Command {
return &core.Command{
Short: `Wait for snapshot to reach a stable state`,
Long: `Wait for snapshot to reach a stable state. This is similar to using --wait flag on other action commands, but without requiring a new action on the snapshot.`,
Namespace: "instance",
Resource: "snapshot",
Verb: "wait",
Groups: []string{"workflow"},
ArgsType: reflect.TypeOf(instance.WaitForSnapshotRequest{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
api := instance.NewAPI(core.ExtractClient(ctx))
return api.WaitForSnapshot(&instance.WaitForSnapshotRequest{
Zone: argsI.(*instance.WaitForSnapshotRequest).Zone,
SnapshotID: argsI.(*instance.WaitForSnapshotRequest).SnapshotID,
Timeout: scw.TimeDurationPtr(snapshotActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
},
ArgSpecs: core.ArgSpecs{
{
Name: "snapshot-id",
Short: `ID of the snapshot.`,
Required: true,
Positional: true,
},
core.ZoneArgSpec(),
},
Examples: []*core.Example{
{
Short: "Wait for a snapshot to reach a stable state",
ArgsJSON: `{"snapshot_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}