From 533e14d6a5fc1ba3dbd24fb0075ef1eafd00a705 Mon Sep 17 00:00:00 2001 From: Dean Troyer Date: Fri, 30 Aug 2013 15:11:22 -0500 Subject: [PATCH] Copy policy_add() from Grenade functions policy_all() was added to Grenade's functions file, which is notmally synced from DevStack so we need to bring it over here before the next sync. Change-Id: Ifd852e9d1ffe39fa23f6312d1ddf2874b5f2b9f0 --- functions | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/functions b/functions index f24cc89e82..14e817c999 100644 --- a/functions +++ b/functions @@ -1645,6 +1645,37 @@ vercmp_numbers() { } +# ``policy_add policy_file policy_name policy_permissions`` +# +# Add a policy to a policy.json file +# Do nothing if the policy already exists + +function policy_add() { + local policy_file=$1 + local policy_name=$2 + local policy_perm=$3 + + if grep -q ${policy_name} ${policy_file}; then + echo "Policy ${policy_name} already exists in ${policy_file}" + return + fi + + # Add a terminating comma to policy lines without one + # Remove the closing '}' and all lines following to the end-of-file + local tmpfile=$(mktemp) + uniq ${policy_file} | sed -e ' + s/]$/],/ + /^[}]/,$d + ' > ${tmpfile} + + # Append policy and closing brace + echo " \"${policy_name}\": ${policy_perm}" >>${tmpfile} + echo "}" >>${tmpfile} + + mv ${tmpfile} ${policy_file} +} + + # Restore xtrace $XTRACE