|
#!/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 |