hostactions.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package hostactions
  2. import (
  3. "sync"
  4. "github.com/gravitl/netmaker/models"
  5. )
  6. // nodeActionHandler - handles the storage of host action updates
  7. var nodeActionHandler sync.Map
  8. // AddAction - adds a host action to a host's list to be retrieved from broker update
  9. func AddAction(hu models.HostUpdate) {
  10. currentRecords, ok := nodeActionHandler.Load(hu.Host.ID.String())
  11. if !ok { // no list exists yet
  12. nodeActionHandler.Store(hu.Host.ID.String(), []models.HostUpdate{hu})
  13. } else { // list exists, append to it
  14. currentList := currentRecords.([]models.HostUpdate)
  15. currentList = append(currentList, hu)
  16. nodeActionHandler.Store(hu.Host.ID.String(), currentList)
  17. }
  18. }
  19. // GetAction - gets an action if exists
  20. // TODO: may need to move to DB rather than sync map for HA
  21. func GetAction(id string) *models.HostUpdate {
  22. currentRecords, ok := nodeActionHandler.Load(id)
  23. if !ok {
  24. return nil
  25. }
  26. currentList := currentRecords.([]models.HostUpdate)
  27. if len(currentList) > 0 {
  28. hu := currentList[0]
  29. nodeActionHandler.Store(hu.Host.ID.String(), currentList[1:])
  30. return &hu
  31. }
  32. return nil
  33. }