问题
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.
回答1:
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.
回答2:
Thanks for @GauravMantri explaination in detail.
How to get list of VMs (non-classic) using Java API, which are created using resource Manager?
According to the Azure Reference for Virtual Machine REST, you can get the list of all VMs in a resource group using the REST API that need to authenticate Azure Resource Manager requests in the common parameters and headers.
Here is the sample code using Java API as below.
// 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();
来源:https://stackoverflow.com/questions/36178842/how-to-get-list-of-azure-vms-non-classic-resource-managed-using-java-api