Skip to content

Commit 9203dd1

Browse files
authored
chore(server): simplify project validation logic (#21191)
* chore(server): simplify project validation logic Signed-off-by: Michael Crenshaw <[email protected]> * improve tests Signed-off-by: Michael Crenshaw <[email protected]> --------- Signed-off-by: Michael Crenshaw <[email protected]>
1 parent 0de5f60 commit 9203dd1

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

server/project/project.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -398,43 +398,31 @@ func (s *Server) Update(ctx context.Context, q *project.ProjectUpdateRequest) (*
398398
return nil, err
399399
}
400400

401-
var srcValidatedApps []v1alpha1.Application
402-
var dstValidatedApps []v1alpha1.Application
403401
getProjectClusters := func(project string) ([]*v1alpha1.Cluster, error) {
404402
return s.db.GetProjectClusters(ctx, project)
405403
}
406404

407-
for _, a := range argo.FilterByProjects(appsList.Items, []string{q.Project.Name}) {
408-
if oldProj.IsSourcePermitted(a.Spec.GetSource()) {
409-
srcValidatedApps = append(srcValidatedApps, a)
410-
}
411-
412-
dstPermitted, err := oldProj.IsDestinationPermitted(a.Spec.Destination, getProjectClusters)
413-
if err != nil {
414-
return nil, err
415-
}
416-
417-
if dstPermitted {
418-
dstValidatedApps = append(dstValidatedApps, a)
419-
}
420-
}
421-
422405
invalidSrcCount := 0
423406
invalidDstCount := 0
424407

425-
for _, a := range srcValidatedApps {
426-
if !q.Project.IsSourcePermitted(a.Spec.GetSource()) {
408+
for _, a := range argo.FilterByProjects(appsList.Items, []string{q.Project.Name}) {
409+
if oldProj.IsSourcePermitted(a.Spec.GetSource()) && !q.Project.IsSourcePermitted(a.Spec.GetSource()) {
427410
invalidSrcCount++
428411
}
429-
}
430-
for _, a := range dstValidatedApps {
431-
dstPermitted, err := q.Project.IsDestinationPermitted(a.Spec.Destination, getProjectClusters)
412+
413+
dstPermitted, err := oldProj.IsDestinationPermitted(a.Spec.Destination, getProjectClusters)
432414
if err != nil {
433415
return nil, err
434416
}
435417

436-
if !dstPermitted {
437-
invalidDstCount++
418+
if dstPermitted {
419+
dstPermitted, err := q.Project.IsDestinationPermitted(a.Spec.Destination, getProjectClusters)
420+
if err != nil {
421+
return nil, err
422+
}
423+
if !dstPermitted {
424+
invalidDstCount++
425+
}
438426
}
439427
}
440428

server/project/project_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ func TestProjectServer(t *testing.T) {
196196
require.Error(t, err)
197197
statusCode, _ := status.FromError(err)
198198
assert.Equal(t, codes.InvalidArgument, statusCode.Code())
199+
assert.Equal(t, "as a result of project update 1 applications destination became invalid", statusCode.Message())
199200
})
200201

201202
t.Run("TestRemoveSourceSuccessful", func(t *testing.T) {
@@ -232,6 +233,7 @@ func TestProjectServer(t *testing.T) {
232233
require.Error(t, err)
233234
statusCode, _ := status.FromError(err)
234235
assert.Equal(t, codes.InvalidArgument, statusCode.Code())
236+
assert.Equal(t, "as a result of project update 1 applications source became invalid", statusCode.Message())
235237
})
236238

237239
t.Run("TestRemoveSourceUsedByAppSuccessfulIfPermittedByAnotherSrc", func(t *testing.T) {
@@ -318,6 +320,7 @@ func TestProjectServer(t *testing.T) {
318320
require.Error(t, err)
319321
statusCode, _ := status.FromError(err)
320322
assert.Equal(t, codes.InvalidArgument, statusCode.Code())
323+
assert.Equal(t, "project is referenced by 1 applications", statusCode.Message())
321324
})
322325

323326
// configure a user named "admin" which is denied by default

0 commit comments

Comments
 (0)