This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
node_handlers.go
94 lines (88 loc) · 2.61 KB
/
node_handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
controller "github.com/gravitl/netmaker/controllers"
"github.com/gravitl/netmaker/functions"
"github.com/gravitl/netmaker/models"
)
//EditNode display a form to update a node
func EditNode(c *gin.Context) {
network := c.PostForm("network")
mac := c.PostForm("mac")
var node models.Node
node, err := controller.GetNode(mac, network)
if err != nil {
fmt.Println("error getting node details \n", err)
ReturnError(c, http.StatusBadRequest, err, "Nodes")
return
}
c.HTML(http.StatusOK, "EditNode", node)
}
//DeleteNode delele node
func DeleteNode(c *gin.Context) {
mac := c.PostForm("mac")
net := c.PostForm("net")
fmt.Println("deleting node ", mac, net)
err := controller.DeleteNode(mac+"###"+net, false)
if err != nil {
ReturnError(c, http.StatusBadRequest, err, "Nodes")
return
}
ReturnSuccess(c, "Nodes", "node deleted")
}
//UpdateNode updates a node
func UpdateNode(c *gin.Context) {
var node *models.Node
if err := c.ShouldBind(&node); err != nil {
fmt.Println("should bind")
ReturnError(c, http.StatusBadRequest, err, "Nodes")
return
}
net := c.Param("net")
mac := c.Param("mac")
fmt.Printf("=============%T %T %T %v %v %v", net, mac, node, net, mac, node)
oldnode, err := functions.GetNodeByMacAddress(net, mac)
if err != nil {
fmt.Println("Get node with mac ", mac, " and Network ", net)
ReturnError(c, http.StatusBadRequest, err, "Nodes")
return
}
if err = oldnode.Update(node); err != nil {
fmt.Println("update network")
ReturnError(c, http.StatusBadRequest, err, "Nodes")
return
}
ReturnSuccess(c, "Nodes", "node updated")
}
//NodeHealth return the last checkin time including health status
//and color code for all nodes
func NodeHealth(c *gin.Context) {
nodes, err := models.GetAllNodes()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var response []NodeStatus
var nodeHealth NodeStatus
for _, node := range nodes {
nodeHealth.Mac = node.MacAddress
nodeHealth.Network = node.Network
lastupdate := time.Now().Sub(time.Unix(node.LastCheckIn, 0))
if lastupdate.Minutes() > 15.0 {
nodeHealth.Status = "Error: Node last checked in more than 15 minutes ago: "
nodeHealth.Color = "w3-deep-orange"
} else if lastupdate.Minutes() > 5.0 {
nodeHealth.Status = "Warning: Node last checked in more than 5 minutes ago: "
nodeHealth.Color = "w3-khaki"
} else {
nodeHealth.Status = "Healthy: Node checked in within the last 5 minutes: "
nodeHealth.Color = "w3-teal"
}
response = append(response, nodeHealth)
}
c.JSON(http.StatusOK, response)
return
}