jumpserver/apps/assets/const/base.py

79 lines
2.3 KiB
Python
Raw Normal View History

2022-09-19 12:11:55 +00:00
from django.db.models import TextChoices
2022-09-18 16:07:59 +00:00
from jumpserver.utils import has_valid_xpack_license
2022-09-19 12:11:55 +00:00
from .protocol import Protocol
2022-09-18 16:07:59 +00:00
2022-09-19 12:11:55 +00:00
class BaseType(TextChoices):
"""
约束应该考虑代是对平台对限制避免多余对选项: mysql 开启 ssh, 或者开启了也没有作用, 比如 k8s 开启了 domain目前还不支持
"""
@classmethod
def get_constrains(cls):
constrains = {}
base = cls._get_base_constrains()
protocols = cls._get_protocol_constrains()
automation = cls._get_automation_constrains()
base_default = base.pop('*', {})
protocols_default = protocols.pop('*', {})
automation_default = automation.pop('*', {})
for k, v in cls.choices:
tp_base = {**base_default, **base.get(k, {})}
tp_auto = {**automation_default, **automation.get(k, {})}
tp_protocols = {**protocols_default, **protocols.get(k, {})}
tp_protocols = cls._parse_protocols(tp_protocols, k)
tp_constrains = {**tp_base, 'protocols': tp_protocols, 'automation': tp_auto}
constrains[k] = tp_constrains
return constrains
2022-09-18 16:07:59 +00:00
2022-09-19 12:11:55 +00:00
@classmethod
def _parse_protocols(cls, protocol, tp):
2022-09-20 13:19:05 +00:00
settings = Protocol.settings()
2022-09-19 12:11:55 +00:00
choices = protocol.get('choices', [])
if choices == '__self__':
choices = [tp]
2022-09-20 13:19:05 +00:00
protocols = [{'name': name, **settings.get(name, {})} for name in choices]
2022-10-18 12:37:17 +00:00
protocols[0]['primary'] = True
2022-09-19 12:11:55 +00:00
return protocols
@classmethod
def _get_base_constrains(cls) -> dict:
2022-09-18 16:07:59 +00:00
raise NotImplementedError
2022-09-19 12:11:55 +00:00
@classmethod
def _get_protocol_constrains(cls) -> dict:
2022-09-18 16:07:59 +00:00
raise NotImplementedError
@classmethod
2022-09-19 12:11:55 +00:00
def _get_automation_constrains(cls) -> dict:
raise NotImplementedError
2022-09-18 16:07:59 +00:00
2022-09-22 07:24:32 +00:00
@classmethod
def internal_platforms(cls):
raise NotImplementedError
@classmethod
def get_community_types(cls):
raise NotImplementedError
@classmethod
def get_types(cls):
tps = [tp for tp in cls]
if not has_valid_xpack_license():
tps = cls.get_community_types()
return tps
@classmethod
def get_choices(cls):
tps = cls.get_types()
cls_choices = cls.choices
return [
choice for choice in cls_choices
if choice[0] in tps
]