dotfiles.2022/_home/dot_sz.shrc.d/32_gcp.shortcuts

65 lines
2.7 KiB
Plaintext

function gcp_project() {
gcloud config get-value core/project 2> /dev/null
}
function gcp_get_project_id() {
gcloud projects list \
--filter="$1" \
--format="value(project_id)" \
2> /dev/null
}
function gcp_get_project_number() {
gcloud projects list \
--filter="project_id:$1" \
--format='value(project_number)' \
2> /dev/null
}
function gcp_get_regions() {
gcloud compute regions list --format="value(name)"
}
function gcp_get_region_quotas() {
region_arr_list=( $(gcloud compute regions list --format="value(name)") )
for region in ${region_arr_list[@]}; do
echo -e $region\\\t$(gcloud compute regions describe $region --format="table(quotas:format='table(metric,limit,usage)')" | grep PREEMPTIBLE_CPUS )
done
#for region in {us-central1,us-east1,us-east4,us-west1,us-west2,us-west3,us-west4}; do
# echo -e $region\\\t$(gcloud compute regions describe $region --format="table(quotas:format='table(metric,limit,usage)')" | grep PREEMPTIBLE_CPUS )
#done
}
# Usage: gcp_get_stack dragonheart 10 22
# Watch the 'dragonheart' stack,
# refresh every 10 seconds,
# set tmux pane to 22 lines high (on every refresh)
function gcp_get_stack() {
local N="${1:-$(basename $PWD)}"
local _cmd=""
local _tmux=''
[ $# -ge 3 ] && [[ -n "$(which-command tmux)" ]] && printf "%s" "$3" > "/tmp/tmux-$1" && _cmd=$(printf "%s" 'tmux resize-pane -t "$TMUX_PANE" -y $(cat "/tmp/tmux-' "$1" '") ; ') && eval $_cmd
_cmd="{ $(printf "%s " \
"N='$N'; " \
"${_cmd}" \
"echo 'Listing $N stack:'; " \
'{ gcloud compute networks list --filter="name:$N" 2>&1 | ' "sed --unbuffered s/^/Networks.:\ /g ; } " \
'&& { gcloud compute networks subnets list --filter="name:$N" 2>&1 | ' "sed --unbuffered s/^/Subnets..:\ /g ; } " \
'&& { gcloud compute disks list --filter="name:$N" 2>&1 | ' "sed --unbuffered s/^/Disks....:\ /g ; } " \
'&& { gcloud compute instances list --filter="name:$N" 2>&1 | ' "sed --unbuffered s/^/Instances:\ /g ; } " \
'&& { gcloud compute routers list --filter="name:$N" 2>&1 | ' "sed --unbuffered s/^/Routers..:\ /g ; } " \
'&& { gcloud compute network-firewall-policies get-effective-firewalls --network="$N-network"' \
" 2>&1 | sed --unbuffered s/^/Firewall:\ /g ; } " \
) ;}"
[ $# -ge 2 ] && _cmd="watch -n${2} '${_cmd}'"
[[ -n ${SZ_DEBUG} ]] && printf "Executing: \n\t$_cmd\n\n"
eval $_cmd
}
# printf '\n\n'; gcp_get_stack 'dragonheart' 5
function gcp_region() {
gcloud config get-value compute/region 2> /dev/null
}