From c10d1ff7ad3b74886911b719f50f4775120db789 Mon Sep 17 00:00:00 2001
From: rearcher <123781007@qq.com>
Date: Thu, 14 Dec 2023 19:58:19 +0800
Subject: [PATCH] add a new query method based on host name for the host list query interface.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 ...6\216\245\345\217\243\346\226\207\346\241\243.yaml" |  4 ++++
 zeus/database/proxy/host.py                            | 10 +++++++++-
 zeus/function/verify/host.py                           |  1 +
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git "a/doc/design/aops-zeus\346\216\245\345\217\243\346\226\207\346\241\243.yaml" "b/doc/design/aops-zeus\346\216\245\345\217\243\346\226\207\346\241\243.yaml"
index efadcc6..87dfe68 100644
--- "a/doc/design/aops-zeus\346\216\245\345\217\243\346\226\207\346\241\243.yaml"
+++ "b/doc/design/aops-zeus\346\216\245\345\217\243\346\226\207\346\241\243.yaml"
@@ -998,6 +998,10 @@ definitions:
           type: string
         description: 获取指定主机组里的主机信息，为空表示所有
         example: '[]'
+      search_key:
+        type: string
+        description: 输入主机名称或主机host_ip获取指定主机信息
+        example: search_key
       management:
         type: boolean
         description: 管理节点or监控节点，不传表示所有
diff --git a/zeus/database/proxy/host.py b/zeus/database/proxy/host.py
index 477c482..441ef21 100644
--- a/zeus/database/proxy/host.py
+++ b/zeus/database/proxy/host.py
@@ -19,7 +19,7 @@ import math
 from typing import Dict, List, Tuple
 
 import sqlalchemy
-from sqlalchemy import func
+from sqlalchemy import func, or_
 from sqlalchemy.sql.expression import asc, desc
 from sqlalchemy.orm.collections import InstrumentedList
 
@@ -210,11 +210,19 @@ class HostProxy(MysqlProxy):
         username = data['username']
         host_group_list = data.get('host_group_list')
         management = data.get('management')
+        search_key = data.get('search_key')
         filters = {Host.user == username}
         if host_group_list:
             filters.add(Host.host_group_name.in_(host_group_list))
         if management is not None:
             filters.add(Host.management == management)
+        if search_key:
+            filters.add(
+                or_(
+                    Host.host_name.like("%" + search_key + "%"),
+                    Host.host_ip.like("%" + search_key + "%"),
+                )
+            )
         if data.get('status'):
             filters.add(Host.status.in_(data.get('status')))
 
diff --git a/zeus/function/verify/host.py b/zeus/function/verify/host.py
index f746968..3f8bab9 100644
--- a/zeus/function/verify/host.py
+++ b/zeus/function/verify/host.py
@@ -52,6 +52,7 @@ class GetHostSchema(Schema):
     """
 
     host_group_list = fields.List(fields.String(), required=True)
+    search_key = fields.String(required=False, validate=lambda s: 50 > len(s) > 0)
     management = fields.Boolean(required=False)
     status = fields.List(fields.Integer(validate=lambda s: s >= 0), required=False)
     sort = fields.String(required=False, validate=validate.OneOf(["host_name", "host_group_name", ""]))
-- 
2.33.0

