Delete the mapping relationship between resources and labels when the label is deleted

This commit is contained in:
Wenkai Yin 2018-03-24 02:22:51 +08:00
parent 0c95bd6ffa
commit 85f357ec6b
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) _, err := qs.All(&rls)
return rls, err 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) labels, err = GetLabelsOfResource(resourceType, resourceID)
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, 0, len(labels)) 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 // Delete the label
func (l *LabelAPI) Delete() { func (l *LabelAPI) Delete() {
id := l.label.ID 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 { if err := dao.DeleteLabel(id); err != nil {
l.HandleInternalServerError(fmt.Sprintf("failed to delete label %d: %v", id, err)) l.HandleInternalServerError(fmt.Sprintf("failed to delete label %d: %v", id, err))
return return