From ed29acbb84a97217af1bd90331a5e43e720df003 Mon Sep 17 00:00:00 2001 From: rabbitali Date: Fri, 23 Aug 2024 17:19:03 +0800 Subject: [PATCH] update download host template api --- 0001-updated-download-host-template-api.patch | 161 ++++++++++++++++++ aops-zeus.spec | 8 +- 2 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 0001-updated-download-host-template-api.patch diff --git a/0001-updated-download-host-template-api.patch b/0001-updated-download-host-template-api.patch new file mode 100644 index 0000000..e9be569 --- /dev/null +++ b/0001-updated-download-host-template-api.patch @@ -0,0 +1,161 @@ +From 429e3440238fde2e6051443b5b819e9bcb25536f Mon Sep 17 00:00:00 2001 +From: rabbitali +Date: Thu, 22 Aug 2024 16:49:34 +0800 +Subject: [PATCH 1/1] updated download host template api to support english + +--- + .../host_information_service/app/constant.py | 58 ++++++++++++++++++- + .../app/serialize/host.py | 9 +++ + .../app/views/host.py | 15 +++-- + 3 files changed, 77 insertions(+), 5 deletions(-) + +diff --git a/host-information-service/zeus/host_information_service/app/constant.py b/host-information-service/zeus/host_information_service/app/constant.py +index e9814d3..20dfec7 100644 +--- a/host-information-service/zeus/host_information_service/app/constant.py ++++ b/host-information-service/zeus/host_information_service/app/constant.py +@@ -13,7 +13,7 @@ + + + # host template file content +-HOST_TEMPLATE_FILE_CONTENT = """host_ip,ssh_port,ssh_user,password,ssh_pkey,host_name,host_group_name,management ++HOST_TEMPLATE_FILE_CONTENT_FOR_ZH = """host_ip,ssh_port,ssh_user,password,ssh_pkey,host_name,host_group_name,management + 127.0.0.1,22,root,password,private key,test_host,test_host_group,FALSE + 127.0.0.1,23,root,password,private key,test_host,test_host_group,FALSE + ,,,,,,, +@@ -24,6 +24,62 @@ HOST_TEMPLATE_FILE_CONTENT = """host_ip,ssh_port,ssh_user,password,ssh_pkey,host + "4. 上传本文件前,请删除此部分提示内容",,,,,,, + """ + ++# host template file content ++HOST_TEMPLATE_FILE_CONTENT_FOR_EN = """host_ip,ssh_port,ssh_user,password,ssh_pkey,host_name,host_group_name,management ++127.0.0.1,22,root,password,private key,test_host,test_host_group,FALSE ++127.0.0.1,23,root,password,private key,test_host,test_host_group,FALSE ++,,,,,,, ++"Note:",,,,,,, ++"1. Except for the login password and SSH login key, other information should provide valid values",,,,,,, ++"2. Choose to enter password or SSH key, the SSH key will be considered preferly when both are provided.",,,,,,, ++"3. The combination of the host IP and port must be unique; no duplicate entries are allowed.",,,,,,, ++"4. Please delete the note before uploading this file.",,,,,,, ++""" ++ ++ ++class HostTemplate: ++ """A template class for managing host template file contents in different languages. ++ ++ Attributes: ++ _content (dict): A dictionary containing the template file contents for ++ different languages. The keys are language codes ("zh" for Chinese, ++ "en" for English), and the values are the corresponding template file contents. ++ ++ Example: ++ content = HostTemplate.get_file_content("zh") ++ print(content) ++ ++ supported_languages = HostTemplate.support_lang ++ print(supported_languages) ++ """ ++ ++ _content = {"zh": HOST_TEMPLATE_FILE_CONTENT_FOR_ZH, "en": HOST_TEMPLATE_FILE_CONTENT_FOR_EN} ++ ++ @classmethod ++ def get_file_content(cls, lang: str = None) -> str: ++ """Gets the template file content for the specified language. ++ ++ Args: ++ lang (str, optional): The language code for the desired template file content. ++ If not provided or if the specified language is not supported, defaults to English ("en"). ++ ++ Returns: ++ str: The template file content for the specified language, or the English content if ++ the specified language is not supported. ++ """ ++ if lang not in cls._content: ++ lang = "en" ++ return cls._content.get(lang) ++ ++ @staticmethod ++ def support_lang(): ++ """Gets the list of supported languages. ++ ++ Returns: ++ list: A list of language codes representing the supported languages (e.g., ["zh", "en"]). ++ """ ++ return list(HostTemplate._content.keys()) ++ + + class HostStatus: + ONLINE = 0 +diff --git a/host-information-service/zeus/host_information_service/app/serialize/host.py b/host-information-service/zeus/host_information_service/app/serialize/host.py +index c65bc86..e160163 100644 +--- a/host-information-service/zeus/host_information_service/app/serialize/host.py ++++ b/host-information-service/zeus/host_information_service/app/serialize/host.py +@@ -14,6 +14,7 @@ from marshmallow import Schema, ValidationError, fields, validate, validates_sch + from vulcanus.restful.serialize.validate import ValidateRules + + from zeus.host_information_service.database import Host ++from zeus.host_information_service.app.constant import HostTemplate + + + class _AddHost(Schema): +@@ -212,3 +213,11 @@ class HostInfoSchema(Schema): + + basic = fields.Boolean(required=False, missing=True, validate=validate.OneOf([True, False])) + refresh = fields.Boolean(required=False, missing=False, validate=validate.OneOf([True, False])) ++ ++ ++class TemplateLangSchema(Schema): ++ """ ++ File template language ++ """ ++ ++ lang = fields.String(required=False, missing='en', validate=validate.OneOf(HostTemplate.support_lang())) +diff --git a/host-information-service/zeus/host_information_service/app/views/host.py b/host-information-service/zeus/host_information_service/app/views/host.py +index 54d5332..334e01f 100644 +--- a/host-information-service/zeus/host_information_service/app/views/host.py ++++ b/host-information-service/zeus/host_information_service/app/views/host.py +@@ -28,7 +28,7 @@ from vulcanus.restful.response import BaseResponse + from vulcanus.restful.serialize.validate import validate + + from zeus.host_information_service.app import cache +-from zeus.host_information_service.app.constant import HOST_TEMPLATE_FILE_CONTENT, HostStatus ++from zeus.host_information_service.app.constant import HostTemplate, HostStatus + from zeus.host_information_service.app.core.ssh import SSH, generate_key + from zeus.host_information_service.app.proxy.host import HostProxy + from zeus.host_information_service.app.serialize.host import ( +@@ -39,6 +39,7 @@ from zeus.host_information_service.app.serialize.host import ( + HostFilterSchema, + HostInfoSchema, + HostsInfo_ResponseSchema, ++ TemplateLangSchema, + UpdateHostSchema, + UpdateHostStatusSchema, + ) +@@ -481,16 +482,22 @@ class HostTemplateAPI(BaseResponse): + Restful API: Get + """ + +- @BaseResponse.handle() +- def get(self): ++ @BaseResponse.handle(schema=TemplateLangSchema) ++ def get(self, **params): + """ + Download host template file + ++ Args: ++ lang (str): The language code for the desired template file content. ++ If not provided or if the specified language is not supported, defaults to English ("en"). ++ + Returns: + BytesIO + """ ++ file_content = HostTemplate.get_file_content(params.get("lang")) ++ + file = BytesIO() +- file.write(HOST_TEMPLATE_FILE_CONTENT.encode('utf-8')) ++ file.write(file_content.encode('utf-8')) + file.seek(0) + response = send_file(file, mimetype="application/octet-stream") + response.headers['Content-Disposition'] = 'attachment; filename=template.csv' +-- +2.33.0 + diff --git a/aops-zeus.spec b/aops-zeus.spec index 294a4fa..d96c849 100644 --- a/aops-zeus.spec +++ b/aops-zeus.spec @@ -1,10 +1,11 @@ Name: aops-zeus Version: v2.0.0 -Release: 1 +Release: 2 Summary: A service which is the foundation of aops. License: MulanPSL2 URL: https://gitee.com/openeuler/%{name} Source0: %{name}-%{version}.tar.gz +Patch0001: 0001-updated-download-host-template-api.patch BuildRequires: python3-setuptools @@ -48,7 +49,7 @@ Requires: python3-uWSGI python3-gevent A distributed service of aops. %prep -%autosetup -n %{name}-%{version} +%autosetup -n %{name}-%{version} -p1 # build for aops-zeus @@ -139,6 +140,9 @@ popd %{python3_sitelib}/zeus/distribute_service/* %changelog +* Fri Aug 23 2024 wenxin - v2.0.0-2 +- updated download host template api to support english + * Fri Aug 16 2024 wenxin - v2.0.0-1 - Split the existing service module into distinct modules: - Task Module: Handles task management and execution. -- Gitee