← Back to team overview

cloud-init-dev team mailing list archive

[Merge] lp:~harlowja/cloud-init/schema-validate into lp:cloud-init

 

Joshua Harlow has proposed merging lp:~harlowja/cloud-init/schema-validate into lp:cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~harlowja/cloud-init/schema-validate/+merge/231950

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.
-- 
https://code.launchpad.net/~harlowja/cloud-init/schema-validate/+merge/231950
Your team cloud init development team is requested to review the proposed merge of lp:~harlowja/cloud-init/schema-validate into lp:cloud-init.
=== modified file 'cloudinit/config/__init__.py'
--- cloudinit/config/__init__.py	2013-01-15 21:08:43 +0000
+++ cloudinit/config/__init__.py	2014-08-22 18:49:06 +0000
@@ -19,6 +19,10 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
+import functools
+
+import jsonschema
+
 from cloudinit.settings import (PER_INSTANCE, FREQUENCIES)
 
 from cloudinit import log as logging
@@ -31,6 +35,26 @@
 # name in the lookup path...
 MOD_PREFIX = "cc_"
 
+# Special jsonschema validation types/adjustments.
+_SCHEMA_TYPES = {
+    # See: https://github.com/Julian/jsonschema/issues/148
+    'array': (list, tuple),
+}
+
+
+def validator(schema):
+
+    def decorator(handler):
+
+        @functools.wraps(handler)
+        def wrapper(name, cfg, cloud, log, args):
+            jsonschema.validate(cfg, schema, types=_SCHEMA_TYPES)
+            return handler(name, cfg, cloud, log, args)
+
+        return wrapper
+
+    return decorator
+
 
 def form_module_name(name):
     canon_name = name.replace("-", "_")

=== 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 18:49:06 +0000
@@ -18,6 +18,7 @@
 #    You should have received a copy of the GNU General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from cloudinit import config
 from cloudinit import templater
 from cloudinit import util
 from cloudinit import version
@@ -30,7 +31,17 @@
 FINAL_MESSAGE_DEF = ("Cloud-init v. ${version} finished at ${timestamp}."
                      " Datasource ${datasource}.  Up ${uptime} seconds")
 
-
+SCHEMA = {
+    "type": "object",
+    'properties': {
+        'final_message': {
+            "type": "string",
+        },
+    },
+}
+
+
+@config.validator(SCHEMA)
 def handle(_name, cfg, cloud, log, args):
 
     msg_in = ''

=== modified file 'requirements.txt'
--- requirements.txt	2014-03-05 23:05:59 +0000
+++ requirements.txt	2014-08-22 18:49:06 +0000
@@ -32,3 +32,6 @@
 
 # For patching pieces of cloud-config together
 jsonpatch
+
+# For validating config modules desired configuration
+jsonschema


Follow ups