Skip to content

Instantly share code, notes, and snippets.

@krisrice
Last active June 14, 2022 21:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisrice/096ae7a4d9eaf6aa7783fdba10d64c5c to your computer and use it in GitHub Desktop.
Save krisrice/096ae7a4d9eaf6aa7783fdba10d64c5c to your computer and use it in GitHub Desktop.
#!/bin/bash
OCIPROFILE="MYPROFILE"
DBOCID="ocid1.autonomousdatabase.oc1.phx.anyhqljrcsc5xxqnxxxxxxxxxxxxxxxxxxxxxxxxkulv5ipmbqiq"
REGION="us-phoenix-1"
#
# GET my IP Address
#
# Example : "1.2.3.4"
CURRENT_IP=$(oci raw-request --profile $OCIPROFILE --target-uri https://database.${REGION}.oraclecloud.com/20160918/autonomousDatabases/capabilities --http-method GET | jq ".data.clientIpAddress")
echo Current IP Address: $CURRENT_IP
#
# Get Current Whitelisted IPs
#
# Example : [ "192.168.2.2","192.168.2.3"]
CURRENT_WHITELIST=$(oci --profile $OCIPROFILE db autonomous-database get --autonomous-database-id ${DBOCID} | jq '.data."whitelisted-ips"')
echo Current Whitelist : $CURRENT_WHITELIST
#
# Combine Current Whitelist and Current IP
#
COMBINED_WHITELIST=$(echo -e $CURRENT_WHITELIST "[$CURRENT_IP]" | jq -s 'add')
#
# Wrapper with json tag name
#
PAYLOAD=$(echo ${COMBINED_WHITELIST} | jq '{"whitelistedIps": .}' )
#
# Push payload to a file
#
echo ${PAYLOAD} > tempWhitelist.json
echo Combined Whitelist
cat tempWhitelist.json
#
# Add IP to Whitelist
#
NEW_WHITELIST=$(oci raw-request --profile ${OCIPROFILE} --target-uri https://database.${REGION}.oraclecloud.com/20160918/autonomousDatabases/${DBOCID} --http-method PUT --request-body file://./tempWhitelist.json | jq '.data.whitelistedIps' )
echo New Whitelist Submitted
rm tempWhitelist.jso
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment