1 Star 0 Fork 5

luxuexian/aops-zeus-src

forked from src-openEuler/aops-zeus 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0001-updated-download-host-template-api.patch 6.86 KB
一键复制 编辑 原始数据 按行查看 历史
wency 提交于 2024-08-23 17:19 . update download host template api
From 429e3440238fde2e6051443b5b819e9bcb25536f Mon Sep 17 00:00:00 2001
From: rabbitali <wenxin32@foxmail.com>
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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/rearcher/aops-zeus_1.git
git@gitee.com:rearcher/aops-zeus_1.git
rearcher
aops-zeus_1
aops-zeus-src
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385