Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NET-1640: Include static Nodes in the nodes api #3160

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions controllers/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func getNetworkNodes(w http.ResponseWriter, r *http.Request) {
if len(filteredNodes) > 0 {
nodes = filteredNodes
}
nodes = logic.AddStaticNodestoList(nodes)

// returns all the nodes in JSON/API format
apiNodes := logic.GetAllNodesAPI(nodes[:])
Expand Down Expand Up @@ -363,7 +364,9 @@ func getAllNodes(w http.ResponseWriter, r *http.Request) {
if !userPlatformRole.FullAccess {
nodes = logic.GetFilteredNodesByUserAccess(*user, nodes)
}

}
nodes = logic.AddStaticNodestoList(nodes)
// return all the nodes in JSON/API format
apiNodes := logic.GetAllNodesAPI(nodes[:])
logger.Log(3, r.Header.Get("user"), "fetched all nodes they have access to")
Expand Down
37 changes: 37 additions & 0 deletions logic/extpeers.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,40 @@ func GetExtclientAllowedIPs(client models.ExtClient) (allowedIPs []string) {
}
return
}

func GetStaticNodesByNetwork(network models.NetworkID) (staticNode []models.Node) {
extClients, err := GetAllExtClients()
if err != nil {
return
}
for _, extI := range extClients {
if extI.Network == network.String() {
n := models.Node{
IsStatic: true,
StaticNode: extI,
IsUserNode: extI.RemoteAccessClientID != "",
}
staticNode = append(staticNode, n)
}
}

return
}

func GetStaticNodesByGw(gwNode models.Node) (staticNode []models.Node) {
extClients, err := GetAllExtClients()
if err != nil {
return
}
for _, extI := range extClients {
if extI.IngressGatewayID == gwNode.ID.String() {
n := models.Node{
IsStatic: true,
StaticNode: extI,
IsUserNode: extI.RemoteAccessClientID != "",
}
staticNode = append(staticNode, n)
}
}
return
}
14 changes: 14 additions & 0 deletions logic/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,20 @@ func GetAllNodes() ([]models.Node, error) {
return nodes, nil
}

func AddStaticNodestoList(nodes []models.Node) []models.Node {
netMap := make(map[string]struct{})
for _, node := range nodes {
if _, ok := netMap[node.Network]; ok {
continue
}
if node.IsIngressGateway {
nodes = append(nodes, GetStaticNodesByNetwork(models.NetworkID(node.Network))...)
netMap[node.Network] = struct{}{}
}
}
return nodes
}

// GetNetworkByNode - gets the network model from a node
func GetNetworkByNode(node *models.Node) (models.Network, error) {

Expand Down
6 changes: 6 additions & 0 deletions models/api_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type ApiNode struct {
InetNodeReq InetNodeReq `json:"inet_node_req" yaml:"inet_node_req"`
InternetGwID string `json:"internetgw_node_id" yaml:"internetgw_node_id"`
AdditionalRagIps []string `json:"additional_rag_ips" yaml:"additional_rag_ips"`
IsStatic bool `json:"is_static"`
IsUserNode bool `json:"is_user_node"`
StaticNode ExtClient `json:"static_node"`
}

// ApiNode.ConvertToServerNode - converts an api node to a server node
Expand Down Expand Up @@ -183,6 +186,9 @@ func (nm *Node) ConvertToAPINode() *ApiNode {
for _, ip := range nm.AdditionalRagIps {
apiNode.AdditionalRagIps = append(apiNode.AdditionalRagIps, ip.String())
}
apiNode.IsStatic = nm.IsStatic
apiNode.IsUserNode = nm.IsUserNode
apiNode.StaticNode = nm.StaticNode
return &apiNode
}

Expand Down
5 changes: 4 additions & 1 deletion models/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ type Node struct {
IsInternetGateway bool `json:"isinternetgateway" yaml:"isinternetgateway"`
InetNodeReq InetNodeReq `json:"inet_node_req" yaml:"inet_node_req"`
InternetGwID string `json:"internetgw_node_id" yaml:"internetgw_node_id"`
AdditionalRagIps []net.IP `json:"additional_rag_ips" yaml:"additional_rag_ips" swaggertype:"array,number"`
AdditionalRagIps []net.IP `json:"additional_rag_ips" yaml:"additional_rag_ips" swaggertype:"array,number"`
IsStatic bool `json:"is_static"`
IsUserNode bool `json:"is_user_node"`
StaticNode ExtClient `json:"static_node"`
}

// LegacyNode - legacy struct for node model
Expand Down
10 changes: 7 additions & 3 deletions pro/logic/user_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,9 @@ func GetFilteredNodesByUserAccess(user models.User, nodes []models.Node) (filter

nodesMap := make(map[string]struct{})
allNetworkRoles := make(map[models.UserRoleID]struct{})

defer func() {
filteredNodes = logic.AddStaticNodestoList(filteredNodes)
}()
if len(user.NetworkRoles) > 0 {
for _, netRoles := range user.NetworkRoles {
for netRoleI := range netRoles {
Expand All @@ -696,15 +698,17 @@ func GetFilteredNodesByUserAccess(user models.User, nodes []models.Node) (filter
}
}
if _, ok := user.NetworkRoles[models.AllNetworks]; ok {
return nodes
filteredNodes = nodes
return
}
if len(user.UserGroups) > 0 {
for userGID := range user.UserGroups {
userG, err := GetUserGroup(userGID)
if err == nil {
if len(userG.NetworkRoles) > 0 {
if _, ok := userG.NetworkRoles[models.AllNetworks]; ok {
return nodes
filteredNodes = nodes
return
}
for _, netRoles := range userG.NetworkRoles {
for netRoleI := range netRoles {
Expand Down
Loading