Azure WebsiteManagementClient for Python: how to get web app by ID?

Question:

I’m using version 7.0.0 of azure-mgmt-web for Python. I know we can fetch a site with:

from azure.mgmt.web import WebSiteManagementClient

web_client = WebSiteManagementClient(...credentials here...)
site = web_client.web_apps.get(RESOURCE_GROUP_NAME, SITE_NAME)

But is there a way to fetch the same resource by using its full ID? (Which for me looks like: /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>/providers/Microsoft.Web/sites/SITE_NAME)

I guess I can parse this ID with Python myself but it feels like something Azure could do more safely. Any idea if this is an available feature?

Asked By: Roméo Després

||

Answers:

I can parse this ID with Python myself, but it feels like something Azure could do more safely.

Yes, we can parse full ID with python, Azure makes it simple.
Follow the below code.

from  azure.mgmt.web  import  WebSiteManagementClient
from  azure.identity  import  DefaultAzureCredential
from  azure.core.resource import ResourceId

  
credential = DefaultAzureCredential()
subscription_id = '****'
web_client = WebSiteManagementClient(credential, subscription_id)

resource_id = '/subscriptions/****/resourceGroups/***/providers/Microsoft.Web/sites/sampletest0'
parsed_id = ResourceId.from_string(resource_id)

site = web_client.web_apps.get(parsed_id.resource_group, parsed_id.name)

print("Site resource group:", site.resource_group)
print("Site name:", site.name)
print("Site location:", site.location)
  • Here, I used azure.core.resourceid module in the azure-core library.

  • ResourceId class allows to parse and manipulate resource IDs using a pythonic interface.

enter image description here

  • Finally, I achieved the requirement.
Answered By: Suresh Chikkam