#!/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

#颜色
Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m"

ProjectName="项目名称"
InstanceName="instance-3"
IPName="a"
RegionName="asia-east2"
Result=""
#1.删除IP
echo -e "${Green_font_prefix} 正在删除旧静态IP地址 ${Font_color_suffix} "
gcloud compute addresses delete ${IPName} -q

#2.创建IP
echo -e "${Green_font_prefix} 正在创建新IP地址 ${Font_color_suffix}"
gcloud compute addresses create ${IPName} --project=${ProjectName} --region=${RegionName}

#3.获取IP
echo -e "${Green_font_prefix} 正在获取新IP地址 ${Font_color_suffix}"
NewIP=$(gcloud compute addresses list --format="get(address)")
echo -e "新IP地址为：${NewIP}"

#4.删除ACCESS-CONFIG
echo -e "${Green_font_prefix} 正在删除ACCESS-CONFIG ${Font_color_suffix}"
Result=$(gcloud compute instances describe $InstanceName --format="get(networkInterfaces[0].accessConfigs[0].name)")
gcloud compute instances delete-access-config ${InstanceName} --access-config-name $Result

#5.绑定新IP
echo -e "${Green_font_prefix} 正在绑定新IP地址 ${Font_color_suffix}"
gcloud compute instances add-access-config ${InstanceName} --project=${ProjectName} --address=${NewIP}

#6.通知DDNS服务更新域名解析






