Skip to content

Commit ec5c4df

Browse files
committed
add support for ExportSnapshot
1 parent 6340fc1 commit ec5c4df

File tree

3 files changed

+571
-0
lines changed

3 files changed

+571
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package instance
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/action"
8+
"github.com/hashicorp/terraform-plugin-framework/action/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
11+
"github.com/scaleway/scaleway-sdk-go/scw"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
13+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
14+
)
15+
16+
var (
17+
_ action.Action = (*ExportSnapshot)(nil)
18+
_ action.ActionWithConfigure = (*ExportSnapshot)(nil)
19+
)
20+
21+
type ExportSnapshot struct {
22+
instanceAPI *instance.API
23+
}
24+
25+
func (e *ExportSnapshot) Configure(_ context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) {
26+
if req.ProviderData == nil {
27+
return
28+
}
29+
30+
m, ok := req.ProviderData.(*meta.Meta)
31+
if !ok {
32+
resp.Diagnostics.AddError(
33+
"Unexpected Action Configure Type",
34+
fmt.Sprintf("Expected *scw.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
35+
)
36+
37+
return
38+
}
39+
40+
client := m.ScwClient()
41+
e.instanceAPI = instance.NewAPI(client)
42+
}
43+
44+
func (e *ExportSnapshot) Metadata(_ context.Context, req action.MetadataRequest, resp *action.MetadataResponse) {
45+
resp.TypeName = req.ProviderTypeName + "_instance_export_snapshot"
46+
}
47+
48+
type ExportSnapshotModel struct {
49+
Zone types.String `tfsdk:"zone"`
50+
SnapshotID types.String `tfsdk:"snapshot_id"`
51+
Bucket types.String `tfsdk:"bucket"`
52+
Key types.String `tfsdk:"key"`
53+
Wait types.Bool `tfsdk:"wait"`
54+
}
55+
56+
func NewExportSnapshot() action.Action {
57+
return &ExportSnapshot{}
58+
}
59+
60+
func (e *ExportSnapshot) Schema(_ context.Context, _ action.SchemaRequest, resp *action.SchemaResponse) {
61+
resp.Schema = schema.Schema{
62+
Attributes: map[string]schema.Attribute{
63+
"snapshot_id": schema.StringAttribute{
64+
Required: true,
65+
Description: "ID of the snapshot to export",
66+
},
67+
"zone": schema.StringAttribute{
68+
Optional: true,
69+
Description: "Zone of the snapshot to export",
70+
},
71+
"bucket": schema.StringAttribute{
72+
Required: true,
73+
Description: "Name of the bucket to export the snapshot to",
74+
},
75+
"key": schema.StringAttribute{
76+
Required: true,
77+
Description: "Object key to save the snapshot to",
78+
},
79+
"wait": schema.BoolAttribute{
80+
Optional: true,
81+
Description: "Wait for exporting operation to be completed",
82+
},
83+
},
84+
}
85+
}
86+
87+
func (e *ExportSnapshot) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) {
88+
var data ExportSnapshotModel
89+
// Read action config data into the model
90+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
91+
92+
if resp.Diagnostics.HasError() {
93+
return
94+
}
95+
96+
if e.instanceAPI == nil {
97+
resp.Diagnostics.AddError(
98+
"Unconfigured instanceAPI",
99+
"The action was not properly configured. The Scaleway client is missing. "+
100+
"This is usually a bug in the provider. Please report it to the maintainers.",
101+
)
102+
103+
return
104+
}
105+
106+
zone, snapshotID, _ := locality.ParseLocalizedID(data.SnapshotID.ValueString())
107+
if zone == "" {
108+
if !data.Zone.IsNull() {
109+
zone = data.Zone.ValueString()
110+
} else {
111+
resp.Diagnostics.AddError(
112+
"missing zone in config",
113+
fmt.Sprintf("zone could not be extracted from either the action configuration or the resource ID (%s)",
114+
data.SnapshotID.ValueString(),
115+
),
116+
)
117+
118+
return
119+
}
120+
}
121+
122+
actionReq := &instance.ExportSnapshotRequest{
123+
SnapshotID: snapshotID,
124+
Zone: scw.Zone(zone),
125+
Bucket: data.Bucket.ValueString(),
126+
Key: data.Key.ValueString(),
127+
}
128+
if !data.Zone.IsNull() {
129+
actionReq.Zone = scw.Zone(data.Zone.ValueString())
130+
}
131+
132+
_, err := e.instanceAPI.ExportSnapshot(actionReq, scw.WithContext(ctx))
133+
if err != nil {
134+
resp.Diagnostics.AddError(
135+
"error exporting snapshot",
136+
fmt.Sprintf("%s", err))
137+
138+
return
139+
}
140+
141+
if data.Wait.ValueBool() {
142+
_, err = e.instanceAPI.WaitForSnapshot(&instance.WaitForSnapshotRequest{
143+
SnapshotID: snapshotID,
144+
Zone: scw.Zone(zone),
145+
}, scw.WithContext(ctx))
146+
if err != nil {
147+
resp.Diagnostics.AddError(
148+
"error waiting snapshot",
149+
fmt.Sprintf("%s", err))
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)