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#207 from Tinyblargon/CLI-Overhaul
Cli overhaul: guest create,get,delete commands (partial basic tests)
- Loading branch information
Showing
36 changed files
with
333 additions
and
124 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
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,20 @@ | ||
package guest | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var guest_lxcCmd = &cobra.Command{ | ||
Use: "lxc GUESTID NODEID", | ||
Short: "Creates a new Guest System of the type Lxc on the specified Node", | ||
Long: `Creates a new Guest System of the type Lxc on the specified Node. | ||
The config can be set with the --file flag or piped from stdin. | ||
For config examples see "example guest lxc"`, | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
return createGuest(args, "LxcGuest") | ||
}, | ||
} | ||
|
||
func init() { | ||
guestCmd.AddCommand(guest_lxcCmd) | ||
} |
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,20 @@ | ||
package guest | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var guest_qemuCmd = &cobra.Command{ | ||
Use: "qemu GUESTID NODEID", | ||
Short: "Creates a new Guest System of the type Qemu on the specified Node", | ||
Long: `Creates a new Guest System of the type Qemu on the specified Node. | ||
The config can be set with the --file flag or piped from stdin. | ||
For config examples see "example guest qemu"`, | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
return createGuest(args, "QemuGuest") | ||
}, | ||
} | ||
|
||
func init() { | ||
guestCmd.AddCommand(guest_qemuCmd) | ||
} |
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,48 @@ | ||
package guest | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/Telmate/proxmox-api-go/cli" | ||
"github.com/Telmate/proxmox-api-go/cli/command/create" | ||
"github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var guestCmd = &cobra.Command{ | ||
Use: "guest", | ||
Short: "With this command you can create new Lxc containers and Qemu virtual machines in proxmox", | ||
} | ||
|
||
func init() { | ||
create.CreateCmd.AddCommand(guestCmd) | ||
} | ||
|
||
func createGuest(args []string, IDtype string) (err error) { | ||
id := cli.ValidateIntIDset(args, IDtype+"ID") | ||
node := cli.ValidateIDset(args, 1, "NodeID") | ||
vmr := proxmox.NewVmRef(id) | ||
vmr.SetNode(node) | ||
c := cli.NewClient() | ||
switch IDtype { | ||
case "LxcGuest": | ||
var config proxmox.ConfigLxc | ||
config, err = proxmox.NewConfigLxcFromJson(cli.NewConfig()) | ||
if err != nil { | ||
return | ||
} | ||
err = config.CreateLxc(vmr, c) | ||
case "QemuGuest": | ||
var config *proxmox.ConfigQemu | ||
config, err = proxmox.NewConfigQemuFromJson(cli.NewConfig()) | ||
if err != nil { | ||
return | ||
} | ||
err = config.CreateVm(vmr, c) | ||
} | ||
if err != nil { | ||
return | ||
} | ||
cli.PrintItemCreated(guestCmd.OutOrStdout(), strconv.Itoa(id), IDtype) | ||
return | ||
} |
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,33 @@ | ||
package delete | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/Telmate/proxmox-api-go/cli" | ||
"github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var delete_guestCmd = &cobra.Command{ | ||
Use: "guest GUESTID", | ||
Short: "Deletes the Speciefied Guest", | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
id := cli.ValidateIntIDset(args, "GuestID") | ||
vmr := proxmox.NewVmRef(id) | ||
c := cli.NewClient() | ||
_, err = c.StopVm(vmr) | ||
if err != nil { | ||
return | ||
} | ||
_, err = c.DeleteVm(vmr) | ||
if err != nil { | ||
return | ||
} | ||
cli.PrintItemDeleted(deleteCmd.OutOrStdout(), strconv.Itoa(id), "GuestID") | ||
return | ||
}, | ||
} | ||
|
||
func init() { | ||
deleteCmd.AddCommand(delete_guestCmd) | ||
} |
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
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,38 @@ | ||
package get | ||
|
||
import ( | ||
"github.com/Telmate/proxmox-api-go/cli" | ||
"github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var get_guestCmd = &cobra.Command{ | ||
Use: "guest GUESTID", | ||
Short: "Gets the configuration of the specified guest", | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
id := cli.ValidateIntIDset(args, "GuestID") | ||
vmr := proxmox.NewVmRef(id) | ||
c := cli.NewClient() | ||
err = c.CheckVmRef(vmr) | ||
if err != nil { | ||
return | ||
} | ||
vmType := vmr.GetVmType() | ||
var config interface{} | ||
switch vmType { | ||
case "qemu": | ||
config, err = proxmox.NewConfigQemuFromApi(vmr, c) | ||
case "lxc": | ||
config, err = proxmox.NewConfigLxcFromApi(vmr, c) | ||
} | ||
if err != nil { | ||
return | ||
} | ||
cli.PrintFormattedJson(GetCmd.OutOrStdout(), config) | ||
return | ||
}, | ||
} | ||
|
||
func init() { | ||
GetCmd.AddCommand(get_guestCmd) | ||
} |
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
Oops, something went wrong.