|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Devtron Labs |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package externalLink |
| 19 | + |
| 20 | +import ( |
| 21 | + "encoding/json" |
| 22 | + "github.com/devtron-labs/devtron/api/restHandler/common" |
| 23 | + "github.com/devtron-labs/devtron/pkg/externalLink" |
| 24 | + "github.com/devtron-labs/devtron/pkg/user" |
| 25 | + "github.com/devtron-labs/devtron/pkg/user/casbin" |
| 26 | + "github.com/gorilla/mux" |
| 27 | + "github.com/juju/errors" |
| 28 | + "go.uber.org/zap" |
| 29 | + "net/http" |
| 30 | + "strconv" |
| 31 | +) |
| 32 | + |
| 33 | +type ExternalLinkRestHandler interface { |
| 34 | + CreateExternalLinks(w http.ResponseWriter, r *http.Request) |
| 35 | + GetExternalLinkMonitoringTools(w http.ResponseWriter, r *http.Request) |
| 36 | + GetExternalLinks(w http.ResponseWriter, r *http.Request) |
| 37 | + UpdateExternalLink(w http.ResponseWriter, r *http.Request) |
| 38 | + DeleteExternalLink(w http.ResponseWriter, r *http.Request) // Update is_active to false link |
| 39 | +} |
| 40 | +type ExternalLinkRestHandlerImpl struct { |
| 41 | + logger *zap.SugaredLogger |
| 42 | + externalLinkService externalLink.ExternalLinkService |
| 43 | + userService user.UserService |
| 44 | + enforcer casbin.Enforcer |
| 45 | +} |
| 46 | + |
| 47 | +func NewExternalLinkRestHandlerImpl(logger *zap.SugaredLogger, |
| 48 | + externalLinkService externalLink.ExternalLinkService, |
| 49 | + userService user.UserService, |
| 50 | + enforcer casbin.Enforcer, |
| 51 | +) *ExternalLinkRestHandlerImpl { |
| 52 | + return &ExternalLinkRestHandlerImpl{ |
| 53 | + logger: logger, |
| 54 | + externalLinkService: externalLinkService, |
| 55 | + userService: userService, |
| 56 | + enforcer: enforcer, |
| 57 | + } |
| 58 | +} |
| 59 | +func (impl ExternalLinkRestHandlerImpl) CreateExternalLinks(w http.ResponseWriter, r *http.Request) { |
| 60 | + userId, err := impl.userService.GetLoggedInUser(r) |
| 61 | + if userId == 0 || err != nil { |
| 62 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) |
| 63 | + return |
| 64 | + } |
| 65 | + decoder := json.NewDecoder(r.Body) |
| 66 | + var beans []*externalLink.ExternalLinkDto |
| 67 | + err = decoder.Decode(&beans) |
| 68 | + if err != nil { |
| 69 | + impl.logger.Errorw("request err, SaveLink", "err", err, "payload", beans) |
| 70 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + token := r.Header.Get("token") |
| 75 | + if ok := impl.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*"); !ok { |
| 76 | + common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + res, err := impl.externalLinkService.Create(beans, userId) |
| 81 | + if err != nil { |
| 82 | + impl.logger.Errorw("service err, SaveLink", "err", err, "payload", beans) |
| 83 | + common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) |
| 84 | + return |
| 85 | + } |
| 86 | + common.WriteJsonResp(w, err, res, http.StatusOK) |
| 87 | +} |
| 88 | +func (impl ExternalLinkRestHandlerImpl) GetExternalLinkMonitoringTools(w http.ResponseWriter, r *http.Request) { |
| 89 | + userId, err := impl.userService.GetLoggedInUser(r) |
| 90 | + if userId == 0 || err != nil { |
| 91 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + // auth free api as we using this for multiple places |
| 96 | + |
| 97 | + res, err := impl.externalLinkService.GetAllActiveTools() |
| 98 | + if err != nil { |
| 99 | + impl.logger.Errorw("service err, GetAllActiveTools", "err", err) |
| 100 | + common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) |
| 101 | + return |
| 102 | + } |
| 103 | + common.WriteJsonResp(w, err, res, http.StatusOK) |
| 104 | +} |
| 105 | +func (impl ExternalLinkRestHandlerImpl) GetExternalLinks(w http.ResponseWriter, r *http.Request) { |
| 106 | + userId, err := impl.userService.GetLoggedInUser(r) |
| 107 | + if userId == 0 || err != nil { |
| 108 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) |
| 109 | + return |
| 110 | + } |
| 111 | + v := r.URL.Query() |
| 112 | + id := v.Get("clusterId") |
| 113 | + clusterId := 0 |
| 114 | + if len(id) > 0 { |
| 115 | + clusterId, err = strconv.Atoi(id) |
| 116 | + if err != nil { |
| 117 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 118 | + return |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + //apply auth only in case when requested for all links |
| 123 | + if clusterId == 0 { |
| 124 | + token := r.Header.Get("token") |
| 125 | + if ok := impl.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); !ok { |
| 126 | + common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden) |
| 127 | + return |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + res, err := impl.externalLinkService.FetchAllActiveLinks(clusterId) |
| 132 | + if err != nil { |
| 133 | + impl.logger.Errorw("service err, FetchAllActive", "err", err) |
| 134 | + common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) |
| 135 | + return |
| 136 | + } |
| 137 | + common.WriteJsonResp(w, err, res, http.StatusOK) |
| 138 | +} |
| 139 | +func (impl ExternalLinkRestHandlerImpl) UpdateExternalLink(w http.ResponseWriter, r *http.Request) { |
| 140 | + userId, err := impl.userService.GetLoggedInUser(r) |
| 141 | + if userId == 0 || err != nil { |
| 142 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) |
| 143 | + return |
| 144 | + } |
| 145 | + decoder := json.NewDecoder(r.Body) |
| 146 | + var bean externalLink.ExternalLinkDto |
| 147 | + err = decoder.Decode(&bean) |
| 148 | + if err != nil { |
| 149 | + impl.logger.Errorw("request err, Update Link", "err", err, "bean", bean) |
| 150 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 151 | + return |
| 152 | + } |
| 153 | + bean.UserId = userId |
| 154 | + |
| 155 | + token := r.Header.Get("token") |
| 156 | + if ok := impl.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionUpdate, "*"); !ok { |
| 157 | + common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + impl.logger.Infow("request payload, UpdateLink", "err", err, "bean", bean) |
| 162 | + res, err := impl.externalLinkService.Update(&bean) |
| 163 | + if err != nil { |
| 164 | + impl.logger.Errorw("service err, Update Links", "err", err, "bean", bean) |
| 165 | + common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) |
| 166 | + return |
| 167 | + } |
| 168 | + common.WriteJsonResp(w, err, res, http.StatusOK) |
| 169 | +} |
| 170 | +func (impl ExternalLinkRestHandlerImpl) DeleteExternalLink(w http.ResponseWriter, r *http.Request) { |
| 171 | + userId, err := impl.userService.GetLoggedInUser(r) |
| 172 | + if userId == 0 || err != nil { |
| 173 | + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) |
| 174 | + return |
| 175 | + } |
| 176 | + params := mux.Vars(r) |
| 177 | + id := params["id"] |
| 178 | + idi, err := strconv.Atoi(id) |
| 179 | + if err != nil { |
| 180 | + impl.logger.Errorw("request err, DeleteExternalLink", "err", err, "id", id) |
| 181 | + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) |
| 182 | + return |
| 183 | + } |
| 184 | + |
| 185 | + token := r.Header.Get("token") |
| 186 | + if ok := impl.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionDelete, "*"); !ok { |
| 187 | + common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden) |
| 188 | + return |
| 189 | + } |
| 190 | + |
| 191 | + res, err := impl.externalLinkService.DeleteLink(idi, userId) |
| 192 | + if err != nil { |
| 193 | + impl.logger.Errorw("service err, delete Links", "err", err, "id", idi) |
| 194 | + common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) |
| 195 | + return |
| 196 | + } |
| 197 | + common.WriteJsonResp(w, err, res, http.StatusOK) |
| 198 | +} |
0 commit comments