Skip to content

Instantly share code, notes, and snippets.

@ljtill
Last active December 6, 2019 15:33
Show Gist options
  • Save ljtill/6b16c3b552e990b6669d38a8d0f4e64e to your computer and use it in GitHub Desktop.
Save ljtill/6b16c3b552e990b6669d38a8d0f4e64e to your computer and use it in GitHub Desktop.
Provides the ability to retrieve Azure resources
import requests
import json
def tenants(token):
print('Tenants...')
url = 'https://management.azure.com/tenants?api-version=2018-02-01'
headers = {'Authorization': ('Bearer ' + token)}
try:
response = requests.get(url, headers=headers)
output = (json.loads(response.text))['value']
print('Tenant Id: ' + output[0]['tenantId'])
except:
print('An error occured.')
print('')
def subscriptions(token):
print('Subscriptions...')
url = 'https://management.azure.com/subscriptions?api-version=2018-02-01'
headers = {'Authorization': ('Bearer ' + token)}
try:
response = requests.get(url, headers=headers)
output = json.loads(response.text)['value']
print('Subscription Id: ' + output[0]['subscriptionId'])
except:
print('An error occured.')
print('')
def resourcegroups(token, subscriptionId):
print('Resource Groups...')
url = ('https://management.azure.com/subscriptions/' + subscriptionId + '/resourcegroups?api-version=2018-02-01')
headers = {'Authorization': ('Bearer ' + token)}
try:
response = requests.get(url, headers=headers)
output = json.loads(response.text)['value']
for r in output:
print('Resource Group Name: ' + r['name'])
except:
print('An error occured.')
print('')
token = ''
subscriptionid = ''
tenants(token)
subscriptions(token)
resourcegroups(token, subscriptionid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment