@@ -18,14 +18,18 @@ package infraConfig
18
18
19
19
import (
20
20
"encoding/json"
21
+ "fmt"
21
22
"github.com/devtron-labs/devtron/api/restHandler/common"
22
23
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
23
24
"github.com/devtron-labs/devtron/pkg/auth/user"
24
25
"github.com/devtron-labs/devtron/pkg/infraConfig/adapter"
25
- "github.com/devtron-labs/devtron/pkg/infraConfig/bean"
26
+ "github.com/devtron-labs/devtron/pkg/infraConfig/bean/v0"
27
+ "github.com/devtron-labs/devtron/pkg/infraConfig/bean/v1"
28
+ errors2 "github.com/devtron-labs/devtron/pkg/infraConfig/errors"
26
29
"github.com/devtron-labs/devtron/pkg/infraConfig/service"
27
- util2 "github.com/devtron-labs/devtron/pkg/infraConfig/util"
30
+ "github.com/devtron-labs/devtron/pkg/infraConfig/util"
28
31
"github.com/devtron-labs/devtron/util/rbac"
32
+ "github.com/go-pg/pg"
29
33
"github.com/gorilla/mux"
30
34
"github.com/pkg/errors"
31
35
"go.uber.org/zap"
@@ -42,13 +46,15 @@ type InfraConfigRestHandler interface {
42
46
GetProfileV0 (w http.ResponseWriter , r * http.Request )
43
47
// Deprecated: UpdateInfraProfileV0 is deprecated in favour of UpdateInfraProfile
44
48
UpdateInfraProfileV0 (w http.ResponseWriter , r * http.Request )
49
+
50
+ InfraConfigRestHandlerEnt
45
51
}
52
+
46
53
type InfraConfigRestHandlerImpl struct {
47
54
logger * zap.SugaredLogger
48
55
infraProfileService service.InfraConfigService
49
56
userService user.UserService
50
57
enforcer casbin.Enforcer
51
- enforcerUtil rbac.EnforcerUtil
52
58
validator * validator.Validate
53
59
}
54
60
@@ -58,7 +64,6 @@ func NewInfraConfigRestHandlerImpl(logger *zap.SugaredLogger, infraProfileServic
58
64
infraProfileService : infraProfileService ,
59
65
userService : userService ,
60
66
enforcer : enforcer ,
61
- enforcerUtil : enforcerUtil ,
62
67
validator : validator ,
63
68
}
64
69
}
@@ -76,29 +81,48 @@ func (handler *InfraConfigRestHandlerImpl) GetProfile(w http.ResponseWriter, r *
76
81
}
77
82
78
83
identifier := r .URL .Query ().Get ("name" )
79
-
80
84
if len (identifier ) == 0 {
81
- common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
85
+ common .WriteJsonResp (w , errors .New (errors2 .InvalidProfileName ), nil , http .StatusBadRequest )
82
86
return
83
87
}
84
88
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
+ var profile * v1.ProfileBeanDto
90
+ if profileName != v1 .GLOBAL_PROFILE_NAME {
91
+ profile , err = handler .infraProfileService .GetProfileByName (profileName )
92
+ if err != nil {
93
+ statusCode := http .StatusInternalServerError
94
+ if errors .Is (err , pg .ErrNoRows ) {
95
+ err = errors .New (fmt .Sprintf ("profile %s not found" , profileName ))
96
+ statusCode = http .StatusNotFound
97
+ }
98
+ common .WriteJsonResp (w , err , nil , statusCode )
99
+ return
100
+ }
89
101
}
90
- defaultProfile , err := handler .infraProfileService .GetProfileByName (profileName )
102
+
103
+ defaultProfile , err := handler .infraProfileService .GetProfileByName (v1 .GLOBAL_PROFILE_NAME )
91
104
if err != nil {
92
- common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
105
+ statusCode := http .StatusInternalServerError
106
+ if errors .Is (err , pg .ErrNoRows ) {
107
+ err = errors .New (fmt .Sprintf ("profile %s not found" , v1 .GLOBAL_PROFILE_NAME ))
108
+ statusCode = http .StatusNotFound
109
+ }
110
+ common .WriteJsonResp (w , err , nil , statusCode )
93
111
return
94
112
}
95
- profile = defaultProfile
96
-
97
- resp := bean.ProfileResponse {
113
+ if profileName == v1 .GLOBAL_PROFILE_NAME {
114
+ profile = defaultProfile
115
+ }
116
+ resp := v1.ProfileResponse {
98
117
Profile : * profile ,
99
118
}
100
- resp .ConfigurationUnits = handler .infraProfileService .GetConfigurationUnits ()
101
- resp .DefaultConfigurations = defaultProfile .Configurations
119
+ resp .ConfigurationUnits , err = handler .infraProfileService .GetConfigurationUnits ()
120
+ if err != nil {
121
+ handler .logger .Errorw ("error in getting configuration units" , "err" , err )
122
+ common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
123
+ return
124
+ }
125
+ resp .DefaultConfigurations = defaultProfile .GetConfigurations ()
102
126
common .WriteJsonResp (w , nil , resp , http .StatusOK )
103
127
}
104
128
@@ -117,28 +141,26 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfile(w http.ResponseWri
117
141
//vars := mux.Vars(r)
118
142
val := r .URL .Query ().Get ("name" )
119
143
if len (val ) == 0 {
120
- common .WriteJsonResp (w , errors .New (bean . InvalidProfileName ), nil , http .StatusBadRequest )
144
+ common .WriteJsonResp (w , errors .New ("name is required" ), nil , http .StatusBadRequest )
121
145
return
122
146
}
123
147
profileName := strings .ToLower (val )
124
-
125
- payload := & bean.ProfileBeanDto {}
148
+ if profileName == "" {
149
+ common .WriteJsonResp (w , errors .New (errors2 .InvalidProfileName ), nil , http .StatusBadRequest )
150
+ return
151
+ }
152
+ payload := & v1.ProfileBeanDto {}
126
153
decoder := json .NewDecoder (r .Body )
127
154
err = decoder .Decode (payload )
128
155
if err != nil {
129
156
handler .logger .Errorw ("error in decoding the request payload" , "err" , err , "requestBody" , r .Body )
130
157
common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
131
158
return
132
159
}
133
- //handler.validator.RegisterValidation("validateValue", ValidateValue)
134
- payload .Name = strings .ToLower (payload .Name )
135
160
err = handler .validator .Struct (payload )
136
161
if err != nil {
137
- err = errors .Wrap (err , bean .PayloadValidationError )
162
+ err = errors .Wrap (err , errors2 .PayloadValidationError )
138
163
common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
139
- }
140
- if ! util2 .IsValidProfileNameRequested (profileName , payload .Name ) {
141
- common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
142
164
return
143
165
}
144
166
err = handler .infraProfileService .UpdateProfile (userId , profileName , payload )
@@ -150,7 +172,7 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfile(w http.ResponseWri
150
172
common .WriteJsonResp (w , nil , nil , http .StatusOK )
151
173
}
152
174
153
- // Deprecated
175
+ // Deprecated: GetProfileV0 is deprecated in favour of GetProfile
154
176
func (handler * InfraConfigRestHandlerImpl ) GetProfileV0 (w http.ResponseWriter , r * http.Request ) {
155
177
userId , err := handler .userService .GetLoggedInUser (r )
156
178
if userId == 0 || err != nil {
@@ -166,34 +188,53 @@ func (handler *InfraConfigRestHandlerImpl) GetProfileV0(w http.ResponseWriter, r
166
188
vars := mux .Vars (r )
167
189
profileName := strings .ToLower (vars ["name" ])
168
190
if profileName == "" {
169
- common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
191
+ common .WriteJsonResp (w , errors .New (errors2 .InvalidProfileName ), nil , http .StatusBadRequest )
170
192
return
171
193
}
172
194
173
- if profileName != bean .DEFAULT_PROFILE_NAME {
174
- common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
175
- return
195
+ var profileV0 * v0.ProfileBeanV0
196
+ if profileName != v1 .DEFAULT_PROFILE_NAME {
197
+ profileV1 , err := handler .infraProfileService .GetProfileByName (profileName )
198
+ profileV0 = adapter .GetV0ProfileBean (profileV1 )
199
+ if err != nil {
200
+ statusCode := http .StatusInternalServerError
201
+ if errors .Is (err , pg .ErrNoRows ) {
202
+ err = errors .New (fmt .Sprintf ("profile %s not found" , profileName ))
203
+ statusCode = http .StatusNotFound
204
+ }
205
+ common .WriteJsonResp (w , err , nil , statusCode )
206
+ return
207
+ }
176
208
}
177
209
178
- profileName = bean .GLOBAL_PROFILE_NAME
179
-
180
- var profile * bean.ProfileBeanV0
181
- defaultProfileV1 , err := handler .infraProfileService .GetProfileByName (profileName )
182
- defaultProfileV0 := adapter .GetV0ProfileBean (defaultProfileV1 )
210
+ defaultProfileV1 , err := handler .infraProfileService .GetProfileByName (v1 .GLOBAL_PROFILE_NAME )
183
211
if err != nil {
184
- common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
212
+ statusCode := http .StatusInternalServerError
213
+ if errors .Is (err , pg .ErrNoRows ) {
214
+ err = errors .New (fmt .Sprintf ("profile %s not found" , v1 .GLOBAL_PROFILE_NAME ))
215
+ statusCode = http .StatusNotFound
216
+ }
217
+ common .WriteJsonResp (w , err , nil , statusCode )
185
218
return
186
219
}
187
- profile = defaultProfileV0
188
- resp := bean.ProfileResponseV0 {
189
- Profile : * profile ,
220
+ defaultProfileV0 := adapter .GetV0ProfileBean (defaultProfileV1 )
221
+ if profileName == v1 .DEFAULT_PROFILE_NAME {
222
+ profileV0 = defaultProfileV0
223
+ }
224
+ resp := v0.ProfileResponseV0 {
225
+ Profile : * profileV0 ,
190
226
}
191
- resp .ConfigurationUnits = handler .infraProfileService .GetConfigurationUnits ()
227
+ resp .ConfigurationUnits , err = handler .infraProfileService .GetConfigurationUnits ()
228
+ if err != nil {
229
+ handler .logger .Errorw ("error in getting configuration units" , "err" , err )
230
+ common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
231
+ return
232
+ }
233
+ //returning the default configuration for UI inheriting purpose
192
234
resp .DefaultConfigurations = defaultProfileV0 .Configurations
193
235
common .WriteJsonResp (w , nil , resp , http .StatusOK )
194
236
}
195
237
196
- // Deprecated
197
238
func (handler * InfraConfigRestHandlerImpl ) UpdateInfraProfileV0 (w http.ResponseWriter , r * http.Request ) {
198
239
userId , err := handler .userService .GetLoggedInUser (r )
199
240
if userId == 0 || err != nil {
@@ -209,38 +250,36 @@ func (handler *InfraConfigRestHandlerImpl) UpdateInfraProfileV0(w http.ResponseW
209
250
vars := mux .Vars (r )
210
251
profileName := strings .ToLower (vars ["name" ])
211
252
if profileName == "" {
212
- common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
253
+ common .WriteJsonResp (w , errors .New (errors2 .InvalidProfileName ), nil , http .StatusBadRequest )
213
254
return
214
255
}
215
- payload := & bean .ProfileBeanV0 {}
256
+ payload := & v0 .ProfileBeanV0 {}
216
257
decoder := json .NewDecoder (r .Body )
217
258
err = decoder .Decode (payload )
218
259
if err != nil {
219
260
handler .logger .Errorw ("error in decoding the request payload" , "err" , err , "requestBody" , r .Body )
220
261
common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
221
262
return
222
263
}
223
- payload .Name = strings .ToLower (payload .Name )
224
264
err = handler .validator .Struct (payload )
225
265
if err != nil {
226
- err = errors .Wrap (err , bean .PayloadValidationError )
266
+ err = errors .Wrap (err , errors2 .PayloadValidationError )
227
267
common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
268
+ return
228
269
}
229
- if ! util2 .IsValidProfileNameRequestedV0 (profileName , payload .Name ) {
230
- common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
270
+ if ! util .IsValidProfileNameRequestedV0 (profileName , payload .GetName () ) {
271
+ common .WriteJsonResp (w , errors .New (errors2 .InvalidProfileName ), nil , http .StatusBadRequest )
231
272
return
232
273
}
233
- if payload .Name != bean .DEFAULT_PROFILE_NAME {
234
- common .WriteJsonResp (w , errors .New (bean .InvalidProfileName ), nil , http .StatusBadRequest )
274
+ if profileName == v1 . DEFAULT_PROFILE_NAME && payload .GetName () != v1 .DEFAULT_PROFILE_NAME {
275
+ common .WriteJsonResp (w , errors .New (errors2 .InvalidProfileName ), nil , http .StatusBadRequest )
235
276
return
236
277
}
237
-
238
- profileName = bean .GLOBAL_PROFILE_NAME
239
- payloadV1 := adapter .GetV1ProfileBean (payload )
240
- err = handler .infraProfileService .UpdateProfile (userId , profileName , payloadV1 )
278
+ payloadV1 := adapter .ConvertToV1ProfileBean (payload )
279
+ err = handler .infraProfileService .UpdateProfileV0 (userId , profileName , payloadV1 )
241
280
if err != nil {
242
281
handler .logger .Errorw ("error in updating profile and configurations" , "profileName" , profileName , "payLoad" , payload , "err" , err )
243
- common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
282
+ common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
244
283
return
245
284
}
246
285
common .WriteJsonResp (w , nil , nil , http .StatusOK )
0 commit comments