Created
August 5, 2025 06:30
-
-
Save mahrous-amer/2eebe2ea8828b1f1a285c11fdb6dd6de to your computer and use it in GitHub Desktop.
Bash script to list BigQuery Data Transfer Service (DTS) jobs in specified Google Cloud project locations (us, asia-southeast1), check for a specific owner, and transfer ownership to a new user or service account using the bq command-line tool. Requires Google Cloud SDK and jq.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Configuration variables | |
PROJECT_ID="project" | |
LOCATIONS=("us" "asia-southeast1") | |
CURRENT_OWNER="old" | |
NEW_OWNER="new" | |
# Ensure bq command-line tool is installed | |
if ! command -v bq &> /dev/null; then | |
echo "Error: bq command-line tool not found. Please install Google Cloud SDK." | |
exit 1 | |
fi | |
# Ensure jq is installed | |
if ! command -v jq &> /dev/null; then | |
echo "Error: jq command-line tool not found. Please install jq." | |
exit 1 | |
fi | |
# Function to list and process transfer configurations for a given location | |
process_location() { | |
local LOCATION=$1 | |
echo "Listing transfer configurations in project $PROJECT_ID, location $LOCATION..." | |
# List all transfer configurations in the project and location | |
TRANSFER_CONFIGS=$(bq ls --transfer_config --transfer_location="$LOCATION" --project_id="$PROJECT_ID" --format=prettyjson | jq -r '.[] | .name') | |
# Check if any transfer configurations were found | |
if [ -z "$TRANSFER_CONFIGS" ]; then | |
echo "No transfer configurations found in project $PROJECT_ID, location $LOCATION." | |
return | |
fi | |
# Iterate through each transfer configuration | |
for CONFIG in $TRANSFER_CONFIGS; do | |
echo "Checking transfer configuration: $CONFIG" | |
# Get the owner of the transfer configuration | |
OWNER=$(bq show --format=prettyjson --transfer_config "$CONFIG" | jq -r '.userId') | |
# Check if the owner matches the specified CURRENT_OWNER | |
if [ "$OWNER" = "$CURRENT_OWNER" ]; then | |
echo "Match found! Transfer configuration $CONFIG is owned by $CURRENT_OWNER." | |
echo "Transferring ownership to $NEW_OWNER..." | |
# Attempt to transfer ownership | |
if bq update --transfer_config --update_credentials=true --service_account_name="$NEW_OWNER" "$CONFIG"; then | |
echo "Successfully transferred ownership of $CONFIG to $NEW_OWNER." | |
else | |
echo "Error: Failed to transfer ownership of $CONFIG." | |
fi | |
else | |
echo "No match: $CONFIG is owned by $OWNER, not $CURRENT_OWNER." | |
fi | |
done | |
} | |
# Iterate through both locations | |
for LOCATION in "${LOCATIONS[@]}"; do | |
process_location "$LOCATION" | |
done | |
echo "Completed checking and transferring ownership for all transfer configurations in us and asia-southeast1." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment