diff --git a/cmd/climc/main.go b/cmd/climc/main.go index 4c7f0695d7..f10a5671bc 100644 --- a/cmd/climc/main.go +++ b/cmd/climc/main.go @@ -33,7 +33,7 @@ import ( _ "yunion.io/x/onecloud/cmd/climc/shell/logger" _ "yunion.io/x/onecloud/cmd/climc/shell/misc" _ "yunion.io/x/onecloud/cmd/climc/shell/monitor" - _ "yunion.io/x/onecloud/cmd/climc/shell/notifyv2" + _ "yunion.io/x/onecloud/cmd/climc/shell/notify" _ "yunion.io/x/onecloud/cmd/climc/shell/quota" _ "yunion.io/x/onecloud/cmd/climc/shell/scheduledtask" _ "yunion.io/x/onecloud/cmd/climc/shell/scheduler" diff --git a/cmd/climc/shell/notifyv2/common.go b/cmd/climc/shell/notify/common.go similarity index 100% rename from cmd/climc/shell/notifyv2/common.go rename to cmd/climc/shell/notify/common.go diff --git a/cmd/climc/shell/notifyv2/config.go b/cmd/climc/shell/notify/config.go similarity index 100% rename from cmd/climc/shell/notifyv2/config.go rename to cmd/climc/shell/notify/config.go diff --git a/cmd/climc/shell/notifyv2/emailqueues.go b/cmd/climc/shell/notify/emailqueues.go similarity index 100% rename from cmd/climc/shell/notifyv2/emailqueues.go rename to cmd/climc/shell/notify/emailqueues.go diff --git a/cmd/climc/shell/notifyv2/notification.go b/cmd/climc/shell/notify/notification.go similarity index 100% rename from cmd/climc/shell/notifyv2/notification.go rename to cmd/climc/shell/notify/notification.go diff --git a/cmd/climc/shell/notifyv2/receiver.go b/cmd/climc/shell/notify/receiver.go similarity index 95% rename from cmd/climc/shell/notifyv2/receiver.go rename to cmd/climc/shell/notify/receiver.go index 15454f6548..aee6275b39 100644 --- a/cmd/climc/shell/notifyv2/receiver.go +++ b/cmd/climc/shell/notify/receiver.go @@ -34,4 +34,5 @@ func init() { cmd.Perform("enable-contact-type", new(options.ReceiverEnableContactTypeInput)) cmd.PerformClass("intellij-get", new(options.ReceiverIntellijGetOptions)) cmd.PerformClass("get-types", new(options.ReceiverGetTypeOptions)) + cmd.Perform("get-subscription", new(options.ReceiverGetSubscriptionOptions)) } diff --git a/cmd/climc/shell/notifyv2/robot.go b/cmd/climc/shell/notify/robot.go similarity index 100% rename from cmd/climc/shell/notifyv2/robot.go rename to cmd/climc/shell/notify/robot.go diff --git a/cmd/climc/shell/notifyv2/subscription.go b/cmd/climc/shell/notify/subscription.go similarity index 100% rename from cmd/climc/shell/notifyv2/subscription.go rename to cmd/climc/shell/notify/subscription.go diff --git a/cmd/climc/shell/notifyv2/template.go b/cmd/climc/shell/notify/template.go similarity index 100% rename from cmd/climc/shell/notifyv2/template.go rename to cmd/climc/shell/notify/template.go diff --git a/pkg/mcclient/options/notify/receiver.go b/pkg/mcclient/options/notify/receiver.go index ebf033ee5f..da373e9338 100644 --- a/pkg/mcclient/options/notify/receiver.go +++ b/pkg/mcclient/options/notify/receiver.go @@ -101,6 +101,14 @@ func (rt *ReceiverTriggerVerifyOptions) Params() (jsonutils.JSONObject, error) { return jsonutils.Marshal(rt.SreceiverTriggerVerifyOptions), nil } +type ReceiverGetSubscriptionOptions struct { + ReceiverOptions +} + +func (rt *ReceiverGetSubscriptionOptions) Params() (jsonutils.JSONObject, error) { + return jsonutils.Marshal(rt), nil +} + type ReceiverVerifyOptions struct { ReceiverOptions SreceiverVerifyOptions diff --git a/pkg/notify/models/receiver.go b/pkg/notify/models/receiver.go index b3cdb3797b..8bb83757aa 100644 --- a/pkg/notify/models/receiver.go +++ b/pkg/notify/models/receiver.go @@ -784,6 +784,31 @@ func (r *SReceiver) IsOwner(userCred mcclient.TokenCredential) bool { return r.Id == userCred.GetUserId() } +// 获取用户订阅 +func (r *SReceiver) PerformGetSubscription(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ReceiverIntellijGetInput) (jsonutils.JSONObject, error) { + subscribers, err := getSubscriberByReceiverId(r.Id) + if err != nil { + return nil, errors.Wrap(err, "getSubscriberByReceiverId") + } + type retStruct struct { + SSubscriber + TopicName string + } + res := []retStruct{} + for _, subscriber := range subscribers { + topicModel, err := TopicManager.FetchById(subscriber.TopicId) + topic := topicModel.(*STopic) + if topic.Enabled == tristate.False { + continue + } + if err != nil { + return nil, errors.Wrap(err, "fetch topic by id") + } + res = append(res, retStruct{subscriber, topic.GetName()}) + } + return jsonutils.Marshal(res), nil +} + func (rm *SReceiverManager) PerformIntellijGet(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ReceiverIntellijGetInput) (jsonutils.JSONObject, error) { getParam := jsonutils.NewDict() getParam.Set("scope", jsonutils.NewString(input.Scope)) diff --git a/pkg/notify/models/subscriber.go b/pkg/notify/models/subscriber.go index 415cd2c3a9..5b01787692 100644 --- a/pkg/notify/models/subscriber.go +++ b/pkg/notify/models/subscriber.go @@ -44,6 +44,7 @@ import ( "yunion.io/x/pkg/utils" "yunion.io/x/sqlchemy" + identityapi "yunion.io/x/onecloud/pkg/apis/identity" api "yunion.io/x/onecloud/pkg/apis/notify" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/httperrors" @@ -80,7 +81,7 @@ type SSubscriber struct { db.SStandaloneAnonResourceBase db.SEnabledResourceBase - TopicID string `width:"128" charset:"ascii" nullable:"false" index:"true" get:"user" list:"user" create:"required"` + TopicId string `width:"128" charset:"ascii" nullable:"false" index:"true" get:"user" list:"user" create:"required"` Type string `width:"16" charset:"ascii" nullable:"false" index:"true" get:"user" list:"user" create:"required"` Identification string `width:"128" charset:"ascii" nullable:"false" index:"true"` RoleScope string `width:"8" charset:"ascii" nullable:"false" get:"user" list:"user" create:"optional"` @@ -685,7 +686,7 @@ func (sm *SSubscriberManager) InitializeData() error { subscriber := SSubscriber{} subscriber.Type = api.SUBSCRIBER_TYPE_ROLE subscriber.Identification = roleId - subscriber.TopicID = topic.Id + subscriber.TopicId = topic.Id subscriber.Scope = api.SUBSCRIBER_SCOPE_SYSTEM subscriber.ResourceScope = api.SUBSCRIBER_SCOPE_SYSTEM subscriber.Enabled = tristate.True @@ -693,3 +694,49 @@ func (sm *SSubscriberManager) InitializeData() error { } return nil } + +// 根据接受人ID获取订阅 +func getSubscriberByReceiverId(receiverId string) ([]SSubscriber, error) { + // 获取当前接受人所有角色 + s := auth.GetAdminSession(context.Background(), options.Options.Region) + query := jsonutils.NewDict() + query.Add(jsonutils.NewString(receiverId), "user", "id") + resp, err := identity.RoleAssignments.List(s, query) + if err != nil { + return nil, errors.Wrap(err, "UserCacheManager.FetchUserByIdOrName") + } + roleAssignments := []identityapi.SRoleAssignment{} + err = jsonutils.Update(&roleAssignments, resp.Data) + if err != nil { + return nil, errors.Wrap(err, "update roleAssignments") + } + roleArr := []string{} + for _, roleAssignment := range roleAssignments { + roleArr = append(roleArr, roleAssignment.Role.Id) + } + + results := []SSubscriber{} + // q1 根据角色查找 + q1 := SubscriberManager.Query() + q1 = q1.Equals("type", api.SUBSCRIBER_TYPE_ROLE) + q1 = q1.In("identification", roleArr) + tempRes := []SSubscriber{} + err = db.FetchModelObjects(SubscriberManager, q1, &tempRes) + if err != nil { + return nil, errors.Wrap(err, "fetch role") + } + results = append(results, tempRes...) + + // q2 根据接受人ID查找 + q2 := SubscriberManager.Query() + srq := SubscriberReceiverManager.Query().Equals("receiver_id", receiverId) + srsq := srq.SubQuery() + q2 = q2.Equals("type", api.SUBSCRIBER_TYPE_RECEIVER) + q2.Join(srsq, sqlchemy.Equals(q2.Field("id"), srsq.Field("subscriber_id"))) + err = db.FetchModelObjects(SubscriberManager, q2, &tempRes) + if err != nil { + return nil, errors.Wrap(err, "fetch receiver") + } + results = append(results, tempRes...) + return results, nil +}