Skip to content

Commit 02d039f

Browse files
check if an addon that is not a platform exists when the project is deleted (#3419) (#3450)
* check if an addon that is not a platform exists * add comment Co-authored-by: littlejian <[email protected]>
1 parent 99c4b0a commit 02d039f

File tree

3 files changed

+79
-46
lines changed

3 files changed

+79
-46
lines changed

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,6 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
479479
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
480480
github.com/erda-project/elastic v0.0.1-ex h1:5ajfxQ5S5YjpzFqY9LzL9hiKWCn6q/JDT4n8sNv7+pU=
481481
github.com/erda-project/elastic v0.0.1-ex/go.mod h1:iAVsas6fcmt9pxtge1+dErMhecv+RLSXlD4rnZRJVW0=
482-
github.com/erda-project/erda-infra v0.0.0-20211217092649-00fbcca89a94 h1:tWFSZ4YKi/Ob6wgiVa5aV88zK9Z/AWLvqszm5WK/2z0=
483482
github.com/erda-project/erda-infra v0.0.0-20211217092649-00fbcca89a94/go.mod h1:JEXUFWDC/a97+3AgX/cL4xRlqL9vTDM+wVV3Ee5FXYI=
484483
github.com/erda-project/erda-infra v0.0.0-20211220060639-a30b7096b5ff h1:Whg+CnwfzC/YTlFmvnmYFBeGec0he2EdB37W7JV7+9E=
485484
github.com/erda-project/erda-infra v0.0.0-20211220060639-a30b7096b5ff/go.mod h1:JEXUFWDC/a97+3AgX/cL4xRlqL9vTDM+wVV3Ee5FXYI=
@@ -1186,7 +1185,6 @@ github.com/mitchellh/hashstructure v0.0.0-20170609045927-2bca23e0e452/go.mod h1:
11861185
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
11871186
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
11881187
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
1189-
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
11901188
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
11911189
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
11921190
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=

modules/core-services/services/project/project.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -649,15 +649,17 @@ func (p *Project) Delete(projectID int64) (*model.Project, error) {
649649
return nil, errors.Errorf(p.trans.Text(langCodes, "FailedGetProject")+"(%v)", err)
650650
}
651651

652-
// TODO We need to turn this check on after adding the delete portal to the UI
653-
// check if addon exists
654-
// addOnListResp, err := p.bdl.ListAddonByProjectID(projectID, project.OrgID)
655-
// if err != nil {
656-
// return nil, err
657-
// }
658-
// if addOnListResp != nil && len(addOnListResp.Data) > 0 {
659-
// return nil, errors.Errorf("failed to delete project(there exists addons)")
660-
// }
652+
// Check if an addon that is not a platform exists
653+
addOnListResp, err := p.bdl.ListAddonByProjectID(projectID, project.OrgID)
654+
if err != nil {
655+
return nil, err
656+
}
657+
if addOnListResp != nil && len(addOnsFilterIn(addOnListResp.Data, func(addOn *apistructs.AddonFetchResponseData) bool {
658+
// The platformServiceType is 0 means it can be deleted by the platform
659+
return addOn.PlatformServiceType == 0
660+
})) > 0 {
661+
return nil, errors.Errorf("failed to delete project(there exists addons)")
662+
}
661663

662664
if err = p.db.DeleteProject(projectID); err != nil {
663665
return nil, errors.Errorf(p.trans.Text(langCodes, "FailedDeleteProject")+"(%v)", err)
@@ -675,6 +677,15 @@ func (p *Project) Delete(projectID int64) (*model.Project, error) {
675677
return &project, nil
676678
}
677679

680+
func addOnsFilterIn(addOns []apistructs.AddonFetchResponseData, fn func(addOn *apistructs.AddonFetchResponseData) bool) (newAddons []apistructs.AddonFetchResponseData) {
681+
for i := range addOns {
682+
if fn(&addOns[i]) {
683+
newAddons = append(newAddons, addOns[i])
684+
}
685+
}
686+
return
687+
}
688+
678689
// Get 获取项目
679690
func (p *Project) Get(ctx context.Context, projectID int64, withQuota bool) (*apistructs.ProjectDTO, error) {
680691
project, err := p.db.GetProjectByID(projectID)

modules/core-services/services/project/project_test.go

+59-35
Original file line numberDiff line numberDiff line change
@@ -396,38 +396,62 @@ func Test_defaultResourceConfig(t *testing.T) {
396396
}
397397
}
398398

399-
// TODO We need to turn this ut on after adding the delete portal to the UI
400-
// func TestDeleteProjectWhenAddonExists(t *testing.T) {
401-
// db := &dao.DBClient{}
402-
// monkey.PatchInstanceMethod(reflect.TypeOf(db), "GetApplicationCountByProjectID",
403-
// func(*dao.DBClient, int64) (int64, error) {
404-
// return 0, nil
405-
// })
406-
// defer monkey.UnpatchAll()
407-
408-
// monkey.PatchInstanceMethod(reflect.TypeOf(db), "GetProjectByID",
409-
// func(*dao.DBClient, int64) (model.Project, error) {
410-
// return model.Project{}, nil
411-
// })
412-
413-
// bdl := &bundle.Bundle{}
414-
// monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "ListAddonByProjectID",
415-
// func(*bundle.Bundle, int64, int64) (*apistructs.AddonListResponse, error) {
416-
// return &apistructs.AddonListResponse{
417-
// Header: apistructs.Header{},
418-
// Data: []apistructs.AddonFetchResponseData{
419-
// {
420-
// ID: "1",
421-
// },
422-
// },
423-
// }, nil
424-
// })
425-
// p := &Project{}
426-
// _, err := p.Delete(1)
427-
// if err == nil {
428-
// assert.Fail(t, "fail")
429-
// return
430-
// }
431-
// assert.Equal(t, "failed to delete project(there exists addons)", err.Error())
432-
433-
// }
399+
func TestDeleteProjectWhenAddonExists(t *testing.T) {
400+
db := &dao.DBClient{}
401+
monkey.PatchInstanceMethod(reflect.TypeOf(db), "GetApplicationCountByProjectID",
402+
func(*dao.DBClient, int64) (int64, error) {
403+
return 0, nil
404+
})
405+
defer monkey.UnpatchAll()
406+
407+
monkey.PatchInstanceMethod(reflect.TypeOf(db), "GetProjectByID",
408+
func(*dao.DBClient, int64) (model.Project, error) {
409+
return model.Project{}, nil
410+
})
411+
412+
bdl := &bundle.Bundle{}
413+
monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "ListAddonByProjectID",
414+
func(*bundle.Bundle, int64, int64) (*apistructs.AddonListResponse, error) {
415+
return &apistructs.AddonListResponse{
416+
Header: apistructs.Header{},
417+
Data: []apistructs.AddonFetchResponseData{
418+
{
419+
ID: "1",
420+
PlatformServiceType: 1,
421+
},
422+
{
423+
ID: "1",
424+
PlatformServiceType: 0,
425+
},
426+
},
427+
}, nil
428+
})
429+
p := &Project{}
430+
_, err := p.Delete(1)
431+
if err == nil {
432+
assert.Fail(t, "fail")
433+
return
434+
}
435+
assert.Equal(t, "failed to delete project(there exists addons)", err.Error())
436+
}
437+
438+
func TestAddOnsFilterIn(t *testing.T) {
439+
addOns := []apistructs.AddonFetchResponseData{
440+
{
441+
ID: "1",
442+
PlatformServiceType: 1,
443+
},
444+
{
445+
ID: "2",
446+
PlatformServiceType: 0,
447+
},
448+
{
449+
ID: "3",
450+
PlatformServiceType: 1,
451+
},
452+
}
453+
newAddOns := addOnsFilterIn(addOns, func(addOn *apistructs.AddonFetchResponseData) bool {
454+
return addOn.PlatformServiceType == 0
455+
})
456+
assert.Equal(t, 1, len(newAddOns))
457+
}

0 commit comments

Comments
 (0)