netmaker/functions/helpers.go

74 lines
1.6 KiB
Go
Raw Normal View History

2021-03-26 00:17:52 +08:00
package functions
import (
2021-07-14 11:08:10 +08:00
"encoding/json"
"strings"
2021-07-21 05:18:45 +08:00
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/models"
2021-03-26 00:17:52 +08:00
)
2021-10-09 03:07:12 +08:00
// NetworkExists - check if network exists
func NetworkExists(name string) (bool, error) {
2021-03-26 00:17:52 +08:00
2021-07-21 05:18:45 +08:00
var network string
var err error
if network, err = database.FetchRecord(database.NETWORKS_TABLE_NAME, name); err != nil {
return false, err
2021-03-26 00:17:52 +08:00
}
2021-07-21 05:18:45 +08:00
return len(network) > 0, nil
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// NameInDNSCharSet - name in dns char set
2021-04-27 11:39:15 +08:00
func NameInDNSCharSet(name string) bool {
2021-05-01 19:57:49 +08:00
charset := "abcdefghijklmnopqrstuvwxyz1234567890-."
2021-04-27 11:39:15 +08:00
2021-05-01 19:57:49 +08:00
for _, char := range name {
if !strings.Contains(charset, strings.ToLower(string(char))) {
return false
}
}
return true
}
2021-04-27 11:39:15 +08:00
2021-10-09 03:07:12 +08:00
// NameInNodeCharSet - name in node char set
func NameInNodeCharSet(name string) bool {
2021-03-26 00:17:52 +08:00
charset := "abcdefghijklmnopqrstuvwxyz1234567890-"
2021-03-26 00:17:52 +08:00
for _, char := range name {
if !strings.Contains(charset, strings.ToLower(string(char))) {
return false
}
}
return true
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// RemoveDeletedNode - remove deleted node
2021-08-10 10:31:01 +08:00
func RemoveDeletedNode(nodeid string) bool {
return database.DeleteRecord(database.DELETED_NODES_TABLE_NAME, nodeid) == nil
}
2021-10-09 03:07:12 +08:00
// GetAllExtClients - get all ext clients
2021-05-20 01:59:10 +08:00
func GetAllExtClients() ([]models.ExtClient, error) {
var extclients []models.ExtClient
2021-07-21 05:18:45 +08:00
collection, err := database.FetchRecords(database.EXT_CLIENT_TABLE_NAME)
if err != nil {
2021-07-21 05:18:45 +08:00
return extclients, err
}
2021-07-21 05:18:45 +08:00
for _, value := range collection {
var extclient models.ExtClient
err := json.Unmarshal([]byte(value), &extclient)
if err != nil {
return []models.ExtClient{}, err
}
// add node to our array
extclients = append(extclients, extclient)
}
2021-05-20 01:59:10 +08:00
return extclients, nil
}