Skip to content

Commit 98b1f8f

Browse files
feat: App grouping BE (#2979)
* added environment wise app workflow * app workflow list for environment * fix * added new api for app grouping * pipelines for env ids * fix * added workflow status api * added workflow status api * fix * wip * wip * urls modified for app-grouping * code refactor * rbac added for app grouping api's * added offset and size filter for env list for app grouping * offset and size refix for app grouping api * total count addeded for app grouping * app count in app grouping api * handle no data found for app-grouping api's * pagination changes * deployment status * env deployemt status for app * env app deployment status fix * fix * default size removed for app grouping list * app deployment status for app grouping fix * review changes * app listing size optional * review changes rbac refactor * order by env name for app grouping env list * token replaced by emailId for app grouping api's * fixes * review changes for const on app grouping
1 parent 9408845 commit 98b1f8f

25 files changed

+1432
-62
lines changed

Wire.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,9 @@ func InitializeApp() (*App, error) {
819819

820820
kubernetesResourceAuditLogs.Newk8sResourceHistoryServiceImpl,
821821
wire.Bind(new(kubernetesResourceAuditLogs.K8sResourceHistoryService), new(*kubernetesResourceAuditLogs.K8sResourceHistoryServiceImpl)),
822+
823+
router.NewAppGroupingRouterImpl,
824+
wire.Bind(new(router.AppGroupingRouter), new(*router.AppGroupingRouterImpl)),
822825
)
823826
return &App{}, nil
824827
}

