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

minor: 导入导出支持草稿功能 #7244

Open
wants to merge 1 commit into
base: draft
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion gcloud/core/apis/drf/viewsets/draft_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def update_template_draft(self, manager, template, request):

serializer.validated_data["labels"] = serializer.validated_data.pop("template_labels", [])
result = manager.update_draft_pipeline(
template.draft_template, request.user.username, serializer.validated_data
template.draft_template, request.user.username, serializer.validated_data["pipeline_tree"]
)
return result

Expand Down
6 changes: 3 additions & 3 deletions gcloud/iam_auth/view_interceptors/base_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""
from gcloud.iam_auth import IAMMeta
from gcloud.iam_auth.intercept import ViewInterceptor
from gcloud.iam_auth.utils import iam_resource_auth_or_raise, iam_multi_resource_auth_or_raise
from gcloud.iam_auth.utils import iam_multi_resource_auth_or_raise, iam_resource_auth_or_raise


class YamlImportInterceptor(ViewInterceptor):
Expand All @@ -27,13 +27,13 @@ def process(self, request, *args, **kwargs):
project_resource_id = data["project_id"]
project_get_resource_func = "resources_for_project"
project_action = IAMMeta.FLOW_CREATE_ACTION
template_action = IAMMeta.FLOW_EDIT_ACTION
template_action = IAMMeta.FLOW_PUBLISH_DRAFT_ACTION
template_get_resource_func = "resources_list_for_flows"
else:
project_resource_id = None
project_get_resource_func = None
project_action = IAMMeta.COMMON_FLOW_CREATE_ACTION
template_action = IAMMeta.COMMON_FLOW_EDIT_ACTION
template_action = IAMMeta.COMMON_FLOW_PUBLISH_DRAFT_ACTION
template_get_resource_func = "resources_list_for_common_flows"

iam_resource_auth_or_raise(username, project_action, project_resource_id, project_get_resource_func)
Expand Down
27 changes: 17 additions & 10 deletions gcloud/template_base/domains/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific lan
"""
import logging

from django.apps import apps
from django.db import transaction
from django.utils.translation import ugettext_lazy as _

from .template_manager import TemplateManager
from ..utils import replace_biz_id_value
from ...common_template.models import CommonTemplate
from django.utils.translation import ugettext_lazy as _
import logging
from ..utils import replace_biz_id_value
from .template_manager import TemplateManager

logger = logging.getLogger("root")

Expand Down Expand Up @@ -81,9 +82,9 @@ def import_template(self, operator: str, template_data: list, bk_biz_id: int = N
template_id = override_template_id or refer_template_config["template_id"]
try:
if (
refer_template_config
and self.template_model_cls is apps.get_model("tasktmpl3", "TaskTemplate")
and refer_template_config["template_type"] == "common"
refer_template_config
and self.template_model_cls is apps.get_model("tasktmpl3", "TaskTemplate")
and refer_template_config["template_type"] == "common"
):
template = CommonTemplate.objects.get(id=template_id)
else:
Expand All @@ -105,13 +106,19 @@ def import_template(self, operator: str, template_data: list, bk_biz_id: int = N
continue

if override_template_id:
# 如果选择了替换已有流程,则选择更新模板和草稿
operate_result = manager.update(
template=template,
editor=operator,
name=name,
pipeline_tree=pipeline_tree,
description=description,
)
# 如果需要覆盖的流程已经有草稿了,则更新草稿,没有草稿则不创建
if template.draft_template_id:
# 更新草稿的时候不再替换 id
manager.update_draft_pipeline(template.draft_template, operator, pipeline_tree)

if operate_result["result"]:
pipeline_id_map[td["id"]] = operate_result["data"].id
pipeline_version_map[td["id"]] = operate_result["data"].version
Expand All @@ -120,8 +127,8 @@ def import_template(self, operator: str, template_data: list, bk_biz_id: int = N
for key, constant in pipeline_tree["constants"].items():
source_info_map.setdefault(td["id"], {}).update({key: constant.get("source_info", {})})
if (
self.template_model_cls is apps.get_model("tasktmpl3", "TaskTemplate")
and refer_template_config["template_type"] == "common"
self.template_model_cls is apps.get_model("tasktmpl3", "TaskTemplate")
and refer_template_config["template_type"] == "common"
):
common_child_templates[td["id"]] = {"constants": pipeline_tree["constants"]}
pipeline_id_map[td["id"]] = refer_template_config["template_id"]
Expand All @@ -144,7 +151,7 @@ def import_template(self, operator: str, template_data: list, bk_biz_id: int = N

@staticmethod
def _replace_subprocess_template_info(
pipeline_tree: dict, pipeline_id_map: dict, source_map_info: dict, pipeline_version_map: dict
pipeline_tree: dict, pipeline_id_map: dict, source_map_info: dict, pipeline_version_map: dict
) -> dict:
"""
将模板数据中临时的模板信息 替换成数据库中模型的对应信息
Expand Down
66 changes: 32 additions & 34 deletions gcloud/template_base/domains/template_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def __init__(self, template_model_cls):
self.template_model_cls = template_model_cls

