#!/bin/bash
#
# Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
# Licensed under the GNU General Public License Version 3 as shown at https://www.gnu.org/licenses/gpl-3.0.txt.

flag="$1"

case $flag in
instance-id )
    output=$(curl -s --write-out '\n%{http_code}\n' --max-time 3 --connect-timeout 1 --noproxy '*' http://169.254.169.254/opc/v1/instance/id)
    [[ $(echo "$output"| tail -1) -ne 200 ]] && exit 1
    echo $(echo "$output"|head -1)
;;
availability-domain )
    output=$(curl -s --write-out '\n%{http_code}\n' --max-time 3 --connect-timeout 1 --noproxy '*' http://169.254.169.254/opc/v1/instance/)
    [[ $(echo "$output"| tail -1) -ne 200 ]] && exit 1

    # strip the status code line before jq parsing
    echo $(sed '$d' <<< "$output"|jq -r '.availabilityDomain' | cut -d':' -f2)
;;
*)
   echo "invalid argument $1" >> /dev/stderr
   exit 1
;;
esac

