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

Proposed by Joshua Harlow
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
cloud-init Commiters 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

Makes the jsonschema usage optional

1002. By Joshua Harlow

Fix wording

Revision history for this message
Server Team CI bot (server-team-bot) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
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
=== modified file 'cloudinit/config/__init__.py'
--- cloudinit/config/__init__.py 2013-01-15 21:08:43 +0000
+++ cloudinit/config/__init__.py 2014-08-22 19:02:06 +0000
@@ -19,6 +19,15 @@
19# along with this program. If not, see <http://www.gnu.org/licenses/>.19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20#20#
2121
22import functools
23
24try:
25 import jsonschema
26 VALIDATION_AVAILABLE = True
27except ImportError:
28 VALIDATION_AVAILABLE = False
29
30
22from cloudinit.settings import (PER_INSTANCE, FREQUENCIES)31from cloudinit.settings import (PER_INSTANCE, FREQUENCIES)
2332
24from cloudinit import log as logging33from cloudinit import log as logging
@@ -31,6 +40,27 @@
31# name in the lookup path...40# name in the lookup path...
32MOD_PREFIX = "cc_"41MOD_PREFIX = "cc_"
3342
43# Special jsonschema validation types/adjustments.
44_SCHEMA_TYPES = {
45 # See: https://github.com/Julian/jsonschema/issues/148
46 'array': (list, tuple),
47}
48
49
50def validator(schema):
51
52 def decorator(handler):
53
54 @functools.wraps(handler)
55 def wrapper(name, cfg, cloud, log, args):
56 if VALIDATION_AVAILABLE:
57 jsonschema.validate(cfg, schema, types=_SCHEMA_TYPES)
58 return handler(name, cfg, cloud, log, args)
59
60 return wrapper
61
62 return decorator
63
3464
35def form_module_name(name):65def form_module_name(name):
36 canon_name = name.replace("-", "_")66 canon_name = name.replace("-", "_")
3767
=== modified file 'cloudinit/config/cc_final_message.py'
--- cloudinit/config/cc_final_message.py 2014-03-12 14:59:13 +0000
+++ cloudinit/config/cc_final_message.py 2014-08-22 19:02:06 +0000
@@ -18,6 +18,7 @@
18# You should have received a copy of the GNU General Public License18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.19# along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
21from cloudinit import config
21from cloudinit import templater22from cloudinit import templater
22from cloudinit import util23from cloudinit import util
23from cloudinit import version24from cloudinit import version
@@ -30,7 +31,17 @@
30FINAL_MESSAGE_DEF = ("Cloud-init v. ${version} finished at ${timestamp}."31FINAL_MESSAGE_DEF = ("Cloud-init v. ${version} finished at ${timestamp}."
31 " Datasource ${datasource}. Up ${uptime} seconds")32 " Datasource ${datasource}. Up ${uptime} seconds")
3233
3334SCHEMA = {
35 "type": "object",
36 'properties': {
37 'final_message': {
38 "type": "string",
39 },
40 },
41}
42
43
44@config.validator(SCHEMA)
34def handle(_name, cfg, cloud, log, args):45def handle(_name, cfg, cloud, log, args):
3546
36 msg_in = ''47 msg_in = ''
3748
=== modified file 'requirements.txt'
--- requirements.txt 2014-03-05 23:05:59 +0000
+++ requirements.txt 2014-08-22 19:02:06 +0000
@@ -32,3 +32,8 @@
3232
33# For patching pieces of cloud-config together33# For patching pieces of cloud-config together
34jsonpatch34jsonpatch
35
36# For validating config modules desired configuration (optional, if not
37# avaiable then validation will not occur and config modules will be passed
38# the unvalidated configuration).
39jsonschema