forked from Telmate/proxmox-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Telmate#212 from Tinyblargon/Feature-Snapshot
Cli overhaul: snapshot
- Loading branch information
Showing
24 changed files
with
735 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package create | ||
|
||
import ( | ||
"github.com/Telmate/proxmox-api-go/cli" | ||
"github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
// flag needs to be reset, as this value will persist during tests | ||
memory bool | ||
create_snapshotCmd = &cobra.Command{ | ||
Use: "snapshot GUESTID SNAPSHOTNAME [DESCRIPTION]", | ||
Short: "Creates a new snapshot of the specefied guest", | ||
TraverseChildren: true, | ||
Args: cobra.RangeArgs(2, 3), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
id := cli.ValidateIntIDset(args, "GuestID") | ||
snapName := cli.RequiredIDset(args, 1, "SnapshotName") | ||
config := proxmox.ConfigSnapshot{ | ||
Name: snapName, | ||
Description: cli.OptionalIDset(args, 2), | ||
VmState: memory, | ||
} | ||
memory = false | ||
err = config.CreateSnapshot(uint(id), cli.NewClient()) | ||
if err != nil { | ||
return | ||
} | ||
cli.PrintItemCreated(CreateCmd.OutOrStdout(), snapName, "Snapshot") | ||
return | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
CreateCmd.AddCommand(create_snapshotCmd) | ||
create_snapshotCmd.Flags().BoolVar(&memory, "memory", false, "Snapshot memory") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package delete | ||
|
||
import ( | ||
"github.com/Telmate/proxmox-api-go/cli" | ||
"github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
delete_snapshotCmd = &cobra.Command{ | ||
Use: "snapshot GUESTID SNAPSHOTNAME", | ||
Short: "Deletes the Speciefied snapshot", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
id := cli.ValidateIntIDset(args, "GuestID") | ||
snapName := cli.RequiredIDset(args, 1, "SnapshotName") | ||
c := cli.NewClient() | ||
_, err = c.DeleteSnapshot(proxmox.NewVmRef(id), snapName) | ||
if err != nil { | ||
return | ||
} | ||
cli.PrintItemDeleted(deleteCmd.OutOrStdout(), snapName, "Snapshot") | ||
return | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
deleteCmd.AddCommand(delete_snapshotCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package guest | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Telmate/proxmox-api-go/cli" | ||
"github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var guest_rollbackCmd = &cobra.Command{ | ||
Use: "rollback GUESTID SNAPSHOT", | ||
Short: "Shuts the speciefid guest down", | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
vmr := proxmox.NewVmRef(cli.ValidateIntIDset(args, "GuestID")) | ||
snapName := cli.RequiredIDset(args, 1, "SnapshotName") | ||
c := cli.NewClient() | ||
_, err = c.RollbackSnapshot(vmr, snapName) | ||
if err == nil { | ||
fmt.Fprintf(GuestCmd.OutOrStdout(), "Guest with id (%d) has been rolled back to snapshot (%s)\n", vmr.VmId(), snapName) | ||
} | ||
return | ||
}, | ||
} | ||
|
||
func init() { | ||
GuestCmd.AddCommand(guest_rollbackCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,44 @@ | ||
package list | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Telmate/proxmox-api-go/cli" | ||
"github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var list_snapshotsCmd = &cobra.Command{ | ||
Use: "snapshots GuestID", | ||
Short: "Prints a list of QemuSnapshots in raw json format", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
id := cli.ValidateExistinGuestID(args, 0) | ||
c := cli.NewClient() | ||
vmr := proxmox.NewVmRef(id) | ||
_, err := c.GetVmInfo(vmr) | ||
cli.LogFatalError(err) | ||
jbody, _, err := c.ListQemuSnapshot(vmr) | ||
cli.LogFatalError(err) | ||
temp := jbody["data"].([]interface{}) | ||
if len(temp) == 1 { | ||
fmt.Printf("Guest with ID (%d) has no snapshots",id) | ||
} else { | ||
for _, e := range temp { | ||
snapshotName := e.(map[string]interface{})["name"].(string) | ||
if snapshotName != "current" { | ||
fmt.Println(snapshotName) | ||
} | ||
var ( | ||
// flag needs to be reset, as this value will persist during tests | ||
noTree bool | ||
list_snapshotsCmd = &cobra.Command{ | ||
Use: "snapshots GuestID", | ||
Short: "Prints a list of QemuSnapshots in json format", | ||
TraverseChildren: true, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
id := cli.ValidateExistinGuestID(args, 0) | ||
jbody, err := cli.NewClient().ListSnapshots(proxmox.NewVmRef(id)) | ||
if err != nil { | ||
noTree = false | ||
return | ||
} | ||
} | ||
}, | ||
} | ||
var list []*proxmox.Snapshot | ||
if noTree { | ||
noTree = false | ||
list = proxmox.FormatSnapshotsList(jbody) | ||
} else { | ||
list = proxmox.FormatSnapshotsTree(jbody) | ||
} | ||
if len(list) == 0 { | ||
listCmd.Printf("Guest with ID (%d) has no snapshots", id) | ||
} else { | ||
cli.PrintFormattedJson(listCmd.OutOrStdout(), list) | ||
} | ||
return | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
listCmd.AddCommand(list_snapshotsCmd) | ||
list_snapshotsCmd.Flags().BoolVar(&noTree, "no-tree", false, "Format output as list instead of a tree.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package update | ||
|
||
import ( | ||
"github.com/Telmate/proxmox-api-go/cli" | ||
"github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var update_snapshotCmd = &cobra.Command{ | ||
Use: "snapshot GUESTID SNAPSHOTNAME [DESCRIPTION]", | ||
Short: "Updates the description on the speciefied snapshot", | ||
Args: cobra.RangeArgs(2, 3), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
id := cli.ValidateIntIDset(args, "GuestID") | ||
snapName := cli.RequiredIDset(args, 1, "SnapshotName") | ||
des := cli.OptionalIDset(args, 2) | ||
err = cli.NewClient().UpdateSnapshotDescription(proxmox.NewVmRef(id), snapName, des) | ||
if err != nil { | ||
return | ||
} | ||
cli.PrintItemUpdated(updateCmd.OutOrStdout(), snapName, "Snapshot") | ||
return | ||
}, | ||
} | ||
|
||
func init() { | ||
updateCmd.AddCommand(update_snapshotCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.