Preserve data types in Hiera where possible

We didn't preserve the data types from Heat metadata when writing hiera
files. We treated the metadata as raw YAML, unless it was multiline, in
which case we wrapped it in quotes.

This commit fixes those things as much as possible:

* Proper JSON escaping is preserved for multi-line strings.

* Other data types such as arrays and hashes are properly passed into
  hiera, regardless if their string representation would be single-line
  or multi-line.

However, single-line strings are still treated as raw YAML, because
tripleo-heat-templates already depend on that behavior (instead of
having arrays in Heat metadata, we have strings there formatted as
arrays).

Change-Id: Ie984f3c7782687235cdc2d72ef1f94af89dc3ed4
Closes-Bug: #1462369
This commit is contained in:
Jiri Stransky 2015-06-05 15:26:23 +02:00
parent 1b6bfa4965
commit 5d2d14f690

View File

@ -37,10 +37,22 @@ function write_mapped_data() {
for i in $(seq 0 $COUNT); do
local KEY=$(jq -r ".[$i]" <<< $HIERA_DATAMAP_KEYS)
local VALUE=$(jq -r -a ".[\"$KEY\"]" <<< $HIERA_DATA)
# Quote multi-line strings for YAML
if [ $(echo -ne "$VALUE" | grep -c '$') -gt 1 ]; then
echo "$KEY: '$VALUE'" >> $filename
local TYPE=$(jq -r ".[\"$KEY\"] | type" <<< $HIERA_DATA)
local VALUE=$(jq -a ".[\"$KEY\"]" <<< $HIERA_DATA)
# FIXME: We should pass data types unchanged from Heat metadata to
# hiera. For now we need to treat single-line strings as raw data
# because we already depend on this in tripleo-heat-templates
# (e.g. we generate strings which look like arrays and depend on
# them being processed as real arrays in hiera).
if [ "$TYPE" = "string" ]; then
local RAW_VALUE=$(jq -r -a ".[\"$KEY\"]" <<< $HIERA_DATA)
# Treat single-line strings as raw data
if [ $(echo -ne "$RAW_VALUE" | grep -c '$') -gt 1 ]; then
echo "$KEY: $VALUE" >> $filename
else
echo "$KEY: $RAW_VALUE" >> $filename
fi
else
echo "$KEY: $VALUE" >> $filename
fi