modify.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package nodeacls
  2. import (
  3. "github.com/gravitl/netmaker/database"
  4. "github.com/gravitl/netmaker/logic/acls"
  5. )
  6. // CreateNodeACL - inserts or updates a node ACL on given network and adds to state
  7. func CreateNodeACL(networkID NetworkID, nodeID NodeID, defaultVal byte) (acls.ACL, error) {
  8. if defaultVal != acls.NotAllowed && defaultVal != acls.Allowed {
  9. defaultVal = acls.NotAllowed
  10. }
  11. var currentNetworkACL, err = FetchAllACLs(networkID)
  12. if err != nil {
  13. if database.IsEmptyRecord(err) {
  14. currentNetworkACL, err = currentNetworkACL.New(acls.ContainerID(networkID))
  15. if err != nil {
  16. return nil, err
  17. }
  18. } else {
  19. return nil, err
  20. }
  21. }
  22. var newNodeACL = make(acls.ACL)
  23. for existingNodeID := range currentNetworkACL {
  24. currentNetworkACL[existingNodeID][acls.AclID(nodeID)] = defaultVal // set the old nodes to default value for new node
  25. newNodeACL[existingNodeID] = defaultVal // set the old nodes in new node ACL to default value
  26. }
  27. currentNetworkACL[acls.AclID(nodeID)] = newNodeACL // append the new node's ACL
  28. retNetworkACL, err := currentNetworkACL.Save(acls.ContainerID(networkID)) // insert into db
  29. if err != nil {
  30. return nil, err
  31. }
  32. return retNetworkACL[acls.AclID(nodeID)], nil
  33. }
  34. // ChangeNodesAccess - changes relationship between two individual nodes in given network in memory
  35. func ChangeNodesAccess(networkID NetworkID, node1, node2 NodeID, value byte) (acls.ACLContainer, error) {
  36. var currentNetworkACL, err = FetchAllACLs(networkID)
  37. if err != nil {
  38. return nil, err
  39. }
  40. currentNetworkACL.ChangeAccess(acls.AclID(node1), acls.AclID(node2), value)
  41. return currentNetworkACL, nil
  42. }
  43. // UpdateNodeACL - updates a node's ACL in state
  44. func UpdateNodeACL(networkID NetworkID, nodeID NodeID, acl acls.ACL) (acls.ACL, error) {
  45. var currentNetworkACL, err = FetchAllACLs(networkID)
  46. if err != nil {
  47. return nil, err
  48. }
  49. currentNetworkACL[acls.AclID(nodeID)] = acl
  50. return currentNetworkACL[acls.AclID(nodeID)].Save(acls.ContainerID(networkID), acls.AclID(nodeID))
  51. }
  52. // RemoveNodeACL - removes a specific Node's ACL, returns the NetworkACL and error
  53. func RemoveNodeACL(networkID NetworkID, nodeID NodeID) (acls.ACLContainer, error) {
  54. var currentNetworkACL, err = FetchAllACLs(networkID)
  55. if err != nil {
  56. return nil, err
  57. }
  58. for currentNodeID := range currentNetworkACL {
  59. if NodeID(currentNodeID) != nodeID {
  60. currentNetworkACL[currentNodeID].Remove(acls.AclID(nodeID))
  61. }
  62. }
  63. delete(currentNetworkACL, acls.AclID(nodeID))
  64. return currentNetworkACL.Save(acls.ContainerID(networkID))
  65. }