How to get a list of Azure VM (non-classic / resource-driven) using Java API

How to get a list of virtual machines (nonclassical) using the Java APIs that are created using the resource manager? What is the tenant identifier, client identifier and client key for creating the object "com.microsoft.azure.management.compute.ComputeManagementClient" for?

Can I use subscription IDs and Azure Portal credentials? The sample provided by the azure-mgmt-compute project requires an identifier identifier, a client identifier where we do not need this data when we create a VM (by selecting Resource Manager) on the Azure Portal.

+4
source share
2 answers

Why do we need a tenant identifier, client identifier and client key to create the 'Com.microsoft.azure.management.compute.ComputeManagementClient' object?

com.microsoft.azure.management.compute.ComputeManagementClient Azure Resource Manager (ARM) REST API , . ARM API Azure Active Directory (AD) . Azure AD , Azure AD Azure Service Management API. Tenant Id, Client Id . , , Azure AD. Tenant Id - Azure AD. Client Id - .

, Azure AD. / , , ARM API .

Azure Portal? , Azure-mgmt-compute, , , ( Resource Manager) Azure Portal.

, Azure Portal, Microsoft "/". . , . , , , .

+3

@GauravMantri .

() Java API, ?

Azure Virtual Machine REST , API REST, Azure .

Java API, .

// The parameters include clientId, clientSecret, tenantId, subscriptionId and resourceGroupName.
private static final String clientId = "<client-id>";
private static final String clientSecret = "<key>";
private static final String tenantId = "<tenant-id>";
private static final String subscriptionId = "<subscription-id>";
private static final String resouceGroupName = "<resource-group-name>";

// The function for getting the access token via Class AuthenticationResult
private static AuthenticationResult getAccessTokenFromServicePrincipalCredentials()
        throws ServiceUnavailableException, MalformedURLException, ExecutionException, InterruptedException {
    AuthenticationContext context;
    AuthenticationResult result = null;
    ExecutorService service = null;
    try {
        service = Executors.newFixedThreadPool(1);
        // TODO: add your tenant id
        context = new AuthenticationContext("https://login.windows.net/" + tenantId, false, service);
        // TODO: add your client id and client secret
        ClientCredential cred = new ClientCredential(clientId, clientSecret);
        Future<AuthenticationResult> future = context.acquireToken("https://management.azure.com/", cred, null);
        result = future.get();
    } finally {
        service.shutdown();
    }

    if (result == null) {
        throw new ServiceUnavailableException("authentication result was null");
    }
    return result;
}

// The process for getting the list of VMs in a resource group
Configuration config = ManagementConfiguration.configure(null, new URI("https://management.core.windows.net"),
        subscriptionId,
        getAccessTokenFromServicePrincipalCredentials().getAccessToken());
ComputeManagementClient client = ComputeManagementService.create(config);
VirtualMachineListResponse listResponse = client.getVirtualMachinesOperations().list(resourceGroupName);
ArrayList<VirtualMachine> list = listResponse.getVirtualMachines();
+1

All Articles