Azure python api for compute images get(). Adding special character * to the argument 'image_name'

120 Views Asked by At

I am using Azure ComputeManagementClient.images.get() function to search and match images based on 'resource_group_name' and 'image_name' parameter and print the image details.

Azure python sdk

Azure ComputeManagementClient.images.get()

I have azure cli based authentication. 'az login'

Python code:

from azure.mgmt.compute import ComputeManagementClient
from azure.identity import AzureCliCredential
credential = AzureCliCredential()
compute_client = ComputeManagementClient(credential, '<subcribtion_id>')

imagee = compute_client.images.get(resource_group_name='ab-westeurope-image-            
                                     rg',image_name='ubuntu-latest-1707454954')
print(imagee)

Image name is in this format: distro-version-time-stamp

Actual image name = 'ubuntu-latest-1707454954' where '1707454954' is the time stamp of the image creation.

I am able to print the output(image details) when i give the entire 'image_name'= 'ubuntu-latest-1707454954'

But, when i give special character * in 'image_name' = 'ubuntu-latest-*' Azure gives output with "Code: ResourceNotFound"

I know, its considering 'image_name' = 'ubuntu-latest-*' as a string to match. But, i want it as asterisk in regex pattern matching.

I get this below error:

<!-- azure.core.exceptions.ResourceNotFoundError: (ResourceNotFound) The Resource 'Microsoft.Compute/images/ubuntu-latest-*' under resource group 'ab-westeurope-image-rg' was not found. For more details please go to https://aka.ms/ARMResourceNotFoundFix
Code: ResourceNotFound

Any help?

2

There are 2 best solutions below

0
Sridevi On BEST ANSWER

I have below VM images starts with "ubuntu-latest-" in resource group named Sri like this:

enter image description here

Note that, Azure SDK for Python doesn't directly support wildcard matching for VM image names. When I tried to retrieve these images by adding special character * , I too got same error:

from azure.mgmt.compute import ComputeManagementClient
from azure.identity import AzureCliCredential
credential = AzureCliCredential()
compute_client = ComputeManagementClient(credential, '<subscription_id>')

imagee = compute_client.images.get(resource_group_name='Sri',image_name='ubuntu-latest-*')
print(imagee)

Response:

enter image description here

Alternatively, you can make use of below modified code that initially retrieves list of all images in resource group and filters that response to print matched images:

from azure.mgmt.compute import ComputeManagementClient
from azure.identity import AzureCliCredential

credential = AzureCliCredential()
compute_client = ComputeManagementClient(credential, '<subscription_id>')

images = compute_client.images.list_by_resource_group(resource_group_name='Sri')
match_string = 'ubuntu-latest-'

for image in images:
    if match_string in image.name:
        print(image.name)

Response:

enter image description here

0
Vinay Gowda On

Thanks @Sridevi for ur reply, i agree with your soln(listing by resourse_group_name)

I was able to achieve the same using ResourceManagementClient and list images by quering the substring to match the image name. Hope this will be helpful to someone looking for the same.

resource_client = ResourceManagementClient(credential, subscription_id)
image_name = "ubuntu-latest"
images_object = resource_client.resources.list(filter=f"resourceType eq 
       'Microsoft.Compute/images' and substringof" f"('{image_name}', name)")}

for image in images_object:
  print(image.name)