mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-06 17:29:15 +08:00
add external client config fetch subcommand
This commit is contained in:
22
cli/cmd/ext_client/config.go
Normal file
22
cli/cmd/ext_client/config.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package ext_client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gravitl/netmaker/cli/functions"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var extClientConfigCmd = &cobra.Command{
|
||||||
|
Use: "config [NETWORK NAME] [EXTERNAL CLIENT ID]",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
Short: "Get an External Client Configuration",
|
||||||
|
Long: `Get an External Client Configuration`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println(functions.GetExtClientConfig(args[0], args[1]))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(extClientConfigCmd)
|
||||||
|
}
|
@@ -26,7 +26,7 @@ var extClientListCmd = &cobra.Command{
|
|||||||
data = *functions.GetAllExtClients()
|
data = *functions.GetAllExtClients()
|
||||||
}
|
}
|
||||||
table := tablewriter.NewWriter(os.Stdout)
|
table := tablewriter.NewWriter(os.Stdout)
|
||||||
table.SetHeader([]string{"ClientID", "Network", "IPv4 Address", "IPv6 Address", "Enabled", "Last Modified"})
|
table.SetHeader([]string{"Client ID", "Network", "IPv4 Address", "IPv6 Address", "Enabled", "Last Modified"})
|
||||||
for _, d := range data {
|
for _, d := range data {
|
||||||
table.Append([]string{d.ClientID, d.Network, d.Address, d.Address6, strconv.FormatBool(d.Enabled), time.Unix(d.LastModified, 0).String()})
|
table.Append([]string{d.ClientID, d.Network, d.Address, d.Address6, strconv.FormatBool(d.Enabled), time.Unix(d.LastModified, 0).String()})
|
||||||
}
|
}
|
||||||
|
@@ -23,7 +23,7 @@ var nodeListCmd = &cobra.Command{
|
|||||||
data = *functions.GetNodes()
|
data = *functions.GetNodes()
|
||||||
}
|
}
|
||||||
table := tablewriter.NewWriter(os.Stdout)
|
table := tablewriter.NewWriter(os.Stdout)
|
||||||
table.SetHeader([]string{"Node Name", "Addresses", "Version", "Network", "Egress Status", "Ingress Status", "Relay Status"})
|
table.SetHeader([]string{"Name", "Addresses", "Version", "Network", "Egress", "Ingress", "Relay", "ID"})
|
||||||
for _, d := range data {
|
for _, d := range data {
|
||||||
addresses := ""
|
addresses := ""
|
||||||
if d.Address != "" {
|
if d.Address != "" {
|
||||||
@@ -35,7 +35,7 @@ var nodeListCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
addresses += d.Address6
|
addresses += d.Address6
|
||||||
}
|
}
|
||||||
table.Append([]string{d.Name, addresses, d.Version, d.Network, d.IsEgressGateway, d.IsIngressGateway, d.IsRelay})
|
table.Append([]string{d.Name, addresses, d.Version, d.Network, d.IsEgressGateway, d.IsIngressGateway, d.IsRelay, d.ID})
|
||||||
}
|
}
|
||||||
table.Render()
|
table.Render()
|
||||||
},
|
},
|
||||||
|
@@ -2,8 +2,11 @@ package functions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gravitl/netmaker/cli/config"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,8 +22,26 @@ func GetExtClient(networkName, clientID string) *models.ExtClient {
|
|||||||
return request[models.ExtClient](http.MethodGet, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), nil)
|
return request[models.ExtClient](http.MethodGet, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetExtClientConfig(networkName, clientID, configType string) *models.ExtClient {
|
func GetExtClientConfig(networkName, clientID string) string {
|
||||||
return request[models.ExtClient](http.MethodGet, fmt.Sprintf("/api/extclients/%s/%s/%s", networkName, clientID, configType), nil)
|
ctx := config.GetCurrentContext()
|
||||||
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/extclients/%s/%s/file", ctx.Endpoint, networkName, clientID), nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if ctx.MasterKey != "" {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
|
||||||
|
} else {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx))
|
||||||
|
}
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
bodyBytes, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return string(bodyBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateExtClient(networkName, nodeID, extClientID string) {
|
func CreateExtClient(networkName, nodeID, extClientID string) {
|
||||||
|
Reference in New Issue
Block a user