def create_pipeline(
self,
name: str,
creator: str,
pipeline_tree: dict,
description: str = "",
self,
name: str,
creator: str,
pipeline_tree: dict,
description: str = "",
) -> dict:
"""
创建 pipeline 层模板
Expand Down Expand Up @@ -68,7 +68,8 @@ def create_pipeline(
"result": False,
"data": None,
"message": message,
"verbose_message": _(f"保存流程失败: 流程树合法性校验失败, 请检查流程. 失败原因: {traceback.format_exc()} | create_pipeline"),
"verbose_message": _(
f"保存流程失败: 流程树合法性校验失败, 请检查流程. 失败原因: {traceback.format_exc()} | create_pipeline"),
}

create_template_kwargs = {
Expand All @@ -80,7 +81,8 @@ def create_pipeline(
try:
pipeline_template = self.template_model_cls.objects.create_pipeline_template(**create_template_kwargs)
except Exception as e:
message = _(f"保存流程失败: 创建Pipeline流程失败, 请检查流程. 创建参数[{create_template_kwargs}], 失败原因: [{e}] | create_pipeline")
message = _(
f"保存流程失败: 创建Pipeline流程失败, 请检查流程. 创建参数[{create_template_kwargs}], 失败原因: [{e}] | create_pipeline")
logger.error(message)
return {
"result": False,
Expand All @@ -95,12 +97,12 @@ def create_pipeline(
return {"result": True, "data": pipeline_template, "message": "success", "verbose_message": "success"}

def create(
self,
name: str,
creator: str,
pipeline_tree: dict,
template_kwargs: dict,
description: str = "",
self,
name: str,
creator: str,
pipeline_tree: dict,
template_kwargs: dict,
description: str = "",
) -> dict:
"""
创建 template 层模板
Expand Down Expand Up @@ -128,7 +130,8 @@ def create(
try:
template = self.template_model_cls.objects.create(**template_kwargs)
except Exception as e:
message = _(f"保存流程失败: 创建模板失败, 请检查流程. 创建参数[{template_kwargs}], 失败原因: [{e}] | create")
message = _(
f"保存流程失败: 创建模板失败, 请检查流程. 创建参数[{template_kwargs}], 失败原因: [{e}] | create")
logger.error(message)
return {
"result": False,
Expand All @@ -142,12 +145,12 @@ def create(
return {"result": True, "data": template, "message": "success", "verbose_message": "success"}

def update_pipeline(
self,
pipeline_template: PipelineTemplate,
editor: str,
name: str = "",
pipeline_tree: str = None,
description: str = "",
self,
pipeline_template: PipelineTemplate,
editor: str,
name: str = "",
pipeline_tree: str = None,
description: str = "",
) -> dict:
"""
更新 pipeline 层模板
Expand Down Expand Up @@ -197,7 +200,8 @@ def update_pipeline(
try:
pipeline_template.update_template(**update_kwargs)
except Exception as e:
message = _(f"更新流程失败: 更新Pipeline失败, 请检查流程. 更新参数: [{update_kwargs}], 失败原因: [{e}] | update_pipeline")
message = _(
f"更新流程失败: 更新Pipeline失败, 请检查流程. 更新参数: [{update_kwargs}], 失败原因: [{e}] | update_pipeline")
logger.error(message)
return {
"result": False,
Expand Down Expand Up @@ -237,14 +241,8 @@ def create_draft(self, template, editor):
template.save(update_fields=["draft_template_id"])

@transaction.atomic()
def update_draft_pipeline(self, draft_template, editor, data):

pipeline_tree = data.pop("pipeline_tree")
def update_draft_pipeline(self, draft_template, editor, pipeline_tree):
# 草稿更新不会触发流程合法性校验
standardize_pipeline_node_name(pipeline_tree)
replace_template_id(self.template_model_cls, pipeline_tree)
pipeline_web_tree = PipelineWebTreeCleaner(pipeline_tree)
pipeline_web_tree.clean()
draft_template.editor = editor
draft_template.save()

Expand Down Expand Up @@ -297,12 +295,12 @@ def discard_draft(self, template, editor):
return {"result": True, "data": None, "message": "success", "verbose_message": "success"}

def update(
self,
template: object,
editor: str,
name: str = "",
pipeline_tree: str = None,
description: str = "",
self,
template: object,
editor: str,
name: str = "",
pipeline_tree: str = None,
description: str = "",
) -> dict:
"""
更新 template 层模板
Expand Down
10 changes: 9 additions & 1 deletion gcloud/template_base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ def _perform_import(self, template_data, check_info, override, defaults_getter,
obj, created = self.update_or_create(id=tid, defaults=defaults)
if created:
new_objects_template_ids.add(template_dict["pipeline_template_id"])
else:
# 如果不是新建,则需要更新模板信息
from gcloud.template_base.domains.template_manager import TemplateManager
template_instance = self.get(id=tid)
if template_instance.draft_template_id:
manger = TemplateManager(template_model_cls=None)
manger.update_draft_pipeline(draft_template=template_instance.draft_template,
editor=operator,
pipeline_tree=template_instance.pipeline_template.data)
else:
new_objects.append(self.model(**defaults))
new_objects_template_ids.add(template_dict["pipeline_template_id"])
Expand Down Expand Up @@ -381,7 +390,6 @@ def draft_pipeline_tree(self):
draft_template = DraftTemplate.objects.get(id=self.draft_template_id)
draft_snapshot_id = draft_template.snapshot_id
tree = Snapshot.objects.get(id=draft_snapshot_id).data
replace_template_id(self.__class__, tree, reverse=True)
return tree

@property
Expand Down
Loading