Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul: pool #335

Merged
merged 23 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -19,7 +19,7 @@
"github.com/joho/godotenv"
)

type AppConfig struct {

Check failure on line 22 in main.go

View workflow job for this annotation

GitHub Actions / audit

exported type AppConfig should have comment or be unexported
APIURL string
HTTPHeaders string
User string
Expand Down Expand Up @@ -450,7 +450,7 @@

//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 @@
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 @@
}
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,14 +511,16 @@
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
case "getUser":
var config interface{}
userId, err := proxmox.NewUserID(flag.Args()[1])

Check failure on line 523 in main.go

View workflow job for this annotation

GitHub Actions / audit

var userId should be userID
failError(err)
config, err = proxmox.NewConfigUserFromApi(userId, c)
failError(err)
Expand All @@ -540,7 +543,7 @@
log.Printf("Error: Userid and Password required")
os.Exit(1)
}
userId, err := proxmox.NewUserID(flag.Args()[1])

Check failure on line 546 in main.go

View workflow job for this annotation

GitHub Actions / audit

var userId should be userID
failError(err)
err = proxmox.ConfigUser{
Password: proxmox.UserPassword(flag.Args()[2]),
Expand All @@ -553,7 +556,7 @@
var password proxmox.UserPassword
config, err := proxmox.NewConfigUserFromJson(GetConfig(*fConfigFile))
failError(err)
userId, err := proxmox.NewUserID(flag.Args()[1])

Check failure on line 559 in main.go

View workflow job for this annotation

GitHub Actions / audit

var userId should be userID
failError(err)
if len(flag.Args()) > 2 {
password = proxmox.UserPassword(flag.Args()[2])
Expand All @@ -566,7 +569,7 @@
log.Printf("Error: userId required")
os.Exit(1)
}
userId, err := proxmox.NewUserID(flag.Args()[1])

Check failure on line 572 in main.go

View workflow job for this annotation

GitHub Actions / audit

var userId should be userID
failError(err)
err = proxmox.ConfigUser{User: userId}.DeleteUser(c)
failError(err)
Expand Down Expand Up @@ -976,9 +979,9 @@
failError(fmt.Errorf("error: invoke with <vmID> <node> <diskID [<forceRemoval: false|true>]"))
}

vmIdUnparsed := flag.Args()[1]

Check failure on line 982 in main.go

View workflow job for this annotation

GitHub Actions / audit

var vmIdUnparsed should be vmIDUnparsed
node := flag.Args()[2]
vmId, err := strconv.Atoi(vmIdUnparsed)

Check failure on line 984 in main.go

View workflow job for this annotation

GitHub Actions / audit

var vmId should be vmID
if err != nil {
failError(fmt.Errorf("failed to convert vmId: %s to a string, error: %+v", vmIdUnparsed, err))
}
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
Loading