Errors when running setup_psc.sh

When trying to run the shell script “setup_psc.sh” to setup a private endpoint between mongodb atlas and GCP I get the following errors:

root@UserPC:/home/username# sh setup_psc.sh
Updated property [core/project].
ERROR: (gcloud.compute.addresses.create) Could not fetch resource:
 - Invalid value for field 'resource.name': 'chosenName-ip-{0..49}'. Must be a match of regex '(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)'

ERROR: (gcloud.compute.addresses.describe) Could not fetch resource:
 - Invalid value for field 'address': 'chosenName-ip-{0..49}'. Must be a match of regex '[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?|[1-9][0-9]{0,19}'

setup_psc.sh: 11: [: !=: unexpected operator
ERROR: (gcloud.compute.forwarding-rules.create) Could not fetch resource:
 - Invalid value for field 'resource.name': 'chosenName-{0..49}'. Must be a match of regex '(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)'

WARNING: The following filter keys were not present in any resource : name, region
WARNING: The following filter keys were not present in any resource : name, region

For the record this is the script:

#!/bin/bash

gcloud config set project projectName

for i in {0..49}

do

  gcloud compute addresses create endpointName-ip-$i --region=us-central1 --subnet=default

done

for i in {0..49}

do

  if [ $(gcloud compute addresses describe endpointName-ip-$i --region=us-central1 --format="value(status)") != "RESERVED" ]; then

    echo "endpointName-ip-$i is not RESERVED";

    exit 1;

  fi

done

for i in {0..49}

do

  gcloud compute forwarding-rules create endpointName-$i --region=us-central1 --network=default --address=endpointName-ip-$i --target-service-attachment=projects/p-xuggng5phsozafushbhsgaiz/regions/us-central1/serviceAttachments/sa-us-central1-621c96dcfcf5d71f84355fd6-$i

done

if [ $(gcloud compute forwarding-rules list --regions=us-central1 --format="csv[no-heading](name)" --filter="name:endpointName" | wc -l) -gt 50 ]; then

  echo "Project has too many forwarding rules that match prefix endpointName. Either delete the competing resources or choose another endpoint prefix."

  exit 2;

fi

gcloud compute forwarding-rules list --regions=us-central1 --format="json(IPAddress,name)" --filter="name:endpointName" > atlasEndpoints-endpointName.json

I am running this on Windows Subsystem for Linux - Ubuntu

Ok, so I did manage to get it working. There were 3 problems:

  1. The WSL shell doesn’t seem to handle the for loop syntax for i in {0…49} so I changed it to be:
    for i in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  2. The Endpoint Name cannot have capital letters in it…

  3. While making the JSON file the filter: --filter=“name:endpointName” was returning nothing so I removed it and manually checked the JSON file.

Also if you are developing on Windows don’t forget to convert your file to Unix format with dos2unix

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.