Skip to content

Commit

Permalink
Merge pull request #835 from alex-smile/esb_db_update
Browse files Browse the repository at this point in the history
esb 优化 db 查询
  • Loading branch information
MrLYC authored Apr 11, 2022
2 parents c565a8b + fb5a113 commit 963d355
Show file tree
Hide file tree
Showing 21 changed files with 451 additions and 141 deletions.
1 change: 0 additions & 1 deletion paas2/esb/VERSION

This file was deleted.

33 changes: 33 additions & 0 deletions paas2/esb/components/bk/apis/esb/echo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS
Community Edition) available.
Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
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.
"""

import os

from common.constants import API_TYPE_Q
from components.component import Component

from .toolkit import configs


class Echo(Component):
sys_name = configs.SYSTEM_NAME
api_type = API_TYPE_Q

def handle(self):
result = self.outgoing.http_client.request(
self.request.wsgi_request.method,
host=os.environ.get("ESB_ECHO_HOST"),
path="/echo/",
params=self.request.kwargs if self.request.wsgi_request.method == "GET" else None,
data=self.request.kwargs if self.request.wsgi_request.method == "POST" else None,
)
self.response.payload = {"result": True, "data": result}
37 changes: 37 additions & 0 deletions paas2/esb/components/bk/apis/esb/sleep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS
Community Edition) available.
Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
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.
"""

import os

from common.constants import API_TYPE_Q
from components.component import Component

from .toolkit import configs


class Sleep(Component):
sys_name = configs.SYSTEM_NAME
api_type = API_TYPE_Q

