added sort logic

This commit is contained in:
walkerwmanuel 2023-03-30 16:04:12 -04:00
parent 9f5e9c1ef5
commit 09872e9ec5
10 changed files with 59 additions and 6 deletions

View file

@ -70,8 +70,9 @@ func getAllDNS(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
sortedDns := logic.SortDNSEntrys(dns)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(dns)
json.NewEncoder(w).Encode(sortedDns)
}
// swagger:route GET /api/dns/adm/{network}/custom dns getCustomDNS

View file

@ -117,8 +117,9 @@ func getAllExtClients(w http.ResponseWriter, r *http.Request) {
}
//Return all the extclients in JSON format
sortedClients := logic.SortExtClient(clients)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(clients)
json.NewEncoder(w).Encode(sortedClients)
}
// swagger:route GET /api/extclients/{network}/{clientid} ext_client getExtClient

View file

@ -51,8 +51,9 @@ func getHosts(w http.ResponseWriter, r *http.Request) {
// return JSON/API formatted hosts
apiHosts := logic.GetAllHostsAPI(currentHosts[:])
logger.Log(2, r.Header.Get("user"), "fetched all hosts")
sortedApiHosts := logic.SortApiHosts(apiHosts)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(apiHosts)
json.NewEncoder(w).Encode(sortedApiHosts)
}
// swagger:route GET /api/v1/host pull pullHost

View file

@ -71,8 +71,9 @@ func getNetworks(w http.ResponseWriter, r *http.Request) {
}
logger.Log(2, r.Header.Get("user"), "fetched networks.")
sortedAllNetworks := logic.SortNetworks(allnetworks)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(allnetworks)
json.NewEncoder(w).Encode(sortedAllNetworks)
}
// swagger:route GET /api/networks/{networkname} networks getNetwork

View file

@ -340,8 +340,9 @@ func getAllNodes(w http.ResponseWriter, r *http.Request) {
// return all the nodes in JSON/API format
apiNodes := logic.GetAllNodesAPI(nodes[:])
logger.Log(3, r.Header.Get("user"), "fetched all nodes they have access to")
sortedApiNodes := logic.SortApiNodes(apiNodes)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(apiNodes)
json.NewEncoder(w).Encode(sortedApiNodes)
}
func getUsersNodes(user models.User) ([]models.Node, error) {

View file

@ -1,6 +1,10 @@
package logic
import "github.com/gravitl/netmaker/models"
import (
"sort"
"github.com/gravitl/netmaker/models"
)
// functions defined here, handle client ACLs, should be set on ee
@ -51,3 +55,11 @@ func IsClientNodeAllowedByID(clientID, networkName, clientOrNodeID string) bool
}
return IsClientNodeAllowed(&client, clientOrNodeID)
}
// SortExtClient - Sorts slice of ExtClients by their ClientID alphabetically with numbers first
func SortExtClient(unsortedExtClient []models.ExtClient) []models.ExtClient {
sort.Slice(unsortedExtClient, func(i, j int) bool {
return unsortedExtClient[i].ClientID < unsortedExtClient[j].ClientID
})
return nil
}

View file

@ -3,6 +3,7 @@ package logic
import (
"encoding/json"
"os"
"sort"
validator "github.com/go-playground/validator/v10"
"github.com/gravitl/netmaker/database"
@ -194,6 +195,14 @@ func GetDNSEntryNum(domain string, network string) (int, error) {
return num, nil
}
// SortDNSEntrys - Sorts slice of DNSEnteys by their Address alphabetically with numbers first
func SortDNSEntrys(unsortedDNSEntrys []models.DNSEntry) []models.DNSEntry {
sort.Slice(unsortedDNSEntrys, func(i, j int) bool {
return unsortedDNSEntrys[i].Address < unsortedDNSEntrys[j].Address
})
return nil
}
// ValidateDNSCreate - checks if an entry is valid
func ValidateDNSCreate(entry models.DNSEntry) error {

View file

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"sort"
"github.com/google/uuid"
"github.com/gravitl/netmaker/database"
@ -424,3 +425,11 @@ func GetHostByNodeID(id string) *models.Host {
}
return nil
}
// SortApiHosts - Sorts slice of ApiHosts by their ID alphabetically with numbers first
func SortApiHosts(unsortedHosts []models.ApiHost) []models.ApiHost {
sort.Slice(unsortedHosts, func(i, j int) bool {
return unsortedHosts[i].ID < unsortedHosts[j].ID
})
return nil
}

View file

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"sort"
"strings"
"github.com/c-robinson/iplib"
@ -622,3 +623,11 @@ func networkNodesUpdateAction(networkName string, action string) error {
}
return nil
}
// SortNetworks - Sorts slice of Networks by their NetID alphabetically with numbers first
func SortNetworks(unsortedNetworks []models.Network) []models.Network {
sort.Slice(unsortedNetworks, func(i, j int) bool {
return unsortedNetworks[i].NetID < unsortedNetworks[j].NetID
})
return nil
}

View file

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"sort"
"time"
validator "github.com/go-playground/validator/v10"
@ -546,4 +547,12 @@ func createNode(node *models.Node) error {
return err
}
// SortApiNodes - Sorts slice of ApiNodes by their ID alphabetically with numbers first
func SortApiNodes(unsortedNodes []models.ApiNode) []models.ApiNode {
sort.Slice(unsortedNodes, func(i, j int) bool {
return unsortedNodes[i].ID < unsortedNodes[j].ID
})
return nil
}
// == END PRO ==