Merge pull request #2171 from walkerwmanuel/GRA-1336-sort-before-sending

GRA-1336: sort before sending
This commit is contained in:
dcarns 2023-04-19 13:08:08 -04:00 committed by GitHub
commit 700147e3ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 58 additions and 1 deletions

View file

@ -70,6 +70,7 @@ func getAllDNS(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
logic.SortDNSEntrys(dns[:])
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(dns)
}

View file

@ -117,6 +117,7 @@ func getAllExtClients(w http.ResponseWriter, r *http.Request) {
}
//Return all the extclients in JSON format
logic.SortExtClient(clients[:])
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(clients)
}

View file

@ -52,6 +52,7 @@ 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")
logic.SortApiHosts(apiHosts[:])
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(apiHosts)
}

View file

@ -70,6 +70,7 @@ func getNetworks(w http.ResponseWriter, r *http.Request) {
}
logger.Log(2, r.Header.Get("user"), "fetched networks.")
logic.SortNetworks(allnetworks[:])
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(allnetworks)
}

View file

@ -340,6 +340,7 @@ 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")
logic.SortApiNodes(apiNodes[:])
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(apiNodes)
}

View file

@ -186,6 +186,7 @@ func getUsers(w http.ResponseWriter, r *http.Request) {
return
}
logic.SortUsers(users[:])
logger.Log(2, r.Header.Get("user"), "fetched users")
json.NewEncoder(w).Encode(users)
}

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,10 @@ 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) {
sort.Slice(unsortedExtClient, func(i, j int) bool {
return unsortedExtClient[i].ClientID < unsortedExtClient[j].ClientID
})
}

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,13 @@ 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) {
sort.Slice(unsortedDNSEntrys, func(i, j int) bool {
return unsortedDNSEntrys[i].Address < unsortedDNSEntrys[j].Address
})
}
// 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"
@ -429,3 +430,10 @@ 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) {
sort.Slice(unsortedHosts, func(i, j int) bool {
return unsortedHosts[i].ID < unsortedHosts[j].ID
})
}

View file

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"sort"
"strings"
"github.com/c-robinson/iplib"
@ -622,3 +623,10 @@ 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) {
sort.Slice(unsortedNetworks, func(i, j int) bool {
return unsortedNetworks[i].NetID < unsortedNetworks[j].NetID
})
}

View file

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net"
"sort"
"time"
validator "github.com/go-playground/validator/v10"
@ -546,4 +547,11 @@ 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) {
sort.Slice(unsortedNodes, func(i, j int) bool {
return unsortedNodes[i].ID < unsortedNodes[j].ID
})
}
// == END PRO ==

View file

@ -2,6 +2,7 @@ package logic
import (
"encoding/json"
"sort"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
@ -77,3 +78,10 @@ func SetUserDefaults(user *models.User) {
user.Groups = []string{pro.DEFAULT_ALLOWED_GROUPS}
}
}
// SortUsers - Sorts slice of Users by username
func SortUsers(unsortedUsers []models.ReturnUser) {
sort.Slice(unsortedUsers, func(i, j int) bool {
return unsortedUsers[i].UserName < unsortedUsers[j].UserName
})
}