Skip to content

Commit

Permalink
upi/vsphere: Make cidr_to_ip.sh more verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
LorbusChris committed Jul 1, 2020
1 parent dd27e4c commit f5afcdd
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions upi/vsphere/ipam/cidr_to_ip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ function parse_input() {
}

is_ip_address() {
if [[ $1 =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ ]]
then
if [[ $1 =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ ]]; then
echo "true"
else
echo "false"
Expand All @@ -33,22 +32,24 @@ is_ip_address() {

get_reservation() {
reservation=$(curl -s "http://${ipam}/api/getIPs.php?apiapp=address&apitoken=${ipam_token}&domain=${hostname}")
if [[ "${reservation}" == "[]" ]]; then echo ""
if [[ "${reservation}" == "[]" ]]; then
echo "empty"
else
reserved_ip=$(echo "${reservation}" | jq -r ".\"${hostname}\"")
if [ "$(is_ip_address "${reserved_ip}")" == "false" ]; then echo ""
else echo "$reserved_ip"
if [ "$(is_ip_address "${reserved_ip}")" == "false" ]; then
echo "malformed data: ${ip_address}"
else
echo "$reserved_ip"
fi
fi
}

function produce_output() {
if [[ "${network}" == "null" ]]
then
if [[ "${network}" == "null" ]]; then
jq -n \
--arg ip_address "$(get_reservation)" \
'{"ip_address":$ip_address}'
exit 0
exit 0
fi

timeout=$((SECONDS + 60))
Expand All @@ -61,25 +62,32 @@ function produce_output() {
do
ip_address=$(curl -s "http://$ipam/api/getFreeIP.php?apiapp=address&apitoken=$ipam_token&subnet=${network}&host=${hostname}")

if [[ "$(is_ip_address "${ip_address}")" != "true" ]]; then error_exit "could not reserve an IP address: ${ip_address}"; fi
if [[ "$(is_ip_address "${ip_address}")" != "true" ]]; then
error_exit "could not reserve an IP address: malformed data: ${ip_address}"
fi

if [[ "$ip_address" == "$(get_reservation)" ]]
then
reservation=$(get_reservation)
if [[ "$ip_address" == "$reservation" ]]; then
jq -n \
--arg ip_address "$ip_address" \
'{"ip_address":$ip_address}'
exit 0
elif [[ "$reservation" == "empty" ]]; then
echo "received IP address reservation is empty. Retrying..."
elif [[ "$(is_ip_address "${reservation}")" != "true" ]]; then
echo "received invalid IP address reservation: $reservation. Retrying..."
else
echo "received IP address $ip_address does not equal reservation $reservation. Retrying..."
fi

sleep 3
done

# IPAM server responds with 0.0.0.0 when there are no available addresses
if [[ "${ip_address}" =~ ^0 ]]
then
error_exit "could not reserve an IP address: no available addresses"
if [[ "${ip_address}" =~ ^0 ]]; then
error_exit "could not reserve IP address $ip_address with reservation $reservation: no available addresses"
else
error_exit "could not reserve an IP address: timed out waiting for a reservation"
error_exit "could not reserve IP address $ip_address with reservation $reservation: timed out waiting for a reservation"
fi
}

Expand Down

0 comments on commit f5afcdd

Please sign in to comment.