0

trying to attain a list of regions/locations from Microsoft Azure that contain the term 'health'. A function that will analyze a .csv file.

I used the following function:

regions = monitor_client.location.list('health')

And received the following error:

AttributeError: 'MonitorManagementClient' object has no attribute 'location'

Is there something that's missing. Is the location attribute present within MonitorManagementClient in Microsoft Azure. Is the location module deprecated?

regions = monitor_client.location.list('health')

Hoping to return a list of all available Azure regions/locations with 'Health'

Edit: Having issue with regions with regards to Environment Variables

DefaultAzureCredential failed to retrieve a token from the included credentials. Attempted credentials: EnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured. Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot.this issue. ManagedIdentityCredential: ManagedIdentityCredential authentication unavailable, no response from the IMDS endpoint. SharedTokenCacheCredential: SharedTokenCacheCredential authentication unavailable. No accounts were found in the cache. VisualStudioCodeCredential: Failed to get Azure user details from Visual Studio Code. AzureCliCredential: Please run 'az login' to set up an account AzurePowerShellCredential: Az.Account module >= 2.2.0 is not installed To mitigate this issue, please refer to the troubleshooting guidelines here at https://aka.ms/azsdk/python/identity/defaultazurecredential/troubleshoot.

Left with this error when trying to get the list of regions. Have the Subscription ID, credential, and resource_client set. Still, don't understand why the error continues to appear.

Does it require the following? AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET

1
  • Why did you expect location to exist as an attribute? Did you see an example that used it? Commented Mar 28, 2023 at 1:16

1 Answer 1

0

By default any Azure regions does not contain Health in its name as, Region names are the name of the Countries where Azure data centres exists. And MonitorManagementClient does not include any parameter with list_locations or locations. Refer these documents:-

azure.mgmt.monitor.MonitorManagementClient class | Microsoft Learn

azure-docs/cloud-services-python-how-to-use-service-management.md at main · MicrosoftDocs/azure-docs · GitHub

I tried using MonitorManagementClient to get list of Azure regions with Health in their name and received an error below:-

    regions = client.locations.list()
AttributeError: 'MonitorManagementClient' object has no attribute 'locations'

I used the code below to get all the Azure regions with Health in its name and got no results:-


from azure.mgmt.resource import  ResourceManagementClient

from azure.identity import  DefaultAzureCredential

  

# Define Azure subscription ID

subscription_id = '<sub-id>'

  

# Create ResourceManagementClient object

credential = DefaultAzureCredential()

resource_client = ResourceManagementClient(credential, subscription_id)

  

# Retrieve the Azure regions that contain the term 'health'

regions = []

for  location  in  resource_client.providers.get('Microsoft.Compute').resource_types[0].locations:

if  'health'  in  location.lower():

regions.append(location)

  

# Print the list of regions that contain the term 'health'

print(regions)

Output:-

enter image description here

In order to get the list of all regions, You can make use of the code below:-

Code 1:-

from azure.mgmt.resource import ResourceManagementClient
from azure.identity import DefaultAzureCredential


subscription_id = '<sub-id>'


credential = DefaultAzureCredential()
resource_client = ResourceManagementClient(credential, subscription_id)


regions = []
for provider in resource_client.providers.list():
    for resource_type in provider.resource_types:
        for location in resource_type.locations:
            regions.append(location)


for region in regions:
    print(region)

Output:-

enter image description here

If you want to check the resource health of your resources, You can make use of the code below to get the availability status of the resources by their regions or at subscription level:-

Code 1:- Availability status by Subscription:

import requests
import json
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()


url = f"https://management.azure.com/subscriptions/<sub-id>/providers/Microsoft.ResourceHealth/availabilityStatuses?api-version=2022-05-01"

headers = {"Authorization": f"Bearer {credential.get_token('https://management.azure.com/.default').token}"}

response = requests.get(url, headers=headers)
print(response)

if response.status_code == 200:
    health_status = json.loads(response.content.decode('utf-8'))
    print(health_status)

Output:-

enter image description here

Resource Type by regions:-

Code2 :-

import requests

import json

from azure.identity import  DefaultAzureCredential

  

credential = DefaultAzureCredential()

subscription_id = "<sub-id>"

resource_type = "Microsoft.Compute"

resource_name = "<vm>"

region = "UK South"

  

#url = f"https://management.azure.com/subscriptions/{subscription_id}/providers/Microsoft.ResourceHealth/availabilityStatuses/{resource_type}/{resource_name}/providers/Microsoft.Compute/locations/{region}?api-version=2018-07-01-preview"

  

url = f"https://management.azure.com/subscriptions/{subscription_id}/providers/Microsoft.ResourceHealth/availabilityStatuses/{resource_type}/{resource_name}/providers/Microsoft.Compute/locations/{region}?api-version=2018-07-01-preview"

  

headers = {"Authorization": f"Bearer {credential.get_token('https://management.azure.com/.default').token}"}

  

response = requests.get(url, headers=headers)

print(response)

  

if  response.status_code == 200:

health_status = json.loads(response.content.decode('utf-8'))

print(health_status)

print(f"The health status of {resource_name} in {region} is {health_status['properties']['availabilityState']}")

else:

print(f"Failed to get the health status of {resource_name} in {region}. Error message: {response.content}")

As, We do not have any health issues in our VM in UK south region, It returned the response below:-

Output:-

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.