From b59658024c5613799617fbb395ae1d73fffc1d80 Mon Sep 17 00:00:00 2001 From: Max Ma Date: Wed, 20 Mar 2024 09:12:42 +0100 Subject: [PATCH] add enable/disable failover in nmctl command (#2857) --- cli/cmd/failover/disable.go | 20 ++++++++++++++++++++ cli/cmd/failover/enable.go | 20 ++++++++++++++++++++ cli/cmd/failover/root.go | 28 ++++++++++++++++++++++++++++ cli/cmd/root.go | 2 ++ cli/functions/failover.go | 18 ++++++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 cli/cmd/failover/disable.go create mode 100644 cli/cmd/failover/enable.go create mode 100644 cli/cmd/failover/root.go create mode 100644 cli/functions/failover.go diff --git a/cli/cmd/failover/disable.go b/cli/cmd/failover/disable.go new file mode 100644 index 00000000..886843aa --- /dev/null +++ b/cli/cmd/failover/disable.go @@ -0,0 +1,20 @@ +package failover + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var disableFailoverCmd = &cobra.Command{ + Use: "disable [NODE ID]", + Args: cobra.ExactArgs(1), + Short: "Disable failover for a given Node", + Long: `Disable failover for a given Node`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.DisableNodeFailover(args[0])) + }, +} + +func init() { + rootCmd.AddCommand(disableFailoverCmd) +} diff --git a/cli/cmd/failover/enable.go b/cli/cmd/failover/enable.go new file mode 100644 index 00000000..d7ee6a3d --- /dev/null +++ b/cli/cmd/failover/enable.go @@ -0,0 +1,20 @@ +package failover + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var enableFailoverCmd = &cobra.Command{ + Use: "enable [NODE ID]", + Args: cobra.ExactArgs(1), + Short: "Enable failover for a given Node", + Long: `Enable failover for a given Node`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.EnableNodeFailover(args[0])) + }, +} + +func init() { + rootCmd.AddCommand(enableFailoverCmd) +} diff --git a/cli/cmd/failover/root.go b/cli/cmd/failover/root.go new file mode 100644 index 00000000..390e49c5 --- /dev/null +++ b/cli/cmd/failover/root.go @@ -0,0 +1,28 @@ +package failover + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "failover", + Short: "Enable/Disable failover for a node associated with a network", + Long: `Enable/Disable failover for a node associated with a network`, +} + +// GetRoot returns the root subcommand +func GetRoot() *cobra.Command { + return rootCmd +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 7376104c..b2164d3f 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -9,6 +9,7 @@ import ( "github.com/gravitl/netmaker/cli/cmd/dns" "github.com/gravitl/netmaker/cli/cmd/enrollment_key" "github.com/gravitl/netmaker/cli/cmd/ext_client" + "github.com/gravitl/netmaker/cli/cmd/failover" "github.com/gravitl/netmaker/cli/cmd/host" "github.com/gravitl/netmaker/cli/cmd/metrics" "github.com/gravitl/netmaker/cli/cmd/network" @@ -53,4 +54,5 @@ func init() { rootCmd.AddCommand(metrics.GetRoot()) rootCmd.AddCommand(host.GetRoot()) rootCmd.AddCommand(enrollment_key.GetRoot()) + rootCmd.AddCommand(failover.GetRoot()) } diff --git a/cli/functions/failover.go b/cli/functions/failover.go new file mode 100644 index 00000000..1c1b3f76 --- /dev/null +++ b/cli/functions/failover.go @@ -0,0 +1,18 @@ +package functions + +import ( + "fmt" + "net/http" + + "github.com/gravitl/netmaker/models" +) + +// EnableNodeFailover - Enable failover for a given Node +func EnableNodeFailover(nodeID string) *models.SuccessResponse { + return request[models.SuccessResponse](http.MethodPost, fmt.Sprintf("/api/v1/node/%s/failover", nodeID), nil) +} + +// DisableNodeFailover - Disable failover for a given Node +func DisableNodeFailover(nodeID string) *models.SuccessResponse { + return request[models.SuccessResponse](http.MethodDelete, fmt.Sprintf("/api/v1/node/%s/failover", nodeID), nil) +}