@@ -20,33 +20,41 @@ gcloud_stderr="$(mktemp)"
2020trap ' rm -f "${gcloud_stderr}"' EXIT
2121
2222echo " Checking for subnet '${SUBNET_NAME} ' in region '${GCP_REGION} '..."
23- current_subnet =" $( gcloud compute networks subnets describe " ${SUBNET_NAME} " \
24- --region =" ${GCP_REGION} " \
23+ existing_subnet_name =" $( gcloud compute networks subnets list \
24+ --regions =" ${GCP_REGION} " \
2525 --project=" ${GCP_PROJECT_ID} " \
26- --format=' csv[no-heading](network.basename(),ipCidrRange,privateIpGoogleAccess,stackType)' \
27- 2> " ${gcloud_stderr} " ) " && subnet_exists=true || subnet_exists=false
26+ --filter=" name=('${SUBNET_NAME} ')" \
27+ --format=' value(name)' \
28+ 2> " ${gcloud_stderr} " ) " && subnet_lookup_ok=true || subnet_lookup_ok=false
2829
29- if ${subnet_exists} ; then
30- expected_subnet=" ${GCP_NETWORK_NAME} ,${SUBNET_CIDR} ,True,IPV4_ONLY"
31- if [[ " ${current_subnet} " != " ${expected_subnet} " ]]; then
32- echo " ERROR: Subnet '${SUBNET_NAME} ' exists but is misconfigured."
33- echo " Expected: ${expected_subnet} "
34- echo " Actual: ${current_subnet} "
35- exit 1
30+ if ${subnet_lookup_ok} ; then
31+ if [[ -n " ${existing_subnet_name} " ]]; then
32+ current_subnet=" $( gcloud compute networks subnets describe " ${SUBNET_NAME} " \
33+ --region=" ${GCP_REGION} " \
34+ --project=" ${GCP_PROJECT_ID} " \
35+ --format=' csv[no-heading](network.basename(),ipCidrRange,privateIpGoogleAccess,stackType)' \
36+ 2> " ${gcloud_stderr} " ) "
37+ expected_subnet=" ${GCP_NETWORK_NAME} ,${SUBNET_CIDR} ,True,IPV4_ONLY"
38+ if [[ " ${current_subnet} " != " ${expected_subnet} " ]]; then
39+ echo " ERROR: Subnet '${SUBNET_NAME} ' exists but is misconfigured."
40+ echo " Expected: ${expected_subnet} "
41+ echo " Actual: ${current_subnet} "
42+ exit 1
43+ fi
44+ echo " Subnet '${SUBNET_NAME} ' already exists and matches expected configuration."
45+ else
46+ echo " Creating subnet '${SUBNET_NAME} '..."
47+ gcloud compute networks subnets create " ${SUBNET_NAME} " \
48+ --network=" ${GCP_NETWORK_NAME} " \
49+ --region=" ${GCP_REGION} " \
50+ --range=" ${SUBNET_CIDR} " \
51+ --enable-private-ip-google-access \
52+ --stack-type=IPV4_ONLY \
53+ --project=" ${GCP_PROJECT_ID} "
54+ echo " Subnet '${SUBNET_NAME} ' created."
3655 fi
37- echo " Subnet '${SUBNET_NAME} ' already exists and matches expected configuration."
38- elif grep -q " was not found" " ${gcloud_stderr} " ; then
39- echo " Creating subnet '${SUBNET_NAME} '..."
40- gcloud compute networks subnets create " ${SUBNET_NAME} " \
41- --network=" ${GCP_NETWORK_NAME} " \
42- --region=" ${GCP_REGION} " \
43- --range=" ${SUBNET_CIDR} " \
44- --enable-private-ip-google-access \
45- --stack-type=IPV4_ONLY \
46- --project=" ${GCP_PROJECT_ID} "
47- echo " Subnet '${SUBNET_NAME} ' created."
4856else
49- echo " ERROR: gcloud describe failed for subnet '${SUBNET_NAME} ':"
57+ echo " ERROR: gcloud subnet lookup failed for subnet '${SUBNET_NAME} ':"
5058 cat " ${gcloud_stderr} " >&2
5159 exit 1
5260fi
@@ -69,7 +77,13 @@ if ${fw_exists}; then
6977 current_tags=" $( gcloud compute firewall-rules describe " ${SUBNET_NAME} " \
7078 --project=" ${GCP_PROJECT_ID} " \
7179 --format=' value(targetTags.list())' \
72- | tr ' ,;' ' \n' | LC_ALL=C sort | tr ' \n' ' ,' | sed ' s/,$//' ) "
80+ 2> " ${gcloud_stderr} " \
81+ | tr ' ,;' ' \n' | LC_ALL=C sort | tr ' \n' ' ,' | sed ' s/,$//' ) " && current_tags_read=true || current_tags_read=false
82+ if ! ${current_tags_read} ; then
83+ echo " ERROR: gcloud describe failed while reading target tags for firewall rule '${SUBNET_NAME} ':"
84+ cat " ${gcloud_stderr} " >&2
85+ exit 1
86+ fi
7387 expected_tags=" $( printf ' %s\n' ${FIREWALL_TAGS// ,/ } | LC_ALL=C sort | tr ' \n' ' ,' | sed ' s/,$//' ) "
7488 if [[ " ${current_tags} " != " ${expected_tags} " ]]; then
7589 echo " ERROR: Firewall rule '${SUBNET_NAME} ' has wrong target tags."
0 commit comments