Skip to content

Instantly share code, notes, and snippets.

@mrlynn
Forked from adamchel/atlas_ip_whitelist.py
Created June 13, 2018 14:50
Show Gist options
  • Save mrlynn/d2e173bfa6eaddf7bfe695780439ad6b to your computer and use it in GitHub Desktop.
Save mrlynn/d2e173bfa6eaddf7bfe695780439ad6b 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