mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-24 01:27:49 +01:00
Delete the mapping relationship between resources and labels when the label is deleted
This commit is contained in:
parent
0c95bd6ffa
commit
85f357ec6b
@ -120,3 +120,9 @@ func ListResourceLabels(query ...*models.ResourceLabelQuery) ([]*models.Resource
|
||||
_, err := qs.All(&rls)
|
||||
return rls, err
|
||||
}
|
||||
|
||||
// DeleteResourceLabelByLabel delete the mapping relationship by label ID
|
||||
func DeleteResourceLabelByLabel(id int64) error {
|
||||
_, err := GetOrmer().QueryTable(&models.ResourceLabel{}).Filter("LabelID", id).Delete()
|
||||
return err
|
||||
}
|
||||
|
@ -81,4 +81,15 @@ func TestMethodsOfResourceLabel(t *testing.T) {
|
||||
labels, err = GetLabelsOfResource(resourceType, resourceID)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 0, len(labels))
|
||||
|
||||
// delete by label ID
|
||||
id, err = AddResourceLabel(rl)
|
||||
require.Nil(t, err)
|
||||
err = DeleteResourceLabelByLabel(labelID)
|
||||
require.Nil(t, err)
|
||||
rls, err = ListResourceLabels(&models.ResourceLabelQuery{
|
||||
LabelID: labelID,
|
||||
})
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 0, len(rls))
|
||||
}
|
||||
|
@ -256,6 +256,10 @@ func (l *LabelAPI) Put() {
|
||||
// Delete the label
|
||||
func (l *LabelAPI) Delete() {
|
||||
id := l.label.ID
|
||||
if err := dao.DeleteResourceLabelByLabel(id); err != nil {
|
||||
l.HandleInternalServerError(fmt.Sprintf("failed to delete resource label mappings of label %d: %v", id, err))
|
||||
return
|
||||
}
|
||||
if err := dao.DeleteLabel(id); err != nil {
|
||||
l.HandleInternalServerError(fmt.Sprintf("failed to delete label %d: %v", id, err))
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user