Skip to content

Commit

Permalink
Merge pull request Telmate#335 from Tinyblargon/Overhaul-Pool
Browse files Browse the repository at this point in the history
Overhaul: pool
  • Loading branch information
Tinyblargon authored May 25, 2024
2 parents 30cfc1a + 2b79dfc commit 6676d89
Show file tree
Hide file tree
Showing 18 changed files with 1,043 additions and 41 deletions.
10 changes: 7 additions & 3 deletions cli/command/create/create-pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package create

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

Expand All @@ -11,12 +12,15 @@ var create_poolCmd = &cobra.Command{
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
id := cli.RequiredIDset(args, 0, "PoolID")
var comment string
var comment *string
if len(args) > 1 {
comment = args[1]
comment = &args[1]
}
c := cli.NewClient()
err = c.CreatePool(id, comment)
err = proxmox.ConfigPool{
Name: proxmox.PoolName(id),
Comment: comment,
}.Create(c)
if err != nil {
return
}
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 @@ -29,7 +29,7 @@ func deleteID(args []string, IDtype string) (err error) {
case "MetricServer":
err = c.DeleteMetricServer(id)
case "Pool":
err = c.DeletePool(id)
err = proxmox.PoolName(id).Delete(c)
case "Storage":
err = c.DeleteStorage(id)
case "User":
Expand Down
12 changes: 10 additions & 2 deletions cli/command/list/list-pools.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package list

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

var list_poolsCmd = &cobra.Command{
Use: "pools",
Short: "Prints a list of Pools in raw json format",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
listRaw("Pools")
RunE: func(cmd *cobra.Command, args []string) (err error) {
c := cli.NewClient()
pools, err := proxmox.ListPools(c)
if err != nil {
return
}
cli.PrintFormattedJson(listCmd.OutOrStdout(), pools)
return
},
}

Expand Down
2 changes: 0 additions & 2 deletions cli/command/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ func listRaw(IDtype string) {
list, err = c.GetMetricsServerList()
case "Nodes":
list, err = c.GetNodeList()
case "Pools":
list, err = c.GetPoolList()
case "Storages":
list, err = c.GetStorageList()
}
Expand Down
10 changes: 7 additions & 3 deletions cli/command/update/update-poolcomment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package update

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

Expand All @@ -10,13 +11,16 @@ var update_poolCmd = &cobra.Command{
Short: "Updates the comment on the specified pool",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
var comment string
id := cli.RequiredIDset(args, 0, "PoolID")
var comment *string
if len(args) > 1 {
comment = args[1]
comment = &args[1]
}
c := cli.NewClient()
err = c.UpdatePoolComment(id, comment)
err = proxmox.ConfigPool{
Name: proxmox.PoolName(id),
Comment: comment,
}.Update(c)
if err != nil {
return
}
Expand Down
17 changes: 10 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func main() {

//Pool
case "getPoolList":
pools, err := c.GetPoolList()
pools, err := proxmox.ListPoolsWithComments(c)
if err != nil {
log.Printf("Error listing pools %+v\n", err)
os.Exit(1)
Expand Down Expand Up @@ -486,8 +486,10 @@ func main() {
comment = flag.Args()[2]
}

err := c.CreatePool(poolid, comment)
failError(err)
failError(proxmox.ConfigPool{
Name: proxmox.PoolName(poolid),
Comment: &comment,
}.Create(c))
fmt.Printf("Pool %s created\n", poolid)

case "deletePool":
Expand All @@ -497,8 +499,7 @@ func main() {
}
poolid := flag.Args()[1]

err := c.DeletePool(poolid)
failError(err)
failError(proxmox.PoolName(poolid).Delete(c))
fmt.Printf("Pool %s removed\n", poolid)

case "updatePoolComment":
Expand All @@ -510,8 +511,10 @@ func main() {
poolid := flag.Args()[1]
comment := flag.Args()[2]

err := c.UpdatePoolComment(poolid, comment)
failError(err)
failError(proxmox.ConfigPool{
Name: proxmox.PoolName(poolid),
Comment: &comment,
}.Update(c))
fmt.Printf("Pool %s updated\n", poolid)

//Users
Expand Down
9 changes: 9 additions & 0 deletions proxmox/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ func (c *Client) Login(username string, password string, otp string) (err error)

// Updates the client's cached version information and returns it.
func (c *Client) GetVersion() (version Version, err error) {
if c == nil {
return Version{}, errors.New(Client_Error_Nil)
}
params, err := c.GetItemConfigMapStringInterface("/version", "version", "data")
version = version.mapToSDK(params)
cachedVersion := Version{ // clones the struct
Expand Down Expand Up @@ -1491,6 +1494,7 @@ func getStorageAndVolumeName(
return storageName, volumeName
}

// Deprecated: use ConfigQemu.Update() instead
func (c *Client) UpdateVMPool(vmr *VmRef, pool string) (exitStatus interface{}, err error) {
// Same pool
if vmr.pool == pool {
Expand Down Expand Up @@ -1628,28 +1632,33 @@ func (c *Client) UpdateVMHA(vmr *VmRef, haState string, haGroup string) (exitSta
return
}

// Deprecated: use ListPoolsWithComments() instead
func (c *Client) GetPoolList() (pools map[string]interface{}, err error) {
return c.GetItemList("/pools")
}

// TODO: implement replacement
func (c *Client) GetPoolInfo(poolid string) (poolInfo map[string]interface{}, err error) {
return c.GetItemConfigMapStringInterface("/pools/"+poolid, "pool", "CONFIG")
}

// Deprecated: use ConfigPool.Create() instead
func (c *Client) CreatePool(poolid string, comment string) error {
return c.Post(map[string]interface{}{
"poolid": poolid,
"comment": comment,
}, "/pools")
}

// Deprecated: use ConfigPool.Update() instead
func (c *Client) UpdatePoolComment(poolid string, comment string) error {
return c.Put(map[string]interface{}{
"poolid": poolid,
"comment": comment,
}, "/pools/"+poolid)
}

// Deprecated: use PoolName.Delete() instead
func (c *Client) DeletePool(poolid string) error {
return c.Delete("/pools/" + poolid)
}
Expand Down
38 changes: 36 additions & 2 deletions proxmox/config_guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type GuestResource struct {
Name string `json:"name"` // TODO custom type
NetworkIn uint `json:"network_in"`
NetworkOut uint `json:"network_out"`
Node string `json:"node"` // TODO custom type
Pool string `json:"pool"` // TODO custom type
Node string `json:"node"` // TODO custom type
Pool PoolName `json:"pool"`
Status string `json:"status"` // TODO custom type?
Tags []Tag `json:"tags"`
Template bool `json:"template"`
Expand Down Expand Up @@ -177,6 +177,40 @@ func GuestReboot(vmr *VmRef, client *Client) (err error) {
return
}

func guestSetPool_Unsafe(c *Client, guestID uint, newPool PoolName, currentPool *PoolName, version Version) (err error) {
if newPool == "" {
if *currentPool != "" { // leave pool
if err = (*currentPool).removeGuests_Unsafe(c, []uint{guestID}, version); err != nil {
return
}
}
} else {
if *currentPool == "" { // join pool
if version.Smaller(Version{8, 0, 0}) {
if err = newPool.addGuests_UnsafeV7(c, []uint{guestID}); err != nil {
return
}
} else {
newPool.addGuests_UnsafeV8(c, []uint{guestID})
}
} else if newPool != *currentPool { // change pool
if version.Smaller(Version{8, 0, 0}) {
if err = (*currentPool).removeGuests_Unsafe(c, []uint{guestID}, version); err != nil {
return
}
if err = newPool.addGuests_UnsafeV7(c, []uint{guestID}); err != nil {
return
}
} else {
if err = newPool.addGuests_UnsafeV8(c, []uint{guestID}); err != nil {
return
}
}
}
}
return
}

func GuestShutdown(vmr *VmRef, client *Client, force bool) (err error) {
if err = client.CheckVmRef(vmr); err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion proxmox/config_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ConfigLxc struct {
OnBoot bool `json:"onboot"`
OsType string `json:"ostype,omitempty"`
Password string `json:"password,omitempty"`
Pool string `json:"pool,omitempty"`
Pool *PoolName `json:"pool,omitempty"`
Protection bool `json:"protection"`
Restore bool `json:"restore,omitempty"`
RootFs QemuDevice `json:"rootfs,omitempty"`
Expand Down
Loading

0 comments on commit 6676d89

Please sign in to comment.