diff --git a/pkg/notify/models/config.go b/pkg/notify/models/config.go index 6a9bcd89d4..ea5fe673dc 100644 --- a/pkg/notify/models/config.go +++ b/pkg/notify/models/config.go @@ -172,7 +172,7 @@ func (c *SConfig) PostCreate(ctx context.Context, userCred mcclient.TokenCredent return } NotifyService.AddConfig(ctx, c.Type, c.Config()) - err = c.StartRepullSubcontactTask(ctx, userCred) + err = c.StartRepullSubcontactTask(ctx, userCred, false) if err != nil { log.Errorf("unable to StartRepullSubcontactTask: %v", err) } @@ -190,7 +190,7 @@ func (c *SConfig) PostUpdate(ctx context.Context, userCred mcclient.TokenCredent Config: configMap, DomainId: c.DomainId, }) - err = c.StartRepullSubcontactTask(ctx, userCred) + err = c.StartRepullSubcontactTask(ctx, userCred, false) if err != nil { log.Errorf("unable to StartRepullSubcontactTask: %v", err) } @@ -200,15 +200,34 @@ func (c *SConfig) PreDelete(ctx context.Context, userCred mcclient.TokenCredenti c.SStandaloneResourceBase.PreDelete(ctx, userCred) NotifyService.DeleteConfig(ctx, c.Type, c.Config().DomainId) } -func (c *SConfig) PostDelete(ctx context.Context, userCred mcclient.TokenCredential) { - err := c.StartRepullSubcontactTask(ctx, userCred) + +func (c *SConfig) CustomizeDelete(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) error { + err := c.SStandaloneResourceBase.CustomizeDelete(ctx, userCred, query, data) if err != nil { - log.Errorf("unable to StartRepullSubcontactTask: %v", err) + return err } + NotifyService.DeleteConfig(ctx, c.Type, c.Config().DomainId) + err = c.StartRepullSubcontactTask(ctx, userCred, true) + if err != nil { + return errors.Wrap(err, "unable to start repull subcontact") + } + return err } -func (c *SConfig) StartRepullSubcontactTask(ctx context.Context, userCred mcclient.TokenCredential) error { - task, err := taskman.TaskManager.NewTask(ctx, "RepullSuncontactTask", c, userCred, nil, "", "") +func (c *SConfig) Delete(ctx context.Context, userCred mcclient.TokenCredential) error { + return nil +} + +func (c *SConfig) RealDelete(ctx context.Context, userCred mcclient.TokenCredential) error { + return c.SStandaloneResourceBase.Delete(ctx, userCred) +} + +func (c *SConfig) StartRepullSubcontactTask(ctx context.Context, userCred mcclient.TokenCredential, del bool) error { + taskData := jsonutils.NewDict() + if del { + taskData.Set("deleted", jsonutils.JSONTrue) + } + task, err := taskman.TaskManager.NewTask(ctx, "RepullSuncontactTask", c, userCred, taskData, "", "") if err != nil { return err } diff --git a/pkg/notify/tasks/repull_subcontact_task.go b/pkg/notify/tasks/repull_subcontact_task.go index 802bf40c91..6e4edfea02 100644 --- a/pkg/notify/tasks/repull_subcontact_task.go +++ b/pkg/notify/tasks/repull_subcontact_task.go @@ -41,7 +41,9 @@ func init() { } func (self *RepullSuncontactTask) taskFailed(ctx context.Context, config *models.SConfig, reason string) { - logclient.AddActionLogWithContext(ctx, config, logclient.ACT_PULL_SUBCONTACT, reason, self.UserCred, false) + if !config.Deleted { + logclient.AddActionLogWithContext(ctx, config, logclient.ACT_PULL_SUBCONTACT, reason, self.UserCred, false) + } self.SetStageFailed(ctx, jsonutils.NewString(reason)) } @@ -66,8 +68,8 @@ func (self *RepullSuncontactTask) OnInit(ctx context.Context, obj db.IStandalone q = q.Equals("domain_id", config.DomainId) } else { // The system-level config update should not affect the receiver under the domain with config - configq := models.ConfigManager.Query("domain_id").Equals("attribution", notify.CONFIG_ATTRIBUTION_DOMAIN).SubQuery() - q = q.Join(configq, sqlchemy.NotEquals(q.Field("domain_id"), configq.Field("domain_id"))) + configq := models.ConfigManager.Query("domain_id").Equals("type", config.Type).Equals("attribution", notify.CONFIG_ATTRIBUTION_DOMAIN).SubQuery() + q = q.NotIn("domain_id", configq) } q.Join(subq, sqlchemy.Equals(q.Field("id"), subq.Field("receiver_id"))) rs := make([]models.SReceiver, 0) @@ -77,6 +79,10 @@ func (self *RepullSuncontactTask) OnInit(ctx context.Context, obj db.IStandalone return } + if del, _ := self.GetParams().Bool("deleted"); del { + config.RealDelete(ctx, self.GetUserCred()) + } + var reasons []string for i := range rs { r := &rs[i] @@ -93,17 +99,16 @@ func (self *RepullSuncontactTask) OnInit(ctx context.Context, obj db.IStandalone return } ctSets := sets.NewString(cts...) - if !ctSets.Has(config.Type) { - return - } - ctSets.Delete(config.Type) - err = r.SetVerifiedContactTypes(ctSets.UnsortedList()) - if err != nil { - reasons = append(reasons, repullFailedReason{ - ReceiverId: r.Id, - Reason: fmt.Sprintf("unable to SetVerifiedContactTypes: %v", err), - }.String()) - return + if ctSets.Has(config.Type) { + ctSets.Delete(config.Type) + err = r.SetVerifiedContactTypes(ctSets.UnsortedList()) + if err != nil { + reasons = append(reasons, repullFailedReason{ + ReceiverId: r.Id, + Reason: fmt.Sprintf("unable to SetVerifiedContactTypes: %v", err), + }.String()) + return + } } // pull params := jsonutils.NewDict()