Skip to content

Instantly share code, notes, and snippets.

@dosaboy
Last active November 23, 2018 10:10
Show Gist options
  • Select an option

  • Save dosaboy/fbd7849cb944491d9ae369f406437f92 to your computer and use it in GitHub Desktop.

Select an option

Save dosaboy/fbd7849cb944491d9ae369f406437f92 to your computer and use it in GitHub Desktop.
#!/bin/bash -eu
#
# Origin: https://gist.github.com/dosaboy/fbd7849cb944491d9ae369f406437f92
#
# Authors:
# - [email protected]
# - [email protected]
#
# NOTE: https://bugs.launchpad.net/charm-designate/+bug/1804022
MAX_RETRIES=50
create_zone ()
{
zone=$1
payload="`cat << EOF | python | sed 's/"/\\"/g'
import json
zone = { "name": "$zone", "email": "joe@${zone%%\.}", "type": "PRIMARY", "ttl": 3600 }
print json.dumps(zone)
EOF`"
resp=`curl -s -X POST -H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" -d "$payload" ${d_ep}/v2/zones`
id="`echo $resp| jq -r .id`"
if [ "$id" = "null" ]; then
echo "Create zone '$zone' failed with response:" > $ERRTMPD/$zone
echo $resp| jq . >> $ERRTMPD/$zone
return 0
fi
status="`echo $resp| jq -r .status`"
retry=$MAX_RETRIES
while [ "$status" != "ACTIVE" ] && ((--retry)); do
sleep 1
status=`curl -s -X GET -H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" ${d_ep}/v2/zones/$id| jq -r '.status'`
done
if [ "$status" = "ACTIVE" ]; then
((retry<$MAX_RETRIES)) && echo "Zone $id $status after $(($MAX_RETRIES-retry)) re-checks ($zone)" 1>&2
else
echo "Zone $id still has status $status after $(($MAX_RETRIES-retry)) re-checks ($zone)" 1>&2
fi
echo $id
}
delete_zone ()
{
zone=$1
resp=`curl -s -X DELETE -H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" ${d_ep}/v2/zones/$zone`
status="`echo $resp| jq -r .status`"
retry=$MAX_RETRIES
while [ "$status" = "PENDING" ] && ((--retry)); do
sleep 1
status=`curl -s -X GET -H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" ${d_ep}/v2/zones/$zone| jq -r '.status'`
done
((retry)) || echo "delete zone $zone failed"
}
update_quota ()
{
project=$1
payload="`cat << EOF | python | sed 's/"/\\"/g'
import json
quota = { "zones": "100", "recordset_records": "100" }
print json.dumps(quota)
EOF`"
curl -s -X PATCH -H "X-Auth-Token: $TOKEN" -H "X-Auth-All-Projects: True" -H "Content-Type: application/json" -d "$payload" ${d_ep}/v2/quotas/$project| jq .
}
cleanup ()
{
readarray -t zones<<<"`curl -s -X GET -H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" ${d_ep}/v2/zones/?limit=10000000000| jq -r .zones[].id`"
[ -n "${zones[0]}" ] || zones=()
echo "${#zones[@]} zone(s) to delete"
for z in ${zones[@]}; do
(
echo "deleting zone $z"
delete_zone $z
) &
done
wait 2>/dev/null
curl -s -X GET -H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" ${d_ep}/v2/zones?limit=10000000000| jq .zones[].status
}
authenticate ()
{
echo "Obtaining token from keystone"
. novarc
TOKEN="`openstack token issue| grep ' id '| awk '{print $4}'`"
d_ep="`curl -s -XGET -H "X-Auth-Token: $TOKEN" "$OS_AUTH_URL/auth/catalog"| jq --raw-output '.catalog[] | select(.name | contains("designate")).endpoints[] | select(.interface | contains("admin")).url'`"
echo "Designate endpoint is $d_ep"
}
do_checks()
{
# check that default pool has at least one ns_record
curl -s -X GET -H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" ${d_ep}/v2/pools| jq .
# check that our quota allows the amount of zones we want to create
curl -s -X GET -H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" ${d_ep}/v2/quotas| jq .
# check zones
curl -s -X GET -H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" ${d_ep}/v2/zones/?limit=10000000000| jq .
}
do_test ()
{
echo "Running test"
num_zones=`curl -s -X GET -H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" ${d_ep}/v2/quotas| jq .zones`
echo "Doing create-then-delete for $num_zones zones"
ERRTMPD=`mktemp -d`
for ((i=0; i<${1:-$num_zones};i++)); do
(
zone=testzone${i}.org.
id=`create_zone $zone`
if [ -z "$id" ]; then
cat $ERRTMPD/$zone
continue
fi
delete_zone $id
) &
done
rm -rf $errtmpd
wait 2>/dev/null
}
authenticate
do_test
# keep running this until they are all gone (assuming none failed)
curl -s -X GET -H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" ${d_ep}/v2/zones/?limit=10000000000| jq -r '.zones[] | .name, .id, .action, .status'
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment