Merge branch 'NM-57' into task/schema-changes

# Conflicts:
#	pro/controllers/metrics.go
This commit is contained in:
Vishal Dalwadi 2025-07-30 22:49:53 +05:30
commit f4ab479593
4 changed files with 37 additions and 23 deletions

View file

@ -60,6 +60,11 @@ func userMiddleWare(handler http.Handler) http.Handler {
if strings.Contains(route, "networks") {
r.Header.Set("TARGET_RSRC", models.NetworkRsrc.String())
}
// check 'graph' after 'networks', otherwise the
// header will be overwritten.
if strings.Contains(route, "graph") {
r.Header.Set("TARGET_RSRC", models.HostRsrc.String())
}
if strings.Contains(route, "acls") {
r.Header.Set("TARGET_RSRC", models.AclRsrc.String())
}

View file

@ -3,8 +3,6 @@ package controllers
import (
"encoding/json"
"net/http"
"slices"
"strings"
proLogic "github.com/gravitl/netmaker/pro/logic"
"golang.org/x/exp/slog"
@ -22,7 +20,6 @@ func MetricHandlers(r *mux.Router) {
r.HandleFunc("/api/metrics/{network}", logic.SecurityCheck(true, http.HandlerFunc(getNetworkNodesMetrics))).Methods(http.MethodGet)
r.HandleFunc("/api/metrics", logic.SecurityCheck(true, http.HandlerFunc(getAllMetrics))).Methods(http.MethodGet)
r.HandleFunc("/api/metrics-ext/{network}", logic.SecurityCheck(true, http.HandlerFunc(getNetworkExtMetrics))).Methods(http.MethodGet)
r.HandleFunc("/api/v1/graph/{network}", logic.SecurityCheck(true, http.HandlerFunc(graph))).Methods(http.MethodGet)
}
// get the metrics of a given node
@ -168,23 +165,3 @@ func getAllMetrics(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(networkMetrics)
}
func graph(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var params = mux.Vars(r)
network := params["network"]
networkNodes, err := logic.GetNetworkNodes(network)
if err != nil {
logger.Log(1, r.Header.Get("user"), "failed to get network nodes", err.Error())
return
}
networkNodes = logic.AddStaticNodestoList(networkNodes)
// return all the nodes in JSON/API format
apiNodes := logic.GetAllNodesAPIWithLocation(networkNodes[:])
slices.SortFunc(apiNodes, func(a, b models.ApiNode) int {
return strings.Compare(a.ID, b.ID)
})
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(apiNodes)
}

View file

@ -0,0 +1,31 @@
package controllers
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic"
"net/http"
)
func NetworkHandlers(r *mux.Router) {
r.HandleFunc("/api/v1/networks/{network}/graph", logic.SecurityCheck(true, http.HandlerFunc(getNetworkGraph))).Methods(http.MethodGet)
}
func getNetworkGraph(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var params = mux.Vars(r)
network := params["network"]
networkNodes, err := logic.GetNetworkNodes(network)
if err != nil {
logger.Log(1, r.Header.Get("user"), "failed to get network nodes", err.Error())
return
}
networkNodes = logic.AddStaticNodestoList(networkNodes)
// return all the nodes in JSON/API format
apiNodes := logic.GetAllNodesAPIWithLocation(networkNodes[:])
logic.SortApiNodes(apiNodes[:])
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(apiNodes)
}

View file

@ -35,6 +35,7 @@ func InitPro() {
proControllers.RacHandlers,
proControllers.EventHandlers,
proControllers.TagHandlers,
proControllers.NetworkHandlers,
)
controller.ListRoles = proControllers.ListRoles
logic.EnterpriseCheckFuncs = append(logic.EnterpriseCheckFuncs, func() {