From 2e6eaac2742caaf6c63b281150fc66f96cd105b7 Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Fri, 3 Mar 2023 14:23:51 -0500 Subject: [PATCH] moved data structure to db --- database/database.go | 3 ++ logic/hostactions/hostactions.go | 50 ++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/database/database.go b/database/database.go index 38d4fd40..c51340ab 100644 --- a/database/database.go +++ b/database/database.go @@ -59,6 +59,8 @@ const ( HOSTS_TABLE_NAME = "hosts" // ENROLLMENT_KEYS_TABLE_NAME - table name for enrollmentkeys ENROLLMENT_KEYS_TABLE_NAME = "enrollmentkeys" + // HOST_ACTIONS_TABLE_NAME - table name for enrollmentkeys + HOST_ACTIONS_TABLE_NAME = "hostactions" // == ERROR CONSTS == // NO_RECORD - no singular result found @@ -141,6 +143,7 @@ func createTables() { createTable(CACHE_TABLE_NAME) createTable(HOSTS_TABLE_NAME) createTable(ENROLLMENT_KEYS_TABLE_NAME) + createTable(HOST_ACTIONS_TABLE_NAME) } func createTable(tableName string) error { diff --git a/logic/hostactions/hostactions.go b/logic/hostactions/hostactions.go index fa215c1c..dcafdcd1 100644 --- a/logic/hostactions/hostactions.go +++ b/logic/hostactions/hostactions.go @@ -1,37 +1,55 @@ package hostactions import ( - "sync" + "encoding/json" + "github.com/gravitl/netmaker/database" "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 func AddAction(hu models.HostUpdate) { - currentRecords, ok := nodeActionHandler.Load(hu.Host.ID.String()) - if !ok { // no list exists yet - nodeActionHandler.Store(hu.Host.ID.String(), []models.HostUpdate{hu}) - } else { // list exists, append to it - currentList := currentRecords.([]models.HostUpdate) - currentList = append(currentList, hu) - nodeActionHandler.Store(hu.Host.ID.String(), currentList) + hostID := hu.Host.ID.String() + currentRecords, err := database.FetchRecord(database.HOST_ACTIONS_TABLE_NAME, hostID) + if err != nil { + if database.IsEmptyRecord(err) { // no list exists yet + newEntry, err := json.Marshal([]models.HostUpdate{hu}) + if err != nil { + 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 -// TODO: may need to move to DB rather than sync map for HA func GetAction(id string) *models.HostUpdate { - currentRecords, ok := nodeActionHandler.Load(id) - if !ok { + currentRecords, err := database.FetchRecord(database.HOST_ACTIONS_TABLE_NAME, id) + if err != nil { + return nil + } + var currentList []models.HostUpdate + if err = json.Unmarshal([]byte(currentRecords), ¤tList); err != nil { return nil } - currentList := currentRecords.([]models.HostUpdate) if len(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 nil