Skip to content

Instantly share code, notes, and snippets.

@adamchel
Last active June 13, 2018 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adamchel/8a04526aea3f3549fa97dc2dcebcb3b0 to your computer and use it in GitHub Desktop.
Save adamchel/8a04526aea3f3549fa97dc2dcebcb3b0 to your computer and use it in GitHub Desktop.
Script to generate a cURL command that bulk adds IPs to a MongoDB Atlas Group IP whitelist.
#
# Generates a cURL command to add IPs to an MongoDB Atlas Group IP whitelist.
#
import sys
atlas_user = sys.argv[1]
atlas_api_key = sys.argv[2]
atlas_group_id = sys.argv[3]
whitelist_file = sys.argv[4] # Each IP or CIDR block should be separated by a newline
url = "https://cloud.mongodb.com/api/atlas/v1.0/groups/{}/whitelist".format(atlas_group_id)
post_json = "["
first = True
for entry in open(whitelist_file).readlines():
if not first:
post_json += ","
else:
first = False
if entry.find("/") > 0:
post_json += '{{"cidrBlock": "{}"}}'.format(entry.strip())
else:
post_json += '{{"ipAddress": "{}"}}'.format(entry.strip())
post_json += "]"
print('''curl -i -u "{}:{}" -H "Content-Type: application/json" -X POST --digest "https://cloud.mongodb.com/api/atlas/v1.0/groups/{}/whitelist" --data '
{}
' '''.format(atlas_user, atlas_api_key, atlas_group_id, post_json))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment