diff --git a/src/common/dao/resource_label.go b/src/common/dao/resource_label.go index 56b17916b..b80b3bb03 100644 --- a/src/common/dao/resource_label.go +++ b/src/common/dao/resource_label.go @@ -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 +} diff --git a/src/common/dao/resource_label_test.go b/src/common/dao/resource_label_test.go index 572bec8b5..467f019a0 100644 --- a/src/common/dao/resource_label_test.go +++ b/src/common/dao/resource_label_test.go @@ -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)) } diff --git a/src/ui/api/label.go b/src/ui/api/label.go index 3d7dfe978..db6144e57 100644 --- a/src/ui/api/label.go +++ b/src/ui/api/label.go @@ -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