Browse code

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

Dean Troyer authored on 2013/08/31 05:11:22
Showing 1 changed files
... ...
@@ -1645,6 +1645,37 @@ vercmp_numbers() {
1645 1645
 }
1646 1646
 
1647 1647
 
1648
+# ``policy_add policy_file policy_name policy_permissions``
1649
+#
1650
+# Add a policy to a policy.json file
1651
+# Do nothing if the policy already exists
1652
+
1653
+function policy_add() {
1654
+    local policy_file=$1
1655
+    local policy_name=$2
1656
+    local policy_perm=$3
1657
+
1658
+    if grep -q ${policy_name} ${policy_file}; then
1659
+        echo "Policy ${policy_name} already exists in ${policy_file}"
1660
+        return
1661
+    fi
1662
+
1663
+    # Add a terminating comma to policy lines without one
1664
+    # Remove the closing '}' and all lines following to the end-of-file
1665
+    local tmpfile=$(mktemp)
1666
+    uniq ${policy_file} | sed -e '
1667
+        s/]$/],/
1668
+        /^[}]/,$d
1669
+    ' > ${tmpfile}
1670
+
1671
+    # Append policy and closing brace
1672
+    echo "    \"${policy_name}\": ${policy_perm}" >>${tmpfile}
1673
+    echo "}" >>${tmpfile}
1674
+
1675
+    mv ${tmpfile} ${policy_file}
1676
+}
1677
+
1678
+
1648 1679
 # Restore xtrace
1649 1680
 $XTRACE
1650 1681