mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-16 13:51:42 +08:00
created functions
This commit is contained in:
@@ -661,7 +661,6 @@ func createNode(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
data.Node.Peers = peerUpdate.Peers
|
data.Node.Peers = peerUpdate.Peers
|
||||||
|
|
||||||
// Create client for this host in Mq
|
// Create client for this host in Mq
|
||||||
event := mq.MqDynsecPayload{
|
event := mq.MqDynsecPayload{
|
||||||
Commands: []mq.MqDynSecCmd{
|
Commands: []mq.MqDynSecCmd{
|
||||||
|
@@ -3,15 +3,21 @@ package logic
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/gravitl/netmaker/database"
|
"github.com/gravitl/netmaker/database"
|
||||||
"github.com/gravitl/netmaker/logger"
|
"github.com/gravitl/netmaker/logger"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrHostExists error indicating that host exists when trying to create new host
|
var (
|
||||||
var ErrHostExists error = errors.New("host already exists")
|
// ErrHostExists error indicating that host exists when trying to create new host
|
||||||
|
ErrHostExists error = errors.New("host already exists")
|
||||||
|
// ErrInvalidHostID
|
||||||
|
ErrInvalidHostID error = errors.New("invalid host id")
|
||||||
|
)
|
||||||
|
|
||||||
// GetAllHosts - returns all hosts in flat list or error
|
// GetAllHosts - returns all hosts in flat list or error
|
||||||
func GetAllHosts() ([]models.Host, error) {
|
func GetAllHosts() ([]models.Host, error) {
|
||||||
@@ -193,3 +199,44 @@ func UpdateHostNetworks(h *models.Host, nets []string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AssociateNodeToHost - associates and creates a node with a given host
|
||||||
|
func AssociateNodeToHost(n *models.Node, h *models.Host) error {
|
||||||
|
if len(h.ID.String()) == 0 || h.ID == uuid.Nil {
|
||||||
|
return ErrInvalidHostID
|
||||||
|
}
|
||||||
|
n.HostID = h.ID
|
||||||
|
err := CreateNode(n)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
h.Nodes = append(h.Nodes, n.ID.String())
|
||||||
|
return UpsertHost(h)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DissasociateNodeFromHost - deletes a node and removes from host nodes
|
||||||
|
func DissasociateNodeFromHost(n *models.Node, h *models.Host) error {
|
||||||
|
if len(h.ID.String()) == 0 || h.ID == uuid.Nil {
|
||||||
|
return ErrInvalidHostID
|
||||||
|
}
|
||||||
|
if n.HostID != h.ID { // check if node actually belongs to host
|
||||||
|
return fmt.Errorf("node is not associated with host")
|
||||||
|
}
|
||||||
|
if len(h.Nodes) == 0 {
|
||||||
|
return fmt.Errorf("no nodes present in given host")
|
||||||
|
}
|
||||||
|
index := -1
|
||||||
|
for i := range h.Nodes {
|
||||||
|
if h.Nodes[i] == n.ID.String() {
|
||||||
|
index = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if index < 0 {
|
||||||
|
if len(h.Nodes) == 0 {
|
||||||
|
return fmt.Errorf("node %s, not found in host, %s", n.ID.String(), h.ID.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h.Nodes = RemoveStringSlice(h.Nodes, index)
|
||||||
|
return UpsertHost(h)
|
||||||
|
}
|
||||||
|
@@ -200,3 +200,9 @@ func CheckIfFileExists(filePath string) bool {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveStringSlice - removes an element at given index i
|
||||||
|
// from a given string slice
|
||||||
|
func RemoveStringSlice(slice []string, i int) []string {
|
||||||
|
return append(slice[:i], slice[i+1:]...)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user