|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/scaleway/scaleway-sdk-go/api/instance/v1" |
| 8 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 9 | +) |
| 10 | + |
| 11 | +// Environment variable constants used to configure the Scaleway API client. |
| 12 | +// These must be set in the environment for the application to authenticate and interact with Scaleway services. |
| 13 | +const ( |
| 14 | + // envOrgID is the Scaleway Organization ID, used for billing and resource ownership (legacy; prefer Project ID). |
| 15 | + envOrgID = "SCW_DEFAULT_ORGANIZATION_ID" |
| 16 | + |
| 17 | + // envAccessKey is the API access key for authenticating requests to Scaleway. |
| 18 | + envAccessKey = "SCW_ACCESS_KEY" |
| 19 | + |
| 20 | + // envSecretKey is the secret key associated with the access key, used for signing requests. |
| 21 | + envSecretKey = "SCW_SECRET_KEY" |
| 22 | + |
| 23 | + // envProjectID is the Scaleway Project ID, which groups resources and is the preferred way to organize infrastructure. |
| 24 | + envProjectID = "SCW_DEFAULT_PROJECT_ID" |
| 25 | + |
| 26 | + // envZone specifies the geographical region/zone where resources will be created (e.g., fr-par-1). |
| 27 | + envZone = "SCW_ZONE" |
| 28 | + |
| 29 | + // envBucket is a custom environment variable for specifying the name of an S3-compatible bucket. |
| 30 | + // This is not a standard Scaleway variable and is application-specific. |
| 31 | + envBucket = "SCW_BUCKET_NAME" |
| 32 | +) |
| 33 | + |
| 34 | +func main() { |
| 35 | + fmt.Println("moving snapshots to s3...") |
| 36 | + |
| 37 | + // Create a Scaleway client with credentials from environment variables. |
| 38 | + client, err := scw.NewClient( |
| 39 | + // Get your organization ID at https://console.scaleway.com/organization/settings |
| 40 | + scw.WithDefaultOrganizationID(os.Getenv(envOrgID)), |
| 41 | + |
| 42 | + // Get your credentials at https://console.scaleway.com/iam/api-keys |
| 43 | + scw.WithAuth(os.Getenv(envAccessKey), os.Getenv(envSecretKey)), |
| 44 | + |
| 45 | + // Set the default project ID to organize resources under a specific project |
| 46 | + scw.WithDefaultProjectID(os.Getenv(envProjectID)), |
| 47 | + |
| 48 | + // Set the default zone where resources such as block volumes and snapshots are located |
| 49 | + scw.WithDefaultZone(scw.Zone(os.Getenv(envZone))), |
| 50 | + ) |
| 51 | + if err != nil { |
| 52 | + panic(err) |
| 53 | + } |
| 54 | + |
| 55 | + instanceAPI := instance.NewAPI(client) |
| 56 | + |
| 57 | + snapList, err := instanceAPI.ListSnapshots(&instance.ListSnapshotsRequest{}, scw.WithAllPages()) |
| 58 | + if err != nil { |
| 59 | + panic(err) |
| 60 | + } |
| 61 | + |
| 62 | + fmt.Printf("number of snapshots: %d\n", snapList.TotalCount) |
| 63 | + |
| 64 | + for _, snapshot := range snapList.Snapshots { |
| 65 | + fmt.Printf("snap %s\n", snapshot.Name) |
| 66 | + |
| 67 | + if snapshot.State == instance.SnapshotStateAvailable { |
| 68 | + fmt.Printf("Exporting snapshot %s (ID: %s) to bucket %s...\n", snapshot.Name, snapshot.ID, os.Getenv(envBucket)) |
| 69 | + |
| 70 | + snap, err := instanceAPI.ExportSnapshot(&instance.ExportSnapshotRequest{ |
| 71 | + SnapshotID: snapshot.ID, |
| 72 | + Bucket: os.Getenv(envBucket), |
| 73 | + Key: snapshot.Name + ".qcow2", |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + fmt.Printf("Failed to export snapshot %s: %v\n", snapshot.Name, err) |
| 77 | + |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + fmt.Printf("Successfully started export of %s to %s/%s\n", snap.Task.ID, os.Getenv(envBucket), snap.Task.Description) |
| 82 | + } else { |
| 83 | + fmt.Printf("Skipping snapshot %s (ID: %s) - status is %s, not available\n", snapshot.Name, snapshot.ID, snapshot.State.String()) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Check for mandatory variables before starting to work. |
| 89 | +func init() { |
| 90 | + mandatoryVariables := [...]string{envOrgID, envAccessKey, envSecretKey, envZone, envProjectID, envBucket} |
| 91 | + |
| 92 | + for idx := range mandatoryVariables { |
| 93 | + if os.Getenv(mandatoryVariables[idx]) == "" { |
| 94 | + panic("missing environment variable " + mandatoryVariables[idx]) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments