Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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