This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
/
configuration.py
156 lines (133 loc) · 5.34 KB
/
configuration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from gettext import gettext as _
import logging
import os
_LOG = logging.getLogger(__name__)
class NonEmptyValidation(object):
"""
Validates that the value is not None
"""
def __call__(self, value, config):
"""
:param value: value to validate
:type value: any
:param config: distributor config
:type config: PulpCallConfig object
:return: tuple indicating whether config value validates and error message or None
:rtype: (bool, str) or (bool, None)
"""
if value:
return (True, None)
else:
return (False, self._err(value))
def _err(self, value):
"""
:param value: value that did not pass validation
:type value: any
:return: error message
:rtype: str
"""
return _("attribute cannot be empty")
class TypeValidation(object):
"""
Validates that the value is one of allowed types
"""
def __init__(self, allowed_types):
"""
:param allowed_types: list of valid types
:type allowed_types: list of types
"""
self.allowed_types = allowed_types
def __call__(self, value, config):
"""
:param value: value to validate
:type value: any
:param config: distributor config
:type config: PulpCallConfig object
:return: tuple indicating whether config value validates and error message or None
:rtype: (bool, str) or (bool, None)
"""
if all(issubclass(type(value), _type) for _type in self.allowed_types):
return (True, None)
else:
return (False, self._err(value))
def _err(self, value):
"""
:param value: value that did not pass validation
:type value: any
:return: error message
:rtype: str
"""
params = {'type': type(value), 'allowed_types': ", ".join(self.allowed_types)}
return _("%(type)s type is not one of allowed types: %(allowed_types)s") % params
class RelativePathValidation(object):
"""
Validates that a path does not start with a forward slash.
"""
def __call__(self, value, *args):
"""
:param value: path to validate
:param args: any extra args that get passed in but are not needed for validation
:return: tuple indicating whether config value validates and error message or None
:rtype: (bool, str) or (bool, None)
"""
if os.path.isabs(value):
return (False, self._err(value))
else:
return (True, None)
def _err(self, value):
"""
:param value: value that did not pass validation
:type value: any
:return: error message
:rtype: str
"""
return _("attribute cannot start with a /")
REMOTE_MANDATORY_KEYS = {
"ssh_identity_file": [TypeValidation([basestring]), NonEmptyValidation()],
"ssh_user": [TypeValidation([basestring]), NonEmptyValidation()],
"host": [TypeValidation([basestring]), NonEmptyValidation()],
"root": [TypeValidation([basestring]), NonEmptyValidation()]
}
REMOTE_OPTIONAL_KEYS = {
"remote_units_path": [TypeValidation([basestring]), RelativePathValidation()]
}
def validate_config(repo, config, config_conduit):
"""
Validate the prospective configuration instance for the given repository.
:param repo: repository to validate the config for
:type repo: pulp.plugins.model.Repository
:param config: configuration instance to validate
:type config: pulp.plugins.config.PluginCallConfiguration
:param config_conduit: conduit providing access to relevant Pulp functionality
:type config_conduit: pulp.plugins.conduits.repo_config.RepoConfigConduit
:return: tuple of (bool, str) stating that the configuration is valid or not and why
:rtype: tuple of (bool, str) or (bool, None)
"""
_config = config.flatten() # now config is {}
err_list = []
if "remote" not in _config or ("remote" in _config and not isinstance(_config["remote"], dict)):
err_list.append("'remote' dict missing in distributor's configuration")
else:
missing_attr_tmpl = _("'%(attribute)s' is missing in 'remote' section of distributor's '"
"configuration")
for attr, validations in REMOTE_MANDATORY_KEYS.iteritems():
if attr not in _config["remote"]:
err_list.append(missing_attr_tmpl % {'attribute': attr})
continue
for validation in validations:
succeed, _err = validation(_config["remote"][attr], _config)
if not succeed:
err_list.append(_("%(attribute)s : %(error)s") % {'attribute': attr,
'error': _err})
for attr, validations in REMOTE_OPTIONAL_KEYS.iteritems():
if attr not in _config["remote"]:
continue
for validation in validations:
succeed, _err = validation(_config["remote"][attr], _config)
if not succeed:
err_list.append(_("%(attribute)s : %(error)s") % {'attribute': attr,
'error': _err})
if err_list:
return (False, "\n".join(err_list))
else:
return (True, None)