add_azure_account_and_set_role_assignment_2.py 768 B

123456789101112131415161718
  1. def get_azure_reader_role_id(AzureApiBearerToken, AzureSubscriptionId):
  2. """
  3. Gets the id of the reader role for this subscription.
  4. https://docs.microsoft.com/en-us/rest/api/authorization/roleassignments/list
  5. """
  6. api_url = "https://management.azure.com/subscriptions/" + AzureSubscriptionId + "/providers/Microsoft.Authorization/roleDefinitions?api-version=2015-07-01&$filter=roleName eq 'Reader'"
  7. authorization_value = "Bearer " + AzureApiBearerToken
  8. response = requests.get(api_url, headers={"Authorization": authorization_value})
  9. if "value" in response.json():
  10. value = (response.json()["value"])[0]
  11. if "id" in value:
  12. return value["id"]
  13. print("Failed to get the Azure Reader Role Id")
  14. return None