Skip to content

Instantly share code, notes, and snippets.

@gspncr
Last active February 25, 2021 10:08
Show Gist options
  • Save gspncr/719caaceb57a1cc271a711cc422ae7db to your computer and use it in GitHub Desktop.
Save gspncr/719caaceb57a1cc271a711cc422ae7db to your computer and use it in GitHub Desktop.
fetch-NR-accounts.py
ID Name
1147177 gspncr
2267296 New Relic_1389
3065959 k6-playground
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)
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