api/restHandler/AppListingRestHandler.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,13 @@ func (handler AppListingRestHandlerImpl) FetchAppsByEnvironment(w http.ResponseW
305305
offset := fetchAppListingRequest.Offset
306306
limit := fetchAppListingRequest.Size
307307

308-
if offset+limit <= len(apps) {
309-
apps = apps[offset : offset+limit]
310-
} else {
311-
apps = apps[offset:]
308+
if limit > 0 {
309+
if offset+limit <= len(apps) {
310+
apps = apps[offset : offset+limit]
311+
} else {
312+
apps = apps[offset:]
313+
}
312314
}
313-
314315
appContainerResponse := bean.AppContainerResponse{
315316
AppContainers: apps,
316317
AppCount: appsCount,

api/restHandler/AppWorkflowRestHandler.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ import (
3232
"go.uber.org/zap"
3333
"net/http"
3434
"strconv"
35+
"strings"
3536
)
3637

3738
type AppWorkflowRestHandler interface {
3839
CreateAppWorkflow(w http.ResponseWriter, r *http.Request)
3940
FindAppWorkflow(w http.ResponseWriter, r *http.Request)
4041
DeleteAppWorkflow(w http.ResponseWriter, r *http.Request)
4142
FindAllWorkflows(w http.ResponseWriter, r *http.Request)
43+
FindAppWorkflowByEnvironment(w http.ResponseWriter, r *http.Request)
4244
}
4345

4446
type AppWorkflowRestHandlerImpl struct {
@@ -216,3 +218,50 @@ func (impl AppWorkflowRestHandlerImpl) FindAllWorkflows(w http.ResponseWriter, r
216218
}
217219
common.WriteJsonResp(w, nil, resp, http.StatusOK)
218220
}
221+
222+
func (impl AppWorkflowRestHandlerImpl) FindAppWorkflowByEnvironment(w http.ResponseWriter, r *http.Request) {
223+
vars := mux.Vars(r)
224+
userId, err := impl.userAuthService.GetLoggedInUser(r)
225+
if userId == 0 || err != nil {
226+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
227+
return
228+
}
229+
user, err := impl.userAuthService.GetById(userId)
230+
if userId == 0 || err != nil {
231+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
232+
return
233+
}
234+
userEmailId := strings.ToLower(user.EmailId)
235+
envId, err := strconv.Atoi(vars["envId"])
236+
if err != nil {
237+
impl.Logger.Errorw("bad request", "err", err)
238+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
239+
return
240+
}
241+
workflows := make(map[string]interface{})
242+
workflowsList, err := impl.appWorkflowService.FindAppWorkflowsByEnvironmentId(envId, userEmailId, impl.checkAuthBatch)
243+
if err != nil {
244+
impl.Logger.Errorw("error in fetching workflows for app", "err", err)
245+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
246+
return
247+
}
248+
workflows["envId"] = envId
249+
if len(workflowsList) > 0 {
250+
workflows["workflows"] = workflowsList
251+
} else {
252+
workflows["workflows"] = []appWorkflow.AppWorkflowDto{}
253+
}
254+
common.WriteJsonResp(w, err, workflows, http.StatusOK)
255+
}
256+
257+
func (handler *AppWorkflowRestHandlerImpl) checkAuthBatch(emailId string, appObject []string, envObject []string) (map[string]bool, map[string]bool) {
258+
var appResult map[string]bool
259+
var envResult map[string]bool
260+
if len(appObject) > 0 {
261+
appResult = handler.enforcer.EnforceByEmailInBatch(emailId, casbin.ResourceApplications, casbin.ActionGet, appObject)
262+
}
263+
if len(envObject) > 0 {
264+
envResult = handler.enforcer.EnforceByEmailInBatch(emailId, casbin.ResourceEnvironment, casbin.ActionGet, envObject)
265+
}
266+
return appResult, envResult
267+
}

api/restHandler/app/BuildPipelineRestHandler.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type DevtronAppBuildRestHandler interface {
4040
CancelWorkflow(w http.ResponseWriter, r *http.Request)
4141

4242
UpdateBranchCiPipelinesWithRegex(w http.ResponseWriter, r *http.Request)
43+
GetCiPipelineByEnvironment(w http.ResponseWriter, r *http.Request)
44+
GetExternalCiByEnvironment(w http.ResponseWriter, r *http.Request)
4345
}
4446

4547
type DevtronAppBuildMaterialRestHandler interface {
@@ -1243,3 +1245,58 @@ func (handler PipelineConfigRestHandlerImpl) FetchWorkflowDetails(w http.Respons
12431245
}
12441246
common.WriteJsonResp(w, err, resp, http.StatusOK)
12451247
}
1248+
1249+
func (handler PipelineConfigRestHandlerImpl) GetCiPipelineByEnvironment(w http.ResponseWriter, r *http.Request) {
1250+
vars := mux.Vars(r)
1251+
userId, err := handler.userAuthService.GetLoggedInUser(r)
1252+
if userId == 0 || err != nil {
1253+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
1254+
return
1255+
}
1256+
user, err := handler.userAuthService.GetById(userId)
1257+
if userId == 0 || err != nil {
1258+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
1259+
return
1260+
}
1261+
userEmailId := strings.ToLower(user.EmailId)
1262+
envId, err := strconv.Atoi(vars["envId"])
1263+
if err != nil {
1264+
handler.Logger.Errorw("request err, GetCdPipelines", "err", err, "envId", envId)
1265+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
1266+
return
1267+
}
1268+
ciConf, err := handler.pipelineBuilder.GetCiPipelineByEnvironment(envId, userEmailId, handler.checkAuthBatch)
1269+
if err != nil {
1270+
handler.Logger.Errorw("service err, GetCiPipeline", "err", err, "envId", envId)
1271+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
1272+
return
1273+
}
1274+
common.WriteJsonResp(w, err, ciConf, http.StatusOK)
1275+
}
1276+
1277+
func (handler PipelineConfigRestHandlerImpl) GetExternalCiByEnvironment(w http.ResponseWriter, r *http.Request) {
1278+
vars := mux.Vars(r)
1279+
userId, err := handler.userAuthService.GetLoggedInUser(r)
1280+
if userId == 0 || err != nil {
1281+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
1282+
return
1283+
}
1284+
user, err := handler.userAuthService.GetById(userId)
1285+
if userId == 0 || err != nil {
1286+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
1287+
return
1288+
}
1289+
userEmailId := strings.ToLower(user.EmailId)
1290+
envId, err := strconv.Atoi(vars["envId"])
1291+
if err != nil {
1292+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
1293+
return
1294+
}
1295+
ciConf, err := handler.pipelineBuilder.GetExternalCiByEnvironment(envId, userEmailId, handler.checkAuthBatch)
1296+
if err != nil {
1297+
handler.Logger.Errorw("service err, GetExternalCi", "err", err, "envId", envId)
1298+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
1299+
return
1300+
}
1301+
common.WriteJsonResp(w, err, ciConf, http.StatusOK)
1302+
}

api/restHandler/app/DeploymentPipelineRestHandler.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type DevtronAppDeploymentRestHandler interface {
3535

3636
IsReadyToTrigger(w http.ResponseWriter, r *http.Request)
3737
FetchCdWorkflowDetails(w http.ResponseWriter, r *http.Request)
38+
GetCdPipelinesByEnvironment(w http.ResponseWriter, r *http.Request)
3839
}
3940

4041
type DevtronAppDeploymentConfigRestHandler interface {
@@ -1578,13 +1579,7 @@ func (handler PipelineConfigRestHandlerImpl) GetCdPipelineById(w http.ResponseWr
15781579
return
15791580
}
15801581

1581-
envId, err := handler.pipelineBuilder.GetEnvironmentByCdPipelineId(pipelineId)
1582-
if err != nil {
1583-
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
1584-
return
1585-
}
1586-
1587-
envObject := handler.enforcerUtil.GetEnvRBACNameByCdPipelineIdAndEnvId(pipelineId, envId)
1582+
envObject := handler.enforcerUtil.GetEnvRBACNameByCdPipelineIdAndEnvId(pipelineId)
15881583
if ok := handler.enforcer.Enforce(token, casbin.ResourceEnvironment, casbin.ActionUpdate, envObject); !ok {
15891584
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
15901585
return
@@ -1874,3 +1869,43 @@ func (handler PipelineConfigRestHandlerImpl) UpgradeForAllApps(w http.ResponseWr
18741869
response["failed"] = failedIds
18751870
common.WriteJsonResp(w, err, response, http.StatusOK)
18761871
}
1872+
1873+
func (handler PipelineConfigRestHandlerImpl) GetCdPipelinesByEnvironment(w http.ResponseWriter, r *http.Request) {
1874+
vars := mux.Vars(r)
1875+
userId, err := handler.userAuthService.GetLoggedInUser(r)
1876+
if userId == 0 || err != nil {
1877+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
1878+
return
1879+
}
1880+
user, err := handler.userAuthService.GetById(userId)
1881+
if userId == 0 || err != nil {
1882+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
1883+
return
1884+
}
1885+
userEmailId := strings.ToLower(user.EmailId)
1886+
envId, err := strconv.Atoi(vars["envId"])
1887+
if err != nil {
1888+
handler.Logger.Errorw("request err, GetCdPipelines", "err", err, "envId", envId)
1889+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
1890+
return
1891+
}
1892+
results, err := handler.pipelineBuilder.GetCdPipelinesByEnvironment(envId, userEmailId, handler.checkAuthBatch)
1893+
if err != nil {
1894+
handler.Logger.Errorw("service err, GetCdPipelines", "err", err, "envId", envId)
1895+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
1896+
return
1897+
}
1898+
common.WriteJsonResp(w, err, results, http.StatusOK)
1899+
}
1900+
1901+
func (handler *PipelineConfigRestHandlerImpl) checkAuthBatch(emailId string, appObject []string, envObject []string) (map[string]bool, map[string]bool) {
1902+
var appResult map[string]bool
1903+
var envResult map[string]bool
1904+
if len(appObject) > 0 {
1905+
appResult = handler.enforcer.EnforceByEmailInBatch(emailId, casbin.ResourceApplications, casbin.ActionGet, appObject)
1906+
}
1907+
if len(envObject) > 0 {
1908+
envResult = handler.enforcer.EnforceByEmailInBatch(emailId, casbin.ResourceEnvironment, casbin.ActionGet, envObject)
1909+
}
1910+
return appResult, envResult
1911+
}

0 commit comments

Comments
 (0)