Skip to content

Instantly share code, notes, and snippets.

@siberex
Last active December 25, 2022 15:51
Show Gist options
  • Save siberex/0e0bfcd0fbcf4181c0e1695f0c935a30 to your computer and use it in GitHub Desktop.
Save siberex/0e0bfcd0fbcf4181c0e1695f0c935a30 to your computer and use it in GitHub Desktop.
Google Cloud: List all projects with billing status
# List all projects with assigned billing and status
gcloud beta billing accounts list --format 'value(ACCOUNT_ID)' | while read -r BILLING_ACCOUNT_ID; do
gcloud beta billing projects list --billing-account "$BILLING_ACCOUNT_ID" --format 'value(PROJECT_ID, BILLING_ACCOUNT_ID, BILLING_ENABLED)'
done
# Check if billing is enabled for Project Id
GOOGLE_CLOUD_PROJECT=${1:-$(gcloud config list --format 'value(core.project)')}
function isBillingEnabled() {
GOOGLE_CLOUD_PROJECT=$1
BILLING_ACCOUNTS_LIST=$(gcloud beta billing accounts list --format 'value(ACCOUNT_ID)')
IFS=$'\n'; for BILLING_ACCOUNT_ID in $BILLING_ACCOUNTS_LIST; do
res=$(gcloud beta billing projects list --billing-account "$BILLING_ACCOUNT_ID" --filter "billingEnabled=True AND projectId=$GOOGLE_CLOUD_PROJECT" --format="get(billingEnabled)" 2> /dev/null)
if [[ "$res" =~ True ]]; then
# echo "Billing is ENABLED - $GOOGLE_CLOUD_PROJECT / $BILLING_ACCOUNT_ID"
return 0
fi
done
false
}
if isBillingEnabled "$GOOGLE_CLOUD_PROJECT"; then
echo "✓ Billing is enabled for Project $GOOGLE_CLOUD_PROJECT"
else
echo "× Billing is NOT ENABLED for Project $GOOGLE_CLOUD_PROJECT"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment