bugfix: return err when unable to get monapi.collectRule (#537)

This commit is contained in:
yubo 2021-01-22 17:39:02 +08:00 committed by GitHub
parent 8fe3d2b0b3
commit 029f0a09ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 11 deletions

View File

@ -40,6 +40,7 @@ func (p BaseCollector) TelegrafInput(rule *models.CollectRule) (telegraf.Input,
return r2.TelegrafInput()
}
// used for ui
func (p BaseCollector) Get(id int64) (interface{}, error) {
collect := &models.CollectRule{}
has, err := models.DB["mon"].Where("id = ?", id).Get(collect)
@ -49,6 +50,18 @@ func (p BaseCollector) Get(id int64) (interface{}, error) {
return collect, err
}
func (p BaseCollector) mustGetRule(id int64) (*models.CollectRule, error) {
collect := &models.CollectRule{}
has, err := models.DB["mon"].Where("id = ?", id).Get(collect)
if err != nil {
return nil, err
}
if !has {
return nil, fmt.Errorf("unable to get the collectRule")
}
return collect, nil
}
func (p BaseCollector) Gets(nids []int64) (ret []interface{}, err error) {
collects := []models.CollectRule{}
err = models.DB["mon"].Where("collect_type=?", p.name).In("nid", nids).Find(&collects)
@ -124,16 +137,11 @@ func (p BaseCollector) Update(data []byte, username string) error {
}
//校验采集是否存在
obj, err := p.Get(collect.Id) //id找不到的情况
obj, err := p.mustGetRule(collect.Id)
if err != nil {
return fmt.Errorf("采集不存在 type:%s id:%d", p.name, collect.Id)
}
tmpId := obj.(*models.CollectRule).Id
if tmpId == 0 {
return fmt.Errorf("采集不存在 type:%s id:%d", p.name, collect.Id)
}
collect.Updater = username
collect.UpdatedAt = time.Now().Unix()
@ -141,7 +149,7 @@ func (p BaseCollector) Update(data []byte, username string) error {
if err != nil {
return err
}
if old != nil && tmpId != old.(*models.CollectRule).Id {
if old != nil && obj.Id != old.(*models.CollectRule).Id {
return fmt.Errorf("同节点下策略名称 %s 已存在", collect.Name)
}
@ -149,12 +157,11 @@ func (p BaseCollector) Update(data []byte, username string) error {
}
func (p BaseCollector) Delete(id int64, username string) error {
tmp, err := p.Get(id) //id找不到的情况
rule, err := p.mustGetRule(id) //id找不到的情况
if err != nil {
return fmt.Errorf("采集不存在 type:%s id:%d", p.name, id)
}
nid := tmp.(*models.CollectRule).Nid
can, err := models.UsernameCandoNodeOp(username, "mon_collect_delete", int64(nid))
can, err := models.UsernameCandoNodeOp(username, "mon_collect_delete", int64(rule.Nid))
if err != nil {
return fmt.Errorf("models.UsernameCandoNodeOp error %s", err)
}

View File

@ -142,7 +142,8 @@ func (p *CollectRuleCache) syncCollectRule() error {
collectRuleCount := len(resp.Data)
stats.Counter.Set("collectrule.count", collectRuleCount)
if collectRuleCount == 0 { //获取策略数为0不正常不更新策略缓存
return fmt.Errorf("clloect rule count is 0")
logger.Debugf("clloect rule count is 0")
return nil
}
for _, rule := range resp.Data {