add ext_client create, get, delete and list commands

This commit is contained in:
Anish Mukherjee
2022-11-23 23:29:27 +05:30
parent 9466124cf2
commit 7004c8dd3e
7 changed files with 170 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
package ext_client
import (
"fmt"
"github.com/gravitl/netmaker/cli/functions"
"github.com/spf13/cobra"
)
var extClientID string
var extClientCreateCmd = &cobra.Command{
Use: "create [NETWORK NAME] [NODE ID]",
Args: cobra.ExactArgs(2),
Short: "Create an External Client",
Long: `Create an External Client`,
Run: func(cmd *cobra.Command, args []string) {
functions.CreateExtClient(args[0], args[1], extClientID)
fmt.Println("Success")
},
}
func init() {
extClientCreateCmd.Flags().StringVar(&extClientID, "id", "", "ID of the external client")
rootCmd.AddCommand(extClientCreateCmd)
}

View File

@@ -0,0 +1,20 @@
package ext_client
import (
"github.com/gravitl/netmaker/cli/functions"
"github.com/spf13/cobra"
)
var extClientDeleteCmd = &cobra.Command{
Use: "delete [NETWORK NAME] [EXTERNAL CLIENT ID]",
Args: cobra.ExactArgs(2),
Short: "Delete an External Client",
Long: `Delete an External Client`,
Run: func(cmd *cobra.Command, args []string) {
functions.PrettyPrint(functions.DeleteExtClient(args[0], args[1]))
},
}
func init() {
rootCmd.AddCommand(extClientDeleteCmd)
}

20
cli/cmd/ext_client/get.go Normal file
View File

@@ -0,0 +1,20 @@
package ext_client
import (
"github.com/gravitl/netmaker/cli/functions"
"github.com/spf13/cobra"
)
var extClientGetCmd = &cobra.Command{
Use: "get [NETWORK NAME] [EXTERNAL CLIENT ID]",
Args: cobra.ExactArgs(2),
Short: "Get an External Client",
Long: `Get an External Client`,
Run: func(cmd *cobra.Command, args []string) {
functions.PrettyPrint(functions.GetExtClient(args[0], args[1]))
},
}
func init() {
rootCmd.AddCommand(extClientGetCmd)
}

View File

@@ -0,0 +1,27 @@
package ext_client
import (
"github.com/gravitl/netmaker/cli/functions"
"github.com/spf13/cobra"
)
var networkName string
var extClientListCmd = &cobra.Command{
Use: "list",
Args: cobra.NoArgs,
Short: "List External Clients",
Long: `List External Clients`,
Run: func(cmd *cobra.Command, args []string) {
if networkName != "" {
functions.PrettyPrint(functions.GetNetworkExtClients(networkName))
} else {
functions.PrettyPrint(functions.GetAllExtClients())
}
},
}
func init() {
extClientListCmd.Flags().StringVar(&networkName, "network", "", "Network name")
rootCmd.AddCommand(extClientListCmd)
}

View File

@@ -0,0 +1,37 @@
package ext_client
import (
"os"
"github.com/spf13/cobra"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ext_client",
Short: "Manage External Clients",
Long: `Manage External Clients`,
// Run: func(cmd *cobra.Command, args []string) { },
}
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)
}
}
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/gravitl/netmaker/cli/cmd/acl"
"github.com/gravitl/netmaker/cli/cmd/context"
"github.com/gravitl/netmaker/cli/cmd/dns"
"github.com/gravitl/netmaker/cli/cmd/ext_client"
"github.com/gravitl/netmaker/cli/cmd/keys"
"github.com/gravitl/netmaker/cli/cmd/network"
"github.com/gravitl/netmaker/cli/cmd/node"
@@ -55,4 +56,5 @@ func init() {
rootCmd.AddCommand(node.GetRoot())
rootCmd.AddCommand(dns.GetRoot())
rootCmd.AddCommand(server.GetRoot())
rootCmd.AddCommand(ext_client.GetRoot())
}

View File

@@ -0,0 +1,38 @@
package functions
import (
"fmt"
"net/http"
"github.com/gravitl/netmaker/models"
)
func GetAllExtClients() *[]models.ExtClient {
return request[[]models.ExtClient](http.MethodGet, "/api/extclients", nil)
}
func GetNetworkExtClients(networkName string) *[]models.ExtClient {
return request[[]models.ExtClient](http.MethodGet, "/api/extclients/"+networkName, nil)
}
func GetExtClient(networkName, clientID string) *models.ExtClient {
return request[models.ExtClient](http.MethodGet, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), nil)
}
func GetExtClientConfig(networkName, clientID, configType string) *models.ExtClient {
return request[models.ExtClient](http.MethodGet, fmt.Sprintf("/api/extclients/%s/%s/%s", networkName, clientID, configType), nil)
}
func CreateExtClient(networkName, nodeID, extClientID string) {
if extClientID != "" {
request[any](http.MethodPost, fmt.Sprintf("/api/extclients/%s/%s", networkName, nodeID), &models.CustomExtClient{
ClientID: extClientID,
})
} else {
request[any](http.MethodPost, fmt.Sprintf("/api/extclients/%s/%s", networkName, nodeID), nil)
}
}
func DeleteExtClient(networkName, clientID string) *models.SuccessResponse {
return request[models.SuccessResponse](http.MethodDelete, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), nil)
}