Skip to content

fix(snapshot-instances): use block api #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jobs/instances-snapshot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ On Scaleway Console on the following link you can create a new Job Definition: h
- `SCW_ACCESS_KEY`: your access key
- `SCW_SECRET_KEY`: your secret key
- `SCW_DEFAULT_ORGANIZATION_ID`: your organzation ID
- `SCW_DEFAULT_PROJECT_ID`: your project ID

* Then click "create job"

Expand Down
4 changes: 2 additions & 2 deletions jobs/instances-snapshot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module github.com/scaleway/serverless-examples/jobs/instances-snapshot

go 1.24

require github.com/scaleway/scaleway-sdk-go v1.0.0-beta.32
require github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33

require (
github.com/kr/pretty v0.3.1 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
8 changes: 4 additions & 4 deletions jobs/instances-snapshot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.32 h1:4+LP7qmsLSGbmc66m1s5dKRMBwztRppfxFKlYqYte/c=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.32/go.mod h1:kzh+BSAvpoyHHdHBCDhmSWtBc1NbLMZ2lWHqnBoxFks=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33 h1:KhF0WejiUTDbL5X55nXowP7zNopwpowa6qaMAWyIE+0=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.33/go.mod h1:792k1RTU+5JeMXm35/e2Wgp71qPH/DmDoZrRc+EFZDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
27 changes: 11 additions & 16 deletions jobs/instances-snapshot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"fmt"
"os"
"time"

"github.com/scaleway/scaleway-sdk-go/api/block/v1"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
Expand All @@ -13,6 +13,7 @@ const (
envOrgID = "SCW_DEFAULT_ORGANIZATION_ID"
envAccessKey = "SCW_ACCESS_KEY"
envSecretKey = "SCW_SECRET_KEY"
envProjectID = "SCW_DEFAULT_PROJECT_ID"
envInstanceZone = "SCW_ZONE"
envInstanceID = "INSTANCE_ID"
)
Expand All @@ -39,12 +40,14 @@ func main() {
// Create SDK objects for Scaleway Instance product
instanceAPI := instance.NewAPI(client)

if err := createSnapshots(instanceAPI); err != nil {
blockAPI := block.NewAPI(client)

if err := createSnapshots(instanceAPI, blockAPI); err != nil {
panic(err)
}
}

func createSnapshots(instanceAPI *instance.API) error {
func createSnapshots(instanceAPI *instance.API, blockAPI *block.API) error {
gotInstance, err := instanceAPI.GetServer(&instance.GetServerRequest{
ServerID: os.Getenv(envInstanceID),
Zone: scw.Zone(os.Getenv(envInstanceZone)),
Expand All @@ -53,25 +56,17 @@ func createSnapshots(instanceAPI *instance.API) error {
return fmt.Errorf("error while getting instance %w", err)
}

now := time.Now().Format(time.DateOnly)

for _, volume := range gotInstance.Server.Volumes {
snapshotName := fmt.Sprintf("snap-vol-%s-%s-%s",
volume.VolumeType.String(),
now,
os.Getenv(envInstanceZone))

snapshotResp, err := instanceAPI.CreateSnapshot(&instance.CreateSnapshotRequest{
Name: snapshotName,
VolumeID: &volume.ID,
VolumeType: instance.SnapshotVolumeType(volume.VolumeType),
Zone: scw.Zone(os.Getenv(envInstanceZone)),
snapshotResp, err := blockAPI.CreateSnapshot(&block.CreateSnapshotRequest{
Zone: scw.Zone(os.Getenv(envInstanceZone)),
VolumeID: volume.ID,
ProjectID: os.Getenv(envProjectID),
})
if err != nil {
return fmt.Errorf("error while creating snapshot %w", err)
}

fmt.Println("created snapshot ", snapshotResp.Snapshot.ID)
fmt.Println("created snapshot ", snapshotResp.ID)
}

return nil
Expand Down
Loading