Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: general http data source problem #1336

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/bk-user/bkuser/apis/web/data_source/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ class DataSourceUpdateInputSLZ(serializers.Serializer):
sync_config = DataSourceSyncConfigSLZ(help_text="数据源同步配置", required=False)

def validate_name(self, name: str) -> str:
# 自己目前在用的名字是可以的,不然每次更新都要修改名字
if name == self.context["current_name"]:
return name

if DataSource.objects.filter(name=name).exists():
raise ValidationError(_("同名数据源已存在"))

Expand Down
1 change: 1 addition & 0 deletions src/bk-user/bkuser/apis/web/data_source/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def put(self, request, *args, **kwargs):
context={
"plugin_id": data_source.plugin_id,
"tenant_id": self.get_current_tenant_id(),
"current_name": data_source.name,
},
)
slz.is_valid(raise_exception=True)
Expand Down
6 changes: 3 additions & 3 deletions src/bk-user/bkuser/apis/web/tenant_setting/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class TenantUserCustomFieldCreateInputSLZ(serializers.Serializer):
data_type = serializers.ChoiceField(help_text="字段类型", choices=UserFieldDataType.get_choices())
required = serializers.BooleanField(help_text="是否必填")
default = serializers.JSONField(help_text="默认值", required=False)
options = serializers.JSONField(help_text="选项", required=False)
options = serializers.JSONField(help_text="选项", required=False, default=list)

def validate_display_name(self, display_name):
if TenantUserCustomField.objects.filter(
Expand Down Expand Up @@ -131,8 +131,8 @@ class TenantUserCustomFieldCreateOutputSLZ(serializers.Serializer):
class TenantUserCustomFieldUpdateInputSLZ(serializers.Serializer):
display_name = serializers.CharField(help_text="展示用名称", max_length=128)
required = serializers.BooleanField(help_text="是否必填")
default = serializers.JSONField(help_text="默认值")
options = serializers.JSONField(help_text="选项")
default = serializers.JSONField(help_text="默认值", required=False)
options = serializers.JSONField(help_text="选项", required=False, default=list)

def validate_display_name(self, display_name):
if (
Expand Down
1 change: 1 addition & 0 deletions src/bk-user/bkuser/apps/sync/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def _get_field_mapping(self) -> List[DataSourceUserFieldMapping]:

def convert(self, user: RawDataSourceUser) -> DataSourceUser:
# TODO (su) 重构,支持复杂字段映射类型,如表达式,目前都当作直接映射处理(本地数据源只有直接映射)
# FIXME (su) username,email,phone 都是有检查规则的
narasux marked this conversation as resolved.
Show resolved Hide resolved
mapping = {m.source_field: m.target_field for m in self.field_mapping}
props = user.properties
return DataSourceUser(
Expand Down
1 change: 1 addition & 0 deletions src/bk-user/bkuser/apps/sync/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def execute(self, plugin_init_extra_kwargs: Optional[Dict[str, Any]] = None) ->
operator=self.sync_options.operator,
start_at=timezone.now(),
extra={
"incremental": self.sync_options.incremental,
"overwrite": self.sync_options.overwrite,
"async_run": self.sync_options.async_run,
},
Expand Down
21 changes: 11 additions & 10 deletions src/bk-user/bkuser/plugins/general/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,16 @@ def fetch_all_data(
resp = session.get(url, headers=headers, params=params, timeout=timeout)
if not resp.ok:
raise RequestApiError(
_("请求数据源 API {} 参数 {} 异常,状态码:{} 响应内容 {}").format(
url,
params,
resp.status_code,
resp.content,
)
_("请求数据源 API {} 参数 {} 异常,状态码 {} 响应内容 {}").format(
url, params, resp.status_code, resp.content
) # noqa: E501
)

try:
resp_data = resp.json()
except JSONDecodeError: # noqa: PERF203
raise RespDataFormatError(
_("数据源 API {} 参数 {} 返回非 Json 格式,响应内容").format(url, params, resp.content)
_("数据源 API {} 参数 {} 返回非 Json 格式,响应内容 {}").format(url, params, resp.content)
) # noqa: E501

total_cnt = resp_data.get("count", 0)
Expand Down Expand Up @@ -120,12 +117,16 @@ def fetch_first_item(url: str, headers: Dict[str, str], timeout: int) -> Dict[st
params = {"page": DEFAULT_PAGE, "page_size": PAGE_SIZE_FOR_FETCH_FIRST}
resp = requests.get(url, headers=headers, params=params, timeout=timeout)
if not resp.ok:
raise RequestApiError(_("请求数据源 API {} 异常:{}").format(url, resp.content))
raise RequestApiError(
_("请求数据源 API {} 参数 {} 异常,状态码 {} 响应内容 {}").format(url, params, resp.status_code, resp.content) # noqa: E501
)

try:
resp_data = resp.json()
except JSONDecodeError:
raise RespDataFormatError(_("数据源 API {} 返回非 Json 格式").format(url))
except JSONDecodeError: # noqa: PERF203
raise RespDataFormatError(
_("数据源 API {} 参数 {} 返回非 Json 格式,响应内容 {}").format(url, params, resp.content)
) # noqa: E501

results = resp_data.get("results", [])
if not results:
Expand Down
5 changes: 5 additions & 0 deletions src/bk-user/bkuser/plugins/general/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ def test_connection(self) -> TestConnectionResult:
logger.exception("general data source plugin test connection error")
err_msg = str(e)

# 请求 API 有异常,直接返回
if err_msg:
return TestConnectionResult(error_message=err_msg)

# 检查获取到的数据情况,若都没有数据,也是异常
if not (user_data and dept_data):
err_msg = _("获取到的用户/部门数据为空,请检查数据源 API 服务")
else:
Expand Down
4 changes: 2 additions & 2 deletions src/bk-user/bkuser/plugins/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class TestConnectionResult(BaseModel):
# 连通性测试错误信息,空则表示正常
error_message: str
# 获取到的首个数据源用户
user: RawDataSourceUser | None
user: RawDataSourceUser | None = None
# 获取到的首个数据源部门
department: RawDataSourceDepartment | None
department: RawDataSourceDepartment | None = None
# 可能便于排查问题的额外数据
extras: Dict[str, Any] | None = None
7 changes: 7 additions & 0 deletions src/bk-user/tests/apis/web/data_source/test_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ def test_update_local_data_source(self, api_client, data_source, local_ds_plugin
assert resp.data["name"] == new_data_source_name
assert resp.data["plugin_config"]["enable_account_password_login"] is False

def test_update_without_change_name(self, api_client, data_source, local_ds_plugin_cfg):
resp = api_client.put(
reverse("data_source.retrieve_update", kwargs={"id": data_source.id}),
data={"name": data_source.name, "plugin_config": local_ds_plugin_cfg},
)
assert resp.status_code == status.HTTP_204_NO_CONTENT

def test_update_with_invalid_plugin_config(self, api_client, data_source, local_ds_plugin_cfg):
local_ds_plugin_cfg.pop("enable_account_password_login")
resp = api_client.put(
Expand Down
Loading