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

feat: add API to retrieve preset env variable #1752

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions apiserver/paasng/paasng/platform/engine/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ def validate_order_by(self, field: str) -> str:
return field


class PresetEnvVarSLZ(serializers.Serializer):
key = serializers.CharField()
value = serializers.CharField()
environment_name = serializers.CharField()
description = serializers.CharField(default="")


class CreateOfflineOperationSLZ(serializers.Serializer):
pass

Expand Down
9 changes: 9 additions & 0 deletions apiserver/paasng/paasng/platform/engine/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@
),
name="api.config_vars.template",
),
re_path(
make_app_pattern(r"/config_vars/preset/$", include_envs=False),
views.PresetConfigVarViewSet.as_view(
{
"get": "list",
}
),
jiayuan929 marked this conversation as resolved.
Show resolved Hide resolved
name="api.preset_config_vars",
),
# deploy
re_path(
make_app_pattern(r"/deployments/%s/result/$" % PVAR_UUID, include_envs=False),
Expand Down
2 changes: 2 additions & 0 deletions apiserver/paasng/paasng/platform/engine/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from .build import BuildProcessViewSet, ImageArtifactViewSet
from .configvar import ConfigVarBuiltinViewSet, ConfigVarImportExportViewSet, ConfigVarViewSet
from .configvar_preset import PresetConfigVarViewSet
from .deploy import DeploymentViewSet, DeployPhaseViewSet
from .misc import OfflineViewset, OperationsViewset, ProcessResourceMetricsViewset
from .release import ReleasedInfoViewSet, ReleasesViewset
Expand All @@ -34,4 +35,5 @@
"ProcessResourceMetricsViewset",
"ReleasedInfoViewSet",
"ReleasesViewset",
"PresetConfigVarViewSet",
]
54 changes: 54 additions & 0 deletions apiserver/paasng/paasng/platform/engine/views/configvar_preset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
# TencentBlueKing is pleased to support the open source community by making
# 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available.
# Copyright (C) 2017 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.
#
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.
from drf_yasg.utils import swagger_auto_schema
from rest_framework import mixins, viewsets
from rest_framework.permissions import IsAuthenticated

from paasng.infras.accounts.permissions.application import application_perm_class
from paasng.infras.iam.permissions.resources.application import AppAction
from paasng.platform.applications.mixins import ApplicationCodeInPathMixin
from paasng.platform.engine.models.preset_envvars import PresetEnvVariable
from paasng.platform.engine.serializers import ListConfigVarsSLZ, PresetEnvVarSLZ


class PresetConfigVarViewSet(mixins.ListModelMixin, viewsets.GenericViewSet, ApplicationCodeInPathMixin):
pagination_class = None
serializer_class = PresetEnvVarSLZ
permission_classes = [IsAuthenticated, application_perm_class(AppAction.BASIC_DEVELOP)]
queryset = PresetEnvVariable.objects.all()

def filter_queryset(self, queryset):
queryset = super().filter_queryset(queryset)
slz = ListConfigVarsSLZ(data=self.request.query_params)
slz.is_valid(raise_exception=True)
query_params = slz.validated_data

if environment_name := query_params.get("environment_name"):
queryset = queryset.filter(environment_name=environment_name)

queryset = queryset.order_by(query_params["order_by"])
return queryset
jiayuan929 marked this conversation as resolved.
Show resolved Hide resolved

@swagger_auto_schema(
query_serializer=ListConfigVarsSLZ(),
tags=["预设环境变量"],
responses={200: PresetEnvVarSLZ(many=True)},
)
def list(self, request, *args, **kwargs):
module = self.get_module_via_path()
self.queryset = self.queryset.filter(module=module)
return super().list(request, *args, **kwargs)
jiayuan929 marked this conversation as resolved.
Show resolved Hide resolved
40 changes: 40 additions & 0 deletions apiserver/paasng/tests/api/test_configvar_preset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
# TencentBlueKing is pleased to support the open source community by making
# 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available.
# Copyright (C) 2017 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.
#
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.

import pytest
from django_dynamic_fixture import G

from paasng.platform.engine.constants import ConfigVarEnvName
from paasng.platform.engine.models.preset_envvars import PresetEnvVariable

pytestmark = pytest.mark.django_db


@pytest.mark.parametrize(
"environment_name",
[ConfigVarEnvName.GLOBAL.value, ConfigVarEnvName.STAG.value, ConfigVarEnvName.PROD.value],
)
def test_get_preset_config_var(api_client, bk_module, environment_name):
G(PresetEnvVariable, module=bk_module, environment_name=ConfigVarEnvName.GLOBAL, key="GLOBAL", value="1")
G(PresetEnvVariable, module=bk_module, environment_name=ConfigVarEnvName.STAG, key="STAG", value="1")
G(PresetEnvVariable, module=bk_module, environment_name=ConfigVarEnvName.PROD, key="PROD", value="1")

params = {"environment_name": environment_name}
# url 定义的时候使用了 make_app_pattern,使用 reverse("api.preset_config_vars") 来获取请求路径会导致缺省 module 模块相关的路径
path = f"/api/bkapps/applications/{bk_module.application.code}/modules/{bk_module.name}/config_vars/preset/"
response = api_client.get(path, params)
assert response.data[0]["environment_name"] == environment_name