class NetworkSerializer(s.InstanceSerializer): """ vms.models.Subnet """ _model_ = Subnet _update_fields_ = ('alias', 'owner', 'access', 'desc', 'network', 'netmask', 'gateway', 'resolvers', 'dns_domain', 'ptr_domain', 'nic_tag', 'vlan_id', 'dc_bound', 'dhcp_passthrough') _default_fields_ = ('name', 'alias', 'owner') _blank_fields_ = frozenset({'desc', 'dns_domain', 'ptr_domain'}) _null_fields_ = frozenset({'gateway'}) # min_length because of API URL: /network/ip/ name = s.RegexField(r'^[A-Za-z0-9][A-Za-z0-9\._-]*$', min_length=3, max_length=32) uuid = s.CharField(read_only=True) alias = s.SafeCharField(max_length=32) owner = s.SlugRelatedField(slug_field='username', queryset=User.objects, required=False) access = s.IntegerChoiceField(choices=Subnet.ACCESS, default=Subnet.PRIVATE) desc = s.SafeCharField(max_length=128, required=False) network = s.IPAddressField() netmask = s.IPAddressField() gateway = s.IPAddressField(required=False) # can be null nic_tag = s.ChoiceField() vlan_id = s.IntegerField(min_value=0, max_value=4096) resolvers = s.IPAddressArrayField(source='resolvers_api', required=False, max_items=8) dns_domain = s.RegexField(r'^[A-Za-z0-9][A-Za-z0-9\._-]*$', max_length=250, required=False) # can be blank ptr_domain = s.RegexField(r'^[A-Za-z0-9][A-Za-z0-9\._-]*$', max_length=250, required=False) # can be blank dhcp_passthrough = s.BooleanField(default=False) dc_bound = s.BooleanField(source='dc_bound_bool', default=True) created = s.DateTimeField(read_only=True, required=False) def __init__(self, request, net, *args, **kwargs): super(NetworkSerializer, self).__init__(request, net, *args, **kwargs) if not kwargs.get('many', False): self._dc_bound = net.dc_bound self.fields['owner'].queryset = get_owners(request, all=True) self.fields['nic_tag'].choices = [ (i, i) for i in DefaultDc().settings.VMS_NET_NIC_TAGS ] def _normalize(self, attr, value): if attr == 'dc_bound': return self._dc_bound # noinspection PyProtectedMember return super(NetworkSerializer, self)._normalize(attr, value) def validate_dc_bound(self, attrs, source): try: value = bool(attrs[source]) except KeyError: pass else: if value != self.object.dc_bound_bool: self._dc_bound = validate_dc_bound(self.request, self.object, value, _('Network')) return attrs def validate_alias(self, attrs, source): try: value = attrs[source] except KeyError: pass else: validate_alias(self.object, value) return attrs def validate_vlan_id(self, attrs, source): try: value = attrs[source] except KeyError: pass else: net = self.object if not net.new: # TODO: Cannot use ip__in=net_ips (ProgrammingError) net_ips = set(net.ipaddress_set.all().values_list('ip', flat=True)) other_ips = set( IPAddress.objects.exclude(subnet=net).filter( subnet__vlan_id=int(value)).values_list('ip', flat=True)) if net_ips.intersection(other_ips): raise s.ValidationError( _('Network has IP addresses that already exist in another ' 'network with the same VLAN ID.')) return attrs # noinspection PyMethodMayBeStatic def validate_ptr_domain(self, attrs, source): try: value = attrs[source] except KeyError: pass else: if value: if not value.endswith('in-addr.arpa'): raise s.ValidationError(_('Invalid PTR domain name.')) if settings.DNS_ENABLED: if not Domain.objects.filter(name=value).exists(): raise s.ObjectDoesNotExist(value) return attrs def validate(self, attrs): try: network = attrs['network'] except KeyError: network = self.object.network try: netmask = attrs['netmask'] except KeyError: netmask = self.object.netmask try: ip_network = Subnet.get_ip_network(network, netmask) if ip_network.is_reserved: raise ValueError except ValueError: self._errors['network'] = self._errors['netmask'] = \ s.ErrorList([_('Enter a valid IPv4 network and netmask.')]) if self.request.method == 'POST' and self._dc_bound: limit = self._dc_bound.settings.VMS_NET_LIMIT if limit is not None: if Subnet.objects.filter( dc_bound=self._dc_bound).count() >= int(limit): raise s.ValidationError( _('Maximum number of networks reached')) if self._dc_bound: try: vlan_id = attrs['vlan_id'] except KeyError: vlan_id = self.object.vlan_id dc_settings = self._dc_bound.settings if dc_settings.VMS_NET_VLAN_RESTRICT and vlan_id not in dc_settings.VMS_NET_VLAN_ALLOWED: self._errors['vlan_id'] = s.ErrorList( [_('VLAN ID is not available in datacenter.')]) return attrs # noinspection PyMethodMayBeStatic def update_errors(self, fields, err_msg): errors = {} for i in fields: errors[i] = s.ErrorList([err_msg]) return errors
class DcSettingsSerializer(s.InstanceSerializer): """ vms.models.Dc.settings """ _global_settings = None _model_ = Dc modules = settings.MODULES # Used in gui forms third_party_modules = [] # Class level storage, updated only with the decorator function third_party_settings = [] # Class level storage, updated only with the decorator function # List of settings which cannot be changed when set to False in (local_)settings.py (booleans only) _override_disabled_ = settings.MODULES _blank_fields_ = frozenset({ 'SITE_LOGO', 'SITE_ICON', 'SHADOW_EMAIL', 'SUPPORT_PHONE', 'VMS_DISK_IMAGE_DEFAULT', 'VMS_DISK_IMAGE_ZONE_DEFAULT', 'VMS_DISK_IMAGE_LX_ZONE_DEFAULT', 'VMS_NET_DEFAULT', 'VMS_STORAGE_DEFAULT', 'MON_ZABBIX_HTTP_USERNAME', 'MON_ZABBIX_HTTP_PASSWORD', 'MON_ZABBIX_HOST_VM_PROXY', 'DNS_SOA_DEFAULT', 'EMAIL_HOST_USER', 'EMAIL_HOST_PASSWORD', 'SMS_FROM_NUMBER', 'SMS_SERVICE_USERNAME', 'SMS_SERVICE_PASSWORD', }) _null_fields_ = frozenset({ 'VMS_VM_DEFINE_LIMIT', 'VMS_VM_SNAPSHOT_DEFINE_LIMIT', 'VMS_VM_SNAPSHOT_LIMIT_AUTO', 'VMS_VM_SNAPSHOT_LIMIT_MANUAL', 'VMS_VM_SNAPSHOT_LIMIT_MANUAL_DEFAULT', 'VMS_VM_SNAPSHOT_SIZE_LIMIT', 'VMS_VM_SNAPSHOT_SIZE_LIMIT_DEFAULT', 'VMS_VM_SNAPSHOT_DC_SIZE_LIMIT', 'VMS_VM_BACKUP_DEFINE_LIMIT', 'VMS_VM_BACKUP_LIMIT', 'VMS_VM_BACKUP_DC_SIZE_LIMIT', 'VMS_NET_LIMIT', 'VMS_IMAGE_VM', 'VMS_IMAGE_LIMIT', 'VMS_ISO_LIMIT' }) dc = s.CharField(label=_('Datacenter'), read_only=True) # Modules VMS_VM_SNAPSHOT_ENABLED = s.BooleanField(label=_('Snapshots')) VMS_VM_BACKUP_ENABLED = s.BooleanField(label=_('Backups')) MON_ZABBIX_ENABLED = s.BooleanField(label=_('Monitoring')) DNS_ENABLED = s.BooleanField(label=_('DNS')) SUPPORT_ENABLED = s.BooleanField(label=_('Support')) REGISTRATION_ENABLED = s.BooleanField(label=_('Registration')) FAQ_ENABLED = s.BooleanField(label=_('FAQ')) # Not part of MODULES (can be overridden even if disabled in settings) # Advanced settings VMS_VM_DOMAIN_DEFAULT = s.RegexField(r'^[A-Za-z0-9][A-Za-z0-9\._/-]*$', label='VMS_VM_DOMAIN_DEFAULT', max_length=255, min_length=3, help_text=_('Default domain part of the hostname of a newly ' 'created virtual server.')) COMPANY_NAME = s.CharField(label='COMPANY_NAME', max_length=255, help_text=_('Name of the company using this virtual datacenter.')) SITE_NAME = s.CharField(label='SITE_NAME', max_length=255, help_text=_('Name of this site used mostly in email and text message templates.')) SITE_LINK = s.CharField(label='SITE_LINK', max_length=255, help_text=_('Link to this site used mostly in email and text message templates.')) SITE_SIGNATURE = s.CharField(label='SITE_SIGNATURE', max_length=255, help_text=_('Signature attached to outgoing emails related ' 'to this virtual datacenter.')) SITE_LOGO = s.URLField(label='SITE_LOGO', max_length=2048, required=False, help_text=_('URL pointing to an image, which will be displayed as a logo on the main page. ' 'If empty the default Danube Cloud logo will be used.')) SITE_ICON = s.URLField(label='SITE_ICON', max_length=2048, required=False, help_text=_('URL pointing to an image, which will be displayed as an icon in the navigation ' 'bar. If empty the default Danube Cloud icon will be used.')) SUPPORT_EMAIL = s.EmailField(label='SUPPORT_EMAIL', max_length=255, help_text=_('Destination email address used for all support tickets ' 'related to this virtual datacenter.')) SUPPORT_PHONE = s.CharField(label='SUPPORT_PHONE', max_length=255, required=False, help_text=_('Phone number displayed in the support contact details.')) SUPPORT_USER_CONFIRMATION = s.BooleanField(label='SUPPORT_USER_CONFIRMATION', help_text=_('Whether to send a confirmation email to the user after ' 'a support ticket has been sent to SUPPORT_EMAIL.')) DEFAULT_FROM_EMAIL = s.EmailField(label='DEFAULT_FROM_EMAIL', max_length=255, help_text=_('Email address used as the "From" address for all outgoing emails ' 'related to this virtual datacenter.')) EMAIL_ENABLED = s.BooleanField(label='EMAIL_ENABLED', help_text=_('Whether to completely disable sending of emails ' 'related to this virtual datacenter.')) API_LOG_USER_CALLBACK = s.BooleanField(label='API_LOG_USER_CALLBACK', help_text=_('Whether to log API user callback requests into the tasklog.')) VMS_ZONE_ENABLED = s.BooleanField(label='VMS_ZONE_ENABLED', # Module help_text=_('Whether to enable support for SunOS and Linux zones in ' 'this virtual datacenter.')) VMS_VM_DEFINE_LIMIT = s.IntegerField(label='VMS_VM_DEFINE_LIMIT', required=False, help_text=_('Maximum number of virtual servers that can be defined in ' 'this virtual datacenter.')) VMS_VM_CPU_CAP_REQUIRED = s.BooleanField(label='VMS_VM_CPU_CAP_REQUIRED', help_text='When disabled, the vCPUs server parameter on SunOS and Linux ' 'Zones can be set to 0, which removes the compute node CPU ' 'limit (cpu_cap) for the virtual server.') VMS_VM_STOP_TIMEOUT_DEFAULT = s.IntegerField(label='VMS_VM_STOP_TIMEOUT_DEFAULT', help_text='Default time period (in seconds) for a graceful VM stop or ' 'reboot, after which a force stop/reboot is send to the VM ' '(KVM only).') VMS_VM_STOP_WIN_TIMEOUT_DEFAULT = s.IntegerField(label='VMS_VM_STOP_WIN_TIMEOUT_DEFAULT', help_text='This is the same setting as VMS_VM_STOP_TIMEOUT_DEFAULT' ' but for a VM with Windows OS type, which usually takes' ' longer to shutdown.') VMS_VM_OSTYPE_DEFAULT = s.IntegerChoiceField(label='VMS_VM_OSTYPE_DEFAULT', choices=Vm.OSTYPE, help_text=_('Default operating system type. One of: 1 - Linux VM, ' '2 - SunOS VM, 3 - BSD VM, 4 - Windows VM, ' '5 - SunOS Zone, 6 - Linux Zone.')) VMS_VM_MONITORED_DEFAULT = s.BooleanField(label='VMS_VM_MONITORED_DEFAULT', help_text=_('Controls whether server synchronization with the monitoring ' 'system is enabled by default.')) VMS_VM_CPU_SHARES_DEFAULT = s.IntegerField(label='VMS_VM_CPU_SHARES_DEFAULT', min_value=0, max_value=1048576, help_text=_("Default value of the server's CPU shares, " "relative to other servers.")) VMS_VM_ZFS_IO_PRIORITY_DEFAULT = s.IntegerField(label='VMS_VM_ZFS_IO_PRIORITY_DEFAULT', min_value=0, max_value=1024, help_text=_("Default value of the server's IO throttling " "priority, relative to other servers.")) VMS_VM_RESOLVERS_DEFAULT = s.IPAddressArrayField(label='VMS_VM_RESOLVERS_DEFAULT', max_items=8, help_text=_('Default DNS resolvers used for newly ' 'created servers.')) VMS_VM_SSH_KEYS_DEFAULT = s.ArrayField(label='VMS_VM_SSH_KEYS_DEFAULT', max_items=32, required=False, help_text=_('List of public SSH keys added to every virtual machine ' 'in this virtual datacenter.')) VMS_VM_MDATA_DEFAULT = s.MetadataField(label='VMS_VM_MDATA_DEFAULT', required=False, validators=(validate_mdata(Vm.RESERVED_MDATA_KEYS),), help_text=_('Default VM metadata (key=value string pairs).')) VMS_DISK_MODEL_DEFAULT = s.ChoiceField(label='VMS_DISK_MODEL_DEFAULT', choices=Vm.DISK_MODEL, help_text=_('Default disk model of newly created server disks. One of: ' 'virtio, ide, scsi.')) VMS_DISK_COMPRESSION_DEFAULT = s.ChoiceField(label='VMS_DISK_COMPRESSION_DEFAULT', choices=Vm.DISK_COMPRESSION, help_text=_('Default disk compression algorithm. ' 'One of: off, lzjb, gzip, gzip-N, zle, lz4.')) VMS_DISK_IMAGE_DEFAULT = s.CharField(label='VMS_DISK_IMAGE_DEFAULT', max_length=64, required=False, help_text=_('Name of the default disk image used for ' 'newly created server disks.')) VMS_DISK_IMAGE_ZONE_DEFAULT = s.CharField(label='VMS_DISK_IMAGE_ZONE_DEFAULT', max_length=64, required=False, help_text=_('Name of the default disk image used for ' 'newly created SunOS zone servers.')) VMS_DISK_IMAGE_LX_ZONE_DEFAULT = s.CharField(label='VMS_DISK_IMAGE_LX_ZONE_DEFAULT', max_length=64, required=False, help_text=_('Name of the default disk image used for ' 'newly created Linux zone servers.')) VMS_NIC_MODEL_DEFAULT = s.ChoiceField(label='VMS_NIC_MODEL_DEFAULT', choices=Vm.NIC_MODEL, help_text=_('Default virtual NIC model of newly created server NICs. ' 'One of: virtio, e1000, rtl8139.')) VMS_NIC_MONITORING_DEFAULT = s.IntegerField(label='VMS_NIC_MONITORING_DEFAULT', min_value=NIC_ID_MIN, max_value=NIC_ID_MAX, help_text=_('Default NIC ID, which will be used for ' 'external monitoring.')) VMS_NET_DEFAULT = s.CharField(label='VMS_NET_DEFAULT', max_length=64, required=False, help_text=_('Name of the default network used for newly created server NICs.')) VMS_NET_LIMIT = s.IntegerField(label='VMS_NET_LIMIT', required=False, help_text=_('Maximum number of DC-bound networks that can be created in ' 'this virtual datacenter.')) VMS_NET_VLAN_RESTRICT = s.BooleanField(label='VMS_NET_VLAN_RESTRICT', help_text=_('Whether to restrict VLAN IDs to the ' 'VMS_NET_VLAN_ALLOWED list.')) VMS_NET_VLAN_ALLOWED = s.IntegerArrayField(label='VMS_NET_VLAN_ALLOWED', required=False, help_text=_('List of VLAN IDs available for newly created DC-bound ' 'networks in this virtual datacenter.')) VMS_NET_VXLAN_RESTRICT = s.BooleanField(label='VMS_NET_VXLAN_RESTRICT', help_text=_('Whether to restrict VXLAN IDs to the ' 'VMS_NET_VXLAN_ALLOWED list.')) VMS_NET_VXLAN_ALLOWED = s.IntegerArrayField(label='VMS_NET_VXLAN_ALLOWED', required=False, help_text=_('List of VXLAN IDs available for newly created DC-bound ' 'networks in this virtual datacenter.')) VMS_IMAGE_LIMIT = s.IntegerField(label='VMS_IMAGE_LIMIT', required=False, help_text=_('Maximum number of DC-bound server images that can be created in ' 'this virtual datacenter.')) VMS_ISO_LIMIT = s.IntegerField(label='VMS_ISO_LIMIT', required=False, help_text=_('Maximum number of DC-bound ISO images that can be created in ' 'this virtual datacenter.')) VMS_STORAGE_DEFAULT = s.CharField(label='VMS_STORAGE_DEFAULT', max_length=64, required=False, help_text=_('Name of the default storage used for newly created servers ' 'and server disks.')) VMS_VGA_MODEL_DEFAULT = s.ChoiceField(label='VMS_VGA_MODEL_DEFAULT', choices=Vm.VGA_MODEL, help_text=_('Default VGA emulation driver of newly created servers. ' 'One of: std, cirrus, vmware.')) VMS_VM_SNAPSHOT_DEFINE_LIMIT = s.IntegerField(label='VMS_VM_SNAPSHOT_DEFINE_LIMIT', required=False, help_text=_('Maximum number of snapshot definitions per server.')) VMS_VM_SNAPSHOT_LIMIT_AUTO = s.IntegerField(label='VMS_VM_SNAPSHOT_LIMIT_AUTO', required=False, help_text=_('Maximum number of automatic snapshots per server.')) VMS_VM_SNAPSHOT_LIMIT_MANUAL = s.IntegerField(label='VMS_VM_SNAPSHOT_LIMIT_MANUAL', required=False, help_text=_('Maximum number of manual snapshots per server.')) VMS_VM_SNAPSHOT_LIMIT_MANUAL_DEFAULT = s.IntegerField(label='VMS_VM_SNAPSHOT_LIMIT_MANUAL_DEFAULT', required=False, help_text=_('Predefined manual snapshot limit ' 'for new servers.')) VMS_VM_SNAPSHOT_SIZE_LIMIT = s.IntegerField(label='VMS_VM_SNAPSHOT_SIZE_LIMIT', required=False, help_text=_('Maximum size (MB) of all snapshots per server.')) VMS_VM_SNAPSHOT_SIZE_LIMIT_DEFAULT = s.IntegerField(label='VMS_VM_SNAPSHOT_SIZE_LIMIT_DEFAULT', required=False, help_text=_('Predefined snapshot size limit (MB) for new ' 'servers.')) VMS_VM_SNAPSHOT_DC_SIZE_LIMIT = s.IntegerField(label='VMS_VM_SNAPSHOT_DC_SIZE_LIMIT', required=False, help_text=_('Maximum size (MB) of all snapshots in this ' 'virtual datacenter.')) VMS_VM_BACKUP_DEFINE_LIMIT = s.IntegerField(label='VMS_VM_BACKUP_DEFINE_LIMIT', required=False, help_text=_('Maximum number of backup definitions per server.')) VMS_VM_BACKUP_LIMIT = s.IntegerField(label='VMS_VM_BACKUP_LIMIT', required=False, help_text=_('Upper retention limit used for new backup definitions.')) VMS_VM_BACKUP_DC_SIZE_LIMIT = s.IntegerField(label='VMS_VM_BACKUP_DC_SIZE_LIMIT', required=False, help_text=_('Maximum size (MB) of all backups in this ' 'virtual datacenter.')) VMS_VM_BACKUP_COMPRESSION_DEFAULT = s.ChoiceField(label='VMS_VM_BACKUP_COMPRESSION_DEFAULT', choices=BackupDefine.COMPRESSION, help_text=_('Predefined compression algorithm for ' 'new file backups.')) DNS_PTR_DEFAULT = s.CharField(label='DNS_PTR_DEFAULT', max_length=255, min_length=4, help_text=_("Default value used for reverse DNS records of virtual server " "NIC's IP addresses. Available placeholders are: " "{ipaddr}, {hostname}, {alias}.")) MON_ZABBIX_SERVER = s.RegexField(r'^https?://.*$', label='MON_ZABBIX_SERVER', max_length=1024, help_text=_('URL address of Zabbix server used for external monitoring of servers ' 'in this virtual datacenter. WARNING: Changing this and other ' 'MON_ZABBIX_* values in default virtual datacenter will ' 'affect the built-in internal monitoring of servers and ' 'compute nodes.')) MON_ZABBIX_SERVER_SSL_VERIFY = s.BooleanField(label='MON_ZABBIX_SERVER_SSL_VERIFY', help_text=_('Whether to perform HTTPS certificate verification when ' 'connecting to the Zabbix API.')) MON_ZABBIX_TIMEOUT = s.IntegerField(label='MON_ZABBIX_TIMEOUT', min_value=1, max_value=180, help_text=_('Timeout in seconds used for connections to the Zabbix API.')) MON_ZABBIX_USERNAME = s.CharField(label='MON_ZABBIX_USERNAME', max_length=255, help_text=_('Username used for connecting to the Zabbix API.')) MON_ZABBIX_PASSWORD = s.CharField(label='MON_ZABBIX_PASSWORD', max_length=255, help_text=_('Password used for connecting to the Zabbix API.')) MON_ZABBIX_HTTP_USERNAME = s.CharField(label='MON_ZABBIX_HTTP_USERNAME', max_length=255, required=False, help_text=_('Username used for the HTTP basic authentication required for ' 'connections to the Zabbix API.')) MON_ZABBIX_HTTP_PASSWORD = s.CharField(label='MON_ZABBIX_HTTP_PASSWORD', max_length=255, required=False, help_text=_('Password used for the HTTP basic authentication required for ' 'connections to the Zabbix API.')) MON_ZABBIX_VM_SLA = s.BooleanField(label='MON_ZABBIX_VM_SLA', help_text=_('Whether to fetch and display the SLA value of virtual servers.')) MON_ZABBIX_VM_SYNC = s.BooleanField(label='MON_ZABBIX_VM_SYNC', help_text=_('Whether newly created virtual servers can be automatically ' 'synchronized with the monitoring server.')) MON_ZABBIX_HOSTGROUP_VM = s.SafeCharField(label='MON_ZABBIX_HOSTGROUP_VM', max_length=255, help_text=_('Existing Zabbix host group, which will be used for all ' 'monitored servers in this virtual datacenter.')) MON_ZABBIX_HOSTGROUPS_VM = s.ArrayField(label='MON_ZABBIX_HOSTGROUPS_VM', max_items=32, required=False, help_text=_('List of Zabbix host groups, which will be used ' 'for all monitored servers in this virtual datacenter. ' 'Available placeholders are: {ostype}, {ostype_text}, ' '{disk_image}, {disk_image_abbr}, {dc_name}.')) MON_ZABBIX_HOSTGROUPS_VM_RESTRICT = s.BooleanField(label='MON_ZABBIX_HOSTGROUPS_VM_RESTRICT', help_text=_('Whether to restrict Zabbix host group names to the ' 'MON_ZABBIX_HOSTGROUPS_VM_ALLOWED list.')) MON_ZABBIX_HOSTGROUPS_VM_ALLOWED = s.ArrayField(label='MON_ZABBIX_HOSTGROUPS_VM_ALLOWED', max_items=32, required=False, help_text=_('List of Zabbix host groups that can be used by servers' ' in this virtual datacenter. Available placeholders' ' are: {ostype}, {ostype_text}, {disk_image},' ' {disk_image_abbr}, {dc_name}.')) MON_ZABBIX_TEMPLATES_VM = s.ArrayField(label='MON_ZABBIX_TEMPLATES_VM', max_items=128, required=False, help_text=_('List of existing Zabbix templates, which will be used for all ' 'monitored servers in this virtual datacenter. ' 'Available placeholders are: {ostype}, {ostype_text}, ' '{disk_image}, {disk_image_abbr}, {dc_name}.')) MON_ZABBIX_TEMPLATES_VM_MAP_TO_TAGS = s.BooleanField(label='MON_ZABBIX_TEMPLATES_VM_MAP_TO_TAGS', help_text=_('Whether to find and use existing Zabbix templates' ' according to tags of a monitored ' 'virtual server.')) MON_ZABBIX_TEMPLATES_VM_RESTRICT = s.BooleanField(label='MON_ZABBIX_TEMPLATES_VM_RESTRICT', help_text=_('Whether to restrict Zabbix template names to the ' 'MON_ZABBIX_TEMPLATES_VM_ALLOWED list.')) MON_ZABBIX_TEMPLATES_VM_ALLOWED = s.ArrayField(label='MON_ZABBIX_TEMPLATES_VM_ALLOWED', max_items=128, required=False, help_text=_('List of Zabbix templates that can be used by servers ' 'in this virtual datacenter. Available placeholders are:' ' {ostype}, {ostype_text}, {disk_image},' ' {disk_image_abbr}, {dc_name}.')) MON_ZABBIX_TEMPLATES_VM_NIC = s.ArrayField(label='MON_ZABBIX_TEMPLATES_VM_NIC', max_items=16, required=False, help_text=_('List of Zabbix templates that will be used for all ' 'monitored servers, for every virtual NIC of a server. ' 'Available placeholders are: {net}, {nic_id} + ' 'MON_ZABBIX_TEMPLATES_VM placeholders.')) MON_ZABBIX_TEMPLATES_VM_DISK = s.ArrayField(label='MON_ZABBIX_TEMPLATES_VM_DISK', max_items=16, required=False, help_text=_('List of Zabbix templates that will be used for all ' 'monitored servers, for every virtual disk of a server. ' 'Available placeholders: {disk}, {disk_id} + ' 'MON_ZABBIX_TEMPLATES_VM placeholders.')) MON_ZABBIX_HOST_VM_PROXY = s.CharField(label='MON_ZABBIX_HOST_VM_PROXY', min_length=1, max_length=128, required=False, help_text=_('Name or ID of the monitoring proxy, which will be used to ' 'monitor all monitored virtual servers.')) def __init__(self, request, dc, *args, **kwargs): # noinspection PyNoneFunctionAssignment global_settings = self.get_global_settings() if global_settings and not dc.is_default(): # Displaying global settings for non default DC dc1_settings = DefaultDc().settings # These setting should be read-only and read from default DC dc_settings = DefAttrDict(dc.custom_settings, defaults=dc1_settings) # instance else: dc1_settings = None dc_settings = dc.settings # instance self.dc_settings = dc_settings dc_settings['dc'] = dc.name super(DcSettingsSerializer, self).__init__(request, dc_settings, *args, **kwargs) self._update_fields_ = self.fields.keys() self._update_fields_.remove('dc') self.settings = {} self.dc = dc if dc1_settings is not None: for i in global_settings: self.fields[i].read_only = True @classmethod def get_global_settings(cls): if cls._global_settings is None: # noinspection PyUnresolvedReferences cls._global_settings = frozenset(set(cls.base_fields.keys()) - set(DcSettingsSerializer.base_fields.keys())) return cls._global_settings @staticmethod def _filter_sensitive_data(dictionary): """Replace sensitive data in input dict with ***""" for key in dictionary.keys(): if any([i in key for i in SENSITIVE_FIELD_NAMES]): dictionary[key] = SENSITIVE_FIELD_VALUE return dictionary def _setattr(self, instance, source, value): # noinspection PyProtectedMember super(DcSettingsSerializer, self)._setattr(instance, source, value) self.settings[source] = value def detail_dict(self, **kwargs): # Remove sensitive data from detail dict return self._filter_sensitive_data(super(DcSettingsSerializer, self).detail_dict(**kwargs)) @property def data(self): if self._data is None: # Remove sensitive data from output self._data = self._filter_sensitive_data(super(DcSettingsSerializer, self).data) return self._data # noinspection PyPep8Naming def validate_VMS_VM_DOMAIN_DEFAULT(self, attrs, source): if self.dc_settings.DNS_ENABLED: try: value = attrs[source] except KeyError: pass else: try: domain = Domain.objects.get(name=value) except Domain.DoesNotExist: raise s.ValidationError(_('Object with name=%s does not exist.') % value) else: if not self.dc.domaindc_set.filter(domain_id=domain.id).exists(): raise s.ValidationError(_('Domain is not available in this datacenter.')) return attrs # noinspection PyMethodMayBeStatic,PyPep8Naming def validate_VMS_VM_SSH_KEYS_DEFAULT(self, attrs, source): try: value = attrs[source] except KeyError: pass else: for key in value: validate_ssh_key(key) return attrs # noinspection PyMethodMayBeStatic,PyPep8Naming def validate_DNS_PTR_DEFAULT(self, attrs, source): try: value = attrs[source] except KeyError: pass else: testvalue = placeholder_validator(value, ipaddr='test', hostname='test', alias='test') RegexValidator(r'^[a-z0-9][a-z0-9\.-]+[a-z0-9]$')(testvalue) return attrs # noinspection PyMethodMayBeStatic,PyPep8Naming def validate_MON_ZABBIX_HOSTGROUPS_VM(self, attrs, source): return validate_array_placeholders(attrs, source, VM_KWARGS) # noinspection PyMethodMayBeStatic,PyPep8Naming def validate_MON_ZABBIX_HOSTGROUPS_VM_ALLOWED(self, attrs, source): return validate_array_placeholders(attrs, source, VM_KWARGS) # noinspection PyMethodMayBeStatic,PyPep8Naming def validate_MON_ZABBIX_TEMPLATES_VM(self, attrs, source): return validate_array_placeholders(attrs, source, VM_KWARGS) # noinspection PyMethodMayBeStatic,PyPep8Naming def validate_MON_ZABBIX_TEMPLATES_VM_ALLOWED(self, attrs, source): return validate_array_placeholders(attrs, source, VM_KWARGS) # noinspection PyMethodMayBeStatic,PyPep8Naming def validate_MON_ZABBIX_TEMPLATES_VM_NIC(self, attrs, source): return validate_array_placeholders(attrs, source, VM_KWARGS_NIC) # noinspection PyMethodMayBeStatic,PyPep8Naming def validate_MON_ZABBIX_TEMPLATES_VM_DISK(self, attrs, source): return validate_array_placeholders(attrs, source, VM_KWARGS_DISK) def validate(self, attrs): # Check if it is possible to override a boolean setting for source, value in attrs.items(): if source in self._override_disabled_ and not getattr(settings, source, False) and value: self._errors[source] = s.ErrorList([_('Cannot override global setting.')]) del attrs[source] return attrs
class NetworkSerializer(s.ConditionalDCBoundSerializer): """ vms.models.Subnet """ _model_ = Subnet _update_fields_ = ('alias', 'owner', 'access', 'desc', 'network', 'netmask', 'gateway', 'resolvers', 'dns_domain', 'ptr_domain', 'nic_tag', 'vlan_id', 'dc_bound', 'dhcp_passthrough', 'vxlan_id', 'mtu') _default_fields_ = ('name', 'alias', 'owner') _blank_fields_ = frozenset({'desc', 'dns_domain', 'ptr_domain'}) _null_fields_ = frozenset({'gateway'}) # min_length because of API URL: /network/ip/ name = s.RegexField(r'^[A-Za-z0-9][A-Za-z0-9\._-]*$', min_length=3, max_length=32) uuid = s.CharField(read_only=True) alias = s.SafeCharField(max_length=32) owner = s.SlugRelatedField(slug_field='username', queryset=User.objects, required=False) access = s.IntegerChoiceField(choices=Subnet.ACCESS, default=Subnet.PRIVATE) desc = s.SafeCharField(max_length=128, required=False) network = s.IPAddressField() netmask = s.IPAddressField() gateway = s.IPAddressField(required=False) # can be null nic_tag = s.ChoiceField() nic_tag_type = s.CharField(read_only=True) vlan_id = s.IntegerField(min_value=0, max_value=4096) vxlan_id = s.IntegerField(min_value=1, max_value=16777215, required=False) # (2**24 - 1) based on RFC 7348 mtu = s.IntegerField(min_value=576, max_value=9000, required=False) # values from man vmadm resolvers = s.IPAddressArrayField(source='resolvers_api', required=False, max_items=8) dns_domain = s.RegexField(r'^[A-Za-z0-9][A-Za-z0-9\._-]*$', max_length=250, required=False) # can be blank ptr_domain = s.RegexField(r'^[A-Za-z0-9][/A-Za-z0-9\._-]*$', max_length=250, required=False) # can be blank dhcp_passthrough = s.BooleanField(default=False) created = s.DateTimeField(read_only=True, required=False) def __init__(self, request, net, *args, **kwargs): super(NetworkSerializer, self).__init__(request, net, *args, **kwargs) if not kwargs.get('many', False): self._dc_bound = net.dc_bound self.fields['owner'].queryset = get_owners(request, all=True) self.fields['nic_tag'].choices = Node.all_nictags_choices() def _normalize(self, attr, value): if attr == 'dc_bound': return self._dc_bound # noinspection PyProtectedMember return super(NetworkSerializer, self)._normalize(attr, value) def validate_alias(self, attrs, source): try: value = attrs[source] except KeyError: pass else: validate_alias(self.object, value) return attrs def validate_vlan_id(self, attrs, source): try: value = attrs[source] except KeyError: pass else: net = self.object if not net.new: # TODO: Cannot use ip__in=net_ips (ProgrammingError) net_ips = set(net.ipaddress_set.all().values_list('ip', flat=True)) other_ips = set( IPAddress.objects.exclude(subnet=net).filter( subnet__vlan_id=int(value)).values_list('ip', flat=True)) if net_ips.intersection(other_ips): raise s.ValidationError( _('Network has IP addresses that already exist in another ' 'network with the same VLAN ID.')) return attrs # noinspection PyMethodMayBeStatic def validate_ptr_domain(self, attrs, source): try: value = attrs[source] except KeyError: pass else: if value: if not value.endswith('in-addr.arpa'): raise s.ValidationError(_('Invalid PTR domain name.')) if settings.DNS_ENABLED: if not Domain.objects.filter(name=value).exists(): raise s.ObjectDoesNotExist(value) return attrs def validate(self, attrs): # noqa: R701 try: network = attrs['network'] except KeyError: network = self.object.network try: netmask = attrs['netmask'] except KeyError: netmask = self.object.netmask try: vxlan_id = attrs['vxlan_id'] except KeyError: vxlan_id = self.object.vxlan_id try: mtu = attrs['mtu'] except KeyError: mtu = self.object.mtu try: nic_tag = attrs['nic_tag'] except KeyError: nic_tag = self.object.nic_tag try: ip_network = Subnet.get_ip_network(network, netmask) if ip_network.is_reserved: raise ValueError except ValueError: self._errors['network'] = self._errors['netmask'] = \ s.ErrorList([_('Enter a valid IPv4 network and netmask.')]) if self.request.method == 'POST' and self._dc_bound: limit = self._dc_bound.settings.VMS_NET_LIMIT if limit is not None: if Subnet.objects.filter( dc_bound=self._dc_bound).count() >= int(limit): raise s.ValidationError( _('Maximum number of networks reached.')) nic_tag_type = Node.all_nictags()[nic_tag] # retrieve all available nictags and see what is the type of the current nic tag # if type is overlay then vxlan is mandatory argument if nic_tag_type == 'overlay rule': if not vxlan_id: self._errors['vxlan_id'] = s.ErrorList([ _('VXLAN ID is required when an ' 'overlay NIC tag is selected.') ]) else: attrs['vxlan_id'] = None # validate MTU for overlays and etherstubs, and physical nics if nic_tag_type == 'overlay rule': # if MTU was not set for the overlay if not mtu: attrs['mtu'] = 1400 if mtu > 8900: self._errors['mtu'] = s.ErrorList([ s.IntegerField.default_error_messages['max_value'] % { 'limit_value': 8900 } ]) if nic_tag_type in ('normal', 'aggr') and mtu and mtu < 1500: self._errors['mtu'] = s.ErrorList([ s.IntegerField.default_error_messages['min_value'] % { 'limit_value': 1500 } ]) if self._dc_bound: try: vlan_id = attrs['vlan_id'] except KeyError: vlan_id = self.object.vlan_id dc_settings = self._dc_bound.settings if dc_settings.VMS_NET_VLAN_RESTRICT and vlan_id not in dc_settings.VMS_NET_VLAN_ALLOWED: self._errors['vlan_id'] = s.ErrorList( [_('VLAN ID is not available in datacenter.')]) if dc_settings.VMS_NET_VXLAN_RESTRICT and vxlan_id not in dc_settings.VMS_NET_VXLAN_ALLOWED: self._errors['vxlan_id'] = s.ErrorList( [_('VXLAN ID is not available in datacenter.')]) return super(NetworkSerializer, self).validate(attrs) # noinspection PyMethodMayBeStatic def update_errors(self, fields, err_msg): errors = {} for i in fields: errors[i] = s.ErrorList([err_msg]) return errors