Skip to content

Commit

Permalink
feat: local data source import & export
Browse files Browse the repository at this point in the history
  • Loading branch information
narasux committed Sep 12, 2023
1 parent 8af8703 commit ce60877
Show file tree
Hide file tree
Showing 37 changed files with 1,492 additions and 101 deletions.
21 changes: 21 additions & 0 deletions src/bk-user/bkuser/apis/web/data_source/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 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.
"""
from bkuser.apis.web.mixins import CurrentUserTenantMixin
from bkuser.apps.data_source.models import DataSource


class CurrentUserTenantDataSourceMixin(CurrentUserTenantMixin):
"""获取当前用户所在租户下属数据源"""

lookup_url_kwarg = "id"

def get_queryset(self):
return DataSource.objects.filter(owner_tenant_id=self.get_current_tenant_id())
23 changes: 21 additions & 2 deletions src/bk-user/bkuser/apis/web/data_source/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class DataSourcePluginOutputSLZ(serializers.Serializer):
logo = serializers.CharField(help_text="数据源插件 Logo")


class DataSourcePluginDefaultConfigOutputSLZ(serializers.Serializer):
config = serializers.JSONField(help_text="数据源插件默认配置")


class DataSourceRetrieveOutputSLZ(serializers.Serializer):
id = serializers.IntegerField(help_text="数据源 ID")
name = serializers.CharField(help_text="数据源名称")
Expand Down Expand Up @@ -181,5 +185,20 @@ class DataSourceTestConnectionOutputSLZ(serializers.Serializer):
"""数据源连通性测试"""

error_message = serializers.CharField(help_text="错误信息")
user = serializers.CharField(help_text="用户")
department = serializers.CharField(help_text="部门")
user = RawDataSourceUserSLZ(help_text="用户")
department = RawDataSourceDepartmentSLZ(help_text="部门")


class LocalDataSourceImportInputSLZ(serializers.Serializer):
"""本地数据源导入"""

file = serializers.FileField(help_text="数据源用户信息文件(Excel 格式)")
overwrite = serializers.BooleanField(help_text="允许对同名用户覆盖更新", default=False)


class LocalDataSourceImportOutputSLZ(serializers.Serializer):
"""本地数据源导入结果"""

task_id = serializers.CharField(help_text="任务 ID")
status = serializers.CharField(help_text="任务状态")
summary = serializers.CharField(help_text="任务执行结果概述")
6 changes: 6 additions & 0 deletions src/bk-user/bkuser/apis/web/data_source/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
urlpatterns = [
# 数据源插件列表
path("plugins/", views.DataSourcePluginListApi.as_view(), name="data_source_plugin.list"),
# 数据源插件默认配置
path(
"plugins/<str:id>/default-config/",
views.DataSourcePluginDefaultConfigApi.as_view(),
name="data_source_plugin.default_config",
),
# 数据源创建/获取列表
path("", views.DataSourceListCreateApi.as_view(), name="data_source.list_create"),
# 数据源更新/获取
Expand Down
128 changes: 103 additions & 25 deletions src/bk-user/bkuser/apis/web/data_source/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,80 @@
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 logging

import openpyxl
from django.conf import settings
from django.db import transaction
from django.utils.translation import gettext_lazy as _
from drf_yasg.utils import swagger_auto_schema
from openpyxl.utils.exceptions import InvalidFileException
from rest_framework import generics, status
from rest_framework.response import Response

from bkuser.apis.web.data_source.mixins import CurrentUserTenantDataSourceMixin
from bkuser.apis.web.data_source.serializers import (
DataSourceCreateInputSLZ,
DataSourceCreateOutputSLZ,
DataSourcePluginDefaultConfigOutputSLZ,
DataSourcePluginOutputSLZ,
DataSourceRetrieveOutputSLZ,
DataSourceSearchInputSLZ,
DataSourceSearchOutputSLZ,
DataSourceSwitchStatusOutputSLZ,
DataSourceTestConnectionOutputSLZ,
DataSourceUpdateInputSLZ,
LocalDataSourceImportInputSLZ,
LocalDataSourceImportOutputSLZ,
)
from bkuser.apis.web.mixins import CurrentUserTenantMixin
from bkuser.apps.data_source.constants import DataSourceStatus
from bkuser.apps.data_source.exporter import DataSourceUserExporter
from bkuser.apps.data_source.models import DataSource, DataSourcePlugin
from bkuser.apps.data_source.plugins.constants import DATA_SOURCE_PLUGIN_CONFIG_SCHEMA_MAP
from bkuser.apps.data_source.signals import post_create_data_source, post_update_data_source
from bkuser.apps.sync.constants import SyncTaskTrigger
from bkuser.apps.sync.data_models import DataSourceSyncOptions
from bkuser.apps.sync.manager import DataSourceSyncManager
from bkuser.biz.data_source_plugin import DefaultPluginConfigProvider
from bkuser.common.error_codes import error_codes
from bkuser.common.response import convert_workbook_to_response
from bkuser.common.views import ExcludePatchAPIViewMixin, ExcludePutAPIViewMixin

logger = logging.getLogger(__name__)


class DataSourcePluginListApi(generics.ListAPIView):
queryset = DataSourcePlugin.objects.all()
pagination_class = None
serializer_class = DataSourcePluginOutputSLZ

@swagger_auto_schema(
tags=["data_source"],
tags=["data_source_plugin"],
operation_description="数据源插件列表",
responses={status.HTTP_200_OK: DataSourcePluginOutputSLZ(many=True)},
)
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)


class DataSourcePluginDefaultConfigApi(generics.RetrieveAPIView):
@swagger_auto_schema(
tags=["data_source_plugin"],
operation_description="数据源插件默认配置",
responses={
status.HTTP_200_OK: DataSourcePluginDefaultConfigOutputSLZ(),
**DATA_SOURCE_PLUGIN_CONFIG_SCHEMA_MAP,
},
)
def get(self, request, *args, **kwargs):
config = DefaultPluginConfigProvider().get(kwargs["id"])
if not config:
raise error_codes.DATA_SOURCE_PLUGIN_NOT_DEFAULT_CONFIG

return Response(DataSourcePluginDefaultConfigOutputSLZ(instance={"config": config.model_dump()}).data)


class DataSourceListCreateApi(CurrentUserTenantMixin, generics.ListCreateAPIView):
pagination_class = None
serializer_class = DataSourceSearchOutputSLZ
Expand Down Expand Up @@ -108,13 +143,11 @@ def post(self, request, *args, **kwargs):
)


class DataSourceRetrieveUpdateApi(CurrentUserTenantMixin, ExcludePatchAPIViewMixin, generics.RetrieveUpdateAPIView):
class DataSourceRetrieveUpdateApi(
CurrentUserTenantDataSourceMixin, ExcludePatchAPIViewMixin, generics.RetrieveUpdateAPIView
):
pagination_class = None
serializer_class = DataSourceRetrieveOutputSLZ
lookup_url_kwarg = "id"

def get_queryset(self):
return DataSource.objects.filter(owner_tenant_id=self.get_current_tenant_id())

@swagger_auto_schema(
tags=["data_source"],
Expand Down Expand Up @@ -156,14 +189,10 @@ def put(self, request, *args, **kwargs):
return Response(status=status.HTTP_204_NO_CONTENT)


class DataSourceTestConnectionApi(CurrentUserTenantMixin, generics.RetrieveAPIView):
class DataSourceTestConnectionApi(CurrentUserTenantDataSourceMixin, generics.RetrieveAPIView):
"""数据源连通性测试"""

serializer_class = DataSourceTestConnectionOutputSLZ
lookup_url_kwarg = "id"

def get_queryset(self):
return DataSource.objects.filter(owner_tenant_id=self.get_current_tenant_id())

@swagger_auto_schema(
tags=["data_source"],
Expand Down Expand Up @@ -196,14 +225,10 @@ def get(self, request, *args, **kwargs):
return Response(DataSourceTestConnectionOutputSLZ(instance=mock_data).data)


class DataSourceSwitchStatusApi(CurrentUserTenantMixin, ExcludePutAPIViewMixin, generics.UpdateAPIView):
class DataSourceSwitchStatusApi(CurrentUserTenantDataSourceMixin, ExcludePutAPIViewMixin, generics.UpdateAPIView):
"""切换数据源状态(启/停)"""

serializer_class = DataSourceSwitchStatusOutputSLZ
lookup_url_kwarg = "id"

def get_queryset(self):
return DataSource.objects.filter(owner_tenant_id=self.get_current_tenant_id())

@swagger_auto_schema(
tags=["data_source"],
Expand All @@ -223,29 +248,82 @@ def patch(self, request, *args, **kwargs):
return Response(DataSourceSwitchStatusOutputSLZ(instance={"status": data_source.status.value}).data)


class DataSourceTemplateApi(generics.ListAPIView):
class DataSourceTemplateApi(CurrentUserTenantDataSourceMixin, generics.ListAPIView):
"""获取本地数据源数据导入模板"""

@swagger_auto_schema(
tags=["data_source"],
operation_description="下载数据源导入模板",
responses={status.HTTP_200_OK: "org_tmpl.xlsx"},
)
def get(self, request, *args, **kwargs):
"""数据源导出模板"""
# TODO (su) 实现代码逻辑
return Response()
# 获取数据源信息,用于后续填充模板中的动态字段
data_source = self.get_object()
if not data_source.is_local:
raise error_codes.DATA_SOURCE_OPERATION_UNSUPPORTED.f(_("仅本地数据源类型有提供导入模板"))

workbook = DataSourceUserExporter(data_source).get_template()
return convert_workbook_to_response(workbook, f"{settings.EXPORT_EXCEL_FILENAME_PREFIX}_org_tmpl.xlsx")


class DataSourceExportApi(generics.ListAPIView):
class DataSourceExportApi(CurrentUserTenantDataSourceMixin, generics.ListAPIView):
"""本地数据源用户导出"""

@swagger_auto_schema(
tags=["data_source"],
operation_description="下载本地数据源用户数据",
responses={status.HTTP_200_OK: "org_data.xlsx"},
)
def get(self, request, *args, **kwargs):
"""导出指定的本地数据源用户数据(Excel 格式)"""
# TODO (su) 实现代码逻辑,注意:仅本地数据源可以导出
return Response()
data_source = self.get_object()
if not data_source.is_local:
raise error_codes.DATA_SOURCE_OPERATION_UNSUPPORTED.f(_("仅能导出本地数据源数据"))

workbook = DataSourceUserExporter(data_source).export()
return convert_workbook_to_response(workbook, f"{settings.EXPORT_EXCEL_FILENAME_PREFIX}_org_data.xlsx")


class DataSourceImportApi(generics.CreateAPIView):
class DataSourceImportApi(CurrentUserTenantDataSourceMixin, generics.CreateAPIView):
"""从 Excel 导入数据源用户数据"""

@swagger_auto_schema(
tags=["data_source"],
operation_description="本地数据源用户数据导入",
request_body=LocalDataSourceImportInputSLZ(),
responses={status.HTTP_200_OK: LocalDataSourceImportOutputSLZ()},
)
def post(self, request, *args, **kwargs):
"""从 Excel 导入数据源用户数据"""
# TODO (su) 实现代码逻辑,注意:仅本地数据源可以导入
return Response()
slz = LocalDataSourceImportInputSLZ(data=request.data)
slz.is_valid(raise_exception=True)
data = slz.validated_data

data_source = self.get_object()
if not data_source.is_local:
raise error_codes.DATA_SOURCE_OPERATION_UNSUPPORTED.f(_("仅本地数据源支持导入功能"))

# Request file 转换成 openpyxl.workbook
try:
workbook = openpyxl.load_workbook(data["file"])
except InvalidFileException:
logger.exception("本地数据源导入失败")
raise error_codes.DATA_SOURCE_IMPORT_FAILED.f(_("文件格式异常"))

options = DataSourceSyncOptions(
operator=request.user.username,
overwrite=data["overwrite"],
async_run=False,
trigger=SyncTaskTrigger.MANUAL,
)

task = DataSourceSyncManager(data_source, options).execute(context={"workbook": workbook})
return Response(
LocalDataSourceImportOutputSLZ(
instance={"task_id": task.id, "status": task.status, "summary": task.summary}
).data
)


class DataSourceSyncApi(generics.CreateAPIView):
Expand Down
Loading

0 comments on commit ce60877

Please sign in to comment.