-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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