@@ -81,7 +81,10 @@ func main() {
8181 }
8282}
8383
84+ // cleanSnapshots when called will clean snapshots in the project (if specified)
85+ // that are older than the number of days.
8486func cleanSnapshots (days int , instanceAPI * instance.API ) error {
87+ // Get the list of all snapshots
8588 snapshotsList , err := instanceAPI .ListSnapshots (& instance.ListSnapshotsRequest {
8689 Zone : scw .Zone (os .Getenv (envZone )),
8790 Project : scw .StringPtr (os .Getenv (envProjectID )),
@@ -95,10 +98,13 @@ func cleanSnapshots(days int, instanceAPI *instance.API) error {
9598
9699 currentTime := time .Now ()
97100
101+ // For each snapshot, check conditions
98102 for _ , snapshot := range snapshotsList .Snapshots {
103+ // Check if snapshot is in ready state and if it's older than the number of days definied.
99104 if snapshot .State == instance .SnapshotStateAvailable && (currentTime .Sub (* snapshot .CreationDate ).Hours ()/ hoursPerDay ) > float64 (days ) {
100105 fmt .Printf ("\n Deleting snapshot <%s>:%s created at: %s\n " , snapshot .ID , snapshot .Name , snapshot .CreationDate .Format (time .RFC3339 ))
101106
107+ // Delete snapshot found.
102108 err := instanceAPI .DeleteSnapshot (& instance.DeleteSnapshotRequest {
103109 SnapshotID : snapshot .ID ,
104110 Zone : snapshot .Zone ,
@@ -112,6 +118,7 @@ func cleanSnapshots(days int, instanceAPI *instance.API) error {
112118 return nil
113119}
114120
121+ // Check for mandatory variables before starting to work.
115122func init () {
116123 mandatoryVariables := [... ]string {envOrgID , envAccessKey , envSecretKey , envZone , envProjectID }
117124
0 commit comments