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:
Wenkai Yin 2018-03-28 14:38:01 +08:00 committed by GitHub
commit 55035ada2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 0 deletions

View File

@ -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
}

View File

@ -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))
}

View File

@ -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