Projects
Eulaceura:Mainline:GA
aops-zeus
_service:obs_scm:0001-updated-download-host-tem...
Sign Up
Log In
Username
Password
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File _service:obs_scm:0001-updated-download-host-template-api.patch of Package aops-zeus
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
Locations
Projects
Search
Status Monitor
Help
Open Build Service
OBS Manuals
API Documentation
OBS Portal
Reporting a Bug
Contact
Mailing List
Forums
Chat (IRC)
Twitter
Open Build Service (OBS)
is an
openSUSE project
.
浙ICP备2022010568号-2