Skip to content

Instantly share code, notes, and snippets.

@mahrous-amer
Created August 5, 2025 06:30
Show Gist options
  • Select an option

  • Save mahrous-amer/2eebe2ea8828b1f1a285c11fdb6dd6de to your computer and use it in GitHub Desktop.

Select an option

Save mahrous-amer/2eebe2ea8828b1f1a285c11fdb6dd6de to your computer and use it in GitHub Desktop.

Revisions

  1. mahrous-amer created this gist Aug 5, 2025.
    62 changes: 62 additions & 0 deletions BigQueryDTSOwnershipTransfer.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    # 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."