monitor_host_create.py 2.7 KB
from .models import MonitorHost, db
from sqlalchemy import and_
from app.util.component.ApiTemplate import ApiTemplate
import uuid


class Api(ApiTemplate):
    api_name = "注册主机"

    def para_check(self):
        if not self.para.get("host"):
            raise Exception("缺乏host参数")
        # if not self.para.get("user"):
        #     raise Exception("缺乏user参数")
        # if not self.para.get("type"):
        #     raise Exception("缺乏type参数")
        # if not self.para.get("passwd"):
        #     raise Exception("缺乏passwd参数")

    def process(self):

        # 返回结果
        res = {}
        res["data"] = {}
        try:
            host = self.para.get("host")  # server
            # user = self.para.get("user")
            # password = self.para.get("passwd")
            # type = self.para.get("type")
            host_name = self.para.get("host_name")
            srcid = uuid.uuid1().__str__()

            if(db.session.query(MonitorHost.host).filter(MonitorHost.host == host).count() > 0):
                res['message'] = '禁止重复注册host'
                res["result"] = False
            else:
                monitorHost = MonitorHost(
                    srcid=srcid, host=host, host_name=host_name)
                db.session.add(monitorHost)
                db.session.commit()
                res['data'] = {'host': host, 'srcid': srcid}
                res["result"] = True
        except Exception as e:
            db.session.rollback()
            raise e
        return res

    api_doc = {
        "tags": ["监控接口"],
        "parameters": [
            {"name": "host",
             "in": "formData",
             "type": "string",
             "description": "主机地址",
             "required": "true"},
            # {"name": "user",
            #  "in": "formData",
            #  "type": "string",
            #  "description": "用户",
            #  "required": "true"},
            # {"name": "passwd",
            #  "in": "formData",
            #  "type": "string",
            #  "description": "密码",
            #  "required": "true"},
            # {"name": "type",
            #  "in": "formData",
            #  "type": "string",
            #  "description": "服务器类型",
            #  "required": "true"},
            {"name": "host_name",
             "in": "formData",
             "type": "string",
             "description": "主机名"}
        ],
        "responses": {
            200: {
                "schema": {
                    "properties": {
                    }
                }
            }
        }
    }