Skip to content

Instantly share code, notes, and snippets.

@MatthewDaniels
Last active August 19, 2022 00:39
Show Gist options
  • Save MatthewDaniels/5c03f6d26199a10e10a6e749be323180 to your computer and use it in GitHub Desktop.
Save MatthewDaniels/5c03f6d26199a10e10a6e749be323180 to your computer and use it in GitHub Desktop.
Use this script to update the billing account on one or more GCP projects. You can either set an individual project or specify a list file.

XPON Technologies GCP Bulk Billing Account Setting

Use this script to update the billing account on one or more GCP projects. You can either set an individual project or specify a list file.

Prerequisites

  • Bash terminal
  • Google Cloud SDK (CLI version): https://cloud.google.com/sdk/docs/install
  • Credentials setup in the CLI with the appropriate permissions on target projects - either Project Owner or Project Billing Manager

Installation

  • Download the raw scipt gcp-bulk-billing-update.sh
  • Set the file to be executable chmod +x ./gcp-bulk-billing-update.sh
  • [Optional] Create & activate a GCLOUD CLI configuration (https://cloud.google.com/sdk/docs/configurations) with the appropriate user who has the correct permissions

Usage

  • Ensure you have the correct GCLOUD Configuration set (https://cloud.google.com/sdk/docs/configurations) - list your configurations with gcloud config configurations lsit
  • Create the project ids list file or have the single project id required
  • Run the command with the appropriate parameters eg: ./gcp-bulk-billing-update.sh -b 0X0X0X-0X0X0X-0X0X0X -p my-first-project-123456

Parameters are as follows:

  • -b or --billingAcc (REQUIRED) The billing account to set the project to. Must be in the format 0X0X0X-0X0X0X-0X0X0X
  • -p or --project (REQUIRED) The project to set the billing on (only required if file is not set)
  • -f or --file (REQUIRED) The text file to use that has the list of project ids in it. Project ids should be on individual lines.
#!/bin/bash
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
echo
echo
echo -e "${GREEN}╭──────────────────────────────────────────────────────────────╮${NC}"
echo -e "${GREEN}├─────────── XPON Tech Bulk Billing Account Setting ───────────┤${NC}"
echo -e "${GREEN}╰──────────────────────────────────────────────────────────────╯${NC}"
echo
# Parse the input parameters
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-b|--billingAcc)
BILLING_ACCOUNT="$2"
shift # past argument
shift # past value
;;
-f|--file)
PROJECT_LIST_FILENAME="$2"
shift # past argument
shift # past value
;;
-p|--project)
PROJECT="$2"
shift # past argument
shift # past value
;;
-h|--help)
SHOW_HELP="help"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}"
getHelp() {
echo
echo -e "${RED}╭───────────────────────────────────╮${NC}"
echo -e "${RED}├───────────── HELP!!! ─────────────┤${NC}"
echo -e "${RED}╰───────────────────────────────────╯${NC}"
echo
echo -e "Use this script to update the billing account on one or more GCP projects. You can either set an individual project or specify a list file."
echo
echo -e "Parameters are as follows:"
echo -e " ${YELLOW}-b${NC} or ${YELLOW}--billingAcc${NC} (${GREEN}REQUIRED${NC}) The billing account to set the project to. Must be in the format ${YELLOW}0X0X0X-0X0X0X-0X0X0X${NC}"
echo -e " ${YELLOW}-p${NC} or ${YELLOW}--project${NC} (${GREEN}REQUIRED*${NC}) The project to set the billing on (only required if file is not set)"
echo -e " ${YELLOW}-f${NC} or ${YELLOW}--file${NC} (${GREEN}REQUIRED${NC}) The text file to use that has the list of project ids in it. Project ids should be on individual lines."
echo
}
# error - need to set a dataset!
if [ -z "$BILLING_ACCOUNT" ]; then
echo
echo -e "${RED}ERROR${NC} - no billing account specified, please use the '-b or --billingAcc' parameter to specify the billing account."
getHelp
exit 1
fi
# todo: add format checks to billing account
if [[ "$SHOW_HELP" ]]; then
getHelp
exit 1
fi
# Make sure we have the project name or project list file set
if [[ -z "$PROJECT" ]] && [[ -z "$PROJECT_LIST_FILENAME" ]]; then
echo
echo -e "${RED}Error.${NC} Must specify either '${GREEN}--project${NC}' or '${GREEN}-p${NC}' as the project id OR '${GREEN}--file${NC}' or '${GREEN}-f${NC}' as the project list file for the billing account linkage."
exit 1
fi
if [[ "$PROJECT" ]]; then
echo
echo -e "Setting billing account ${GREEN}${BILLING_ACCOUNT}${NC} to project ${GREEN}${PROJECT}${NC}..."
gcloud beta billing projects link $PROJECT --billing-account=$BILLING_ACCOUNT
echo
else
# we have a list file - parse it, iterate the lines and run the command
while IFS= read -r line; do
echo
echo -e "Setting billing account ${GREEN}${BILLING_ACCOUNT}${NC} to project ${GREEN}${line}${NC}..."
gcloud beta billing projects link $line --billing-account=$BILLING_ACCOUNT
echo
done < "${PROJECT_LIST_FILENAME}"
fi
if [[ $? != 0 ]]; then
echo
echo -e "${RED}Uh Oh!${NC} Something went wrong... check the console."
echo
else
echo
echo -e "${GREEN}All done!${NC} Better check all the projects are connected..."
echo
gcloud beta billing projects list --billing-account=$BILLING_ACCOUNT
echo
echo
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment