-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support build infra configurations properties configurable from…
… ui (#4583) * intial commit * wip * default related crud * wip * update oss code * refactoring * add int to string conversions for cpu units * units loading fix * refactoring * refactoring * fix * const variable names updated * pointer fix * test: temporary fix for empty unit * wip * configurations key fix * update spec * unit conversion method update * add type param in profile bean * move constants to constants file * move constants to constants file * fix * change time units from s,m,h to seconds,minutes,hours * code refactoring around default configurations loading * add get infraConfig by scope method * folder structure refactoring * improve logging * sql script number update * resolving build infra configuration values form infraConfig service * script fix * remove readonly check from id * refactor * query fix * validation logic fix * validations added * update units * move infra getter logic to seperate service * script update * refactoring wip * refactoring done * rebase local * rebase local * wire * pointer fix * wire * script number update * safe check added * remove test code * refactoring * remove cd infra getter * add params in err log * minimize diff w.r.t ent * minor updates * support values upto 2 decimal points only * int max as upper limit on timeout * int max as upper limit on timeout * infra providers architecture refactoring * unique constraint remove * script number update
- Loading branch information
1 parent
b615f0d
commit 52c3e35
Showing
27 changed files
with
1,999 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package infraConfig | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/devtron-labs/devtron/api/restHandler/common" | ||
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin" | ||
"github.com/devtron-labs/devtron/pkg/auth/user" | ||
"github.com/devtron-labs/devtron/pkg/infraConfig" | ||
"github.com/devtron-labs/devtron/util/rbac" | ||
"github.com/gorilla/mux" | ||
"github.com/pkg/errors" | ||
"go.uber.org/zap" | ||
"gopkg.in/go-playground/validator.v9" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type InfraConfigRestHandler interface { | ||
UpdateInfraProfile(w http.ResponseWriter, r *http.Request) | ||
GetProfile(w http.ResponseWriter, r *http.Request) | ||
} | ||
type InfraConfigRestHandlerImpl struct { | ||
logger *zap.SugaredLogger | ||
infraProfileService infraConfig.InfraConfigService | ||
userService user.UserService | ||
enforcer casbin.Enforcer | ||
enforcerUtil rbac.EnforcerUtil | ||
validator *validator.Validate | ||
} | ||
|
||
func NewInfraConfigRestHandlerImpl(logger *zap.SugaredLogger, infraProfileService infraConfig.InfraConfigService, userService user.UserService, enforcer casbin.Enforcer, enforcerUtil rbac.EnforcerUtil, validator *validator.Validate) *InfraConfigRestHandlerImpl { | ||
return &InfraConfigRestHandlerImpl{ | ||
logger: logger, | ||
infraProfileService: infraProfileService, | ||
userService: userService, | ||
enforcer: enforcer, | ||
enforcerUtil: enforcerUtil, | ||
validator: validator, | ||
} | ||
} | ||
|
||
func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfile(w http.ResponseWriter, r *http.Request) { | ||
userId, err := handler.userService.GetLoggedInUser(r) | ||
if userId == 0 || err != nil { | ||
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) | ||
return | ||
} | ||
token := r.Header.Get("token") | ||
if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionUpdate, "*"); !ok { | ||
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden) | ||
return | ||
} | ||
|
||
vars := mux.Vars(r) | ||
profileName := strings.ToLower(vars["name"]) | ||
if profileName == "" { | ||
common.WriteJsonResp(w, errors.New(infraConfig.InvalidProfileName), nil, http.StatusBadRequest) | ||
return | ||
} | ||
payload := &infraConfig.ProfileBean{} | ||
decoder := json.NewDecoder(r.Body) | ||
err = decoder.Decode(payload) | ||
if err != nil { | ||
handler.logger.Errorw("error in decoding the request payload", "err", err, "requestBody", r.Body) | ||
common.WriteJsonResp(w, err, nil, http.StatusBadRequest) | ||
return | ||
} | ||
payload.Name = strings.ToLower(payload.Name) | ||
err = handler.validator.Struct(payload) | ||
if err != nil { | ||
err = errors.Wrap(err, infraConfig.PayloadValidationError) | ||
common.WriteJsonResp(w, err, nil, http.StatusBadRequest) | ||
} | ||
if profileName == "" || (profileName == infraConfig.DEFAULT_PROFILE_NAME && payload.Name != infraConfig.DEFAULT_PROFILE_NAME) { | ||
common.WriteJsonResp(w, errors.New(infraConfig.InvalidProfileName), nil, http.StatusBadRequest) | ||
} | ||
err = handler.infraProfileService.UpdateProfile(userId, profileName, payload) | ||
if err != nil { | ||
handler.logger.Errorw("error in updating profile and configurations", "profileName", profileName, "payLoad", payload, "err", err) | ||
common.WriteJsonResp(w, err, nil, http.StatusBadRequest) | ||
return | ||
} | ||
common.WriteJsonResp(w, nil, nil, http.StatusOK) | ||
} | ||
|
||
func (handler *InfraConfigRestHandlerImpl) GetProfile(w http.ResponseWriter, r *http.Request) { | ||
userId, err := handler.userService.GetLoggedInUser(r) | ||
if userId == 0 || err != nil { | ||
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) | ||
return | ||
} | ||
token := r.Header.Get("token") | ||
if ok := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); !ok { | ||
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden) | ||
return | ||
} | ||
|
||
vars := mux.Vars(r) | ||
profileName := strings.ToLower(vars["name"]) | ||
if profileName == "" { | ||
common.WriteJsonResp(w, errors.New(infraConfig.InvalidProfileName), nil, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
var profile *infraConfig.ProfileBean | ||
defaultProfile, err := handler.infraProfileService.GetProfileByName(profileName) | ||
if err != nil { | ||
common.WriteJsonResp(w, err, nil, http.StatusBadRequest) | ||
return | ||
} | ||
if profileName == infraConfig.DEFAULT_PROFILE_NAME { | ||
profile = defaultProfile | ||
} | ||
resp := infraConfig.ProfileResponse{ | ||
Profile: *profile, | ||
} | ||
resp.ConfigurationUnits = handler.infraProfileService.GetConfigurationUnits() | ||
resp.DefaultConfigurations = defaultProfile.Configurations | ||
common.WriteJsonResp(w, nil, resp, http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package infraConfig | ||
|
||
import "github.com/gorilla/mux" | ||
|
||
type InfraConfigRouter interface { | ||
InitInfraConfigRouter(configRouter *mux.Router) | ||
} | ||
|
||
type InfraConfigRouterImpl struct { | ||
infraConfigRestHandler InfraConfigRestHandler | ||
} | ||
|
||
func NewInfraProfileRouterImpl(infraConfigRestHandler InfraConfigRestHandler) *InfraConfigRouterImpl { | ||
return &InfraConfigRouterImpl{ | ||
infraConfigRestHandler: infraConfigRestHandler, | ||
} | ||
} | ||
|
||
func (impl *InfraConfigRouterImpl) InitInfraConfigRouter(configRouter *mux.Router) { | ||
configRouter.Path("/profile/{name}"). | ||
HandlerFunc(impl.infraConfigRestHandler.GetProfile). | ||
Methods("GET") | ||
|
||
configRouter.Path("/profile/{name}"). | ||
HandlerFunc(impl.infraConfigRestHandler.UpdateInfraProfile). | ||
Methods("PUT") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.