mirror of
https://github.com/gravitl/netmaker.git
synced 2026-04-22 16:07:11 +08:00
88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
package functions
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/gravitl/netmaker/models"
|
|
)
|
|
|
|
type hostNetworksUpdatePayload struct {
|
|
Networks []string `json:"networks"`
|
|
}
|
|
|
|
// GetHosts - fetch all host entries
|
|
func GetHosts() *[]models.ApiHost {
|
|
return request[[]models.ApiHost](http.MethodGet, "/api/hosts", nil)
|
|
}
|
|
|
|
// DeleteHost - delete a host
|
|
func DeleteHost(hostID string, force bool) *models.ApiHost {
|
|
return request[models.ApiHost](http.MethodDelete, fmt.Sprintf("/api/hosts/%s?force=%t", hostID, force), nil)
|
|
}
|
|
|
|
// GetHost - fetches a host
|
|
func GetHost(hostID string) *models.ApiHost {
|
|
resp := request[models.SuccessResponse](http.MethodGet, fmt.Sprintf("/api/hosts/%s", hostID), nil)
|
|
if resp.Code != http.StatusOK {
|
|
log.Fatalf("Error Status: %d Response: %s", resp.Code, resp.Message)
|
|
}
|
|
|
|
if resp.Response == nil {
|
|
log.Fatalf("Empty response")
|
|
}
|
|
|
|
bytes, err := json.Marshal(resp.Response)
|
|
if err != nil {
|
|
log.Fatalf("Error reading Response: %s", err)
|
|
}
|
|
|
|
var host models.ApiHost
|
|
err = json.Unmarshal(bytes, &host)
|
|
if err != nil {
|
|
log.Fatalf("Error unmarshalling JSON: %s", err)
|
|
}
|
|
|
|
return &host
|
|
}
|
|
|
|
// UpdateHost - update a host
|
|
func UpdateHost(hostID string, body *models.ApiHost) *models.ApiHost {
|
|
return request[models.ApiHost](http.MethodPut, "/api/hosts/"+hostID, body)
|
|
}
|
|
|
|
// AddHostToNetwork - add a network to host
|
|
func AddHostToNetwork(hostID, network string) *hostNetworksUpdatePayload {
|
|
return request[hostNetworksUpdatePayload](http.MethodPost, "/api/hosts/"+hostID+"/networks/"+network, nil)
|
|
}
|
|
|
|
// DeleteHostFromNetwork - deletes a network from host
|
|
func DeleteHostFromNetwork(hostID, network string) *hostNetworksUpdatePayload {
|
|
return request[hostNetworksUpdatePayload](http.MethodDelete, "/api/hosts/"+hostID+"/networks/"+network, nil)
|
|
}
|
|
|
|
// CreateRelay - add relay to a node
|
|
func CreateRelay(netID, nodeID string, relayedNodes []string) *models.ApiNode {
|
|
return request[models.ApiNode](http.MethodPost, fmt.Sprintf("/api/nodes/%s/%s/createrelay", netID, nodeID), &models.RelayRequest{
|
|
NodeID: nodeID,
|
|
NetID: netID,
|
|
RelayedNodes: relayedNodes,
|
|
})
|
|
}
|
|
|
|
// DeleteRelay - remove relay from a node
|
|
func DeleteRelay(netID, nodeID string) *models.ApiNode {
|
|
return request[models.ApiNode](http.MethodDelete, fmt.Sprintf("/api/nodes/%s/%s/deleterelay", netID, nodeID), nil)
|
|
}
|
|
|
|
// RefreshKeys - refresh wireguard keys
|
|
func RefreshKeys(hostID string) any {
|
|
if hostID == "" {
|
|
return request[any](http.MethodPut, "/api/hosts/keys", nil)
|
|
}
|
|
return request[any](http.MethodPut, fmt.Sprintf("/api/hosts/%s/keys", hostID), nil)
|
|
|
|
}
|