区服模型新增数据库字段,保存数据库实例中的某个库的id

This commit is contained in:
chenzuoqing 2021-12-17 18:28:35 +08:00
parent 4d0bc44849
commit bd20291004
4 changed files with 94 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -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,
}

View File

@ -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 {}