def handle(self):
sleep = self.request.kwargs.get("sleep", 1000)
result = self.outgoing.http_client.request(
self.request.wsgi_request.method,
host=os.environ.get("ESB_ECHO_HOST"),
path="/sleep/%s/" % sleep,
response_type="text",
)
self.response.payload = {
"result": True,
"data": {},
"message": result,
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
specific language governing permissions and limitations under the License.
"""

from django.conf import settings

from common.constants import API_TYPE_Q
from components.component import ConfComponent
from .toolkit import configs
Expand All @@ -27,7 +29,11 @@ def handle(self):
extra_params["username"] = username

request_info = self.get_request_info(extra_params=extra_params)
# request_info = self.get_request_info()

# bk-user 要求 app_code 不能为空,而 cmsi tools 调用此组件时,未传递 app_code,
# 为更好地兼容 cmsi 作为自定义组件的场景,值为空时替换为网关的 app_code
if not self.request.app_code:
self.request.app_code = getattr(settings, "BK_APP_CODE", "")

response = self.outgoing.http_client.request(
self.dest_http_method,
Expand Down
2 changes: 2 additions & 0 deletions paas2/esb/components/esb_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ def _rel_path(x):
"is_hidden": True,
},
),
("/esb/echo/", {"comp_codename": "generic.esb.echo", "is_hidden": True}),
("/esb/sleep/", {"comp_codename": "generic.esb.sleep", "is_hidden": True}),
# BKUSER
(
"/v2/usermanage/fe_list_departments/",
Expand Down
28 changes: 20 additions & 8 deletions paas2/esb/components/generic/templates/cmsi/send_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import base64

from django import forms
from django.utils.encoding import force_text

from components.component import Component, SetupConfMixin
from common.forms import BaseComponentForm, ListField, DefaultBooleanField, TypeCheckField
Expand Down Expand Up @@ -108,12 +109,22 @@ def handle(self):
tools.validate_receiver(data["cc"], contact_way=self.contact_way)
# 根据蓝鲸平台用户数据,将用户名转换为邮箱地址
if data["receiver__username"] or data["cc__username"]:
user_data = tools.get_receiver_with_username(
receiver__username=data["receiver__username"],
cc__username=data["cc__username"],
contact_way=self.contact_way,
)
try:
user_data = tools.get_receiver_with_username(
receiver__username=data["receiver__username"],
cc__username=data["cc__username"],
contact_way=self.contact_way,
)
except tools.NoValidUser as err:
result = {
"result": False,
"message": force_text(err),
}
self.response.payload = tools.inject_invalid_usernames(result, err.invalid_usernames)
return

data.update(user_data)

if not data["sender"]:
data["sender"] = self.mail_sender

Expand All @@ -127,7 +138,7 @@ def handle(self):
"result": False,
"message": u"Some users failed to send email. %s" % data["_extra_user_error_msg"],
}
self.response.payload = result
self.response.payload = tools.inject_invalid_usernames(result, data.get("_invalid_usernames"))

elif self.smtp_host:
# 如果配置了 SMTP 服务,则通过 SMTP 邮件服务器,发送邮件
Expand All @@ -149,9 +160,10 @@ def handle(self):
"message": u"Some users failed to send email. %s" % data["_extra_user_error_msg"],
}

self.response.payload = result
self.response.payload = tools.inject_invalid_usernames(result, data.get("_invalid_usernames"))
else:
self.response.payload = {
result = {
"result": False,
"message": "Unfinished interface shall be improved by the component developer",
}
self.response.payload = tools.inject_invalid_usernames(result, data.get("_invalid_usernames"))
23 changes: 17 additions & 6 deletions paas2/esb/components/generic/templates/cmsi/send_sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import base64

from django import forms
from django.utils.encoding import force_text

from components.component import Component, SetupConfMixin
from common.forms import BaseComponentForm, ListField, DefaultBooleanField
Expand Down Expand Up @@ -65,9 +66,18 @@ def handle(self):
if data["receiver"]:
tools.validate_receiver(data["receiver"], contact_way=self.contact_way)
if data["receiver__username"]:
user_data = tools.get_receiver_with_username(
receiver__username=data["receiver__username"], contact_way=self.contact_way
)
try:
user_data = tools.get_receiver_with_username(
receiver__username=data["receiver__username"], contact_way=self.contact_way
)
except tools.NoValidUser as err:
result = {
"result": False,
"message": force_text(err),
}
self.response.payload = tools.inject_invalid_usernames(result, err.invalid_usernames)
return

data.update(user_data)

# TODO: can be updated
Expand All @@ -80,7 +90,7 @@ def handle(self):
"message": u"Some users failed to send sms. %s" % data["_extra_user_error_msg"],
}

self.response.payload = result
self.response.payload = tools.inject_invalid_usernames(result, data.get("_invalid_usernames"))
elif self.qcloud_app_id and self.qcloud_app_key:
for tel in tools.group_by_nation_code(data["receiver"]):
params = {
Expand All @@ -101,9 +111,10 @@ def handle(self):
"result": False,
"message": u"Some users failed to send sms. %s" % data["_extra_user_error_msg"],
}
self.response.payload = result
self.response.payload = tools.inject_invalid_usernames(result, data.get("_invalid_usernames"))
else:
self.response.payload = {
result = {
"result": False,
"message": "Unfinished interface shall be improved by the component developer",
}
self.response.payload = tools.inject_invalid_usernames(result, data.get("_invalid_usernames"))
25 changes: 18 additions & 7 deletions paas2/esb/components/generic/templates/cmsi/send_voice_msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import json
from django import forms
from django.utils.encoding import force_text

from components.component import Component, SetupConfMixin
from common.forms import BaseComponentForm, TypeCheckField, ListField
Expand Down Expand Up @@ -69,10 +70,19 @@ def handle(self):
data = self.form_data
# 将 receiver__username 中的用户名,转换为接口需要的 user_list_information 信息
if data["receiver__username"]:
user_data = tools.get_user_contact_with_username(
username_list=data["receiver__username"],
contact_way=self.contact_way,
)
try:
user_data = tools.get_user_contact_with_username(
username_list=data["receiver__username"],
contact_way=self.contact_way,
)
except tools.NoValidUser as err:
result = {
"result": False,
"message": force_text(err),
}
self.response.payload = tools.inject_invalid_usernames(result, err.invalid_usernames)
return

data["user_list_information"] = [
{
"username": username,
Expand All @@ -93,7 +103,7 @@ def handle(self):
"data": result.get("data"),
"message": u"Some users failed to send voice. %s" % data["_extra_user_error_msg"],
}
self.response.payload = result
self.response.payload = tools.inject_invalid_usernames(result, data.get("_invalid_usernames"))
elif self.qcloud_app_id and self.qcloud_app_key:
params = {
"user_list_information": data["user_list_information"],
Expand All @@ -118,9 +128,10 @@ def handle(self):
}
else:
result = {"result": True, "data": ret, "message": "OK"}
self.response.payload = result
self.response.payload = tools.inject_invalid_usernames(result, data.get("_invalid_usernames"))
else:
self.response.payload = {
result = {
"result": False,
"message": "Unfinished interface shall be improved by the component developer",
}
self.response.payload = tools.inject_invalid_usernames(result, data.get("_invalid_usernames"))
20 changes: 15 additions & 5 deletions paas2/esb/components/generic/templates/cmsi/send_weixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from django import forms
from django.utils import timezone
from django.utils.encoding import force_text

from components.component import Component, SetupConfMixin
from common.forms import BaseComponentForm, ListField, TypeCheckField, DefaultBooleanField
Expand Down Expand Up @@ -130,10 +131,19 @@ def handle(self):
data = self.form_data
# 根据蓝鲸平台用户数据,将用户名转换为微信用户ID
if data["receiver__username"]:
user_data = tools.get_receiver_with_username(
receiver__username=data["receiver__username"],
contact_way=self.contact_way,
)
try:
user_data = tools.get_receiver_with_username(
receiver__username=data["receiver__username"],
contact_way=self.contact_way,
)
except tools.NoValidUser as err:
result = {
"result": False,
"message": force_text(err),
}
self.response.payload = tools.inject_invalid_usernames(result, err.invalid_usernames)
return

data.update(user_data)

if self.wx_type == "mp":
Expand Down Expand Up @@ -167,4 +177,4 @@ def handle(self):
"result": False,
"message": u"Some users failed to send wechat message. %s" % data["_extra_user_error_msg"],
}
self.response.payload = result
self.response.payload = tools.inject_invalid_usernames(result, data.get("_invalid_usernames"))
41 changes: 36 additions & 5 deletions paas2/esb/components/generic/templates/cmsi/toolkit/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
ICON_DIR = os.path.dirname(os.path.abspath(__file__))


class NoValidUser(Exception):
def __init__(self, invalid_usernames, message):
self.invalid_usernames = invalid_usernames
super(NoValidUser, self).__init__(message)


def valid_email(email):
if not isinstance(email, basestring) or email.find("@") < 0:
return False
Expand Down Expand Up @@ -116,9 +122,10 @@ def get_receiver_with_username(receiver__username=None, cc__username=None, conta
u"The following users contact format are incorrect: %s" % ",".join(error_contact_user)
)
result["_extra_user_error_msg"] = ";".join(_extra_user_error_msg)
result["_invalid_usernames"] = not_exist_user

if receiver__username and not result.get("receiver"):
raise CommonAPIError(u"All users message failed to be sent. %s" % result["_extra_user_error_msg"])
raise NoValidUser(not_exist_user, u"All users message failed to be sent. %s" % result["_extra_user_error_msg"])

return result

Expand Down Expand Up @@ -150,9 +157,13 @@ def get_user_contact_with_username(username_list=None, contact_way="phone"):
_extra_user_error_msg = ";".join(_extra_user_error_msg)

if not user_contact_info:
raise CommonAPIError(u"All users contact information failed to get, %s" % _extra_user_error_msg)
raise NoValidUser(not_exist_user, u"All users contact information failed to get, %s" % _extra_user_error_msg)

return {"user_contact_info": user_contact_info, "_extra_user_error_msg": _extra_user_error_msg}
return {
"user_contact_info": user_contact_info,
"_extra_user_error_msg": _extra_user_error_msg,
"_invalid_usernames": not_exist_user,
}


def get_base64_icon(path):
Expand All @@ -169,13 +180,19 @@ def _get_users(usernames):
"no_page": True,
"best_match": 1,
"exact_lookups": ",".join(usernames),
"fields": "username,country_code,telephone,email,wx_userid",
"fields": "username,country_code,telephone,email,wx_userid,status,staff_status",
}
)
if not result["result"]:
raise CommonAPIError(u"Failed to get users contact information based on username, %s" % result["message"])

return {user["username"]: user for user in result["data"]}
return {
user["username"]: user
for user in result["data"]
# status: NORMAL: 正常, LOCKED: 被冻结, DELETED: 被删除, DISABLED: 被禁用
# staff_status: IN: 在职, OUT: 离职
if user["status"] in ("NORMAL", "LOCKED") and user["staff_status"] == "IN"
}


def group_by_nation_code(telephones):
Expand All @@ -188,3 +205,17 @@ def group_by_nation_code(telephones):
groups["other"].append(tel)

return [groups[key] for key in sorted(groups.keys())]


def inject_invalid_usernames(result, invalid_usernames):
try:
result.setdefault("data", {})
result["data"].update(
username_check={
"invalid": invalid_usernames or [],
}
)
except AttributeError:
pass

return result
Loading

0 comments on commit 963d355

Please sign in to comment.