@@ -20,14 +20,15 @@ import (
2020	"context" 
2121	"encoding/json" 
2222	"errors" 
23- 	bean2 "github.com/devtron-labs/devtron/pkg/cluster/bean" 
24- 	"github.com/devtron-labs/devtron/pkg/cluster/environment" 
25- 	"github.com/devtron-labs/devtron/pkg/cluster/rbac" 
2623	"net/http" 
2724	"strconv" 
2825	"strings" 
2926	"time" 
3027
28+ 	bean2 "github.com/devtron-labs/devtron/pkg/cluster/bean" 
29+ 	"github.com/devtron-labs/devtron/pkg/cluster/environment" 
30+ 	"github.com/devtron-labs/devtron/pkg/cluster/rbac" 
31+ 
3132	"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin" 
3233	"github.com/devtron-labs/devtron/pkg/auth/user" 
3334	"github.com/devtron-labs/devtron/pkg/genericNotes" 
@@ -60,6 +61,7 @@ type ClusterRestHandler interface {
6061	GetClusterNamespaces (w  http.ResponseWriter , r  * http.Request )
6162	GetAllClusterNamespaces (w  http.ResponseWriter , r  * http.Request )
6263	FindAllForClusterPermission (w  http.ResponseWriter , r  * http.Request )
64+ 	FindByIds (w  http.ResponseWriter , r  * http.Request )
6365}
6466
6567type  ClusterRestHandlerImpl  struct  {
@@ -296,6 +298,59 @@ func (impl ClusterRestHandlerImpl) FindAll(w http.ResponseWriter, r *http.Reques
296298	common .WriteJsonResp (w , err , result , http .StatusOK )
297299}
298300
301+ func  (impl  ClusterRestHandlerImpl ) FindByIds (w  http.ResponseWriter , r  * http.Request ) {
302+ 	token  :=  r .Header .Get ("token" )
303+ 
304+ 	// Parse clusterId query parameter 
305+ 	clusterIdsStr  :=  r .URL .Query ().Get ("clusterId" )
306+ 	if  clusterIdsStr  ==  ""  {
307+ 		// If no clusterId parameter, return all clusters (same as FindAll) 
308+ 		impl .FindAll (w , r )
309+ 		return 
310+ 	}
311+ 
312+ 	// Parse comma-separated cluster IDs 
313+ 	var  clusterIds  []int 
314+ 	clusterIdStrs  :=  strings .Split (clusterIdsStr , "," )
315+ 	for  _ , idStr  :=  range  clusterIdStrs  {
316+ 		idStr  =  strings .TrimSpace (idStr )
317+ 		if  idStr  ==  ""  {
318+ 			continue 
319+ 		}
320+ 		id , err  :=  strconv .Atoi (idStr )
321+ 		if  err  !=  nil  {
322+ 			impl .logger .Errorw ("request err, FindByIds" , "error" , err , "clusterId" , idStr )
323+ 			common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
324+ 			return 
325+ 		}
326+ 		clusterIds  =  append (clusterIds , id )
327+ 	}
328+ 
329+ 	if  len (clusterIds ) ==  0  {
330+ 		// If no valid cluster IDs, return empty result 
331+ 		common .WriteJsonResp (w , nil , []* bean2.ClusterBean {}, http .StatusOK )
332+ 		return 
333+ 	}
334+ 
335+ 	clusterList , err  :=  impl .clusterService .FindByIdsWithoutConfig (clusterIds )
336+ 	if  err  !=  nil  {
337+ 		impl .logger .Errorw ("service err, FindByIds" , "err" , err )
338+ 		common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
339+ 		return 
340+ 	}
341+ 
342+ 	// RBAC enforcer applying 
343+ 	var  result  []* bean2.ClusterBean 
344+ 	for  _ , item  :=  range  clusterList  {
345+ 		if  ok  :=  impl .enforcer .Enforce (token , casbin .ResourceCluster , casbin .ActionGet , item .ClusterName ); ok  {
346+ 			result  =  append (result , item )
347+ 		}
348+ 	}
349+ 	//RBAC enforcer Ends 
350+ 
351+ 	common .WriteJsonResp (w , nil , result , http .StatusOK )
352+ }
353+ 
299354func  (impl  ClusterRestHandlerImpl ) FindById (w  http.ResponseWriter , r  * http.Request ) {
300355	vars  :=  mux .Vars (r )
301356	id  :=  vars ["id" ]
@@ -671,7 +726,14 @@ func (impl ClusterRestHandlerImpl) GetClusterNamespaces(w http.ResponseWriter, r
671726
672727	allClusterNamespaces , err  :=  impl .clusterService .FindAllNamespacesByUserIdAndClusterId (userId , clusterId , isActionUserSuperAdmin )
673728	if  err  !=  nil  {
674- 		common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
729+ 		// Check if it's a cluster connectivity error and return appropriate status code 
730+ 		if  err .Error () ==  cluster .ErrClusterNotReachable  {
731+ 			impl .logger .Errorw ("cluster connectivity error in GetClusterNamespaces" , "error" , err , "clusterId" , clusterId )
732+ 			common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
733+ 		} else  {
734+ 			impl .logger .Errorw ("error in GetClusterNamespaces" , "error" , err , "clusterId" , clusterId )
735+ 			common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
736+ 		}
675737		return 
676738	}
677739	common .WriteJsonResp (w , nil , allClusterNamespaces , http .StatusOK )
0 commit comments