本文整理汇总了Python中middlewared.service.ValidationErrors.extend方法的典型用法代码示例。如果您正苦于以下问题:Python ValidationErrors.extend方法的具体用法?Python ValidationErrors.extend怎么用?Python ValidationErrors.extend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类middlewared.service.ValidationErrors
的用法示例。
在下文中一共展示了ValidationErrors.extend方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate_attributes
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import extend [as 别名]
def validate_attributes(schema, data, additional_attrs=False):
verrors = ValidationErrors()
schema = Dict("attributes", *schema, additional_attrs=additional_attrs)
try:
data["attributes"] = schema.clean(data["attributes"])
except Error as e:
verrors.add(e.attribute, e.errmsg, e.errno)
try:
schema.validate(data["attributes"])
except ValidationErrors as e:
verrors.extend(e)
return verrors
示例2: validate_attributes
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import extend [as 别名]
def validate_attributes(schema, data, additional_attrs=False, attr_key="attributes"):
from middlewared.schema import Dict, Error
from middlewared.service import ValidationErrors
verrors = ValidationErrors()
schema = Dict("attributes", *schema, additional_attrs=additional_attrs)
try:
data[attr_key] = schema.clean(data[attr_key])
except Error as e:
verrors.add(e.attribute, e.errmsg, e.errno)
try:
schema.validate(data[attr_key])
except ValidationErrors as e:
verrors.extend(e)
return verrors
示例3: do_update
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import extend [as 别名]
async def do_update(self, data):
"""
Update S3 Service Configuration.
`access_key` must only contain alphanumeric characters and should be between 5 and 20 characters.
`secret_key` must only contain alphanumeric characters and should be between 8 and 40 characters.
`browser` when set, enables the web user interface for the S3 Service.
`certificate` is a valid certificate id which exists in the system. This is used to enable secure
S3 connections.
"""
old = await self.config()
new = old.copy()
new.update(data)
verrors = ValidationErrors()
for attr, minlen, maxlen in (
('access_key', 5, 20),
('secret_key', 8, 40),
):
curlen = len(new.get(attr, ''))
if curlen < minlen or curlen > maxlen:
verrors.add(
f's3_update.{attr}', f'Attribute should be {minlen} to {maxlen} in length'
)
if not new['storage_path']:
verrors.add('s3_update.storage_path', 'Storage path is required')
else:
await check_path_resides_within_volume(
verrors, self.middleware, 's3_update.storage_path', new['storage_path']
)
if not verrors:
if new['storage_path'].rstrip('/').count('/') < 3:
verrors.add(
's3_update.storage_path',
'Top level datasets are not allowed. i.e /mnt/tank/dataset is allowed'
)
else:
# If the storage_path does not exist, let's create it
if not os.path.exists(new['storage_path']):
os.makedirs(new['storage_path'])
if new['certificate']:
verrors.extend((await self.middleware.call(
'certificate.cert_services_validation', new['certificate'], 's3_update.certificate', False
)))
if verrors:
raise verrors
new['disks'] = new.pop('storage_path')
await self._update_service(old, new)
if (await self.middleware.call('filesystem.stat', new['disks']))['user'] != 'minio':
await self.middleware.call('notifier.winacl_reset', new['disks'], 'minio', 'minio')
return await self.config()
示例4: validate_general_settings
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import extend [as 别名]
async def validate_general_settings(self, data, schema):
verrors = ValidationErrors()
language = data.get('language')
if language:
system_languages = self.language_choices()
if language not in system_languages.keys():
verrors.add(
f'{schema}.language',
f'Specified "{language}" language not found, kindly correct it'
)
# kbd map needs work
timezone = data.get('timezone')
if timezone:
timezones = await self.timezone_choices()
if timezone not in timezones:
verrors.add(
f'{schema}.timezone',
'Please select a correct timezone'
)
ip_addresses = await self.middleware.call(
'interface.ip_in_use'
)
ip4_addresses_list = [alias_dict['address'] for alias_dict in ip_addresses if alias_dict['type'] == 'INET']
ip6_addresses_list = [alias_dict['address'] for alias_dict in ip_addresses if alias_dict['type'] == 'INET6']
ip4_addresses = data.get('ui_address')
for ip4_address in ip4_addresses:
if (
ip4_address and
ip4_address != '0.0.0.0' and
ip4_address not in ip4_addresses_list
):
verrors.add(
f'{schema}.ui_address',
f'{ip4_address} ipv4 address is not associated with this machine'
)
ip6_addresses = data.get('ui_v6address')
for ip6_address in ip6_addresses:
if (
ip6_address and
ip6_address != '::' and
ip6_address not in ip6_addresses_list
):
verrors.add(
f'{schema}.ui_v6address',
f'{ip6_address} ipv6 address is not associated with this machine'
)
for key, wildcard, ips in [('ui_address', '0.0.0.0', ip4_addresses), ('ui_v6address', '::', ip6_addresses)]:
if wildcard in ips and len(ips) > 1:
verrors.add(
f'{schema}.{key}',
f'When "{wildcard}" has been selected, selection of other addresses is not allowed'
)
syslog_server = data.get('syslogserver')
if syslog_server:
match = re.match(r"^[\w\.\-]+(\:\d+)?$", syslog_server)
if not match:
verrors.add(
f'{schema}.syslogserver',
'Invalid syslog server format'
)
elif ':' in syslog_server:
port = int(syslog_server.split(':')[-1])
if port < 0 or port > 65535:
verrors.add(
f'{schema}.syslogserver',
'Port specified should be between 0 - 65535'
)
certificate_id = data.get('ui_certificate')
cert = await self.middleware.call(
'certificate.query',
[["id", "=", certificate_id]]
)
if not cert:
verrors.add(
f'{schema}.ui_certificate',
'Please specify a valid certificate which exists in the system'
)
else:
cert = cert[0]
verrors.extend(
await self.middleware.call(
'certificate.cert_services_validation', certificate_id, f'{schema}.ui_certificate', False
)
)
if cert['fingerprint']:
syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_USER)
syslog.syslog(syslog.LOG_ERR, 'Fingerprint of the certificate used in UI : ' + cert['fingerprint'])
syslog.closelog()
return verrors
示例5: validate_attrs
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import extend [as 别名]
async def validate_attrs(self, data):
verrors = ValidationErrors()
additional_params = data.get('additional_params')
if additional_params:
# Let's be very generic here and introduce very basic validation
# Expected format is as following
# [ipv6.icmpneighbor]
# history = 86400
# enabled = yes
#
# While we are here, we will also introduce basic formatting to the file to ensure
# that we can make it as compliable as possible
param_str = ''
for i in additional_params.split('\n'):
i = i.strip()
if not i:
continue
if i.startswith('#'):
# Let's not validate this
if i.replace('#', '').startswith('['):
param_str += f'\n\n{i}'
else:
param_str += f'\n\t{i}'
continue
if i.startswith('[') and not i.endswith(']'):
verrors.add(
'netdata_update.additional_params',
f'Please correct format for {i}. i.e [system.intr]'
)
elif not i.startswith('[') and '=' not in i:
verrors.add(
'netdata_update.additional_params',
f'Please correct format for {i}. i.e enabled = yes'
)
if i.startswith('['):
param_str += f'\n\n{i}'
else:
param_str += f'\n\t{i}'
data['additional_params'] = param_str + '\n'
bind_to_ips = data.get('bind')
if bind_to_ips:
valid_ips = [ip['address'] for ip in await self.middleware.call('interface.ip_in_use')]
valid_ips.extend(['127.0.0.1', '::1', '0.0.0.0', '::'])
for bind_ip in bind_to_ips:
if bind_ip not in valid_ips:
verrors.add(
'netdata_update.bind',
f'Invalid {bind_ip} bind IP'
)
else:
verrors.add(
'netdata_update.bind',
'This field is required'
)
update_alarms = data.pop('update_alarms', {})
valid_alarms = self._alarms
if update_alarms:
for alarm in update_alarms:
if alarm not in valid_alarms:
verrors.add(
'netdata_update.alarms',
f'{alarm} not a valid alarm'
)
verrors.extend(
validate_attributes(
[Dict(key, Bool('enabled', required=True)) for key in update_alarms],
{'attributes': update_alarms}
)
)
# Validating streaming metrics now
stream_mode = data.get('stream_mode')
if stream_mode == 'SLAVE':
for key in ('api_key', 'destination'):
if not data.get(key):
verrors.add(
f'netdata_update.{key}',
f'{key} is required with stream mode as SLAVE'
)
destinations = data.get('destination')
if destinations:
ip_addr = IpAddress()
port = Port()
for dest in destinations:
ip = dest.split(':')[0]
try:
ip_addr(ip)
except ValueError as e:
verrors.add(
#.........这里部分代码省略.........