mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-23 10:45:45 +01:00
Merge pull request #4490 from ywk253100/180326_label
Delete the mapping relationship between resources and labels when the label is deleted
This commit is contained in:
commit
55035ada2d
@ -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