mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-07 17:51:56 +08:00
moved data structure to db
This commit is contained in:
@@ -59,6 +59,8 @@ const (
|
|||||||
HOSTS_TABLE_NAME = "hosts"
|
HOSTS_TABLE_NAME = "hosts"
|
||||||
// ENROLLMENT_KEYS_TABLE_NAME - table name for enrollmentkeys
|
// ENROLLMENT_KEYS_TABLE_NAME - table name for enrollmentkeys
|
||||||
ENROLLMENT_KEYS_TABLE_NAME = "enrollmentkeys"
|
ENROLLMENT_KEYS_TABLE_NAME = "enrollmentkeys"
|
||||||
|
// HOST_ACTIONS_TABLE_NAME - table name for enrollmentkeys
|
||||||
|
HOST_ACTIONS_TABLE_NAME = "hostactions"
|
||||||
|
|
||||||
// == ERROR CONSTS ==
|
// == ERROR CONSTS ==
|
||||||
// NO_RECORD - no singular result found
|
// NO_RECORD - no singular result found
|
||||||
@@ -141,6 +143,7 @@ func createTables() {
|
|||||||
createTable(CACHE_TABLE_NAME)
|
createTable(CACHE_TABLE_NAME)
|
||||||
createTable(HOSTS_TABLE_NAME)
|
createTable(HOSTS_TABLE_NAME)
|
||||||
createTable(ENROLLMENT_KEYS_TABLE_NAME)
|
createTable(ENROLLMENT_KEYS_TABLE_NAME)
|
||||||
|
createTable(HOST_ACTIONS_TABLE_NAME)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTable(tableName string) error {
|
func createTable(tableName string) error {
|
||||||
|
@@ -1,37 +1,55 @@
|
|||||||
package hostactions
|
package hostactions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/gravitl/netmaker/database"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// nodeActionHandler - handles the storage of host action updates
|
|
||||||
var nodeActionHandler sync.Map
|
|
||||||
|
|
||||||
// AddAction - adds a host action to a host's list to be retrieved from broker update
|
// AddAction - adds a host action to a host's list to be retrieved from broker update
|
||||||
func AddAction(hu models.HostUpdate) {
|
func AddAction(hu models.HostUpdate) {
|
||||||
currentRecords, ok := nodeActionHandler.Load(hu.Host.ID.String())
|
hostID := hu.Host.ID.String()
|
||||||
if !ok { // no list exists yet
|
currentRecords, err := database.FetchRecord(database.HOST_ACTIONS_TABLE_NAME, hostID)
|
||||||
nodeActionHandler.Store(hu.Host.ID.String(), []models.HostUpdate{hu})
|
if err != nil {
|
||||||
} else { // list exists, append to it
|
if database.IsEmptyRecord(err) { // no list exists yet
|
||||||
currentList := currentRecords.([]models.HostUpdate)
|
newEntry, err := json.Marshal([]models.HostUpdate{hu})
|
||||||
currentList = append(currentList, hu)
|
if err != nil {
|
||||||
nodeActionHandler.Store(hu.Host.ID.String(), currentList)
|
return
|
||||||
|
}
|
||||||
|
_ = database.Insert(hostID, string(newEntry), database.HOST_ACTIONS_TABLE_NAME)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
var currentList []models.HostUpdate
|
||||||
|
if err := json.Unmarshal([]byte(currentRecords), ¤tList); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
currentList = append(currentList, hu)
|
||||||
|
newData, err := json.Marshal(currentList)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = database.Insert(hostID, string(newData), database.HOST_ACTIONS_TABLE_NAME)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAction - gets an action if exists
|
// GetAction - gets an action if exists
|
||||||
// TODO: may need to move to DB rather than sync map for HA
|
|
||||||
func GetAction(id string) *models.HostUpdate {
|
func GetAction(id string) *models.HostUpdate {
|
||||||
currentRecords, ok := nodeActionHandler.Load(id)
|
currentRecords, err := database.FetchRecord(database.HOST_ACTIONS_TABLE_NAME, id)
|
||||||
if !ok {
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var currentList []models.HostUpdate
|
||||||
|
if err = json.Unmarshal([]byte(currentRecords), ¤tList); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
currentList := currentRecords.([]models.HostUpdate)
|
|
||||||
if len(currentList) > 0 {
|
if len(currentList) > 0 {
|
||||||
hu := currentList[0]
|
hu := currentList[0]
|
||||||
nodeActionHandler.Store(hu.Host.ID.String(), currentList[1:])
|
newData, err := json.Marshal(currentList[1:])
|
||||||
|
if err != nil {
|
||||||
|
newData, _ = json.Marshal([]models.HostUpdate{})
|
||||||
|
}
|
||||||
|
_ = database.Insert(id, string(newData), database.HOST_ACTIONS_TABLE_NAME)
|
||||||
return &hu
|
return &hu
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
Reference in New Issue
Block a user