From bd2029100462bb2a04b036acd3602dcfa8316f79 Mon Sep 17 00:00:00 2001 From: chenzuoqing Date: Fri, 17 Dec 2021 18:28:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8C=BA=E6=9C=8D=E6=A8=A1=E5=9E=8B=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AD=97=E6=AE=B5=EF=BC=8C?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AE=9E=E4=BE=8B?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E6=9F=90=E4=B8=AA=E5=BA=93=E7=9A=84id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/views.py | 1 + src/controller/project/parsers.py | 14 ++++++ src/models/project/fields.py | 4 ++ src/models/project/models.py | 76 ++++++++++++++++++++++++++++++- 4 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/common/views.py b/src/common/views.py index 560de7b..da5d3d7 100644 --- a/src/common/views.py +++ b/src/common/views.py @@ -148,6 +148,7 @@ class ModelViewBase(Resource): for field, model, required in self.relation_fields: val = args.get(field) # 判断 required: 参数有提交值才校验,未在参数中或零值不校验 + if not required and not val: continue self.validate_relation_pk(val, model, field) diff --git a/src/controller/project/parsers.py b/src/controller/project/parsers.py index a18d4c0..50b040d 100644 --- a/src/controller/project/parsers.py +++ b/src/controller/project/parsers.py @@ -72,6 +72,8 @@ class ServerParse: self.request_parse.add_argument("labels", type=dict, location='json') self.request_parse.add_argument("host_id", type=str, location='json') + self.request_parse.add_argument("game_db_id", type=str, location='json') + self.request_parse.add_argument("admin_db_id", type=str, location='json') self.request_parse.add_argument("domain", type=str, location='json', default="") # status是枚举 self.request_parse.add_argument("status", choices=projectModel.Server.STATUS.keys(), @@ -92,4 +94,16 @@ class ServerParse: version = args.get("version") if version: args["version"] = projectModel.Version(**version) + game_db_id = args.get("game_db_id") + if game_db_id: + try: + assert assetModel.DatabaseServer.objects(databases__id=game_db_id).first() + except: + abort_response(400, 1400, msg="game_db_id not exists") + admin_db_id = args.get("admin_db_id") + if admin_db_id: + try: + assert assetModel.DatabaseServer.objects(databases__id=admin_db_id).first() + except: + abort_response(400, 1400, msg="admin_db_id not exists") return args diff --git a/src/models/project/fields.py b/src/models/project/fields.py index 12b26ff..3c1b887 100644 --- a/src/models/project/fields.py +++ b/src/models/project/fields.py @@ -73,6 +73,8 @@ ServerFields = { "weight": fields.Integer, "slot": fields.Integer, "data": fields.Raw, + "game_database": fields.Raw, + "admin_database": fields.Raw, "tags": fields.List(fields.String), "labels": fields.Raw, } @@ -98,6 +100,8 @@ AgentServerFields = { "weight": fields.Integer, "slot": fields.Integer, "data": fields.Raw, + "game_db": fields.Raw, + "admin_db": fields.Raw, # "tags": serializer.List(serializer.String), # "labels": serializer.Raw, } diff --git a/src/models/project/models.py b/src/models/project/models.py index 8a1cb40..1981704 100644 --- a/src/models/project/models.py +++ b/src/models/project/models.py @@ -5,7 +5,7 @@ from settings import common from common.document import DocumentBase from common.validator import isalnum, is_hex_string -from models.asset.models import Host +from models.asset.models import Host, DatabaseServer class Project(DocumentBase): @@ -95,6 +95,8 @@ class Channel(DocumentBase): class Server(DocumentBase): """服务""" + meta = {"strict": False} + STATUS = { "prepare": "预备", "stopped": "停止", @@ -113,6 +115,8 @@ class Server(DocumentBase): # 机器字段,TODO 先允许为空 # host_id = mongo.StringField(max_length=128, null=True, validation=is_hex_string) host_id = mongo.ObjectIdField(required=True) + game_db_id = mongo.ObjectIdField(required=False) + admin_db_id = mongo.ObjectIdField(required=False) domain = mongo.StringField(max_length=128, required=False, default="") port = mongo.IntField() version = mongo.EmbeddedDocumentField(Version) @@ -238,3 +242,73 @@ class Server(DocumentBase): if hosts: return queryset.filter(host_id__in=hosts) return cls.objects.none() + + @property + def game_db(self) -> dict: + """给机器的返回,数据库集合是 DatabaseServer 的子类,可以通过它找到,不管是""" + if self.game_db_id: + instance = DatabaseServer.objects(databases__id=self.game_db_id).first() + if not instance: + return {} + database = instance.databases.filter(id=self.game_db_id).first() + data = dict(database.to_mongo()) + if "id" in data: + data.pop("id") + data["host"] = instance.host + data["port"] = instance.port + return data + return {} + + @property + def admin_db(self) -> dict: + """给机器的返回,数据库集合是 DatabaseServer 的子类,可以通过它找到,不管是""" + if self.admin_db_id: + instance = DatabaseServer.objects(databases__id=self.admin_db_id).first() + if not instance: + return {} + database = instance.databases.filter(id=self.admin_db_id).first() + data = dict(database.to_mongo()) + if "id" in data: + data.pop("id") + data["host"] = instance.host + data["port"] = instance.port + return data + return {} + + @property + def game_database(self) -> dict: + """给前端的返回,去除密码等敏感信息""" + pop_item = ["id", "username", "password"] + if self.game_db_id: + instance = DatabaseServer.objects(databases__id=self.game_db_id).first() + if not instance: + return {} + database = instance.databases.filter(id=self.game_db_id).first() + data = dict(database.to_mongo()) + for item in pop_item: + if item in data: + data.pop(item) + data["id"] = str(database.id) + data["host"] = instance.host + data["port"] = instance.port + return data + return {} + + @property + def admin_database(self) -> dict: + """给前端的返回,去除密码等敏感信息""" + pop_item = ["username", "password"] + if self.admin_db_id: + instance = DatabaseServer.objects(databases__id=self.admin_db_id).first() + if not instance: + return {} + database = instance.databases.filter(id=self.admin_db_id).first() + data = dict(database.to_mongo()) + for item in pop_item: + if item in data: + data.pop(item) + data["id"] = str(database.id) + data["host"] = instance.host + data["port"] = instance.port + return data + return {}