@@ -21,10 +21,10 @@ import (
21
21
"github.com/devtron-labs/devtron/api/restHandler/common"
22
22
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
23
23
"github.com/devtron-labs/devtron/pkg/auth/user"
24
- "github.com/devtron-labs/devtron/pkg/infraConfig"
25
24
"github.com/devtron-labs/devtron/pkg/infraConfig/adapter"
26
25
"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"
28
28
"github.com/devtron-labs/devtron/util/rbac"
29
29
"github.com/gorilla/mux"
30
30
"github.com/pkg/errors"
@@ -35,25 +35,24 @@ import (
35
35
)
36
36
37
37
type InfraConfigRestHandler interface {
38
- UpdateInfraProfile (w http.ResponseWriter , r * http.Request )
39
38
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 )
43
40
44
41
// Deprecated: GetProfileV0 is deprecated in favour of GetProfile
45
42
GetProfileV0 (w http.ResponseWriter , r * http.Request )
43
+ // Deprecated: UpdateInfraProfileV0 is deprecated in favour of UpdateInfraProfile
44
+ UpdateInfraProfileV0 (w http.ResponseWriter , r * http.Request )
46
45
}
47
46
type InfraConfigRestHandlerImpl struct {
48
47
logger * zap.SugaredLogger
49
- infraProfileService infraConfig .InfraConfigService
48
+ infraProfileService service .InfraConfigService
50
49
userService user.UserService
51
50
enforcer casbin.Enforcer
52
51
enforcerUtil rbac.EnforcerUtil
53
52
validator * validator.Validate
54
53
}
55
54
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 {
57
56
return & InfraConfigRestHandlerImpl {
58
57
logger : logger ,
59
58
infraProfileService : infraProfileService ,
@@ -64,6 +63,46 @@ func NewInfraConfigRestHandlerImpl(logger *zap.SugaredLogger, infraProfileServic
64
63
}
65
64
}
66
65
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
+
67
106
func (handler * InfraConfigRestHandlerImpl ) UpdateInfraProfile (w http.ResponseWriter , r * http.Request ) {
68
107
userId , err := handler .userService .GetLoggedInUser (r )
69
108
if userId == 0 || err != nil {
@@ -76,39 +115,44 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfile(w http.ResponseWri
76
115
return
77
116
}
78
117
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 )
83
122
return
84
123
}
85
- payload := & bean.ProfileBean {}
124
+ profileName := strings .ToLower (val )
125
+
126
+ payload := & bean.ProfileBeanDto {}
86
127
decoder := json .NewDecoder (r .Body )
87
128
err = decoder .Decode (payload )
88
129
if err != nil {
89
130
handler .logger .Errorw ("error in decoding the request payload" , "err" , err , "requestBody" , r .Body )
90
131
common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
91
132
return
92
133
}
134
+ //handler.validator.RegisterValidation("validateValue", ValidateValue)
93
135
payload .Name = strings .ToLower (payload .Name )
94
136
err = handler .validator .Struct (payload )
95
137
if err != nil {
96
- err = errors .Wrap (err , util .PayloadValidationError )
138
+ err = errors .Wrap (err , bean .PayloadValidationError )
97
139
common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
98
140
}
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
101
144
}
102
145
err = handler .infraProfileService .UpdateProfile (userId , profileName , payload )
103
146
if err != nil {
104
147
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 )
106
149
return
107
150
}
108
151
common .WriteJsonResp (w , nil , nil , http .StatusOK )
109
152
}
110
153
111
- func (handler * InfraConfigRestHandlerImpl ) GetProfile (w http.ResponseWriter , r * http.Request ) {
154
+ // Deprecated
155
+ func (handler * InfraConfigRestHandlerImpl ) GetProfileV0 (w http.ResponseWriter , r * http.Request ) {
112
156
userId , err := handler .userService .GetLoggedInUser (r )
113
157
if userId == 0 || err != nil {
114
158
common .WriteJsonResp (w , err , "Unauthorized User" , http .StatusUnauthorized )
@@ -123,25 +167,30 @@ func (handler *InfraConfigRestHandlerImpl) GetProfile(w http.ResponseWriter, r *
123
167
vars := mux .Vars (r )
124
168
profileName := strings .ToLower (vars ["name" ])
125
169
if profileName == "" {
126
- common .WriteJsonResp (w , errors .New (util .InvalidProfileName ), nil , http .StatusBadRequest )
170
+ common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
127
171
return
128
172
}
129
173
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 )
132
184
if err != nil {
133
185
common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
134
186
return
135
187
}
136
- if profileName == util .DEFAULT_PROFILE_NAME {
137
- profile = defaultProfile
138
- }
139
- resp := bean.ProfileResponse {
188
+ profile = defaultProfileV0
189
+ resp := bean.ProfileResponseV0 {
140
190
Profile : * profile ,
141
191
}
142
192
resp .ConfigurationUnits = handler .infraProfileService .GetConfigurationUnits ()
143
- //TODO: why below line ??
144
- resp .DefaultConfigurations = defaultProfile .Configurations
193
+ resp .DefaultConfigurations = defaultProfileV0 .Configurations
145
194
common .WriteJsonResp (w , nil , resp , http .StatusOK )
146
195
}
147
196
@@ -161,7 +210,7 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfileV0(w http.ResponseW
161
210
vars := mux .Vars (r )
162
211
profileName := strings .ToLower (vars ["name" ])
163
212
if profileName == "" {
164
- common .WriteJsonResp (w , errors .New (util .InvalidProfileName ), nil , http .StatusBadRequest )
213
+ common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
165
214
return
166
215
}
167
216
payload := & bean.ProfileBeanV0 {}
@@ -175,57 +224,25 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfileV0(w http.ResponseW
175
224
payload .Name = strings .ToLower (payload .Name )
176
225
err = handler .validator .Struct (payload )
177
226
if err != nil {
178
- err = errors .Wrap (err , util .PayloadValidationError )
227
+ err = errors .Wrap (err , bean .PayloadValidationError )
179
228
common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
180
229
}
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 )
189
232
return
190
233
}
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 )
204
236
return
205
237
}
206
238
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 )
217
242
if err != nil {
243
+ handler .logger .Errorw ("error in updating profile and configurations" , "profileName" , profileName , "payLoad" , payload , "err" , err )
218
244
common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
219
245
return
220
246
}
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 )
231
248
}
0 commit comments