新增资产相关视图,模型的增删查改接口
This commit is contained in:
parent
b6686adef5
commit
58d8cdf3d5
|
@ -67,6 +67,7 @@ RedisInstanceFields = {
|
|||
RedisInstanceFields.update(DatabaseServerFields)
|
||||
|
||||
MiddlewareFields = {
|
||||
"id": fields.String,
|
||||
"host": fields.String,
|
||||
"manage": fields.String,
|
||||
"data": fields.Raw,
|
||||
|
@ -74,8 +75,17 @@ MiddlewareFields = {
|
|||
"labels": fields.Raw,
|
||||
}
|
||||
|
||||
NginxInstance = {
|
||||
NginxInstanceFields = {
|
||||
"port": fields.Integer,
|
||||
"url": fields.String,
|
||||
}
|
||||
NginxInstance.update(MiddlewareFields)
|
||||
NginxInstanceFields.update(MiddlewareFields)
|
||||
|
||||
# CDN 序列化字段
|
||||
CDNFields = {
|
||||
"id": fields.String,
|
||||
"domain": fields.String,
|
||||
"data": fields.Raw,
|
||||
"tags": fields.List(fields.String),
|
||||
"labels": fields.Raw,
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ class RedisInstance(DatabaseServer):
|
|||
port = mongo.IntField(default=6379)
|
||||
memory = mongo.IntField(required=True)
|
||||
replicas = mongo.IntField(required=True, default=0)
|
||||
password = mongo.StringField(max_length=128, required=True)
|
||||
password = mongo.StringField(max_length=128, default="")
|
||||
|
||||
|
||||
class Middleware(DocumentBase):
|
||||
|
@ -92,12 +92,13 @@ class Middleware(DocumentBase):
|
|||
class NginxInstance(Middleware):
|
||||
"""nginx服务器"""
|
||||
port = mongo.IntField(default=80)
|
||||
url = mongo.StringField(required=True)
|
||||
url = mongo.StringField(max_length=128, default="/")
|
||||
|
||||
|
||||
class CDN(DocumentBase):
|
||||
"""cdn域名"""
|
||||
domain = mongo.StringField(max_length=256, required=True) # cdn域名
|
||||
data = mongo.DictField(default=dict)
|
||||
# 标记和标签
|
||||
tags = mongo.ListField(mongo.StringField(), default=list) # tags 默认是空列表
|
||||
labels = mongo.DictField(default=dict)
|
||||
|
|
|
@ -14,3 +14,12 @@ api.add_resource(views.HostDetailViews, '/host/<string:pk>/', endpoint="host-det
|
|||
|
||||
api.add_resource(views.MySQLInstanceViews, '/database/mysql/', endpoint="db-mysql")
|
||||
api.add_resource(views.MySQLInstanceDetail, '/database/mysql/<string:pk>/', endpoint="db-mysql-detail")
|
||||
|
||||
api.add_resource(views.RedisInstanceViews, '/database/redis/', endpoint="db-redis")
|
||||
api.add_resource(views.RedisInstanceDetail, '/database/redis/<string:pk>/', endpoint="db-redis-detail")
|
||||
|
||||
api.add_resource(views.NginxInstanceViews, '/middleware/nginx/', endpoint="middleware-nginx")
|
||||
api.add_resource(views.NginxInstanceDetail, '/middleware/nginx/<string:pk>/', endpoint="middleware-nginx-detail")
|
||||
|
||||
api.add_resource(views.CDNViews, '/cdn/', endpoint="cdn")
|
||||
api.add_resource(views.CDNDetail, '/cdn/<string:pk>/', endpoint="cdn-detail")
|
||||
|
|
|
@ -4,14 +4,13 @@ from flask_restful import reqparse
|
|||
|
||||
from asset import fields
|
||||
from asset import models
|
||||
|
||||
from common.views import ListCreateViewSet, DetailViewSet
|
||||
from common.utils import abort_response
|
||||
|
||||
|
||||
class HostParse:
|
||||
model = None
|
||||
request_parse = None
|
||||
uniq_fields = ("public_ip", "minion_id")
|
||||
|
||||
def init_parse(self):
|
||||
self.request_parse = reqparse.RequestParser()
|
||||
|
@ -27,25 +26,14 @@ class HostParse:
|
|||
self.request_parse.add_argument("tags", required=False, type=list, location='json')
|
||||
self.request_parse.add_argument("labels", required=False, type=dict, location='json')
|
||||
|
||||
def validate_or_abort(self, args, obj=None):
|
||||
"""判断是否冲突"""
|
||||
exists = self.model.objects(public_ip=args.get('public_ip', '')).first()
|
||||
if exists:
|
||||
if obj and exists.id == obj.id: # 同个对象,不算冲突
|
||||
return
|
||||
# 截断请求
|
||||
abort_response(400, 1001, msg="ip已存在")
|
||||
|
||||
def pre_create(self, args):
|
||||
# 标记创建时间
|
||||
args["created"] = datetime.datetime.now()
|
||||
self.validate_or_abort(args)
|
||||
return args
|
||||
|
||||
def pre_update(self, obj, args):
|
||||
if "created" in args:
|
||||
args.pop("created")
|
||||
self.validate_or_abort(args, obj)
|
||||
return args
|
||||
|
||||
|
||||
|
@ -71,23 +59,18 @@ class HostDetailViews(HostParse, DetailViewSet):
|
|||
|
||||
|
||||
class DatabaseServerParse:
|
||||
model = None
|
||||
request_parse = None
|
||||
# 给子类用的唯一字段,用于校验
|
||||
uniq_fields = ("name", "host", "domain")
|
||||
|
||||
def init_parse(self):
|
||||
self.request_parse = reqparse.RequestParser()
|
||||
self.request_parse.add_argument("domain", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("data", required=False, type=dict, location='json')
|
||||
self.request_parse.add_argument("tags", required=False, type=list, location='json')
|
||||
self.request_parse.add_argument("labels", required=False, type=dict, location='json')
|
||||
|
||||
|
||||
class RedisInstanceParse(DatabaseServerParse):
|
||||
|
||||
def init_parse(self):
|
||||
super(RedisInstanceParse, self).init_parse()
|
||||
self.request_parse.add_argument("port", required=False, type=int, location='json')
|
||||
|
||||
|
||||
class MySQLInstanceViews(DatabaseServerParse, ListCreateViewSet):
|
||||
model = models.MySQLInstance
|
||||
fields = fields.MySQLInstanceFields
|
||||
|
@ -125,3 +108,106 @@ class MySQLInstanceDetail(DatabaseServerParse, DetailViewSet):
|
|||
self.request_parse.add_argument("memory", required=False, type=int, location='json')
|
||||
self.request_parse.add_argument("core", required=False, type=int, location='json')
|
||||
super(MySQLInstanceDetail, self).__init__()
|
||||
|
||||
|
||||
class RedisInstanceViews(DatabaseServerParse, ListCreateViewSet):
|
||||
model = models.RedisInstance
|
||||
fields = fields.RedisInstanceFields
|
||||
|
||||
def __init__(self):
|
||||
self.init_parse()
|
||||
self.request_parse.add_argument("name", required=True, type=str, location='json')
|
||||
self.request_parse.add_argument("host", required=True, type=str, location='json')
|
||||
self.request_parse.add_argument("manage", required=True, type=str, location='json')
|
||||
|
||||
self.request_parse.add_argument("port", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("memory", required=True, type=str, location='json')
|
||||
self.request_parse.add_argument("password", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("replicas", required=False, type=int, location='json')
|
||||
super(RedisInstanceViews, self).__init__()
|
||||
|
||||
|
||||
class RedisInstanceDetail(DatabaseServerParse, DetailViewSet):
|
||||
model = models.RedisInstance
|
||||
fields = fields.RedisInstanceFields
|
||||
|
||||
def __init__(self):
|
||||
self.init_parse()
|
||||
self.request_parse.add_argument("name", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("host", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("manage", required=False, type=str, location='json')
|
||||
|
||||
self.request_parse.add_argument("port", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("memory", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("password", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("replicas", required=False, type=int, location='json')
|
||||
super().__init__()
|
||||
|
||||
|
||||
class MiddlewareParse:
|
||||
request_parse = None
|
||||
# 给子类用的唯一字段,用于校验
|
||||
uniq_fields = ("name", "host")
|
||||
|
||||
def init_parse(self):
|
||||
self.request_parse = reqparse.RequestParser()
|
||||
# self.request_parse.add_argument("domain", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("data", required=False, type=dict, location='json')
|
||||
self.request_parse.add_argument("tags", required=False, type=list, location='json')
|
||||
self.request_parse.add_argument("labels", required=False, type=dict, location='json')
|
||||
|
||||
|
||||
class NginxInstanceViews(MiddlewareParse, ListCreateViewSet):
|
||||
model = models.NginxInstance
|
||||
fields = fields.NginxInstanceFields
|
||||
|
||||
def __init__(self):
|
||||
self.init_parse()
|
||||
self.request_parse.add_argument("host", required=True, type=str, location='json')
|
||||
self.request_parse.add_argument("manage", required=True, type=str, location='json')
|
||||
|
||||
self.request_parse.add_argument("port", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("url", required=True, type=str, location='json')
|
||||
super().__init__()
|
||||
|
||||
|
||||
class NginxInstanceDetail(MiddlewareParse, DetailViewSet):
|
||||
model = models.NginxInstance
|
||||
fields = fields.NginxInstanceFields
|
||||
|
||||
def __init__(self):
|
||||
self.init_parse()
|
||||
self.request_parse.add_argument("host", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("manage", required=False, type=str, location='json')
|
||||
|
||||
self.request_parse.add_argument("port", required=False, type=str, location='json')
|
||||
self.request_parse.add_argument("url", required=False, type=str, location='json')
|
||||
super().__init__()
|
||||
|
||||
|
||||
class CDNViews(ListCreateViewSet):
|
||||
model = models.CDN
|
||||
fields = fields.CDNFields
|
||||
uniq_fields = ("domain",)
|
||||
|
||||
def __init__(self):
|
||||
self.request_parse = reqparse.RequestParser()
|
||||
self.request_parse.add_argument("domain", required=True, type=str, location='json')
|
||||
|
||||
self.request_parse.add_argument("data", required=False, type=dict, location='json')
|
||||
self.request_parse.add_argument("tags", required=False, type=list, location='json')
|
||||
self.request_parse.add_argument("labels", required=False, type=dict, location='json')
|
||||
|
||||
|
||||
class CDNDetail(DetailViewSet):
|
||||
model = models.CDN
|
||||
fields = fields.CDNFields
|
||||
uniq_fields = ("domain",)
|
||||
|
||||
def __init__(self):
|
||||
self.request_parse = reqparse.RequestParser()
|
||||
self.request_parse.add_argument("domain", required=False, type=str, location='json')
|
||||
|
||||
self.request_parse.add_argument("data", required=False, type=dict, location='json')
|
||||
self.request_parse.add_argument("tags", required=False, type=list, location='json')
|
||||
self.request_parse.add_argument("labels", required=False, type=dict, location='json')
|
||||
|
|
Loading…
Reference in New Issue