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

feat: context for all api related calls #387

Merged
merged 1 commit into from
Dec 5, 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
29 changes: 25 additions & 4 deletions cli/cobra.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package cli

import (
"context"
"crypto/tls"
"fmt"
"io"
"os"
"os/signal"
"regexp"
"syscall"

"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
Expand All @@ -25,6 +28,24 @@ func init() {
RootCmd.PersistentFlags().StringP("proxyurl", "p", "", "proxy url to connect to")
}

func Context() context.Context {
ctx, cancel := context.WithCancel(context.Background())

// Channel to catch OS signals
signalChan := make(chan os.Signal, 1)

// Notify signalChan when SIGINT or SIGTERM is received
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)

// Goroutine to handle signal
go func() {
defer signal.Stop(signalChan) // Cleanup when done
<-signalChan // Wait for a signal
cancel() // Cancel the context
}()
return ctx
}

func Execute() (err error) {
if err = RootCmd.Execute(); err != nil {
return
Expand All @@ -33,12 +54,12 @@ func Execute() (err error) {
}

func NewClient() (c *proxmox.Client) {
c, err := Client("", "", "", "", "")
c, err := Client(Context(), "", "", "", "", "")
LogFatalError(err)
return
}

func Client(apiUrl, userID, password, otp string, http_headers string) (c *proxmox.Client, err error) {
func Client(ctx context.Context, apiUrl, userID, password, otp string, http_headers string) (c *proxmox.Client, err error) {
insecure, _ := RootCmd.Flags().GetBool("insecure")
timeout, _ := RootCmd.Flags().GetInt("timeout")
proxyUrl, _ := RootCmd.Flags().GetString("proxyurl")
Expand Down Expand Up @@ -67,12 +88,12 @@ func Client(apiUrl, userID, password, otp string, http_headers string) (c *proxm
if userRequiresAPIToken(userID) {
c.SetAPIToken(userID, password)
// As test, get the version of the server
_, err = c.GetVersion()
_, err = c.GetVersion(Context())
if err != nil {
err = fmt.Errorf("login error: %s", err)
}
} else {
err = c.Login(userID, password, otp)
err = c.Login(ctx, userID, password, otp)
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/content/iso/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var iso_downloadCmd = &cobra.Command{
if err != nil {
return
}
err = proxmox.DownloadIsoFromUrl(c, config)
err = proxmox.DownloadIsoFromUrl(cli.Context(), c, config)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/content/template/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var template_downloadCmd = &cobra.Command{
if err != nil {
return
}
err = proxmox.DownloadLxcTemplate(c, config)
err = proxmox.DownloadLxcTemplate(cli.Context(), c, config)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/content/template/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var template_listCmd = &cobra.Command{
Short: "Prints a list of all LXC templates available for download in raw json format",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
templates, err := proxmox.ListTemplates(cli.NewClient(), args[0])
templates, err := proxmox.ListTemplates(cli.Context(), cli.NewClient(), args[0])
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/create/create-acmeaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ For config examples see "example acmeaccount"`,
return
}
c := cli.NewClient()
err = config.CreateAcmeAccount(id, c)
err = config.CreateAcmeAccount(cli.Context(), id, c)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/create/create-pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var create_poolCmd = &cobra.Command{
err = proxmox.ConfigPool{
Name: proxmox.PoolName(id),
Comment: comment,
}.Create(c)
}.Create(cli.Context(), c)
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions cli/command/create/create-snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ var (
memory = false
client := cli.NewClient()
vmr := proxmox.NewVmRef(id)
_, err = client.GetVmInfo(vmr)
_, err = client.GetVmInfo(cli.Context(), vmr)
if err != nil {
return
}
err = config.Create(client, vmr)
err = config.Create(cli.Context(), client, vmr)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/create/create-storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ For config examples see "example storage"`,
return
}
c := cli.NewClient()
err = config.CreateWithValidate(id, c)
err = config.CreateWithValidate(cli.Context(), id, c)
if err != nil {
return
}
Expand Down
3 changes: 2 additions & 1 deletion cli/command/create/guest/create-guest-lxc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package guest

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

Expand All @@ -12,7 +13,7 @@ The config can be set with the --file flag or piped from stdin.
For config examples see "example guest lxc"`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
return createGuest(args, "LxcGuest")
return createGuest(cli.Context(), args, "LxcGuest")
},
}

Expand Down
3 changes: 2 additions & 1 deletion cli/command/create/guest/create-guest-qemu.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package guest

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

Expand All @@ -12,7 +13,7 @@ var guest_qemuCmd = &cobra.Command{
For config examples see "example guest qemu"`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
return createGuest(args, "QemuGuest")
return createGuest(cli.Context(), args, "QemuGuest")
},
}

Expand Down
7 changes: 4 additions & 3 deletions cli/command/create/guest/create-guest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package guest

import (
"context"
"strconv"

"github.com/Telmate/proxmox-api-go/cli"
Expand All @@ -19,7 +20,7 @@ func init() {
create.CreateCmd.AddCommand(guestCmd)
}

func createGuest(args []string, IDtype string) (err error) {
func createGuest(ctx context.Context, args []string, IDtype string) (err error) {
id := cli.ValidateIntIDset(args, IDtype+"ID")
node := cli.RequiredIDset(args, 1, "NodeID")
vmr := proxmox.NewVmRef(id)
Expand All @@ -32,7 +33,7 @@ func createGuest(args []string, IDtype string) (err error) {
if err != nil {
return
}
err = config.CreateLxc(vmr, c)
err = config.CreateLxc(ctx, vmr, c)
case "QemuGuest":
// var config *proxmox.ConfigQemu
// config, err = proxmox.NewConfigQemuFromJson(cli.NewConfig())
Expand All @@ -54,7 +55,7 @@ func createGuest(args []string, IDtype string) (err error) {
Units: util.Pointer(proxmox.CpuUnits(1024)),
VirtualCores: util.Pointer(proxmox.CpuVirtualCores(2)),
},
}.Update(true, vmr, c)
}.Update(ctx, true, vmr, c)
// err = config.Create(vmr, c)
}
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cli/command/delete/delete-acmeaccount.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package delete

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

Expand All @@ -9,7 +10,7 @@ var delete_acmeaccountCmd = &cobra.Command{
Short: "Deletes the Specified AcmeAccount",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return deleteID(args, "AcmeAccount")
return deleteID(cli.Context(), args, "AcmeAccount")
},
}

Expand Down
2 changes: 1 addition & 1 deletion cli/command/delete/delete-file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var delete_fileCmd = &cobra.Command{
if Type.Validate() != nil {
return
}
err = proxmox.DeleteFile(c, args[0], proxmox.Content_File{
err = proxmox.DeleteFile(cli.Context(), c, args[0], proxmox.Content_File{
Storage: args[1],
ContentType: Type,
FilePath: args[3],
Expand Down
3 changes: 2 additions & 1 deletion cli/command/delete/delete-group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package delete

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

Expand All @@ -9,7 +10,7 @@ var delete_groupCmd = &cobra.Command{
Short: "Deletes the Specified group",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return deleteID(args, "Group")
return deleteID(cli.Context(), args, "Group")
},
}

Expand Down
4 changes: 2 additions & 2 deletions cli/command/delete/delete-guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ var delete_guestCmd = &cobra.Command{
id := cli.ValidateIntIDset(args, "GuestID")
vmr := proxmox.NewVmRef(id)
c := cli.NewClient()
_, err = c.StopVm(vmr)
_, err = c.StopVm(cli.Context(), vmr)
if err != nil {
return
}
_, err = c.DeleteVm(vmr)
_, err = c.DeleteVm(cli.Context(), vmr)
if err != nil {
return
}
Expand Down
3 changes: 2 additions & 1 deletion cli/command/delete/delete-metricserver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package delete

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

Expand All @@ -9,7 +10,7 @@ var delete_metricserverCmd = &cobra.Command{
Short: "Deletes the specified MetricServer",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return deleteID(args, "MetricServer")
return deleteID(cli.Context(), args, "MetricServer")
},
}

Expand Down
3 changes: 2 additions & 1 deletion cli/command/delete/delete-pool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package delete

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

Expand All @@ -9,7 +10,7 @@ var delete_poolCmd = &cobra.Command{
Short: "Deletes the Specified pool",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return deleteID(args, "Pool")
return deleteID(cli.Context(), args, "Pool")
},
}

Expand Down
2 changes: 1 addition & 1 deletion cli/command/delete/delete-snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
RunE: func(cmd *cobra.Command, args []string) (err error) {
id := cli.ValidateIntIDset(args, "GuestID")
snapName := cli.RequiredIDset(args, 1, "SnapshotName")
_, err = proxmox.SnapshotName(snapName).Delete(cli.NewClient(), proxmox.NewVmRef(id))
_, err = proxmox.SnapshotName(snapName).Delete(cli.Context(), cli.NewClient(), proxmox.NewVmRef(id))
if err != nil {
return
}
Expand Down
3 changes: 2 additions & 1 deletion cli/command/delete/delete-storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package delete

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

Expand All @@ -9,7 +10,7 @@ var delete_storageCmd = &cobra.Command{
Short: "Deletes the specified Storage",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return deleteID(args, "Storage")
return deleteID(cli.Context(), args, "Storage")
},
}

Expand Down
3 changes: 2 additions & 1 deletion cli/command/delete/delete-user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package delete

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

Expand All @@ -9,7 +10,7 @@ var delete_userCmd = &cobra.Command{
Short: "Deletes the specified User",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return deleteID(args, "User")
return deleteID(cli.Context(), args, "User")
},
}

Expand Down
15 changes: 8 additions & 7 deletions cli/command/delete/delete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package delete

import (
"context"
"fmt"

"github.com/Telmate/proxmox-api-go/cli"
Expand All @@ -17,28 +18,28 @@ func init() {
cli.RootCmd.AddCommand(deleteCmd)
}

func deleteID(args []string, IDtype string) (err error) {
func deleteID(ctx context.Context, args []string, IDtype string) (err error) {
var exitStatus string
id := cli.RequiredIDset(args, 0, IDtype+"ID")
c := cli.NewClient()
switch IDtype {
case "AcmeAccount":
exitStatus, err = c.DeleteAcmeAccount(id)
exitStatus, err = c.DeleteAcmeAccount(ctx, id)
case "Group":
err = proxmox.GroupName(id).Delete(c)
err = proxmox.GroupName(id).Delete(ctx, c)
case "MetricServer":
err = c.DeleteMetricServer(id)
err = c.DeleteMetricServer(ctx, id)
case "Pool":
err = proxmox.PoolName(id).Delete(c)
err = proxmox.PoolName(id).Delete(ctx, c)
case "Storage":
err = c.DeleteStorage(id)
err = c.DeleteStorage(ctx, id)
case "User":
var userId proxmox.UserID
userId, err = proxmox.NewUserID(id)
if err != nil {
return
}
err = proxmox.ConfigUser{User: userId}.DeleteUser(c)
err = proxmox.ConfigUser{User: userId}.DeleteUser(ctx, c)
}
if err != nil {
if exitStatus != "" {
Expand Down
Loading
Loading