Skip to content

Commit fc13db9

Browse files
uzaxirrgiornetta
andauthored
feat(snapshot): add snapshot schedule management commands (#540)
* feat(snapshot): add snapshot schedule management commands * fix: add snapshot command to root command * refactor: update snapshot schedule creation and show commands. * chore: update civogo dependency to version v0.5.5 * reset readme * fix unpausing schedule * fix missing newline * add paused field to list output * more compact format * add additional fields to json output * fix conflicts * fix README.md * fix README.md * documentation * typo --------- Co-authored-by: Michele Giornetta <[email protected]>
1 parent 7d031d8 commit fc13db9

11 files changed

+563
-33
lines changed

README.md

Lines changed: 97 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ Civo CLI is a tool to manage your [Civo.com](https://www.civo.com) account from
2929
- [SSH Keys](#ssh-keys)
3030
- [Disk Images](#disk-images)
3131
- [Volumes](#volumes)
32+
- [Resource Snapshots](#resource-snapshots)
3233
- [Teams](#teams)
3334
- [Permissions](#permissions)
3435
- [Region](#region)
35-
- [Resource Snapshots](#resource-snapshots)
3636
- [Enabling shell autocompletion](#enabling-shell-autocompletion)
3737
- [Contributing](#contributing)
3838
- [License](#license)
@@ -2155,6 +2155,101 @@ The volume called CLI-demo-volume with ID 59076ec8-edba-4071-80d0-e9cfcce37b12 w
21552155
21562156
If a Kubernetes volume is showing with a status of `dangling` it can be deleted to release the quota and prevent further billing by running `civo volume delete <VOLUME-NAME> --region <REGION-NAME>`.
21572157
2158+
2159+
## Resource Snapshots
2160+
2161+
Resource snapshots allow you to manage snapshots of your resources.
2162+
2163+
> [!IMPORTANT]
2164+
> Currently, Resource Snapshots will only display Instance Snapshots. More resource types will be supported in the future.
2165+
2166+
You can list, show details, update, delete, and restore snapshots using the following commands:
2167+
2168+
```sh
2169+
# List all resource snapshots
2170+
civo resource-snapshot list
2171+
2172+
# Show details of a specific snapshot
2173+
civo resource-snapshot show SNAPSHOT_ID/NAME
2174+
2175+
# Update a snapshot
2176+
civo resource-snapshot update SNAPSHOT_ID/NAME
2177+
2178+
# Delete a snapshot
2179+
civo resource-snapshot delete SNAPSHOT_ID/NAME
2180+
2181+
# Restore a snapshot
2182+
civo resource-snapshot restore SNAPSHOT_ID/NAME
2183+
```
2184+
2185+
The available fields for custom output format when listing snapshots are:
2186+
- id
2187+
- name
2188+
- description
2189+
- resource_type
2190+
- created_at
2191+
2192+
### Snapshot Schedules
2193+
2194+
Snapshot schedules allow you to automate the creation and management of snapshots of your resources.
2195+
2196+
> [!IMPORTANT]
2197+
> Currently, Snapshot Schedules only work with Instance Snapshots. More resource types will be supported in the future.
2198+
2199+
#### Creating a Schedule
2200+
2201+
To create a snapshot schedule:
2202+
2203+
```sh
2204+
$ civo snapshot schedule create --name SCHEDULE_NAME --cron CRON_EXPRESSION --instance-id="INSTANCE_ID1,INSTANCE_ID2,..." --max-snapshots MAX_SNAPSHOTS [flags]
2205+
2206+
Flags:
2207+
-d, --description string Description for the snapshot schedule
2208+
-v, --include-volumes Include attached volumes in snapshots
2209+
```
2210+
2211+
##### Listing Schedules
2212+
2213+
To view all available schedules:
2214+
2215+
```sh
2216+
$ civo snapshot schedule list
2217+
```
2218+
2219+
##### Viewing Schedule Details
2220+
2221+
To view the details of a specific schedule:
2222+
2223+
```sh
2224+
$ civo snapshot schedule show SCHEDULE_NAME/ID
2225+
```
2226+
2227+
##### Updating a Schedule
2228+
2229+
To update the details of a snapshot schedule:
2230+
2231+
```sh
2232+
$ civo snapshot schedule update SCHEDULE_NAME/ID [flags]
2233+
2234+
Flags:
2235+
-d, --description string New description for the snapshot schedule
2236+
-n, --name string New name for the snapshot schedule
2237+
-p, --paused string Whether to pause the snapshot schedule (use 'true' or 'false')
2238+
```
2239+
2240+
Example:
2241+
```sh
2242+
$ civo snapshot schedule update my-snapshot --name new-schedule-name --description "Updated schedule description" --paused true
2243+
```
2244+
2245+
##### Removing a Schedule
2246+
2247+
To delete a snapshot schedule:
2248+
2249+
```sh
2250+
$ civo snapshot schedule remove SCHEDULE_NAME/ID
2251+
```
2252+
21582253
## Teams
21592254
21602255
Teams are a grouping of users, each member of a team having one or more permissions, or roles. When a user logs in, they don't have to select which team to use - only which account they want to act within. The permissions available are the total set of permissions they have across the teams in that account, combined.
@@ -2280,35 +2375,6 @@ $ civo permissions ls
22802375
22812376
## Region
22822377
2283-
## Resource Snapshots
2284-
2285-
Resource snapshots allow you to manage snapshots of your resources. You can list, show details, update, delete, and restore snapshots using the following commands:
2286-
2287-
```sh
2288-
# List all resource snapshots
2289-
civo resource-snapshot list
2290-
2291-
# Show details of a specific snapshot
2292-
civo resource-snapshot show SNAPSHOT_ID/NAME
2293-
2294-
# Update a snapshot
2295-
civo resource-snapshot update SNAPSHOT_ID/NAME
2296-
2297-
# Delete a snapshot
2298-
civo resource-snapshot delete SNAPSHOT_ID/NAME
2299-
2300-
# Restore a snapshot
2301-
civo resource-snapshot restore SNAPSHOT_ID/NAME
2302-
```
2303-
2304-
The available fields for custom output format when listing snapshots are:
2305-
- id
2306-
- name
2307-
- description
2308-
- resource_type
2309-
- created_at
2310-
2311-
23122378
As Civo grows, more regions for your instances will become available. You can run `civo region ls` to list the regions available. Block storage (Volumes) is region-specific, so if you configure an instance in one region, any volumes you wish to attach to that instance would have to be in the same region.
23132379
23142380
#### List all region
@@ -2442,4 +2508,4 @@ The code is available as open source under the terms of the [Apache License 2.0]
24422508
24432509
<a href = "https://github.com/civo/cli/graphs/contributors">
24442510
<img src = "https://contrib.rocks/image?repo=civo/cli"/>
2445-
</a>
2511+
</a>

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/civo/cli/cmd/region"
2020
"github.com/civo/cli/cmd/resourcesnapshot"
2121
"github.com/civo/cli/cmd/size"
22+
"github.com/civo/cli/cmd/snapshot"
2223
"github.com/civo/cli/cmd/sshkey"
2324
"github.com/civo/cli/cmd/teams"
2425
"github.com/civo/cli/cmd/volume"
@@ -161,5 +162,5 @@ func init() {
161162
rootCmd.AddCommand(teams.TeamsCmd)
162163
rootCmd.AddCommand(volume.VolumeCmd)
163164
rootCmd.AddCommand(volumetype.VolumeTypeCmd)
164-
165+
rootCmd.AddCommand(snapshot.SnapshotCmd)
165166
}

cmd/snapshot/snapshot.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package snapshot
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// SnapshotCmd represents the snapshot command
8+
var SnapshotCmd = &cobra.Command{
9+
Use: "snapshot",
10+
Aliases: []string{"snapshots"},
11+
Short: "Manage snapshots and snapshot schedules",
12+
Long: `Create, list, update and delete snapshots and snapshot schedules`,
13+
}
14+
15+
func init() {
16+
SnapshotCmd.AddCommand(snapshotScheduleCmd)
17+
}

cmd/snapshot/snapshot_schedule.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package snapshot
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
// snapshotScheduleCmd represents the snapshot schedule command
8+
var snapshotScheduleCmd = &cobra.Command{
9+
Use: "schedule",
10+
Aliases: []string{"schedules"},
11+
Short: "Manage snapshot schedules",
12+
Long: `Create, list, update and delete snapshot schedules`,
13+
}
14+
15+
func init() {
16+
snapshotScheduleCmd.AddCommand(snapshotScheduleCreateCmd)
17+
snapshotScheduleCmd.AddCommand(snapshotScheduleListCmd)
18+
snapshotScheduleCmd.AddCommand(snapshotScheduleShowCmd)
19+
snapshotScheduleCmd.AddCommand(snapshotScheduleUpdateCmd)
20+
snapshotScheduleCmd.AddCommand(snapshotScheduleDeleteCmd)
21+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package snapshot
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/civo/civogo"
8+
"github.com/civo/cli/common"
9+
"github.com/civo/cli/config"
10+
"github.com/civo/cli/utility"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
var scheduleDescription, scheduleCron string
15+
var maxSnapshots int
16+
var instanceIDs []string
17+
var includeVolumes bool
18+
19+
var snapshotScheduleCreateCmd = &cobra.Command{
20+
Use: "create",
21+
Short: "Create a new snapshot schedule",
22+
Long: "Create a new snapshot schedule with a specified cron expression and max number of snapshots to retain.",
23+
Example: "civo snapshot schedule create --name my-schedule --cron '0 0 * * *' --instance-id instance-123 --max-snapshots 5",
24+
Run: func(cmd *cobra.Command, args []string) {
25+
utility.EnsureCurrentRegion()
26+
27+
client, err := config.CivoAPIClient()
28+
if common.RegionSet != "" {
29+
client.Region = common.RegionSet
30+
}
31+
if err != nil {
32+
utility.Error("Creating the connection to Civo's API failed with %s", err)
33+
os.Exit(1)
34+
}
35+
36+
config := &civogo.CreateSnapshotScheduleRequest{
37+
Name: scheduleName,
38+
Description: scheduleDescription,
39+
CronExpression: scheduleCron,
40+
Retention: civogo.SnapshotRetention{
41+
MaxSnapshots: maxSnapshots,
42+
},
43+
Instances: make([]civogo.CreateSnapshotInstance, 0),
44+
}
45+
46+
for _, instanceID := range instanceIDs {
47+
config.Instances = append(config.Instances, civogo.CreateSnapshotInstance{
48+
InstanceID: instanceID,
49+
IncludeVolumes: includeVolumes,
50+
})
51+
}
52+
53+
schedule, err := client.CreateSnapshotSchedule(config)
54+
if err != nil {
55+
utility.Error("Creating snapshot schedule failed with %s", err)
56+
os.Exit(1)
57+
}
58+
59+
ow := utility.NewOutputWriterWithMap(map[string]string{
60+
"id": schedule.ID,
61+
"name": schedule.Name,
62+
})
63+
64+
switch common.OutputFormat {
65+
case "json":
66+
ow.WriteSingleObjectJSON(common.PrettySet)
67+
case "custom":
68+
ow.WriteCustomOutput(common.OutputFields)
69+
default:
70+
fmt.Printf("Created snapshot schedule %s with ID %s\n", utility.Green(schedule.Name), utility.Green(schedule.ID))
71+
}
72+
},
73+
}
74+
75+
func init() {
76+
snapshotScheduleCreateCmd.Flags().StringVarP(&scheduleName, "name", "n", "", "Name for the snapshot schedule")
77+
snapshotScheduleCreateCmd.Flags().StringVarP(&scheduleDescription, "description", "d", "", "Description for the snapshot schedule")
78+
snapshotScheduleCreateCmd.Flags().StringVarP(&scheduleCron, "cron", "c", "", "Cron expression for the schedule (e.g., '0 0 * * *' for daily at midnight)")
79+
snapshotScheduleCreateCmd.Flags().IntVarP(&maxSnapshots, "max-snapshots", "m", 0, "Maximum number of snapshots to retain")
80+
snapshotScheduleCreateCmd.Flags().StringSliceVarP(&instanceIDs, "instance-id", "i", []string{}, "Instance IDs to snapshot (can be specified multiple times)")
81+
snapshotScheduleCreateCmd.Flags().BoolVarP(&includeVolumes, "include-volumes", "v", false, "Include attached volumes in snapshots")
82+
83+
_ = snapshotScheduleCreateCmd.MarkFlagRequired("name")
84+
_ = snapshotScheduleCreateCmd.MarkFlagRequired("cron")
85+
_ = snapshotScheduleCreateCmd.MarkFlagRequired("instance-id")
86+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package snapshot
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/civo/cli/common"
8+
"github.com/civo/cli/config"
9+
"github.com/civo/cli/utility"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var snapshotScheduleDeleteCmd = &cobra.Command{
14+
Use: "delete [ID/NAME]",
15+
Aliases: []string{"remove", "rm"},
16+
Short: "Delete a snapshot schedule",
17+
Long: "Delete a snapshot schedule by its ID or name",
18+
Example: "civo snapshot schedule delete my-schedule",
19+
Args: cobra.ExactArgs(1),
20+
Run: func(cmd *cobra.Command, args []string) {
21+
utility.EnsureCurrentRegion()
22+
23+
client, err := config.CivoAPIClient()
24+
if common.RegionSet != "" {
25+
client.Region = common.RegionSet
26+
}
27+
if err != nil {
28+
utility.Error("Creating the connection to Civo's API failed with %s", err)
29+
os.Exit(1)
30+
}
31+
32+
schedule, err := client.FindSnapshotSchedule(args[0])
33+
if err != nil {
34+
utility.Error("Finding snapshot schedule failed with %s", err)
35+
os.Exit(1)
36+
}
37+
38+
_, err = client.DeleteSnapshotSchedule(schedule.ID)
39+
if err != nil {
40+
utility.Error("Deleting snapshot schedule failed with %s", err)
41+
os.Exit(1)
42+
}
43+
44+
ow := utility.NewOutputWriterWithMap(map[string]string{
45+
"result": "success",
46+
"id": schedule.ID,
47+
"name": schedule.Name,
48+
})
49+
50+
switch common.OutputFormat {
51+
case "json":
52+
ow.WriteSingleObjectJSON(common.PrettySet)
53+
case "custom":
54+
ow.WriteCustomOutput(common.OutputFields)
55+
default:
56+
fmt.Printf("The snapshot schedule %s (%s) has been deleted\n", utility.Green(schedule.Name), utility.Green(schedule.ID))
57+
}
58+
},
59+
}

0 commit comments

Comments
 (0)