Last active
February 25, 2021 10:08
-
-
Save gspncr/719caaceb57a1cc271a711cc422ae7db to your computer and use it in GitHub Desktop.
fetch-NR-accounts.py
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
ID | Name | |
---|---|---|
1147177 | gspncr | |
2267296 | New Relic_1389 | |
3065959 | k6-playground |
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
import requests, json | |
graphQLEndpoint = 'https://api.newrelic.com/graphql' | |
#graphQLEndpoint = 'https://api.eu.newrelic.com/graphql' | |
#hidestream is hiding APIKey = '<API Key>' | |
APIKey = '<API Key>' | |
headers = { | |
"Content-Type": 'application/json', | |
"API-Key": APIKey | |
} | |
query = """ | |
{ | |
actor { | |
accounts(scope: GLOBAL) { | |
id | |
name | |
} | |
} | |
} | |
""" | |
request = requests.post( | |
graphQLEndpoint, | |
json={'query': query}, | |
headers=headers | |
) | |
def parseIDs(request): | |
jsonpayload = json.loads(request.text) | |
entries = len(jsonpayload["data"]["actor"]["accounts"]) | |
for x in range(entries): | |
print(jsonpayload["data"]["actor"]["accounts"][x]["id"], | |
jsonpayload["data"]["actor"]["accounts"][x]["name"]) | |
parseIDs(request) | |
def IDstoCSV(request): | |
with open('accountList.csv', 'w', newline='') as csvfile: | |
fieldnames = ['ID', 'Name'] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
jsonpayload = json.loads(request.text) | |
entries = len(jsonpayload["data"]["actor"]["accounts"]) | |
writer.writeheader() | |
for x in range(entries): | |
writer.writerow({'ID': jsonpayload["data"]["actor"]["accounts"][x]["id"], 'Name': jsonpayload["data"]["actor"]["accounts"][x]["name"]}) | |
IDstoCSV(request) |
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
gspencer@C02Y4BU8JG5J terraform % python3 fetchAccounts.py | |
1147177 gspncr | |
2267296 New Relic_1389 | |
3065959 k6-playground |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment