diff --git a/config/default.py b/config/default.py index e8dbb5a5c..df6f1aab0 100644 --- a/config/default.py +++ b/config/default.py @@ -890,7 +890,7 @@ def check_engine_admin_permission(request, *args, **kwargs): ENABLE_TEMPLATE_MARKET = env.ENABLE_TEMPLATE_MARKET # 流程商店 API 地址 TEMPLATE_MARKET_API_URL = env.TEMPLATE_MARKET_API_URL -# SRE商店路由 -SRE_STORE_API_URL = env.SRE_STORE_API_URL -# SRE商店帮助文档路由 -SRE_HELP_DOCUMENT_URL = env.SRE_HELP_DOCUMENT_URL +# 模板市场路由 +TEMPLATE_MARKET_HOST = env.TEMPLATE_MARKET_HOST +# 模板市场文档路由 +TEMPLATE_MARKET_DOC_URL = env.TEMPLATE_MARKET_DOC_URL diff --git a/env.py b/env.py index 18ffc706f..b0bb44774 100644 --- a/env.py +++ b/env.py @@ -158,7 +158,7 @@ ENABLE_TEMPLATE_MARKET = False if os.getenv("ENABLE_TEMPLATE_MARKET") is None else True # 流程商店 API 地址 TEMPLATE_MARKET_API_URL = os.getenv("TEMPLATE_MARKET_API_URL", "") -# SRE商店路由 -SRE_STORE_API_URL = os.getenv("SRE_STORE_API_URL", "") -# SRE商店帮助文档路由 -SRE_HELP_DOCUMENT_URL = os.getenv("SRE_HELP_DOCUMENT_URL", "") +# 模板市场路由 +TEMPLATE_MARKET_HOST = os.getenv("TEMPLATE_MARKET_HOST", "") +# 模板市场文档路由 +TEMPLATE_MARKET_DOC_URL = os.getenv("TEMPLATE_MARKET_DOC_URL", "") diff --git a/gcloud/contrib/template_market/clients.py b/gcloud/contrib/template_market/clients.py index 2a1fe0f88..68b1d3442 100644 --- a/gcloud/contrib/template_market/clients.py +++ b/gcloud/contrib/template_market/clients.py @@ -10,7 +10,7 @@ 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 json import requests from gcloud.conf import settings @@ -19,41 +19,47 @@ class MarketAPIClient: def __init__(self): self.base_url = settings.TEMPLATE_MARKET_API_URL + self.headers = { + "X-Bkapi-Authorization": json.dumps( + {"bk_app_code": settings.APP_CODE, "bk_app_secret": settings.SECRET_KEY} + ), + "Content-Type": "application/json", + } def _get_url(self, endpoint): return f"{self.base_url}{endpoint}" def get_service_category(self): url = self._get_url("/category/get_service_category/") - response = requests.get(url) + response = requests.get(url, headers=self.headers) return response.json() def get_scene_label(self): url = self._get_url("/sre_property/scene_label/") - response = requests.get(url) + response = requests.get(url, headers=self.headers) return response.json() - def get_risk_level(self, request): + def get_risk_level(self): url = self._get_url("/sre_scene/risk_level/") - response = requests.get(url) + response = requests.get(url, headers=self.headers) return response.json() def get_template_scene_detail(self, market_record_id): url = self._get_url(f"/sre_scene/flow_template_scene/{market_record_id}/") - response = requests.get(url) + response = requests.get(url, headers=self.headers) return response.json() def get_template_scene_list(self): url = self._get_url("/sre_scene/flow_template_scene/?is_all=true") - response = requests.get(url) + response = requests.get(url, headers=self.headers) return response.json() def create_template_scene(self, data): url = self._get_url("/sre_scene/flow_template_scene/") - response = requests.post(url, json=data) + response = requests.post(url, json=data, headers=self.headers) return response.json() def patch_template_scene(self, data, market_record_id): url = self._get_url(f"/sre_scene/flow_template_scene/{market_record_id}/") - response = requests.patch(url, json=data) + response = requests.patch(url, json=data, headers=self.headers) return response.json() diff --git a/gcloud/contrib/template_market/migrations/0001_initial.py b/gcloud/contrib/template_market/migrations/0001_initial.py index edf2dcb1c..92421809e 100644 --- a/gcloud/contrib/template_market/migrations/0001_initial.py +++ b/gcloud/contrib/template_market/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.15 on 2024-12-13 10:48 +# Generated by Django 3.2.15 on 2024-12-16 11:45 from django.db import migrations, models @@ -17,6 +17,8 @@ class Migration(migrations.Migration): ("project_id", models.IntegerField(default=-1, help_text="项目 ID", verbose_name="项目 ID")), ("template_id", models.IntegerField(db_index=True, help_text="模板 ID", verbose_name="模板 ID")), ("creator", models.CharField(default="", max_length=32, verbose_name="创建者")), + ("create_at", models.DateTimeField(auto_now_add=True, verbose_name="创建时间")), + ("update_at", models.DateTimeField(auto_now=True, verbose_name="更新时间")), ("extra_info", models.JSONField(blank=True, null=True, verbose_name="额外信息")), ], options={ diff --git a/gcloud/contrib/template_market/models.py b/gcloud/contrib/template_market/models.py index f70c58439..229616f31 100644 --- a/gcloud/contrib/template_market/models.py +++ b/gcloud/contrib/template_market/models.py @@ -84,6 +84,8 @@ class TemplateSharedRecord(models.Model): project_id = models.IntegerField(_("项目 ID"), default=-1, help_text="项目 ID") template_id = models.IntegerField(_("模板 ID"), help_text="模板 ID", db_index=True) creator = models.CharField(_("创建者"), max_length=32, default="") + create_at = models.DateTimeField(verbose_name=_("创建时间"), auto_now_add=True) + update_at = models.DateTimeField(verbose_name=_("更新时间"), auto_now=True) extra_info = models.JSONField(_("额外信息"), blank=True, null=True) objects = TemplateSharedManager() diff --git a/gcloud/contrib/template_market/viewsets.py b/gcloud/contrib/template_market/viewsets.py index 4c85b5b86..da6463ecf 100644 --- a/gcloud/contrib/template_market/viewsets.py +++ b/gcloud/contrib/template_market/viewsets.py @@ -67,59 +67,47 @@ def _build_template_data(self, serializer, **kwargs): data["id"] = market_record_id return data - @action(detail=False, methods=["get"]) - def get_service_category(self, request, *args, **kwargs): - response_data = self.market_client.get_service_category() - if not response_data["result"]: - logging.warning("Failed to obtain the market service category") + def _handle_response(self, response_data, error_message): + if not response_data.get("result"): + logging.exception(error_message) return Response( { "result": False, - "message": "Failed to obtain the market service category", + "message": error_message, "code": err_code.OPERATION_FAIL.code, } ) + return None + + @action(detail=False, methods=["get"]) + def get_service_category(self, request, *args, **kwargs): + response_data = self.market_client.get_service_category() + error_response = self._handle_response(response_data, "Failed to obtain scene category") + if error_response: + return error_response return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code}) @action(detail=False, methods=["get"]) def get_scene_label(self, request, *args, **kwargs): response_data = self.market_client.get_scene_label() - if not response_data["result"]: - logging.exception("Failed to obtain scene tag list") - return Response( - { - "result": False, - "message": "Failed to obtain scene tag list", - "code": err_code.OPERATION_FAIL.code, - } - ) + error_response = self._handle_response(response_data, "Failed to obtain scene tag list") + if error_response: + return error_response return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code}) @action(detail=False, methods=["get"]) def get_risk_level(self, request, *args, **kwargs): - response_data = self.market_client.get_risk_level(request) - if not response_data["result"]: - logging.exception("Failed to obtain the market risk level list") - return Response( - { - "result": False, - "message": "Failed to obtain the market risk level list", - "code": err_code.OPERATION_FAIL.code, - } - ) + response_data = self.market_client.get_risk_level() + error_response = self._handle_response(response_data, "Failed to obtain the market risk level list") + if error_response: + return error_response return Response({"result": True, "data": response_data["data"], "code": err_code.SUCCESS.code}) def list(self, request, *args, **kwargs): response_data = self.market_client.get_template_scene_list() - if not response_data["result"]: - logging.exception("Failed to obtain the market template list") - return Response( - { - "result": False, - "message": "Failed to obtain the market template list", - "code": err_code.OPERATION_FAIL.code, - } - ) + error_response = self._handle_response(response_data, "Failed to obtain the market template list") + if error_response: + return error_response return Response({"result": True, "data": response_data, "code": err_code.SUCCESS.code}) @swagger_auto_schema(request_body=TemplateSharedRecordSerializer) @@ -129,14 +117,10 @@ def create(self, request, *args, **kwargs): data = self._build_template_data(serializer) response_data = self.market_client.create_template_scene(data) - if not response_data.get("result"): - return Response( - { - "result": False, - "message": "Failed to create market template record", - "code": err_code.OPERATION_FAIL.code, - } - ) + error_response = self._handle_response(response_data, "Failed to create market template record") + if error_response: + return error_response + TemplateSharedRecord.objects.update_shared_record( project_id=int(serializer.validated_data["project_code"]), new_template_ids=serializer.validated_data["template_ids"], @@ -155,16 +139,13 @@ def partial_update(self, request, *args, **kwargs): existing_market_template_ids = set( [template["id"] for template in json.loads(existing_records["data"]["templates"])] ) + data = self._build_template_data(serializer, market_record_id=market_record_id) response_data = self.market_client.patch_template_scene(data, market_record_id) - if not response_data.get("result"): - return Response( - { - "result": False, - "message": "Failed to update market template record", - "code": err_code.OPERATION_FAIL.code, - } - ) + error_response = self._handle_response(response_data, "Failed to update market template record") + if error_response: + return error_response + TemplateSharedRecord.objects.update_shared_record( project_id=int(serializer.validated_data["project_code"]), new_template_ids=serializer.validated_data["template_ids"], diff --git a/gcloud/core/context_processors.py b/gcloud/core/context_processors.py index 8030c1194..8d23cefcf 100644 --- a/gcloud/core/context_processors.py +++ b/gcloud/core/context_processors.py @@ -97,8 +97,8 @@ def mysetting(request): "REMOTE_ANALYSIS_URL": settings.REMOTE_ANALYSIS_URL, "REMOTE_API_URL": settings.REMOTE_API_URL, "ENABLE_TEMPLATE_MARKET": settings.ENABLE_TEMPLATE_MARKET, - "SRE_STORE_API_URL": settings.SRE_STORE_API_URL, - "SRE_HELP_DOCUMENT_URL": settings.SRE_HELP_DOCUMENT_URL, + "TEMPLATE_MARKET_HOST": settings.TEMPLATE_MARKET_HOST, + "TEMPLATE_MARKET_DOC_URL": settings.TEMPLATE_MARKET_DOC_URL, "USERNAME": request.user.username, # 'NICK': request.session.get('nick', ''), # 用户昵称 "NICK": request.user.username, # 用户昵称