2023-10-18 06:10:24 +00:00
|
|
|
|
from django.conf import settings
|
2023-06-30 07:54:06 +00:00
|
|
|
|
from django.db import models
|
2022-09-19 12:11:55 +00:00
|
|
|
|
from django.db.models import TextChoices
|
2023-06-30 07:54:06 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-09-18 16:07:59 +00:00
|
|
|
|
|
|
|
|
|
|
2023-04-10 02:57:44 +00:00
|
|
|
|
class Type:
|
|
|
|
|
def __init__(self, label, value):
|
2023-04-14 06:45:35 +00:00
|
|
|
|
self.name = value
|
2023-04-10 02:57:44 +00:00
|
|
|
|
self.label = label
|
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.value
|
|
|
|
|
|
2023-04-14 06:45:35 +00:00
|
|
|
|
def __add__(self, other):
|
|
|
|
|
if isinstance(other, str):
|
|
|
|
|
return str(str(self) + other)
|
|
|
|
|
raise TypeError("unsupported operand type(s) for +: '{}' and '{}'".format(
|
|
|
|
|
type(self), type(other))
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def __radd__(self, other):
|
|
|
|
|
if isinstance(other, str):
|
|
|
|
|
return str(other + str(self))
|
|
|
|
|
raise TypeError("unsupported operand type(s) for +(r): '{}' and '{}'".format(
|
|
|
|
|
type(self), type(other))
|
|
|
|
|
)
|
|
|
|
|
|
2023-04-10 02:57:44 +00:00
|
|
|
|
|
2023-06-30 07:54:06 +00:00
|
|
|
|
class FillType(models.TextChoices):
|
|
|
|
|
no = 'no', _('Disabled')
|
|
|
|
|
basic = 'basic', _('Basic')
|
|
|
|
|
script = 'script', _('Script')
|
|
|
|
|
|
|
|
|
|
|
2022-09-19 12:11:55 +00:00
|
|
|
|
class BaseType(TextChoices):
|
|
|
|
|
"""
|
2023-03-22 06:15:25 +00:00
|
|
|
|
约束应该考虑代是对平台对限制,避免多余对选项,如: mysql 开启 ssh,
|
|
|
|
|
或者开启了也没有作用, 比如 k8s 开启了 domain,目前还不支持
|
2022-09-19 12:11:55 +00:00
|
|
|
|
"""
|
2023-03-22 06:15:25 +00:00
|
|
|
|
|
2022-09-19 12:11:55 +00:00
|
|
|
|
@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('*', {})
|
|
|
|
|
|
2023-04-10 02:57:44 +00:00
|
|
|
|
for k, v in cls.get_choices():
|
2022-09-19 12:11:55 +00:00
|
|
|
|
tp_base = {**base_default, **base.get(k, {})}
|
|
|
|
|
tp_auto = {**automation_default, **automation.get(k, {})}
|
2023-07-04 02:29:27 +00:00
|
|
|
|
tp_protocols = {**protocols_default, **{'port_from_addr': False}, **protocols.get(k, {})}
|
2022-09-19 12:11:55 +00:00
|
|
|
|
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):
|
2023-06-30 07:54:06 +00:00
|
|
|
|
from .protocol import Protocol
|
2023-10-24 08:17:38 +00:00
|
|
|
|
_settings = Protocol.settings()
|
2022-09-19 12:11:55 +00:00
|
|
|
|
choices = protocol.get('choices', [])
|
|
|
|
|
if choices == '__self__':
|
|
|
|
|
choices = [tp]
|
2023-07-07 11:47:12 +00:00
|
|
|
|
|
|
|
|
|
protocols = []
|
|
|
|
|
for name in choices:
|
2023-10-24 08:17:38 +00:00
|
|
|
|
protocol = {'name': name, **_settings.get(name, {})}
|
2023-07-07 11:47:12 +00:00
|
|
|
|
setting = protocol.pop('setting', {})
|
|
|
|
|
setting_values = {k: v.get('default', None) for k, v in setting.items()}
|
|
|
|
|
protocol['setting'] = setting_values
|
|
|
|
|
protocols.append(protocol)
|
|
|
|
|
|
2023-04-10 02:57:44 +00:00
|
|
|
|
if protocols:
|
|
|
|
|
protocols[0]['default'] = 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
|
2022-12-19 03:35:50 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
2023-04-10 02:57:44 +00:00
|
|
|
|
def _get_choices_to_types(cls):
|
|
|
|
|
choices = cls.get_choices()
|
|
|
|
|
return [Type(label, value) for value, label in choices]
|
2022-12-19 03:35:50 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_types(cls):
|
2023-04-14 06:45:35 +00:00
|
|
|
|
return cls._get_choices_to_types()
|
2022-12-19 03:35:50 +00:00
|
|
|
|
|
2023-04-10 02:57:44 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def get_community_types(cls):
|
|
|
|
|
return cls._get_choices_to_types()
|
|
|
|
|
|
2022-12-19 03:35:50 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def get_choices(cls):
|
2023-10-24 08:17:38 +00:00
|
|
|
|
if not settings.XPACK_ENABLED:
|
2023-04-14 06:45:35 +00:00
|
|
|
|
return [
|
|
|
|
|
(tp.value, tp.label)
|
|
|
|
|
for tp in cls.get_community_types()
|
|
|
|
|
]
|
2023-04-10 02:57:44 +00:00
|
|
|
|
return cls.choices
|