summaryrefslogtreecommitdiff
path: root/cloudinit/ec2_utils.py
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@yahoo-inc.com>2014-01-10 17:37:10 -0800
committerJoshua Harlow <harlowja@yahoo-inc.com>2014-01-10 17:37:10 -0800
commit4b70c9a952f0d12a3379cd5c1d930c20f5154134 (patch)
tree4b3bc10e4e7d465155d579f110898d067d67dff1 /cloudinit/ec2_utils.py
parent5cdaaee97ea850adc75a4e88dbd2578c84bded51 (diff)
downloadvyos-cloud-init-4b70c9a952f0d12a3379cd5c1d930c20f5154134.tar.gz
vyos-cloud-init-4b70c9a952f0d12a3379cd5c1d930c20f5154134.zip
Add a maybe_json helper function
Diffstat (limited to 'cloudinit/ec2_utils.py')
-rw-r--r--cloudinit/ec2_utils.py25
1 files changed, 18 insertions, 7 deletions
diff --git a/cloudinit/ec2_utils.py b/cloudinit/ec2_utils.py
index c46adc6b..525059f7 100644
--- a/cloudinit/ec2_utils.py
+++ b/cloudinit/ec2_utils.py
@@ -28,6 +28,17 @@ from cloudinit import util
LOG = logging.getLogger(__name__)
+def maybe_json(text):
+ if not text:
+ return False
+ text = text.strip()
+ if text.startswith("{") and text.endswith("}"):
+ return True
+ if text.startswith("[") and text.endswith("]"):
+ return True
+ return False
+
+
def combine_url(base, add_on):
base_parsed = list(urlparse(base))
path = base_parsed[2]
@@ -94,16 +105,16 @@ class MetadataMaterializer(object):
self._md = self._materialize(self._blob, self._base_url)
return self._md
- def _decode_leaf_blob(self, blob):
+ def _decode_leaf_blob(self, field, blob):
if not blob:
return blob
- stripped_blob = blob.strip()
- if stripped_blob.startswith("{") and stripped_blob.endswith("}"):
- # Assume and try with json
+ if maybe_json(blob):
try:
+ # Assume it's json, unless it fails parsing...
return json.loads(blob)
- except (ValueError, TypeError):
- pass
+ except (ValueError, TypeError) as e:
+ LOG.warn("Field %s looked like json, but it was not: %s",
+ field, e)
if blob.find("\n") != -1:
return blob.splitlines()
return blob
@@ -121,7 +132,7 @@ class MetadataMaterializer(object):
for (field, resource) in leaves.items():
leaf_url = combine_url(base_url, resource)
leaf_blob = str(self._caller(leaf_url))
- leaf_contents[field] = self._decode_leaf_blob(leaf_blob)
+ leaf_contents[field] = self._decode_leaf_blob(field, leaf_blob)
joined = {}
joined.update(child_contents)
for field in leaf_contents.keys():