modify.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package nodeacls
  2. import (
  3. "encoding/json"
  4. "github.com/gravitl/netmaker/database"
  5. )
  6. // UpsertNodeACL - inserts or updates a node ACL on given network
  7. func UpsertNodeACL(networkID NetworkID, nodeID NodeID, defaultVal byte) (NodeACL, error) {
  8. if defaultVal != NotAllowed && defaultVal != Allowed {
  9. defaultVal = NotAllowed
  10. }
  11. var currentNetworkACL, err = FetchCurrentACL(networkID)
  12. if err != nil {
  13. return nil, err
  14. }
  15. var newNodeACL = make(NodeACL)
  16. for existingNode := range currentNetworkACL {
  17. currentNetworkACL[existingNode][nodeID] = defaultVal
  18. newNodeACL[existingNode] = defaultVal
  19. }
  20. currentNetworkACL[nodeID] = newNodeACL
  21. return newNodeACL, nil
  22. }
  23. // UpsertNetworkACL - Inserts or updates a network ACL given the json string of the ACL and the network name
  24. // if nil, create it
  25. func UpsertNetworkACL(networkID NetworkID, networkACL NetworkACL) (NetworkACL, error) {
  26. if networkACL == nil {
  27. networkACL = make(NetworkACL)
  28. }
  29. return networkACL, database.Insert(string(networkID), string(convertNetworkACLtoACLJson(&networkACL)), database.NODE_ACLS_TABLE_NAME)
  30. }
  31. // RemoveNodeACL - removes a specific Node's ACL, returns the NetworkACL and error
  32. func RemoveNodeACL(networkID NetworkID, nodeID NodeID) (NetworkACL, error) {
  33. var currentNeworkACL, err = FetchCurrentACL(networkID)
  34. if err != nil {
  35. return nil, err
  36. }
  37. for currentNodeID := range currentNeworkACL {
  38. delete(currentNeworkACL[nodeID], currentNodeID)
  39. }
  40. delete(currentNeworkACL, nodeID)
  41. return UpsertNetworkACL(networkID, currentNeworkACL)
  42. }
  43. // RemoveNetworkACL - just delete the network ACL
  44. func RemoveNetworkACL(networkID NetworkID) error {
  45. return database.DeleteRecord(database.NODE_ACLS_TABLE_NAME, string(networkID))
  46. }
  47. func convertNetworkACLtoACLJson(networkACL *NetworkACL) ACLJson {
  48. data, err := json.Marshal(networkACL)
  49. if err != nil {
  50. return ""
  51. }
  52. return ACLJson(data)
  53. }