Merge pull request #15780 from gouqi11/addTopicResult

fix(notify): Add topic result
This commit is contained in:
Zexi Li
2023-01-14 17:03:11 +08:00
committed by GitHub
6 changed files with 39 additions and 15 deletions
-1
View File
@@ -1059,7 +1059,6 @@ func (self *SGuest) StartGuestDeployTask(
}
func (self *SGuest) EventNotify(ctx context.Context, userCred mcclient.TokenCredential, action noapi.SAction) {
detailsDecro := func(ctx context.Context, details *jsonutils.JSONDict) {
if action != notifyclient.ActionCreate && action != notifyclient.ActionRebuildRoot && action != notifyclient.ActionResetPassword {
return
+1
View File
@@ -235,6 +235,7 @@ func (nm *SNotificationManager) PerformEventNotify(ctx context.Context, userCred
}
// receiver
topic, err := TopicManager.TopicByEvent(input.Event, input.AdvanceDays)
if err != nil {
return output, errors.Wrapf(err, "unable fetch subscriptions by event %q", input.Event)
-1
View File
@@ -678,7 +678,6 @@ func (rm *SReceiverManager) findUserIdsWithProjectDomain(ctx context.Context, us
if err != nil {
return nil, errors.Wrap(err, "unable to list RoleAssignments")
}
log.Debugf("return value for role-assignments: %s", jsonutils.Marshal(listRet))
userIds := sets.NewString()
for i := range listRet.Data {
ras := listRet.Data[i]
-2
View File
@@ -497,12 +497,10 @@ func (srm *SSubscriberManager) getReceiversSent(ctx context.Context, tid string,
query.Add(jsonutils.NewString(projectId), "scope", "project", "id")
}
s := auth.GetAdminSession(ctx, "")
log.Debugf("query for role-assignments: %s", query.String())
listRet, err := modules.RoleAssignments.List(s, query)
if err != nil {
return errors.Wrap(err, "unable to list RoleAssignments")
}
log.Debugf("return value for role-assignments: %s", jsonutils.Marshal(listRet))
for i := range listRet.Data {
ras := listRet.Data[i]
user, err := ras.Get("user")
+38 -10
View File
@@ -20,6 +20,7 @@ import (
"strings"
"sync"
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
@@ -28,6 +29,7 @@ import (
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/apis/notify"
api "yunion.io/x/onecloud/pkg/apis/notify"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
@@ -71,11 +73,11 @@ type STopic struct {
db.SStandaloneResourceBase
db.SEnabledResourceBase
Type string `width:"20" nullable:"false" create:"required" update:"user" list:"user"`
Resources uint64 `nullable:"false"`
Actions uint32 `nullable:"false"`
Results uint8 `nullable:"false"`
AdvanceDays int `nullable:"false"`
Type string `width:"20" nullable:"false" create:"required" update:"user" list:"user"`
Resources uint64 `nullable:"false"`
Actions uint32 `nullable:"false"`
Results tristate.TriState `default:"true"`
AdvanceDays int `nullable:"false"`
WebconsoleDisable tristate.TriState
}
@@ -184,6 +186,7 @@ func (sm *STopicManager) InitializeData() error {
notify.ActionPendingDelete,
)
t.Type = notify.TOPIC_TYPE_RESOURCE
t.Results = tristate.True
case DefaultResourceChangeConfig:
t.addResources(
notify.TOPIC_RESOURCE_SERVER,
@@ -204,6 +207,7 @@ func (sm *STopicManager) InitializeData() error {
t.addAction(notify.ActionResetPassword)
t.addAction(notify.ActionChangeIpaddr)
t.Type = notify.TOPIC_TYPE_RESOURCE
t.Results = tristate.True
case DefaultResourceReleaseDue1Day:
t.addResources(
notify.TOPIC_RESOURCE_SERVER,
@@ -216,6 +220,7 @@ func (sm *STopicManager) InitializeData() error {
t.addAction(notify.ActionExpiredRelease)
t.Type = notify.TOPIC_TYPE_RESOURCE
t.AdvanceDays = 1
t.Results = tristate.True
case DefaultResourceReleaseDue3Day:
t.addResources(
notify.TOPIC_RESOURCE_SERVER,
@@ -228,6 +233,7 @@ func (sm *STopicManager) InitializeData() error {
t.addAction(notify.ActionExpiredRelease)
t.Type = notify.TOPIC_TYPE_RESOURCE
t.AdvanceDays = 3
t.Results = tristate.True
case DefaultResourceReleaseDue30Day:
t.addResources(
notify.TOPIC_RESOURCE_SERVER,
@@ -238,6 +244,7 @@ func (sm *STopicManager) InitializeData() error {
t.addAction(notify.ActionExpiredRelease)
t.Type = notify.TOPIC_TYPE_RESOURCE
t.AdvanceDays = 30
t.Results = tristate.True
case DefaultScheduledTaskExecute:
t.addResources(notify.TOPIC_RESOURCE_SCHEDULEDTASK)
t.addAction(notify.ActionExecute)
@@ -246,10 +253,12 @@ func (sm *STopicManager) InitializeData() error {
t.addResources(notify.TOPIC_RESOURCE_SCALINGPOLICY)
t.addAction(notify.ActionExecute)
t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS
t.Results = tristate.True
case DefaultSnapshotPolicyExecute:
t.addResources(notify.TOPIC_RESOURCE_SNAPSHOTPOLICY)
t.addAction(notify.ActionExecute)
t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS
t.Results = tristate.True
case DefaultResourceOperationFailed:
t.addResources(
notify.TOPIC_RESOURCE_SERVER,
@@ -268,6 +277,7 @@ func (sm *STopicManager) InitializeData() error {
notify.ActionMigrate,
)
t.Type = notify.TOPIC_TYPE_RESOURCE
t.Results = tristate.False
case DefaultResourceSync:
t.addResources(
notify.TOPIC_RESOURCE_SERVER,
@@ -300,6 +310,7 @@ func (sm *STopicManager) InitializeData() error {
)
t.Type = notify.TOPIC_TYPE_RESOURCE
t.WebconsoleDisable = tristate.True
t.Results = tristate.True
case DefaultSystemExceptionEvent:
t.addResources(
notify.TOPIC_RESOURCE_HOST,
@@ -311,6 +322,7 @@ func (sm *STopicManager) InitializeData() error {
notify.ActionOffline,
)
t.Type = notify.TOPIC_TYPE_RESOURCE
t.Results = tristate.True
case DefaultChecksumTestFailed:
t.addResources(
notify.TOPIC_RESOURCE_DB_TABLE_RECORD,
@@ -322,6 +334,7 @@ func (sm *STopicManager) InitializeData() error {
notify.ActionChecksumTest,
)
t.Type = notify.TOPIC_TYPE_SECURITY
t.Results = tristate.True
case DefaultUserLock:
t.addResources(
notify.TOPIC_RESOURCE_USER,
@@ -330,6 +343,7 @@ func (sm *STopicManager) InitializeData() error {
notify.ActionLock,
)
t.Type = notify.TOPIC_TYPE_SECURITY
t.Results = tristate.True
case DefaultActionLogExceedCount:
t.addResources(
notify.TOPIC_RESOURCE_ACTION_LOG,
@@ -338,6 +352,7 @@ func (sm *STopicManager) InitializeData() error {
notify.ActionExceedCount,
)
t.Type = notify.TOPIC_TYPE_RESOURCE
t.Results = tristate.True
case DefaultSyncAccountStatus:
t.addResources(
notify.TOPIC_RESOURCE_ACCOUNT_STATUS,
@@ -346,6 +361,7 @@ func (sm *STopicManager) InitializeData() error {
notify.ActionSyncAccountStatus,
)
t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS
t.Results = tristate.True
case DefaultPasswordExpireDue1Day:
t.addResources(
notify.TOPIC_RESOURCE_USER,
@@ -355,6 +371,7 @@ func (sm *STopicManager) InitializeData() error {
)
t.Type = notify.TOPIC_TYPE_SECURITY
t.AdvanceDays = 1
t.Results = tristate.True
case DefaultPasswordExpireDue7Day:
t.addResources(
notify.TOPIC_RESOURCE_USER,
@@ -364,6 +381,7 @@ func (sm *STopicManager) InitializeData() error {
)
t.Type = notify.TOPIC_TYPE_SECURITY
t.AdvanceDays = 7
t.Results = tristate.True
case DefaultNetOutOfSync:
t.addResources(
notify.TOPIC_RESOURCE_NET,
@@ -373,6 +391,7 @@ func (sm *STopicManager) InitializeData() error {
)
t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS
t.AdvanceDays = 0
t.Results = tristate.True
case DefaultMysqlOutOfSync:
t.addResources(
notify.TOPIC_RESOURCE_DBINSTANCE,
@@ -382,6 +401,7 @@ func (sm *STopicManager) InitializeData() error {
)
t.Type = notify.TOPIC_TYPE_AUTOMATED_PROCESS
t.AdvanceDays = 0
t.Results = tristate.True
}
if topic == nil {
err := sm.TableSpec().Insert(ctx, t)
@@ -393,6 +413,7 @@ func (sm *STopicManager) InitializeData() error {
topic.Resources = t.Resources
topic.Actions = t.Actions
topic.Type = t.Type
topic.Results = t.Results
topic.WebconsoleDisable = t.WebconsoleDisable
return nil
})
@@ -506,12 +527,13 @@ func (sm *STopicManager) TopicByEvent(eventStr string, advanceDays int) (*STopic
if err != nil {
return nil, err
}
if len(topics) == 0 {
return nil, nil
if len(topics) == 1 {
return &topics[0], nil
}
// free memory in time
topic := topics[0]
return &topic, nil
if len(topics) == 0 {
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "eventStr:%s,advanceDays:%d", eventStr, advanceDays)
}
return nil, errors.Wrapf(cloudprovider.ErrDuplicateId, "eventStr:%s,advanceDays:%d", eventStr, advanceDays)
}
func (sm *STopicManager) TopicsByEvent(eventStr string, advanceDays int) ([]STopic, error) {
@@ -531,6 +553,12 @@ func (sm *STopicManager) TopicsByEvent(eventStr string, advanceDays int) ([]STop
return nil, nil
}
q := sm.Query().Equals("advance_days", advanceDays)
if event.Result() == api.ResultSucceed {
q = q.Equals("results", true)
} else {
q = q.Equals("results", false)
}
q = q.Filter(sqlchemy.GT(sqlchemy.AND_Val("", q.Field("resources"), 1<<resourceV), 0))
q = q.Filter(sqlchemy.GT(sqlchemy.AND_Val("", q.Field("actions"), 1<<actionV), 0))
var topics []STopic
@@ -147,7 +147,6 @@ func (self *NotificationSendTask) OnInit(ctx context.Context, obj db.IStandalone
})
}
}
var contactLen int
for lang, receivers := range map[string][]ReceiverSpec{
"": receivers,