@@ -21,10 +21,10 @@ import (
2121 "github.com/devtron-labs/devtron/api/restHandler/common"
2222 "github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
2323 "github.com/devtron-labs/devtron/pkg/auth/user"
24- "github.com/devtron-labs/devtron/pkg/infraConfig"
2524 "github.com/devtron-labs/devtron/pkg/infraConfig/adapter"
2625 "github.com/devtron-labs/devtron/pkg/infraConfig/bean"
27- "github.com/devtron-labs/devtron/pkg/infraConfig/util"
26+ "github.com/devtron-labs/devtron/pkg/infraConfig/service"
27+ util2 "github.com/devtron-labs/devtron/pkg/infraConfig/util"
2828 "github.com/devtron-labs/devtron/util/rbac"
2929 "github.com/gorilla/mux"
3030 "github.com/pkg/errors"
@@ -35,25 +35,24 @@ import (
3535)
3636
3737type InfraConfigRestHandler interface {
38- UpdateInfraProfile (w http.ResponseWriter , r * http.Request )
3938 GetProfile (w http.ResponseWriter , r * http.Request )
40-
41- // Deprecated: UpdateInfraProfileV0 is deprecated in favour of UpdateInfraProfile
42- UpdateInfraProfileV0 (w http.ResponseWriter , r * http.Request )
39+ UpdateInfraProfile (w http.ResponseWriter , r * http.Request )
4340
4441 // Deprecated: GetProfileV0 is deprecated in favour of GetProfile
4542 GetProfileV0 (w http.ResponseWriter , r * http.Request )
43+ // Deprecated: UpdateInfraProfileV0 is deprecated in favour of UpdateInfraProfile
44+ UpdateInfraProfileV0 (w http.ResponseWriter , r * http.Request )
4645}
4746type InfraConfigRestHandlerImpl struct {
4847 logger * zap.SugaredLogger
49- infraProfileService infraConfig .InfraConfigService
48+ infraProfileService service .InfraConfigService
5049 userService user.UserService
5150 enforcer casbin.Enforcer
5251 enforcerUtil rbac.EnforcerUtil
5352 validator * validator.Validate
5453}
5554
56- func NewInfraConfigRestHandlerImpl (logger * zap.SugaredLogger , infraProfileService infraConfig .InfraConfigService , userService user.UserService , enforcer casbin.Enforcer , enforcerUtil rbac.EnforcerUtil , validator * validator.Validate ) * InfraConfigRestHandlerImpl {
55+ func NewInfraConfigRestHandlerImpl (logger * zap.SugaredLogger , infraProfileService service .InfraConfigService , userService user.UserService , enforcer casbin.Enforcer , enforcerUtil rbac.EnforcerUtil , validator * validator.Validate ) * InfraConfigRestHandlerImpl {
5756 return & InfraConfigRestHandlerImpl {
5857 logger : logger ,
5958 infraProfileService : infraProfileService ,
@@ -64,6 +63,46 @@ func NewInfraConfigRestHandlerImpl(logger *zap.SugaredLogger, infraProfileServic
6463 }
6564}
6665
66+ func (handler * InfraConfigRestHandlerImpl ) GetProfile (w http.ResponseWriter , r * http.Request ) {
67+ userId , err := handler .userService .GetLoggedInUser (r )
68+ if userId == 0 || err != nil {
69+ common .WriteJsonResp (w , err , "Unauthorized User" , http .StatusUnauthorized )
70+ return
71+ }
72+ token := r .Header .Get ("token" )
73+ if ok := handler .enforcer .Enforce (token , casbin .ResourceGlobal , casbin .ActionGet , "*" ); ! ok {
74+ common .WriteJsonResp (w , errors .New ("unauthorized" ), nil , http .StatusForbidden )
75+ return
76+ }
77+
78+ identifier := r .URL .Query ().Get ("name" )
79+
80+ if len (identifier ) == 0 {
81+ common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
82+ return
83+ }
84+ profileName := strings .ToLower (identifier )
85+ var profile * bean.ProfileBeanDto
86+ if profileName != bean .GLOBAL_PROFILE_NAME {
87+ common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
88+ return
89+ }
90+ defaultProfile , err := handler .infraProfileService .GetProfileByName (profileName )
91+ if err != nil {
92+ common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
93+ return
94+ }
95+ profile = defaultProfile
96+
97+ resp := bean.ProfileResponse {
98+ Profile : * profile ,
99+ }
100+ resp .ConfigurationUnits = handler .infraProfileService .GetConfigurationUnits ()
101+ //TODO: why below line ??
102+ resp .DefaultConfigurations = defaultProfile .Configurations
103+ common .WriteJsonResp (w , nil , resp , http .StatusOK )
104+ }
105+
67106func (handler * InfraConfigRestHandlerImpl ) UpdateInfraProfile (w http.ResponseWriter , r * http.Request ) {
68107 userId , err := handler .userService .GetLoggedInUser (r )
69108 if userId == 0 || err != nil {
@@ -76,39 +115,44 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfile(w http.ResponseWri
76115 return
77116 }
78117
79- vars := mux .Vars (r )
80- profileName := strings . ToLower ( vars [ "name" ] )
81- if profileName == "" {
82- common .WriteJsonResp (w , errors .New (util .InvalidProfileName ), nil , http .StatusBadRequest )
118+ // vars := mux.Vars(r)
119+ val := r . URL . Query (). Get ( "name" )
120+ if len ( val ) == 0 {
121+ common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
83122 return
84123 }
85- payload := & bean.ProfileBean {}
124+ profileName := strings .ToLower (val )
125+
126+ payload := & bean.ProfileBeanDto {}
86127 decoder := json .NewDecoder (r .Body )
87128 err = decoder .Decode (payload )
88129 if err != nil {
89130 handler .logger .Errorw ("error in decoding the request payload" , "err" , err , "requestBody" , r .Body )
90131 common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
91132 return
92133 }
134+ //handler.validator.RegisterValidation("validateValue", ValidateValue)
93135 payload .Name = strings .ToLower (payload .Name )
94136 err = handler .validator .Struct (payload )
95137 if err != nil {
96- err = errors .Wrap (err , util .PayloadValidationError )
138+ err = errors .Wrap (err , bean .PayloadValidationError )
97139 common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
98140 }
99- if profileName == "" || (profileName == util .DEFAULT_PROFILE_NAME && payload .Name != util .DEFAULT_PROFILE_NAME ) {
100- common .WriteJsonResp (w , errors .New (util .InvalidProfileName ), nil , http .StatusBadRequest )
141+ if ! util2 .IsValidProfileNameRequested (profileName , payload .Name ) {
142+ common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
143+ return
101144 }
102145 err = handler .infraProfileService .UpdateProfile (userId , profileName , payload )
103146 if err != nil {
104147 handler .logger .Errorw ("error in updating profile and configurations" , "profileName" , profileName , "payLoad" , payload , "err" , err )
105- common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
148+ common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
106149 return
107150 }
108151 common .WriteJsonResp (w , nil , nil , http .StatusOK )
109152}
110153
111- func (handler * InfraConfigRestHandlerImpl ) GetProfile (w http.ResponseWriter , r * http.Request ) {
154+ // Deprecated
155+ func (handler * InfraConfigRestHandlerImpl ) GetProfileV0 (w http.ResponseWriter , r * http.Request ) {
112156 userId , err := handler .userService .GetLoggedInUser (r )
113157 if userId == 0 || err != nil {
114158 common .WriteJsonResp (w , err , "Unauthorized User" , http .StatusUnauthorized )
@@ -123,25 +167,30 @@ func (handler *InfraConfigRestHandlerImpl) GetProfile(w http.ResponseWriter, r *
123167 vars := mux .Vars (r )
124168 profileName := strings .ToLower (vars ["name" ])
125169 if profileName == "" {
126- common .WriteJsonResp (w , errors .New (util .InvalidProfileName ), nil , http .StatusBadRequest )
170+ common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
127171 return
128172 }
129173
130- var profile * bean.ProfileBean
131- defaultProfile , err := handler .infraProfileService .GetProfileByName (profileName )
174+ if profileName != bean .DEFAULT_PROFILE_NAME {
175+ common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
176+ return
177+ }
178+
179+ profileName = bean .GLOBAL_PROFILE_NAME
180+
181+ var profile * bean.ProfileBeanV0
182+ defaultProfileV1 , err := handler .infraProfileService .GetProfileByName (profileName )
183+ defaultProfileV0 := adapter .GetV0ProfileBean (defaultProfileV1 )
132184 if err != nil {
133185 common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
134186 return
135187 }
136- if profileName == util .DEFAULT_PROFILE_NAME {
137- profile = defaultProfile
138- }
139- resp := bean.ProfileResponse {
188+ profile = defaultProfileV0
189+ resp := bean.ProfileResponseV0 {
140190 Profile : * profile ,
141191 }
142192 resp .ConfigurationUnits = handler .infraProfileService .GetConfigurationUnits ()
143- //TODO: why below line ??
144- resp .DefaultConfigurations = defaultProfile .Configurations
193+ resp .DefaultConfigurations = defaultProfileV0 .Configurations
145194 common .WriteJsonResp (w , nil , resp , http .StatusOK )
146195}
147196
@@ -161,7 +210,7 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfileV0(w http.ResponseW
161210 vars := mux .Vars (r )
162211 profileName := strings .ToLower (vars ["name" ])
163212 if profileName == "" {
164- common .WriteJsonResp (w , errors .New (util .InvalidProfileName ), nil , http .StatusBadRequest )
213+ common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
165214 return
166215 }
167216 payload := & bean.ProfileBeanV0 {}
@@ -175,57 +224,25 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfileV0(w http.ResponseW
175224 payload .Name = strings .ToLower (payload .Name )
176225 err = handler .validator .Struct (payload )
177226 if err != nil {
178- err = errors .Wrap (err , util .PayloadValidationError )
227+ err = errors .Wrap (err , bean .PayloadValidationError )
179228 common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
180229 }
181- if profileName == "" || (profileName == util .DEFAULT_PROFILE_NAME && payload .Name != util .DEFAULT_PROFILE_NAME ) {
182- common .WriteJsonResp (w , errors .New (util .InvalidProfileName ), nil , http .StatusBadRequest )
183- }
184- payloadV1 := adapter .GetV1ProfileBean (payload )
185- err = handler .infraProfileService .UpdateProfile (userId , profileName , payloadV1 )
186- if err != nil {
187- handler .logger .Errorw ("error in updating profile and configurations" , "profileName" , profileName , "payLoad" , payload , "err" , err )
188- common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
230+ if ! util2 .IsValidProfileNameRequestedV0 (profileName , payload .Name ) {
231+ common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
189232 return
190233 }
191- common .WriteJsonResp (w , nil , nil , http .StatusOK )
192- }
193-
194- // Deprecated
195- func (handler * InfraConfigRestHandlerImpl ) GetProfileV0 (w http.ResponseWriter , r * http.Request ) {
196- userId , err := handler .userService .GetLoggedInUser (r )
197- if userId == 0 || err != nil {
198- common .WriteJsonResp (w , err , "Unauthorized User" , http .StatusUnauthorized )
199- return
200- }
201- token := r .Header .Get ("token" )
202- if ok := handler .enforcer .Enforce (token , casbin .ResourceGlobal , casbin .ActionGet , "*" ); ! ok {
203- common .WriteJsonResp (w , errors .New ("unauthorized" ), nil , http .StatusForbidden )
234+ if payload .Name != bean .DEFAULT_PROFILE_NAME {
235+ common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
204236 return
205237 }
206238
207- vars := mux .Vars (r )
208- profileName := strings .ToLower (vars ["name" ])
209- if profileName == "" {
210- common .WriteJsonResp (w , errors .New (util .InvalidProfileName ), nil , http .StatusBadRequest )
211- return
212- }
213-
214- var profile * bean.ProfileBeanV0
215- defaultProfileV1 , err := handler .infraProfileService .GetProfileByName (profileName )
216- defaultProfileV0 := adapter .GetV0ProfileBean (defaultProfileV1 )
239+ profileName = bean .GLOBAL_PROFILE_NAME
240+ payloadV1 := adapter .GetV1ProfileBean (payload )
241+ err = handler .infraProfileService .UpdateProfile (userId , profileName , payloadV1 )
217242 if err != nil {
243+ handler .logger .Errorw ("error in updating profile and configurations" , "profileName" , profileName , "payLoad" , payload , "err" , err )
218244 common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
219245 return
220246 }
221- if profileName == util .DEFAULT_PROFILE_NAME {
222- profile = defaultProfileV0
223- }
224- resp := bean.ProfileResponseV0 {
225- Profile : * profile ,
226- }
227- resp .ConfigurationUnits = handler .infraProfileService .GetConfigurationUnits ()
228- //TODO: why below line ??
229- resp .DefaultConfigurations = defaultProfileV0 .Configurations
230- common .WriteJsonResp (w , nil , resp , http .StatusOK )
247+ common .WriteJsonResp (w , nil , nil , http .StatusOK )
231248}
0 commit comments