|
| 1 | +package fluxApplication |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/devtron-labs/devtron/api/restHandler/common" |
| 6 | + "github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin" |
| 7 | + clientErrors "github.com/devtron-labs/devtron/pkg/errors" |
| 8 | + "github.com/devtron-labs/devtron/pkg/fluxApplication" |
| 9 | + "github.com/gorilla/mux" |
| 10 | + "go.uber.org/zap" |
| 11 | + "net/http" |
| 12 | +) |
| 13 | + |
| 14 | +type FluxApplicationRestHandler interface { |
| 15 | + ListFluxApplications(w http.ResponseWriter, r *http.Request) |
| 16 | + GetApplicationDetail(w http.ResponseWriter, r *http.Request) |
| 17 | +} |
| 18 | + |
| 19 | +type FluxApplicationRestHandlerImpl struct { |
| 20 | + fluxApplicationService fluxApplication.FluxApplicationService |
| 21 | + logger *zap.SugaredLogger |
| 22 | + enforcer casbin.Enforcer |
| 23 | +} |
| 24 | + |
| 25 | +func NewFluxApplicationRestHandlerImpl(fluxApplicationService fluxApplication.FluxApplicationService, |
| 26 | + logger *zap.SugaredLogger, enforcer casbin.Enforcer) *FluxApplicationRestHandlerImpl { |
| 27 | + return &FluxApplicationRestHandlerImpl{ |
| 28 | + fluxApplicationService: fluxApplicationService, |
| 29 | + logger: logger, |
| 30 | + enforcer: enforcer, |
| 31 | + } |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +func (handler *FluxApplicationRestHandlerImpl) ListFluxApplications(w http.ResponseWriter, r *http.Request) { |
| 36 | + |
| 37 | + //handle super-admin RBAC |
| 38 | + token := r.Header.Get("token") |
| 39 | + if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); !ok { |
| 40 | + common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden) |
| 41 | + return |
| 42 | + } |
| 43 | + v := r.URL.Query() |
| 44 | + clusterIdString := v.Get("clusterIds") |
| 45 | + var clusterIds []int |
| 46 | + var err error |
| 47 | + |
| 48 | + //handling when the clusterIds string is empty ,it will not support the |
| 49 | + if len(clusterIdString) == 0 { |
| 50 | + handler.logger.Errorw("error in getting cluster ids", "error", err, "clusterIds", clusterIds) |
| 51 | + common.WriteJsonResp(w, errors.New("error in getting cluster ids"), nil, http.StatusBadRequest) |
| 52 | + return |
| 53 | + } |
| 54 | + clusterIds, err = common.ExtractIntArrayQueryParam(w, r, "clusterIds") |
| 55 | + if err != nil { |
| 56 | + handler.logger.Errorw("error in parsing cluster ids", "error", err, "clusterIds", clusterIds) |
| 57 | + return |
| 58 | + } |
| 59 | + handler.logger.Debugw("extracted ClusterIds successfully ", "clusterIds", clusterIds) |
| 60 | + handler.fluxApplicationService.ListFluxApplications(r.Context(), clusterIds, w) |
| 61 | +} |
| 62 | + |
| 63 | +func (handler *FluxApplicationRestHandlerImpl) GetApplicationDetail(w http.ResponseWriter, r *http.Request) { |
| 64 | + vars := mux.Vars(r) |
| 65 | + appIdString := vars["appId"] |
| 66 | + appIdentifier, err := fluxApplication.DecodeFluxExternalAppId(appIdString) |
| 67 | + if err != nil { |
| 68 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 69 | + return |
| 70 | + } |
| 71 | + if appIdentifier.IsKustomizeApp == true && appIdentifier.Name == "flux-system" && appIdentifier.Namespace == "flux-system" { |
| 72 | + |
| 73 | + common.WriteJsonResp(w, errors.New("cannot proceed for the flux system root level "), nil, http.StatusBadRequest) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + // handle super-admin RBAC |
| 78 | + token := r.Header.Get("token") |
| 79 | + if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); !ok { |
| 80 | + common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden) |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + res, err := handler.fluxApplicationService.GetFluxAppDetail(r.Context(), appIdentifier) |
| 85 | + if err != nil { |
| 86 | + apiError := clientErrors.ConvertToApiError(err) |
| 87 | + if apiError != nil { |
| 88 | + err = apiError |
| 89 | + } |
| 90 | + common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) |
| 91 | + return |
| 92 | + } |
| 93 | + common.WriteJsonResp(w, err, res, http.StatusOK) |
| 94 | +} |
0 commit comments