feat(notify): SupportReceiverGetContactTypeByRoleId (#19296)

Co-authored-by: 马鸿飞 <mahongfei@yunion.cn>
This commit is contained in:
gouqi11
2024-01-19 10:14:35 +08:00
committed by GitHub
co-authored by 马鸿飞
parent 4187111862
commit bf6a035b53
7 changed files with 95 additions and 1 deletions
+1
View File
@@ -35,4 +35,5 @@ func init() {
cmd.PerformClass("intellij-get", new(options.ReceiverIntellijGetOptions))
cmd.PerformClass("get-types", new(options.ReceiverGetTypeOptions))
cmd.Perform("get-subscription", new(options.ReceiverGetSubscriptionOptions))
cmd.GetProperty(new(options.SReceiverRoleContactType))
}
+11
View File
@@ -206,3 +206,14 @@ type ReceiverGetSubscriptionOptions struct {
Id string
ShowDisabled bool
}
type SRoleContactInput struct {
RoleIds []string
Scope string
ProjectDomainId string
ProjectId string `json:"project_id"`
}
type SRoleContactOutput struct {
ContactType []string
}
+15
View File
@@ -156,3 +156,18 @@ type ReceiverGetTypeOptions struct {
func (rg *ReceiverGetTypeOptions) Params() (jsonutils.JSONObject, error) {
return jsonutils.Marshal(rg), nil
}
type SReceiverRoleContactType struct {
ROLE_IDS []string
Scope string
ProjectDomainId string
ProjectId string
}
func (rg *SReceiverRoleContactType) Property() string {
return "role-contact-type"
}
func (rg *SReceiverRoleContactType) Params() (jsonutils.JSONObject, error) {
return jsonutils.Marshal(rg), nil
}
+55
View File
@@ -44,6 +44,7 @@ import (
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/informer"
identity_modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
modules "yunion.io/x/onecloud/pkg/mcclient/modules/identity"
notify_modules "yunion.io/x/onecloud/pkg/mcclient/modules/notify"
"yunion.io/x/onecloud/pkg/notify/options"
"yunion.io/x/onecloud/pkg/util/logclient"
@@ -1298,3 +1299,57 @@ func (r *SReceiver) IsReceiver() bool {
func (r *SReceiver) GetName() string {
return r.Name
}
func (manager *SReceiverManager) GetPropertyRoleContactType(ctx context.Context, userCred mcclient.TokenCredential, input api.SRoleContactInput) (jsonutils.JSONObject, error) {
out := api.SRoleContactOutput{
ContactType: []string{},
}
if len(input.RoleIds) == 0 {
return nil, httperrors.NewMissingParameterError("role_ids")
}
receiverIds := []string{}
params := jsonutils.NewDict()
params.Set("roles", jsonutils.NewStringArray(input.RoleIds))
params.Set("effective", jsonutils.JSONTrue)
switch input.Scope {
case api.SUBSCRIBER_SCOPE_SYSTEM:
case api.SUBSCRIBER_SCOPE_DOMAIN:
if len(input.ProjectDomainId) == 0 {
return nil, httperrors.NewMissingParameterError("project_domain_id")
}
params.Set("project_domain_id", jsonutils.NewString(input.ProjectDomainId))
case api.SUBSCRIBER_SCOPE_PROJECT:
if len(input.ProjectId) == 0 {
return nil, httperrors.NewMissingParameterError("project_id")
}
params.Add(jsonutils.NewString(input.ProjectId), "scope", "project", "id")
}
s := auth.GetAdminSession(ctx, "")
listRet, err := modules.RoleAssignments.List(s, params)
if err != nil {
return nil, errors.Wrap(err, "unable to list RoleAssignments")
}
roleAssignmentOut := []identity_api.SRoleAssignment{}
jsonutils.Update(&roleAssignmentOut, listRet.Data)
for i := range roleAssignmentOut {
receiverIds = append(receiverIds, roleAssignmentOut[i].User.Id)
}
subContacts, err := manager.FetchSubContacts(receiverIds)
if err != nil {
return nil, errors.Wrap(err, "fetch subcontacts")
}
contactMap := map[string]struct{}{}
for i := range receiverIds {
for _, contact := range subContacts[receiverIds[i]] {
if contact.Enabled.Bool() {
contactMap[contact.Type] = struct{}{}
}
}
}
for contactType := range contactMap {
out.ContactType = append(out.ContactType, contactType)
}
return jsonutils.Marshal(out), nil
}
+3
View File
@@ -144,6 +144,9 @@ func (dingSender *SDingTalkSender) RegisterConfig(config models.SConfig) {
func (dingSender *SDingTalkSender) GetAccessToken(ctx context.Context, domainId string) error {
key := fmt.Sprintf("%s-%s", api.DINGTALK, domainId)
if _, ok := models.ConfigMap[key]; !ok {
return errors.Wrapf(errors.ErrNotSupported, "contact-type:%s,domain_id:%s is missing config", api.DINGTALK, domainId)
}
appKey, appSecret := models.ConfigMap[key].Content.AppKey, models.ConfigMap[key].Content.AppSecret
token, err := dingSender.getAccessToken(ctx, appKey, appSecret)
if err != nil {
+4 -1
View File
@@ -4,7 +4,7 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@@ -158,6 +158,9 @@ func (feishuSender *SFeishuSender) RegisterConfig(config models.SConfig) {
// 获取token
func (feishuSender *SFeishuSender) GetAccessToken(ctx context.Context, domainId string) error {
key := fmt.Sprintf("%s-%s", api.FEISHU, domainId)
if _, ok := models.ConfigMap[key]; !ok {
return errors.Wrapf(errors.ErrNotSupported, "contact-type:%s,domain_id:%s is missing config", api.FEISHU, domainId)
}
appId, appSecret := models.ConfigMap[key].Content.AppId, models.ConfigMap[key].Content.AppSecret
resp, err := feishuSender.getAccessToken(appId, appSecret)
if err != nil {
+6
View File
@@ -133,6 +133,9 @@ func (workwxSender *SWorkwxSender) RegisterConfig(config models.SConfig) {
func (workwxSender *SWorkwxSender) GetAccessToken(ctx context.Context, domainId string) error {
key := fmt.Sprintf("%s-%s", api.WORKWX, domainId)
if _, ok := models.ConfigMap[key]; !ok {
return errors.Wrapf(errors.ErrNotSupported, "contact-type:%s,domain_id:%s is missing config", api.WORKWX, domainId)
}
corpId, secret := models.ConfigMap[key].Content.CorpId, models.ConfigMap[key].Content.Secret
token, err := workwxSender.getAccessToken(ctx, corpId, secret)
if err != nil {
@@ -157,6 +160,9 @@ func (workwxSender *SWorkwxSender) sendMessageWithToken(ctx context.Context, uri
if params == nil {
params = url.Values{}
}
if _, ok := models.ConfigMap[key]; !ok {
return nil, errors.Wrapf(errors.ErrNotSupported, "contact-type:%s,domain_id:%s is missing config", strings.Split(key, "-")[0], strings.Split(key, "-")[1])
}
params.Set("access_token", models.ConfigMap[key].Content.AccessToken)
return sendRequest(ctx, uri, httputils.POST, nil, params, jsonutils.Marshal(body))
}