We have a policy.xml file as below and need to insert a string like below under each session of the xml file under (inbound or backend or outbound) just under <base /> depending on the condition from shell script,
Sample policy.xml
<policies>
<inbound>
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
String want to add
<rate-limit-by-key calls="xx" renewal-period="xx" counter-key="@(Regex.Match(context.x.y.GetValueOrDefault("xxxxxxx",""), @"^[.x-y]*")?.Value)" />`
we tried to use xmlstarlet for this purpose, but couldn't achieve what we are looking for
for example to import the string under [policies/inbound/base]
xmlstarlet edit --subnode "//policies/inbound/" xxxxxxxxxxxxxxxx policy.xml > custompolicy.xml
our Script
#!/bin/bash
################Variables##################
source parse_yaml.sh
eval $(parse_yaml input2.yaml policy)
ipfilter="<rate-limit-by-key calls="xxx" renewal-period="xx" counter-key="@(Regex.Match(context.x.y.GetValueOrDefault("xxxxxxx",""), @"^[.x-y]*")?.Value)" />"
echo ".............Eval Result..............................."
for f in $policy_ ; do eval echo \$f \$${f}_ ; done
echo "............Eval Result................................"
echo " ********policy is ************ "
for f in $policy_ ; do
if [[ $(eval echo \$${f}_name) == "ipfilter" ]]; then
echo " given policy name is ipfilter "
if [[ $(eval echo \$${f}_scope) == "api" ]]; then
echo "decided the scope of Ipfilter policy as api "
fi
for g in $(eval echo \$${f}_apis_); do
echo " this policy will be applied to apis, $g"
for h in $(eval echo \$${g}_session_); do
echo " this policy will be applied to api, $g under sesion $h "
if [[ $(eval echo \$${g}_session_) == "inbound" ]]; then
echo "add the add Ipfilter string to the inbound session of xml"
xmlstarlet edit --subnode "//policies/ibound/" xxxxxxxxxxxxxxxx policy.xml > custompolicy.xml
fi
done
Expected Output for the custom policy file generated for the condition succeeded for "inbound"
<policies>
<inbound>
<base />
<rate-limit-by-key calls="xxx" renewal-period="xx" counter-key="@(Regex.Match(context.x.y.GetValueOrDefault("xxxxxxx",""), @"^[.x-y]*")?.Value)" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>