@@ -26,38 +26,62 @@ JWT_SECRET="your_jwt_secret_here"
2626provision_mongodb () {
2727 echo " ☁️ Provisioning MongoDB Atlas cluster..."
2828
29- # Create cluster
30- curl -u " $ATLAS_PUBLIC_KEY :$ATLAS_PRIVATE_KEY " \
31- -X POST " https://cloud.mongodb.com/api/atlas/v1.0/groups/$ATLAS_PROJECT_ID /clusters" \
32- -H " Content-Type: application/json" \
33- -d " {
34- \" name\" : \" $ATLAS_CLUSTER_NAME \" ,
35- \" providerSettings\" : {\" providerName\" : \" AWS\" , \" instanceSizeName\" : \" M0\" , \" regionName\" : \" US_EAST_1\" }
36- }"
37-
38- echo " ⏳ Waiting for cluster creation (M0 takes ~5 minutes)..."
39- sleep 300 # Wait 5 minutes for free tier cluster to be ready
40-
41- # Create database user
42- curl -u " $ATLAS_PUBLIC_KEY :$ATLAS_PRIVATE_KEY " \
43- -X POST " https://cloud.mongodb.com/api/atlas/v1.0/groups/$ATLAS_PROJECT_ID /databaseUsers" \
44- -H " Content-Type: application/json" \
45- -d " {
46- \" databaseName\" : \" admin\" ,
47- \" username\" : \" fullstackuser\" ,
48- \" password\" : \" FullStackPass123\" ,
49- \" roles\" : [{\" roleName\" : \" readWriteAnyDatabase\" , \" databaseName\" : \" admin\" }]
50- }"
51-
52- # Allow access from anywhere (0.0.0.0/0)
53- curl -u " $ATLAS_PUBLIC_KEY :$ATLAS_PRIVATE_KEY " \
29+ # 1️⃣ Create cluster (ignore if already exists)
30+ CLUSTER_EXISTS=$( curl -s -u " $ATLAS_PUBLIC_KEY :$ATLAS_PRIVATE_KEY " \
31+ " https://cloud.mongodb.com/api/atlas/v1.0/groups/$ATLAS_PROJECT_ID /clusters/$ATLAS_CLUSTER_NAME " | jq -r ' .name' )
32+
33+ if [[ " $CLUSTER_EXISTS " != " $ATLAS_CLUSTER_NAME " ]]; then
34+ curl -s -u " $ATLAS_PUBLIC_KEY :$ATLAS_PRIVATE_KEY " \
35+ -X POST " https://cloud.mongodb.com/api/atlas/v1.0/groups/$ATLAS_PROJECT_ID /clusters" \
36+ -H " Content-Type: application/json" \
37+ -d " {
38+ \" name\" : \" $ATLAS_CLUSTER_NAME \" ,
39+ \" providerSettings\" : {\" providerName\" : \" AWS\" , \" instanceSizeName\" : \" M0\" , \" regionName\" : \" US_EAST_1\" }
40+ }"
41+ echo " Cluster creation initiated."
42+ else
43+ echo " Cluster $ATLAS_CLUSTER_NAME already exists."
44+ fi
45+
46+ # 2️⃣ Wait for cluster to be ready
47+ echo " ⏳ Waiting for cluster to be fully ready..."
48+ STATUS=" "
49+ while [[ " $STATUS " != " IDLE" ]]; do
50+ STATUS=$( curl -s -u " $ATLAS_PUBLIC_KEY :$ATLAS_PRIVATE_KEY " \
51+ " https://cloud.mongodb.com/api/atlas/v1.0/groups/$ATLAS_PROJECT_ID /clusters/$ATLAS_CLUSTER_NAME " | jq -r ' .stateName' )
52+ echo " Cluster status: $STATUS "
53+ if [[ " $STATUS " != " IDLE" ]]; then sleep 15; fi
54+ done
55+ echo " ✅ Cluster is ready."
56+
57+ # 3️⃣ Create database user (ignore if exists)
58+ USER_EXISTS=$( curl -s -u " $ATLAS_PUBLIC_KEY :$ATLAS_PRIVATE_KEY " \
59+ " https://cloud.mongodb.com/api/atlas/v1.0/groups/$ATLAS_PROJECT_ID /databaseUsers?username=fullstackuser" | jq -r ' .[0].username' )
60+
61+ if [[ " $USER_EXISTS " != " fullstackuser" ]]; then
62+ curl -s -u " $ATLAS_PUBLIC_KEY :$ATLAS_PRIVATE_KEY " \
63+ -X POST " https://cloud.mongodb.com/api/atlas/v1.0/groups/$ATLAS_PROJECT_ID /databaseUsers" \
64+ -H " Content-Type: application/json" \
65+ -d " {
66+ \" databaseName\" : \" admin\" ,
67+ \" username\" : \" fullstackuser\" ,
68+ \" password\" : \" FullStackPass123\" ,
69+ \" roles\" : [{\" roleName\" : \" readWriteAnyDatabase\" , \" databaseName\" : \" admin\" }]
70+ }"
71+ echo " Database user created."
72+ else
73+ echo " Database user fullstackuser already exists."
74+ fi
75+
76+ # 4️⃣ Allow access from anywhere
77+ curl -s -u " $ATLAS_PUBLIC_KEY :$ATLAS_PRIVATE_KEY " \
5478 -X POST " https://cloud.mongodb.com/api/atlas/v1.0/groups/$ATLAS_PROJECT_ID /ipAccessList" \
5579 -H " Content-Type: application/json" \
5680 -d " [
5781 {\" ipAddress\" : \" 0.0.0.0/0\" , \" comment\" : \" Allow all\" }
5882 ]"
5983
60- # Construct connection string
84+ # 5️⃣ Construct connection string
6185 MONGO_URI=" mongodb+srv://fullstackuser:FullStackPass123@$ATLAS_CLUSTER_NAME .mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
6286 echo " ✅ MongoDB Atlas provisioned. Connection string: $MONGO_URI "
6387}
0 commit comments