Last active
June 13, 2018 14:50
-
-
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.
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