Skip to content

Instantly share code, notes, and snippets.

@prashanthmadi
Created April 3, 2024 03:43
Show Gist options
  • Save prashanthmadi/4a64b238cbea7fdb0d571f028cd50e53 to your computer and use it in GitHub Desktop.
Save prashanthmadi/4a64b238cbea7fdb0d571f028cd50e53 to your computer and use it in GitHub Desktop.
helps change Cosmos Db throughput model based on input.csv file
#!/bin/bash
# Input file
FILE="input.csv"
# Log file
LOGFILE="output.log"
# Number of retries
RETRIES=3
# Read input file and skip header
tail -n +2 "$FILE" | while IFS=',' read -r resource_id throughput_mode
do
# Extract subscription, resource group, account, database, collection, and API from resource_id
subscription=$(echo $resource_id | cut -d'/' -f3)
resource_group=$(echo $resource_id | cut -d'/' -f5)
account_name=$(echo $resource_id | cut -d'/' -f9)
database_name=$(echo $resource_id | cut -d'/' -f11)
collection_name=$(echo $resource_id | cut -d'/' -f13)
api_kind=$(echo $resource_id | cut -d'/' -f10)
! [[ -z "$collection_name" ]]; is_database=$?
# Set the throughput mode
for attempt in $(seq 1 $RETRIES)
do
echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO - Attempt: $attempt to set throughput mode for subscription: $subscription, resource group: $resource_group, account: $account_name, database: $database_name, collection: $collection_name to: $throughput_mode." | tee -a $LOGFILE
if [[ $api_kind == "sqlDatabases" ]]; then
if (( $is_database )); then
result=$(az cosmosdb sql database throughput migrate \
--subscription $subscription \
--resource-group $resource_group \
--account-name $account_name \
--name $database_name \
--throughput-type $throughput_mode 2>&1 )
else
result=$(az cosmosdb sql container throughput migrate \
--subscription $subscription \
--resource-group $resource_group \
--account-name $account_name \
--database-name $database_name \
--name $collection_name \
--throughput-type $throughput_mode 2>&1 )
fi
elif [[ $api_kind == "mongodbDatabases" ]]; then
if (( $is_database )); then
result=$(az cosmosdb mongodb database throughput migrate \
--subscription $subscription \
--resource-group $resource_group \
--account-name $account_name \
--name $database_name \
--throughput-type $throughput_mode 2>&1 )
else
result=$(az cosmosdb mongodb collection throughput migrate \
--subscription $subscription \
--resource-group $resource_group \
--account-name $account_name \
--database-name $database_name \
--name $collection_name \
--throughput-type $throughput_mode 2>&1 )
fi
elif [[ $api_kind == "cassandraKeyspaces" ]]; then
if (( $is_database )); then
result=$(az cosmosdb cassandra keyspace throughput migrate \
--subscription $subscription \
--resource-group $resource_group \
--account-name $account_name \
--name $database_name \
--throughput-type $throughput_mode 2>&1 )
else
result=$(az cosmosdb cassandra table throughput migrate \
--subscription $subscription \
--resource-group $resource_group \
--account-name $account_name \
--keyspace-name $database_name \
--name $collection_name \
--throughput-type $throughput_mode 2>&1 )
fi
elif [[ $api_kind == "gremlinDatabases" ]]; then
if (( $is_database )); then
result=$(az cosmosdb gremlin database throughput migrate \
--subscription $subscription \
--resource-group $resource_group \
--account-name $account_name \
--name $database_name \
--throughput-type $throughput_mode 2>&1 )
else
result=$(az cosmosdb gremlin graph throughput migrate \
--subscription $subscription \
--resource-group $resource_group \
--account-name $account_name \
--database-name $database_name \
--name $collection_name \
--throughput-type $throughput_mode 2>&1 )
fi
else
result=$(az cosmosdb table throughput migrate \
--subscription $subscription \
--resource-group $resource_group \
--account-name $account_name \
--name $database_name \
--throughput-type $throughput_mode 2>&1 )
fi
# Log successful result
if [[ $? -eq 0 ]]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO - Collection successfully set to desired mode." | tee -a $LOGFILE
break
else
# Don't retry if collection already in desired mode
if [[ $result == *"Current offer is already a provisioned throughput offer."* || $result == *"Current offer is already an autoscale throughput offer."* ]]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO - No action taken, collection already in desired mode." | tee -a $LOGFILE
break
# Don't retry if collection does not exist
elif [[ $result == *"Resource Not Found."* ]]; then
echo $result | tee -a $LOGFILE
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR - Collection not found." | tee -a $LOGFILE
break
# Don't retry if insufficient permissions
elif [[ $result == *"AuthorizationFailed"* ]]; then
echo $result | tee -a $LOGFILE
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR - Insufficient permissions." | tee -a $LOGFILE
break
# Retry in all other cases
else
echo $result | tee -a $LOGFILE
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR - Unable to set collection to desired mode." | tee -a $LOGFILE
# Wait between retries
if [[ $attempt -ne 3 ]]; then
sleep 2
fi
fi
fi
done
done
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
ResourceID TargetMode
/subscriptions/e00d349a-057f-416e-96e8-231d9e933f2e/resourceGroups/cosmosdb-test/providers/Microsoft.DocumentDb/databaseAccounts/tokovari-nosql-latency-test/sqlDatabases/demo/containers/leasess autoscale
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment