Skip to content

Commit

Permalink
Merge pull request #207 from Tinyblargon/CLI-Overhaul
Browse files Browse the repository at this point in the history
Cli overhaul: guest create,get,delete commands (partial basic tests)
  • Loading branch information
mleone87 committed Oct 20, 2022
2 parents 0effa94 + 0080463 commit 82d6490
Show file tree
Hide file tree
Showing 36 changed files with 333 additions and 124 deletions.
1 change: 1 addition & 0 deletions cli/command/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
_ "github.com/Telmate/proxmox-api-go/cli/command/create"
_ "github.com/Telmate/proxmox-api-go/cli/command/create/guest"
_ "github.com/Telmate/proxmox-api-go/cli/command/delete"
_ "github.com/Telmate/proxmox-api-go/cli/command/example"
_ "github.com/Telmate/proxmox-api-go/cli/command/get"
Expand Down
4 changes: 2 additions & 2 deletions cli/command/create/create-acmeaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ For config examples see "example acmeaccount"`,
if err != nil {
return
}
cli.PrintItemCreated(createCmd.OutOrStdout(), id, "AcmeAccount")
cli.PrintItemCreated(CreateCmd.OutOrStdout(), id, "AcmeAccount")
return
},
}

func init() {
createCmd.AddCommand(create_acmeaccountCmd)
CreateCmd.AddCommand(create_acmeaccountCmd)
}
8 changes: 4 additions & 4 deletions cli/command/create/create-pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

var create_poolCmd = &cobra.Command{
Use: "pool POOLID [COMMENT]" ,
Use: "pool POOLID [COMMENT]",
Short: "Creates a new pool",
RunE: func(cmd *cobra.Command, args []string) (err error) {
id := cli.ValidateIDset(args, 0 ,"PoolID")
id := cli.ValidateIDset(args, 0, "PoolID")
var comment string
if len(args) > 1 {
comment = args[1]
Expand All @@ -19,11 +19,11 @@ var create_poolCmd = &cobra.Command{
if err != nil {
return
}
cli.PrintItemCreated(createCmd.OutOrStdout() ,id, "Pool")
cli.PrintItemCreated(CreateCmd.OutOrStdout(), id, "Pool")
return
},
}

func init() {
createCmd.AddCommand(create_poolCmd)
CreateCmd.AddCommand(create_poolCmd)
}
4 changes: 2 additions & 2 deletions cli/command/create/create-storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ For config examples see "example storage"`,
if err != nil {
return
}
cli.PrintItemCreated(createCmd.OutOrStdout(), id, "Storage")
cli.PrintItemCreated(CreateCmd.OutOrStdout(), id, "Storage")
return
},
}

func init() {
createCmd.AddCommand(create_storageCmd)
CreateCmd.AddCommand(create_storageCmd)
}
4 changes: 2 additions & 2 deletions cli/command/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"github.com/spf13/cobra"
)

var createCmd = &cobra.Command{
var CreateCmd = &cobra.Command{
Use: "create",
Short: "With this command you can create new items in proxmox",
}

func init() {
cli.RootCmd.AddCommand(createCmd)
cli.RootCmd.AddCommand(CreateCmd)
}
20 changes: 20 additions & 0 deletions cli/command/create/guest/create-guest-lxc.go
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)
}
20 changes: 20 additions & 0 deletions cli/command/create/guest/create-guest-qemu.go
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)
}
48 changes: 48 additions & 0 deletions cli/command/create/guest/create-guest.go
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
}
2 changes: 1 addition & 1 deletion cli/command/delete/delete-acmeaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var delete_acmeaccountCmd = &cobra.Command{
Use: "acmeaccount ACMEACCOUNTID",
Short: "Deletes the Speciefied AcmeAccount",
RunE: func(cmd *cobra.Command, args []string) error {
return DeleteID(args, "AcmeAccount")
return deleteID(args, "AcmeAccount")
},
}

Expand Down
33 changes: 33 additions & 0 deletions cli/command/delete/delete-guest.go
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)
}
2 changes: 1 addition & 1 deletion cli/command/delete/delete-metricserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var delete_metricserverCmd = &cobra.Command{
Use: "metricserver METRICSID",
Short: "Deletes the speciefied MetricServer",
RunE: func(cmd *cobra.Command, args []string) error {
return DeleteID(args, "MetricServer")
return deleteID(args, "MetricServer")
},
}

Expand Down
5 changes: 2 additions & 3 deletions cli/command/delete/delete-pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
var delete_poolCmd = &cobra.Command{
Use: "pool POOLID",
Short: "Deletes the Speciefied pool",
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = DeleteID(args, "Pool")
return
RunE: func(cmd *cobra.Command, args []string) error {
return deleteID(args, "Pool")
},
}

Expand Down
2 changes: 1 addition & 1 deletion cli/command/delete/delete-storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var delete_storageCmd = &cobra.Command{
Use: "storage STORAGEID",
Short: "Deletes the speciefied Storage",
RunE: func(cmd *cobra.Command, args []string) error {
return DeleteID(args, "Storage")
return deleteID(args, "Storage")
},
}

Expand Down
5 changes: 2 additions & 3 deletions cli/command/delete/delete-user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
var delete_userCmd = &cobra.Command{
Use: "user USERID",
Short: "Deletes the speciefied User",
RunE: func(cmd *cobra.Command, args []string) (err error) {
err = DeleteID(args, "User")
return
RunE: func(cmd *cobra.Command, args []string) error {
return deleteID(args, "User")
},
}

Expand Down
2 changes: 1 addition & 1 deletion cli/command/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func init() {
cli.RootCmd.AddCommand(deleteCmd)
}

func DeleteID(args []string, IDtype string) (err error) {
func deleteID(args []string, IDtype string) (err error) {
var exitStatus string
id := cli.ValidateIDset(args, 0, IDtype+"ID")
c := cli.NewClient()
Expand Down
2 changes: 1 addition & 1 deletion cli/command/get/get-acmeaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var get_acmeaccountCmd = &cobra.Command{
Use: "acmeaccount ACMEACCOUNTID",
Short: "Gets the configuration of the specified AcmeAccount",
RunE: func(cmd *cobra.Command, args []string) (err error) {
return GetConfig(args, "AcmeAccount")
return getConfig(args, "AcmeAccount")
},
}

Expand Down
38 changes: 38 additions & 0 deletions cli/command/get/get-guest.go
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)
}
2 changes: 1 addition & 1 deletion cli/command/get/get-metricserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var get_metricserverCmd = &cobra.Command{
Use: "metricserver METRICSID",
Short: "Gets the configuration of the specified MetricServer",
RunE: func(cmd *cobra.Command, args []string) error {
return GetConfig(args, "MetricServer")
return getConfig(args, "MetricServer")
},
}

Expand Down
19 changes: 2 additions & 17 deletions cli/command/get/get-pool.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
package get

import (
"encoding/json"
"fmt"

"github.com/Telmate/proxmox-api-go/cli"
"github.com/spf13/cobra"
)

var get_poolCmd = &cobra.Command{
Use: "pool POOLID",
Short: "Gets the configuration of the specified Pool",
RunE: func(cmd *cobra.Command, args []string) (err error) {
id := cli.ValidateIDset(args, 0, "PoolID")
c := cli.NewClient()
poolinfo, err := c.GetPoolInfo(id)
if err != nil {
return
}
poolList, err := json.Marshal(poolinfo)
if err != nil {
return
}
fmt.Fprintln(GetCmd.OutOrStdout(), string(poolList))
return
RunE: func(cmd *cobra.Command, args []string) error {
return getConfig(args, "Pool")
},
}

Expand Down
2 changes: 1 addition & 1 deletion cli/command/get/get-storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var get_storageCmd = &cobra.Command{
Use: "storage",
Short: "Gets the configuration of the specified Storage backend",
RunE: func(cmd *cobra.Command, args []string) error {
return GetConfig(args, "Storage")
return getConfig(args, "Storage")
},
}

Expand Down
2 changes: 1 addition & 1 deletion cli/command/get/get-user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var get_userCmd = &cobra.Command{
Use: "user USERID",
Short: "Gets the configuration of the specified User",
RunE: func(cmd *cobra.Command, args []string) error {
return GetConfig(args, "User")
return getConfig(args, "User")
},
}

Expand Down
4 changes: 3 additions & 1 deletion cli/command/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func init() {
cli.RootCmd.AddCommand(GetCmd)
}

func GetConfig(args []string, IDtype string) (err error) {
func getConfig(args []string, IDtype string) (err error) {
id := cli.ValidateIDset(args, 0, IDtype+"ID")
c := cli.NewClient()
var config interface{}
Expand All @@ -24,6 +24,8 @@ func GetConfig(args []string, IDtype string) (err error) {
config, err = proxmox.NewConfigAcmeAccountFromApi(id, c)
case "MetricServer":
config, err = proxmox.NewConfigMetricsFromApi(id, c)
case "Pool":
config, err = c.GetPoolInfo(id)
case "Storage":
config, err = proxmox.NewConfigStorageFromApi(id, c)
case "User":
Expand Down
3 changes: 1 addition & 2 deletions cli/command/list/list-acmeaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ var list_acmeaccountsCmd = &cobra.Command{
Use: "acmeaccounts",
Short: "Prints a list of AcmeAccounts in raw json format",
Run: func(cmd *cobra.Command, args []string) {
ListRaw("AcmeAccounts")
listRaw("AcmeAccounts")
},
}


func init() {
listCmd.AddCommand(list_acmeaccountsCmd)
}
Loading

0 comments on commit 82d6490

Please sign in to comment.