本文整理汇总了Python中middlewared.service.ValidationErrors.add方法的典型用法代码示例。如果您正苦于以下问题:Python ValidationErrors.add方法的具体用法?Python ValidationErrors.add怎么用?Python ValidationErrors.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类middlewared.service.ValidationErrors
的用法示例。
在下文中一共展示了ValidationErrors.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: common_validation
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def common_validation(self, data, schema_name):
verrors = ValidationErrors()
ip = data.get('ip')
if ip:
await resolve_hostname(self.middleware, verrors, f'{schema_name}.ip', ip)
management_ip = data.get('management_ip')
if management_ip and management_ip not in (await self.get_management_ip_choices()):
verrors.add(
f'{schema_name}.management_ip',
'Please select a valid IP for your TrueNAS system'
)
action = data.get('action')
if action and action != 'UNINSTALL':
if (
not (await self.middleware.call('vcenteraux.config'))['enable_https'] and
(await self.middleware.call('system.general.config'))['ui_protocol'].upper() == 'HTTPS'
):
verrors.add(
f'{schema_name}.action',
'Please enable vCenter plugin over HTTPS'
)
return verrors
示例2: validate_data
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def validate_data(self, data, schema):
verrors = ValidationErrors()
user = data.get('user')
if user:
# Windows users can have spaces in their usernames
# http://www.freebsd.org/cgi/query-pr.cgi?pr=164808
if ' ' in user:
verrors.add(
f'{schema}.user',
'Usernames cannot have spaces'
)
elif not (
await self.middleware.call(
'notifier.get_user_object',
user
)
):
verrors.add(
f'{schema}.user',
'Specified user does not exist'
)
return verrors, data
示例3: __rquery_to_start_end
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
def __rquery_to_start_end(self, query):
unit = query.get('unit')
if unit:
verrors = ValidationErrors()
for i in ('start', 'end'):
if i in query:
verrors.add(
f'reporting_query.{i}',
f'{i!r} should only be used if "unit" attribute is not provided.',
)
verrors.check()
else:
if 'start' not in query:
unit = 'HOURLY'
else:
starttime = query['start']
endtime = query.get('end') or 'now'
if unit:
unit = unit[0].lower()
page = query['page']
starttime = f'end-{page + 1}{unit}'
if not page:
endtime = 'now'
else:
endtime = f'now-{page}{unit}'
return starttime, endtime
示例4: validate_data
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def validate_data(self, data, schema):
verrors = ValidationErrors()
pool_pk = data.get('pool')
if pool_pk:
pool_obj = await self.middleware.call(
'datastore.query',
'storage.volume',
[('id', '=', pool_pk)]
)
if len(pool_obj) == 0:
verrors.add(
f'{schema}.pool',
'The specified volume does not exist'
)
elif (
'id' not in data.keys() or
(
'id' in data.keys() and
'original_pool_id' in data.keys() and
pool_pk != data['original_pool_id']
)
):
scrub_obj = await self.query(filters=[('pool', '=', pool_pk)])
if len(scrub_obj) != 0:
verrors.add(
f'{schema}.pool',
'A scrub with this pool already exists'
)
return verrors, data
示例5: validate_data
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def validate_data(self, data, schema):
verrors = ValidationErrors()
smart_tests = await self.query(filters=[('type', '=', data['type'])])
configured_disks = [d for test in smart_tests for d in test['disks']]
disks_dict = {disk['identifier']: disk['name'] for disk in (await self.middleware.call('disk.query'))}
disks = data.get('disks')
used_disks = []
invalid_disks = []
for disk in disks:
if disk in configured_disks:
used_disks.append(disks_dict[disk])
if disk not in disks_dict.keys():
invalid_disks.append(disk)
if used_disks:
verrors.add(
f'{schema}.disks',
f'The following disks already have tests for this type: {", ".join(used_disks)}'
)
if invalid_disks:
verrors.add(
f'{schema}.disks',
f'The following disks are invalid: {", ".join(invalid_disks)}'
)
return verrors
示例6: do_update
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def do_update(self, data):
old = await self.config()
new = old.copy()
new.update(data)
verrors = ValidationErrors()
if not new["v4"] and new["v4_v3owner"]:
verrors.add("nfs_update.v4_v3owner", "This option requires enabling NFSv4")
if new["v4_v3owner"] and new["userd_manage_gids"]:
verrors.add(
"nfs_update.userd_manage_gids", "This option is incompatible with NFSv3 ownership model for NFSv4")
if verrors:
raise verrors
self.nfs_compress(new)
await self._update_service(old, new)
self.nfs_extend(new)
return new
示例7: do_update
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def do_update(self, data):
"""
Update Mail Service Configuration.
`fromemail` is used as a sending address which the mail server will use for sending emails.
`outgoingserver` is the hostname or IP address of SMTP server used for sending an email.
`security` is type of encryption desired.
`smtp` is a boolean value which when set indicates that SMTP authentication has been enabled and `user`/`pass`
are required attributes now.
"""
config = await self.config()
new = config.copy()
new.update(data)
new['security'] = new['security'].lower() # Django Model compatibility
verrors = ValidationErrors()
if new['smtp'] and new['user'] == '':
verrors.add(
'mail_update.user',
'This field is required when SMTP authentication is enabled',
)
self.__password_verify(new['pass'], 'mail_update.pass', verrors)
if verrors:
raise verrors
await self.middleware.call('datastore.update', 'system.email', config['id'], new, {'prefix': 'em_'})
return await self.config()
示例8: do_update
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def do_update(self, id, data):
"""
Update idmap to backend mapping by id.
"""
old = await self._get_instance(id)
new = old.copy()
new.update(data)
new = await self.middleware.call('idmap.common_backend_compress', new)
verrors = ValidationErrors()
if new['domain'] in [dstype.DS_TYPE_LDAP.value, dstype.DS_TYPE_DEFAULT_DOMAIN.value]:
if new['idmap_backend'] not in ['ldap', 'tdb']:
verrors.add(
'domaintobackend_create.idmap_backend',
f'idmap backend [{new["idmap_backend"]}] is not appropriate for the system domain type {dstype[new["domain"]]}'
)
if verrors:
raise verrors
await self.middleware.call(
'datastore.update',
self._config.datastore,
id,
new,
{'prefix': self._config.datastore_prefix}
)
updated_entry = await self._get_instance(id)
try:
await self.middleware.call('idmap.get_or_create_idmap_by_domain', updated_entry['domain']['domain_name'])
except Exception as e:
self.logger.debug('Failed to generate new idmap backend: %s', e)
return updated_entry
示例9: _validate
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def _validate(self, data):
verrors = ValidationErrors()
realms = await self.query()
for realm in realms:
if realm['realm'].upper() == data['realm'].upper():
verrors.add(f'kerberos_realm', f'kerberos realm with name {realm["realm"]} already exists.')
return verrors
示例10: do_update
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def do_update(self, job, data):
config = await self.config()
new = config.copy()
new.update(data)
verrors = ValidationErrors()
if not await self.middleware.call('zfs.pool.query', [('name', '=', data['pool'])]):
verrors.add('sysdataset_update.pool', f'Pool "{data["pool"]}" not found', errno.ENOENT)
if verrors:
raise verrors
new['syslog_usedataset'] = new['syslog']
new['rrd_usedataset'] = new['rrd']
await self.middleware.call('datastore.update', 'system.systemdataset', config['id'], new, {'prefix': 'sys_'})
if 'pool' in data and config['pool'] and data['pool'] != config['pool']:
await self.migrate(config['pool'], data['pool'])
if config['rrd'] != new['rrd']:
# Stop collectd to flush data
await self.middleware.call('service.stop', 'collectd')
await self.setup()
if config['syslog'] != new['syslog']:
await self.middleware.call('service.restart', 'syslogd')
if config['rrd'] != new['rrd']:
await self.rrd_toggle()
await self.middleware.call('service.restart', 'collectd')
return config
示例11: do_create
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
def do_create(self, data):
"""
Creates a ZFS dataset.
"""
verrors = ValidationErrors()
if '/' not in data['name']:
verrors.add('name', 'You need a full name, e.g. pool/newdataset')
if verrors:
raise verrors
properties = data.get('properties') or {}
sparse = properties.pop('sparse', False)
params = {}
for k, v in data['properties'].items():
params[k] = v
try:
with libzfs.ZFS() as zfs:
pool = zfs.get(data['name'].split('/')[0])
pool.create(data['name'], params, fstype=getattr(libzfs.DatasetType, data['type']), sparse_vol=sparse)
except libzfs.ZFSException as e:
self.logger.error('Failed to create dataset', exc_info=True)
raise CallError(f'Failed to create dataset: {e}')
示例12: do_create
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def do_create(self, data):
"""
Creates a dataset/zvol.
`volsize` is required for type=VOLUME and is supposed to be a multiple of the block size.
"""
verrors = ValidationErrors()
if '/' not in data['name']:
verrors.add('pool_dataset_create.name', 'You need a full name, e.g. pool/newdataset')
else:
await self.__common_validation(verrors, 'pool_dataset_create', data, 'CREATE')
if verrors:
raise verrors
props = {}
for i, real_name, transform in (
('atime', None, str.lower),
('casesensitivity', None, str.lower),
('comments', 'org.freenas:description', None),
('compression', None, str.lower),
('copies', None, lambda x: str(x)),
('deduplication', 'dedup', str.lower),
('exec', None, str.lower),
('quota', None, _none),
('readonly', None, str.lower),
('recordsize', None, None),
('refquota', None, _none),
('refreservation', None, _none),
('reservation', None, _none),
('snapdir', None, str.lower),
('sparse', None, None),
('sync', None, str.lower),
('volblocksize', None, None),
('volsize', None, lambda x: str(x)),
):
if i not in data:
continue
name = real_name or i
props[name] = data[i] if not transform else transform(data[i])
await self.middleware.call('zfs.dataset.create', {
'name': data['name'],
'type': data['type'],
'properties': props,
})
data['id'] = data['name']
await self.middleware.call('zfs.dataset.mount', data['name'])
if data['type'] == 'FILESYSTEM':
await self.middleware.call(
'notifier.change_dataset_share_type', data['name'], data.get('share_type', 'UNIX').lower()
)
return await self._get_instance(data['id'])
示例13: add_admin_group
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def add_admin_group(self, admin_group=None, check_deferred=False):
"""
Add a local or directory service group to BUILTIN\\Administrators (S-1-5-32-544)
Members of this group have elevated privileges to the Samba server (ability to
take ownership of files, override ACLs, view and modify user quotas, and administer
the server via the Computer Management MMC Snap-In. Unfortuntely, group membership
must be managed via "net groupmap listmem|addmem|delmem", which requires that
winbind be running when the commands are executed. In this situation, net command
will fail with WBC_ERR_WINBIND_NOT_AVAILABLE. If this error message is returned, then
flag for a deferred command retry when service starts.
@param-in (admin_group): This is the group to add to BUILTIN\\Administrators. If unset, then
look up the value in the config db.
@param-in (check_deferred): If this is True, then only perform the group mapping if this has
been flagged as in need of deferred setup (i.e. Samba wasn't running when it was initially
called). This is to avoid unecessarily calling during service start.
"""
verrors = ValidationErrors()
if check_deferred:
is_deferred = await self.middleware.call('cache.has_key', 'SMB_SET_ADMIN')
if not is_deferred:
self.logger.debug("No cache entry indicating delayed action to add admin_group was found.")
return True
else:
await self.middleware.call('cache.pop', 'SMB_SET_ADMIN')
if not admin_group:
smb = await self.middleware.call('smb.config')
admin_group = smb['admin_group']
# We must use GIDs because wbinfo --name-to-sid expects a domain prefix "FREENAS\user"
group = await self.middleware.call("notifier.get_group_object", admin_group)
if not group:
verrors.add('smb_update.admin_group', f"Failed to validate group: {admin_group}")
raise verrors
sid = await self.wbinfo_gidtosid(group[2])
if sid == "WBC_ERR_WINBIND_NOT_AVAILABLE":
self.logger.debug("Delaying admin group add until winbind starts")
await self.middleware.call('cache.put', 'SMB_SET_ADMIN', True)
return True
must_add_sid = await self.validate_admin_groups(sid)
if not must_add_sid:
return True
proc = await Popen(
['/usr/local/bin/net', 'groupmap', 'addmem', 'S-1-5-32-544', sid],
stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
output = await proc.communicate()
if proc.returncode != 0:
raise CallError(f'net groupmap addmem failed: {output[1].decode()}')
self.logger.debug(f"Successfully added {admin_group} to BUILTIN\\Administrators")
return True
示例14: do_update
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
async def do_update(self, id, data):
"""
Updates a dataset/zvol `id`.
"""
verrors = ValidationErrors()
dataset = await self.middleware.call('pool.dataset.query', [('id', '=', id)])
if not dataset:
verrors.add('id', f'{id} does not exist', errno.ENOENT)
else:
data['type'] = dataset[0]['type']
data['name'] = dataset[0]['name']
if data['type'] == 'VOLUME':
data['volblocksize'] = dataset[0]['volblocksize']['value']
await self.__common_validation(verrors, 'pool_dataset_update', data, 'UPDATE')
if verrors:
raise verrors
props = {}
for i, real_name, transform, inheritable in (
('atime', None, str.lower, True),
('comments', 'org.freenas:description', None, False),
('sync', None, str.lower, True),
('compression', None, str.lower, True),
('deduplication', 'dedup', str.lower, True),
('exec', None, str.lower, True),
('quota', None, _none, False),
('refquota', None, _none, False),
('reservation', None, _none, False),
('refreservation', None, _none, False),
('copies', None, None, False),
('snapdir', None, str.lower, True),
('readonly', None, str.lower, True),
('recordsize', None, None, True),
('volsize', None, lambda x: str(x), False),
):
if i not in data:
continue
name = real_name or i
if inheritable and data[i] == 'INHERIT':
props[name] = {'source': 'INHERIT'}
else:
props[name] = {'value': data[i] if not transform else transform(data[i])}
rv = await self.middleware.call('zfs.dataset.update', id, {'properties': props})
if data['type'] == 'FILESYSTEM' and 'share_type' in data:
await self.middleware.call(
'notifier.change_dataset_share_type', id, data['share_type'].lower()
)
elif data['type'] == 'VOLUME' and 'volsize' in data:
if await self.middleware.call('iscsi.extent.query', [('path', '=', f'zvol/{id}')]):
await self.middleware.call('service.reload', 'iscsitarget')
return rv
示例15: _validate
# 需要导入模块: from middlewared.service import ValidationErrors [as 别名]
# 或者: from middlewared.service.ValidationErrors import add [as 别名]
def _validate(self, schema_name, data):
verrors = ValidationErrors()
if data["provider"] not in REMOTES:
verrors.add(f"{schema_name}.provider", "Invalid provider")
else:
provider = REMOTES[data["provider"]]
attributes_verrors = validate_attributes(provider.credentials_schema, data)
verrors.add_child(f"{schema_name}.attributes", attributes_verrors)
if verrors:
raise verrors