Merge lp:~harlowja/cloud-init/schema-validate into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Joshua Harlow on 2014-08-22
Status: Merged
Merge reported by: Scott Moser
Merged at revision: not available
Proposed branch: lp:~harlowja/cloud-init/schema-validate
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 91 lines (+47/-1)
3 files modified
cloudinit/config/__init__.py (+30/-0)
cloudinit/config/cc_final_message.py (+12/-1)
requirements.txt (+5/-0)
To merge this branch: bzr merge lp:~harlowja/cloud-init/schema-validate
Reviewer Review Type Date Requested Status
Server Team CI bot continuous-integration Needs Fixing on 2016-07-22
cloud-init Commiters 2014-08-22 Pending
Review via email: mp+231950@code.launchpad.net

Description of the change

Add a tiny initial usage of jsonschema to validate config modules input configuration before handlers start using it via a decorator that handlers can choose or not choose to use on there handle() function.

To post a comment you must log in.
1001. By Joshua Harlow on 2014-08-22

Makes the jsonschema usage optional

1002. By Joshua Harlow on 2014-08-22

Fix wording

review: Needs Fixing (continuous-integration)
Scott Moser (smoser) wrote :

Hello,
Thank you for taking the time to contribute to cloud-init. Cloud-init has moved its revision control system to git. As a result, we are marking all bzr merge proposals as 'rejected'. If you would like to re-submit this proposal for review, please do so by following the current HACKING documentation at http://cloudinit.readthedocs.io/en/latest/topics/hacking.html .

I'm going to mark this as 'merged', because I think it is actually
upstream at
https://git.launchpad.net/cloud-init/commit/?id=0a448dd034

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cloudinit/config/__init__.py'
2--- cloudinit/config/__init__.py 2013-01-15 21:08:43 +0000
3+++ cloudinit/config/__init__.py 2014-08-22 19:02:06 +0000
4@@ -19,6 +19,15 @@
5 # along with this program. If not, see <http://www.gnu.org/licenses/>.
6 #
7
8+import functools
9+
10+try:
11+ import jsonschema
12+ VALIDATION_AVAILABLE = True
13+except ImportError:
14+ VALIDATION_AVAILABLE = False
15+
16+
17 from cloudinit.settings import (PER_INSTANCE, FREQUENCIES)
18
19 from cloudinit import log as logging
20@@ -31,6 +40,27 @@
21 # name in the lookup path...
22 MOD_PREFIX = "cc_"
23
24+# Special jsonschema validation types/adjustments.
25+_SCHEMA_TYPES = {
26+ # See: https://github.com/Julian/jsonschema/issues/148
27+ 'array': (list, tuple),
28+}
29+
30+
31+def validator(schema):
32+
33+ def decorator(handler):
34+
35+ @functools.wraps(handler)
36+ def wrapper(name, cfg, cloud, log, args):
37+ if VALIDATION_AVAILABLE:
38+ jsonschema.validate(cfg, schema, types=_SCHEMA_TYPES)
39+ return handler(name, cfg, cloud, log, args)
40+
41+ return wrapper
42+
43+ return decorator
44+
45
46 def form_module_name(name):
47 canon_name = name.replace("-", "_")
48
49=== modified file 'cloudinit/config/cc_final_message.py'
50--- cloudinit/config/cc_final_message.py 2014-03-12 14:59:13 +0000
51+++ cloudinit/config/cc_final_message.py 2014-08-22 19:02:06 +0000
52@@ -18,6 +18,7 @@
53 # You should have received a copy of the GNU General Public License
54 # along with this program. If not, see <http://www.gnu.org/licenses/>.
55
56+from cloudinit import config
57 from cloudinit import templater
58 from cloudinit import util
59 from cloudinit import version
60@@ -30,7 +31,17 @@
61 FINAL_MESSAGE_DEF = ("Cloud-init v. ${version} finished at ${timestamp}."
62 " Datasource ${datasource}. Up ${uptime} seconds")
63
64-
65+SCHEMA = {
66+ "type": "object",
67+ 'properties': {
68+ 'final_message': {
69+ "type": "string",
70+ },
71+ },
72+}
73+
74+
75+@config.validator(SCHEMA)
76 def handle(_name, cfg, cloud, log, args):
77
78 msg_in = ''
79
80=== modified file 'requirements.txt'
81--- requirements.txt 2014-03-05 23:05:59 +0000
82+++ requirements.txt 2014-08-22 19:02:06 +0000
83@@ -32,3 +32,8 @@
84
85 # For patching pieces of cloud-config together
86 jsonpatch
87+
88+# For validating config modules desired configuration (optional, if not
89+# avaiable then validation will not occur and config modules will be passed
90+# the unvalidated configuration).
91+jsonschema