Skip to content

Commit

Permalink
notifychannels add vms and email channel (#3287)
Browse files Browse the repository at this point in the history
* add vms and email notify channel

* add email and vms type in GetNotifyChannelTypes function

* modify cs-i18n.yml voice to phone

* refactor GetNotifyChannelTypes function

* modify notify-channels page size

* use Telephone and set email is default notify channel

* modify cs-i18n.yml

* modify short_message to sms

* delete notifychannel id is null logic

* delete analyzer-alert

* delete analyzer-alert

* delete org config in email,sms and vms
  • Loading branch information
Counterflowwind committed Dec 8, 2021
1 parent 9c11834 commit fb01cc4
Show file tree
Hide file tree
Showing 17 changed files with 384 additions and 231 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
update erda_notify_channel set type='sms' where type='short_message';
1 change: 1 addition & 0 deletions api/proto-go/all.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

304 changes: 159 additions & 145 deletions api/proto-go/core/services/notify/channel/pb/channel.pb.go

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion api/proto/core/services/notify/channel/channel.proto
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,16 @@ message GetNotifyChannelsEnabledResponse {

// channel type
enum Type {
SHORT_MESSAGE = 0;
SMS = 0;
DINGTALK_WORK_NOTICE = 1;
EMAIL = 2;
VMS = 3;
}

// channel service provider type
enum ProviderType {
ALIYUN_SMS = 0;
DINGTALK = 1;
SMTP = 2;
ALIYUN_VMS = 3;
}
10 changes: 9 additions & 1 deletion apistructs/notify_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ type NotifyChannelConfig struct {
AgentId int64 `json:"agentId"`
AppKey string `json:"appKey"`
AppSecret string `json:"appSecret"`
SMTPHost string `json:"smtpHost"`
SMTPUser string `json:"smtpUser"`
SMTPPassword string `json:"smtpPassword"`
SMTPPort int64 `json:"smtpPort"`
SMTPIsSSL bool `json:"smtpIsSsl"`
VMSTtsCode string `json:"vmsTtsCode"`
}

type NotifyChannelType string
type NotifyChannelProviderType string

const NOTIFY_CHANNEL_TYPE_SHORT_MESSAGE = NotifyChannelType("short_message")
const NOTIFY_CHANNEL_TYPE_SMS = NotifyChannelType("sms")
const NOTIFY_CHANNEL_PROVIDER_TYPE_ALIYUN = NotifyChannelProviderType("aliyun_sms")
const NOTIFY_CHANNEL_TYPE_DINGTALK_WORK_NOTICE = NotifyChannelType("dingtalk_work_notice")
const NOTIFY_CHANNEL_TYPE_EMAIL = NotifyChannelType("email")
const NOTIFY_CHANNEL_TYPE_VMS = NotifyChannelType("vms")
4 changes: 2 additions & 2 deletions apistructs/notify_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Test_NotifyChannelDTO_Deserialization(t *testing.T) {
"id": "85fe1f9e-7cc4-4d65-baab-be0346d53849",
"name": "test_channel18",
"type": {
"name": "short_message",
"name": "sms",
"displayName": "短信"
},
"config": {
Expand All @@ -50,7 +50,7 @@ func Test_NotifyChannelDTO_Deserialization(t *testing.T) {
Type: struct {
Name NotifyChannelType `json:"name"`
DisplayName string `json:"displayName"`
}{Name: NOTIFY_CHANNEL_TYPE_SHORT_MESSAGE, DisplayName: "短信"},
}{Name: NOTIFY_CHANNEL_TYPE_SMS, DisplayName: "短信"},
Config: &NotifyChannelConfig{
AccessKeyId: "xx",
AccessKeySecret: "xx",
Expand Down
9 changes: 9 additions & 0 deletions apistructs/notify_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ const (
WebhookNotifyTarget NotifyTargetType = "webhook"
)

var ValidateNotifyChannel = map[string]bool{
"dingding": true,
"sms": true,
"email": true,
"mbox": true,
"webhook": true,
"vms": true,
}

// NotifyTarget 通知目标
type NotifyTarget struct {
Type NotifyTargetType `json:"type"`
Expand Down
12 changes: 10 additions & 2 deletions conf/core-services/i18n/cs-i18n.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
en:
aliyun_sms: "Ali Short Message Service"
short_message: "Short Message"
aliyun_vms: "Ali Voice Service"
sms: "Short Message"
vms: "Telephone"
enabled_exception: "There are other enabled channel of this type %s"
dingtalk: "DingTalk"
dingtalk_work_notice: "DingTalk Work Notice"
smtp: "SMTP"
email: "Email"
zh:
aliyun_sms: "阿里云短信服务"
short_message: "短信"
aliyun_vms: "阿里云语音服务"
sms: "短信"
vms: "电话"
enabled_exception: "%s类型已经存在开启状态的渠道"
dingtalk: "钉钉"
dingtalk_work_notice: "钉钉工作通知"
email: "邮箱"
smtp: "SMTP"
36 changes: 36 additions & 0 deletions modules/core-services/services/notify/channel/kind/aliyun_vms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kind

import "github.com/erda-project/erda/pkg/common/errors"

type AliyunVMS struct {
AccessKeyId string `json:"accessKeyId"`
AccessKeySecret string `json:"accessKeySecret"`
VMSTtsCode string `json:"vmsTtsCode"`
}

func (vms *AliyunVMS) Validate() error {
if vms.AccessKeySecret == "" {
return errors.NewMissingParameterError("accessKeySecret")
}
if vms.AccessKeyId == "" {
return errors.NewMissingParameterError("accessKeyId")
}
if vms.VMSTtsCode == "" {
return errors.NewMissingParameterError("vmsTtsCode")
}
return nil
}
45 changes: 45 additions & 0 deletions modules/core-services/services/notify/channel/kind/email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kind

import (
"fmt"

"github.com/erda-project/erda/pkg/common/errors"
)

type Email struct {
SMTPHost string `json:"smtpHost"`
SMTPUser string `json:"smtpUser"`
SMTPPassword string `json:"smtpPassword"`
SMTPPort int64 `json:"smtpPort"`
SMTPIsSSL bool `json:"smtpIsSSL"`
}

func (email *Email) Validate() error {
if email.SMTPHost == "" {
return errors.NewMissingParameterError("smtpHost")
}
if email.SMTPUser == "" {
return errors.NewMissingParameterError("smtpUser")
}
if email.SMTPPassword == "" {
return errors.NewMissingParameterError("smtpPassword")
}
if email.SMTPPort < 0 || email.SMTPPort > 65535 {
return fmt.Errorf("invalidate parameter %d", email.SMTPPort)
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ type notifyChannelService struct {
NotifyChannelDB *db.NotifyChannelDB
}

var ProviderAndType = map[string]string{
pb.ProviderType_ALIYUN_SMS.String(): pb.Type_SMS.String(),
pb.ProviderType_DINGTALK.String(): pb.Type_DINGTALK_WORK_NOTICE.String(),
pb.ProviderType_SMTP.String(): pb.Type_EMAIL.String(),
pb.ProviderType_ALIYUN_VMS.String(): pb.Type_VMS.String(),
}

func (s *notifyChannelService) CreateNotifyChannel(ctx context.Context, req *pb.CreateNotifyChannelRequest) (*pb.CreateNotifyChannelResponse, error) {
if req.Name == "" {
return nil, pkgerrors.NewMissingParameterError("name")
Expand Down Expand Up @@ -127,8 +134,8 @@ func (s *notifyChannelService) GetNotifyChannels(ctx context.Context, req *pb.Ge
if req.PageNo < 1 {
req.PageNo = 1
}
if req.PageSize < 15 {
req.PageSize = 15
if req.PageSize < 10 {
req.PageSize = 10
}
if req.PageSize > 60 {
req.PageSize = 60
Expand Down Expand Up @@ -284,32 +291,22 @@ func (s *notifyChannelService) DeleteNotifyChannel(ctx context.Context, req *pb.
}

func (s *notifyChannelService) GetNotifyChannelTypes(ctx context.Context, req *pb.GetNotifyChannelTypesRequest) (*pb.GetNotifyChannelTypesResponse, error) {

language := apis.Language(ctx)

var shortMessageProviderTypes []*pb.NotifyChannelProviderType
shortMessageProviderTypes = append(shortMessageProviderTypes, &pb.NotifyChannelProviderType{
Name: strings.ToLower(pb.ProviderType_ALIYUN_SMS.String()),
DisplayName: s.p.I18n.Text(language, strings.ToLower(pb.ProviderType_ALIYUN_SMS.String())),
})
var dingWorkNoticeProviderTypes []*pb.NotifyChannelProviderType
dingWorkNoticeProviderTypes = append(dingWorkNoticeProviderTypes, &pb.NotifyChannelProviderType{
Name: strings.ToLower(pb.ProviderType_DINGTALK.String()),
DisplayName: s.p.I18n.Text(language, strings.ToLower(pb.ProviderType_DINGTALK.String())),
})

var types []*pb.NotifyChannelTypeResponse
types = append(types, &pb.NotifyChannelTypeResponse{
Name: strings.ToLower(pb.Type_SHORT_MESSAGE.String()),
DisplayName: s.p.I18n.Text(language, strings.ToLower(pb.Type_SHORT_MESSAGE.String())),
Providers: shortMessageProviderTypes,
})

types = append(types, &pb.NotifyChannelTypeResponse{
Name: strings.ToLower(pb.Type_DINGTALK_WORK_NOTICE.String()),
DisplayName: s.p.I18n.Text(language, pb.Type_DINGTALK_WORK_NOTICE.String()),
Providers: dingWorkNoticeProviderTypes,
})
for provider, channelType := range ProviderAndType {
providerTypes := []*pb.NotifyChannelProviderType{
{
Name: strings.ToLower(provider),
DisplayName: s.p.I18n.Text(language, strings.ToLower(provider)),
},
}
types = append(types, &pb.NotifyChannelTypeResponse{
Name: strings.ToLower(channelType),
DisplayName: s.p.I18n.Text(language, strings.ToLower(channelType)),
Providers: providerTypes,
})
}
return &pb.GetNotifyChannelTypesResponse{Data: types}, nil
}

Expand Down Expand Up @@ -489,6 +486,40 @@ func (s *notifyChannelService) ConfigValidate(channelType string, c map[string]*
c["need_kms_key"] = structpb.NewStringValue("appSecret")
c["need_kms_data"] = structpb.NewStringValue(dingNotice.AppSecret)
return c, nil
case strings.ToLower(pb.ProviderType_SMTP.String()):
bytes, err := json.Marshal(c)
if err != nil {
return nil, errors.New("Json parser failed.")
}
var email kind.Email
err = json.Unmarshal(bytes, &email)
if err != nil {
return nil, err
}
err = email.Validate()
if err != nil {
return nil, err
}
c["need_kms_key"] = structpb.NewStringValue("smtpPassword")
c["need_kms_data"] = structpb.NewStringValue(email.SMTPPassword)
return c, nil
case strings.ToLower(pb.ProviderType_ALIYUN_VMS.String()):
bytes, err := json.Marshal(c)
if err != nil {
return nil, errors.New("Json parser failed.")
}
var vms kind.AliyunVMS
err = json.Unmarshal(bytes, &vms)
if err != nil {
return nil, err
}
err = vms.Validate()
if err != nil {
return nil, err
}
c["need_kms_key"] = structpb.NewStringValue("accessKeySecret")
c["need_kms_data"] = structpb.NewStringValue(vms.AccessKeySecret)
return c, nil
default:
return nil, errors.New("Not support notify channel type")
}
Expand Down

0 comments on commit fb01cc4

Please sign in to comment.