Prit Prit - 1 month ago 14
Java Question

How to get list of Azure VMs (non-classic/Resource Managed) using Java API

How to get list of VMs (non-classic) using Java API, which are created using resource Manager? Why we need tenant id, client id and client key to create 'com.microsoft.azure.management.compute.ComputeManagementClient' object?

Can it be done using subscription id and Azure Portal credentials?
Sample provided with azure-mgmt-compute project needs these tenant id, client id where as we don't need these details when we create VM (selecting Resource Manager) on Azure Portal.

Answer

Why we need tenant id, client id and client key to create 'com.microsoft.azure.management.compute.ComputeManagementClient' object?

Behind the scenes, com.microsoft.azure.management.compute.ComputeManagementClient consumes Azure Resource Manager (ARM) REST API for performing Virtual Machines related operations. ARM API makes use of Azure Active Directory (AD) for authentication and authorization. In order to use Azure AD for this purpose, you would need to create an application in your Azure AD and grant that application permission to execute Azure Service Management API. You would need Tenant Id, Client Id and other things for that purpose only. So a user uses your application by allowing the application to be installed in their Azure AD. Tenant Id is the unique id of your application in your user's Azure AD. Client Id is the unique id of your application.

Once everything's been setup properly, in order to use the library user is authenticated against their Azure AD. As a part of authentication/authorization flow, user gets a token and this library makes use of this token to make authenticated request against ARM API to manage Virtual Machines.

Can it be done using subscription id and Azure Portal credentials? Sample provided with azure-mgmt-compute project needs these tenant id, client id where as we don't need these details when we create VM (selecting Resource Manager) on Azure Portal.

If you notice, you would first need to login into Azure Portal using your Microsoft account or Work/School account. Portal software fetches the token as a part of the login process. After that it makes use of tenant id, client id and this token to perform all operations. So essentially it is doing the same thing however it is not visible to you.