add enable/disable failover in nmctl command (#2857)

This commit is contained in:
Max Ma 2024-03-20 09:12:42 +01:00 committed by GitHub
parent 4871a0dd1c
commit b59658024c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 88 additions and 0 deletions

View file

@ -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)
}

View file

@ -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)
}

28
cli/cmd/failover/root.go Normal file
View file

@ -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)
}
}

View file

@ -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())
}

18
cli/functions/failover.go Normal file
View file

@ -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)
}