ext_client.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package functions
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. "github.com/gravitl/netmaker/cli/config"
  8. "github.com/gravitl/netmaker/models"
  9. )
  10. func GetAllExtClients() *[]models.ExtClient {
  11. return request[[]models.ExtClient](http.MethodGet, "/api/extclients", nil)
  12. }
  13. func GetNetworkExtClients(networkName string) *[]models.ExtClient {
  14. return request[[]models.ExtClient](http.MethodGet, "/api/extclients/"+networkName, nil)
  15. }
  16. func GetExtClient(networkName, clientID string) *models.ExtClient {
  17. return request[models.ExtClient](http.MethodGet, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), nil)
  18. }
  19. func GetExtClientConfig(networkName, clientID string) string {
  20. ctx := config.GetCurrentContext()
  21. req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/extclients/%s/%s/file", ctx.Endpoint, networkName, clientID), nil)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. if ctx.MasterKey != "" {
  26. req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
  27. } else {
  28. req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx))
  29. }
  30. res, err := http.DefaultClient.Do(req)
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34. bodyBytes, err := io.ReadAll(res.Body)
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. return string(bodyBytes)
  39. }
  40. func CreateExtClient(networkName, nodeID, extClientID string) {
  41. if extClientID != "" {
  42. request[any](http.MethodPost, fmt.Sprintf("/api/extclients/%s/%s", networkName, nodeID), &models.CustomExtClient{
  43. ClientID: extClientID,
  44. })
  45. } else {
  46. request[any](http.MethodPost, fmt.Sprintf("/api/extclients/%s/%s", networkName, nodeID), nil)
  47. }
  48. }
  49. func DeleteExtClient(networkName, clientID string) *models.SuccessResponse {
  50. return request[models.SuccessResponse](http.MethodDelete, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), nil)
  51. }
  52. func UpdateExtClient(networkName, clientID string, payload *models.ExtClient) *models.ExtClient {
  53. return request[models.ExtClient](http.MethodPut, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), payload)
  54. }