Skip to content

Commit

Permalink
feat: 订阅接口支持指定用户操作进程 (closed TencentBlueKing#2297)
Browse files Browse the repository at this point in the history
  • Loading branch information
Huayeaaa committed Sep 9, 2024
1 parent a5e8332 commit 0afc56a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 4 deletions.
17 changes: 17 additions & 0 deletions apps/backend/subscription/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ def validate(self, attrs):
raise ValidationError("目前机器参数必须要有 bk_host_id 或者 (ip/bk_host_innerip + bk_cloud_id)")


class OperateInfoSerializer(serializers.Serializer):
bk_host_id = serializers.IntegerField(required=False, label="主机ID")
operate_user = serializers.CharField(required=False, label="操作用户")
system_account = serializers.DictField(required=False, label="系统对应账户")

def validate(self, attrs):
if attrs.get("bk_host_id") and attrs.get("system_account"):
raise ValidationError(_("仅支持一种方式:实例 or 操作系统 指定操作用户"))
if attrs.get("system_account"):
for key in attrs["system_account"]:
if key not in constants.OS_TUPLE:
raise ValidationError(_(f"操作系统类型只能为{constants.OS_TUPLE}"))
return attrs


class CreateSubscriptionSerializer(GatewaySerializer):
class CreateStepSerializer(serializers.Serializer):
id = serializers.CharField(label="步骤标识符", validators=[])
Expand All @@ -83,6 +98,7 @@ class CreateStepSerializer(serializers.Serializer):
target_hosts = TargetHostSerializer(many=True, label="下发的目标机器列表", required=False, allow_empty=False)
run_immediately = serializers.BooleanField(required=False, default=False, label="是否立即执行")
is_main = serializers.BooleanField(required=False, default=False, label="是否为主配置")
operate_info = serializers.ListField(required=False, child=OperateInfoSerializer(), label="操作信息")

# 策略新参数
plugin_name = serializers.CharField(required=False, label="插件名")
Expand Down Expand Up @@ -147,6 +163,7 @@ class UpdateStepSerializer(serializers.Serializer):
scope = UpdateScopeSerializer()
steps = serializers.ListField(child=UpdateStepSerializer())
run_immediately = serializers.BooleanField(required=False, default=False)
operate_info = serializers.ListField(required=False, child=OperateInfoSerializer(), label="操作信息")

# 策略新参数
plugin_name = serializers.CharField(required=False)
Expand Down
4 changes: 3 additions & 1 deletion apps/backend/subscription/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def create_subscription(self, request):
params = self.validated_data
scope = params["scope"]
run_immediately = params["run_immediately"]
operate_info = params.get("operate_info")

category = params.get("category")
enable = params.get("enable") or False
Expand Down Expand Up @@ -121,7 +122,7 @@ def create_subscription(self, request):
subscription_id=subscription.id, scope=subscription.scope, actions={}
)
tasks.run_subscription_task_and_create_instance.delay(
subscription, subscription_task, language=get_language()
subscription, subscription_task, operate_info=operate_info, language=get_language()
)
result["task_id"] = subscription_task.id

Expand Down Expand Up @@ -212,6 +213,7 @@ def update_subscription(self, request):
# 策略部署新增
subscription.plugin_name = params.get("plugin_name")
subscription.bk_biz_scope = params.get("bk_biz_scope")
subscription.operate_info = params.get("operate_info")
subscription.save()

step_ids: Set[str] = set()
Expand Down
4 changes: 2 additions & 2 deletions apps/core/gray/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def get_host_ap_gse_version(self, bk_biz_id: typing.Any, ap_id: int, is_install_
return gse_version

def inject_meta_to_instances(
self, instances: typing.Dict[str, typing.Dict[str, typing.Union[typing.Dict, typing.Any]]]
self, instances: typing.Dict[str, typing.Dict[str, typing.Union[typing.Dict, typing.Any]]],
):
"""
在 instances 中注入 Meta 信息
:param instances:
:param instances:实例信息
:return:
"""
bk_host_ids: typing.Set[int] = {
Expand Down
4 changes: 3 additions & 1 deletion apps/node_man/handlers/plugin_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def history(query_params: Dict):
return packages

@staticmethod
def operate(job_type: str, plugin_name: str, scope: Dict, steps: List[Dict]):
def operate(job_type: str, plugin_name: str, scope: Dict, steps: List[Dict], operate_info: List[Dict] = None):
bk_biz_scope = list(set([node["bk_biz_id"] for node in scope["nodes"]]))

CmdbHandler().check_biz_permission(bk_biz_scope, IamActionType.plugin_operate)
Expand All @@ -166,6 +166,8 @@ def operate(job_type: str, plugin_name: str, scope: Dict, steps: List[Dict]):
"scope": scope,
"bk_biz_scope": bk_biz_scope,
}
if operate_info:
base_create_kwargs["operate_info"] = operate_info

if job_type == constants.JobType.MAIN_INSTALL_PLUGIN:
create_data = {**base_create_kwargs, "steps": steps}
Expand Down
1 change: 1 addition & 0 deletions apps/node_man/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,7 @@ class CategoryType(object):
category = models.CharField(_("订阅类别"), max_length=32, null=True, blank=True, db_index=True)
plugin_name = models.CharField(_("插件名称"), max_length=64, null=True, blank=True, db_index=True)
bk_biz_scope = JSONField(_("业务范围"), default=list)
operate_info = JSONField(_("操作信息"), default=list)

pid = models.BigIntegerField(_("父订阅ID"), default=ROOT, db_index=True)

Expand Down
6 changes: 6 additions & 0 deletions apps/node_man/serializers/plugin_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,19 @@ def validate(self, data):
return data


class OperateInfoSerializer(serializers.Serializer):
bk_host_id = serializers.IntegerField(label=_("主机ID"), required=False)
operate_user = serializers.CharField(label=_("操作用户"), required=False)


class PluginOperateSerializer(serializers.Serializer):
job_type = serializers.ChoiceField(label=_("任务类型"), choices=list(constants.PLUGIN_JOB_TUPLE))
plugin_name = serializers.CharField(label=_("插件名称"))
# 一次性任务范围
scope = base.ScopeSerializer()
# 参数配置
steps = serializers.ListField(child=base.StepSerializer(), required=False, default=[])
operate_info = serializers.ListField(label=_("操作信息"), child=OperateInfoSerializer(), required=False)

def validate(self, data):
if models.GsePluginDesc.objects.filter(name=data["plugin_name"]).first() is None:
Expand Down
1 change: 1 addition & 0 deletions apps/node_man/views/plugin_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ def operate(self, request):
plugin_name=params["plugin_name"],
scope=params["scope"],
steps=params.get("steps"),
operate_info=params.get("operate_info"),
)
)

Expand Down

0 comments on commit 0afc56a

Please sign in to comment.