如何使用 Java API 获取使用资源管理器创建的 VM(非经典)列表?为什么我们需要租户 ID、客户端 ID 和客户端密钥来创建“com.microsoft.azure.management.compute.ComputeManagementClient”对象?
可以使用订阅 ID 和 Azure 门户凭据来完成吗?azure-mgmt-compute 项目提供的示例需要这些租户 ID、客户端 ID,因为我们在 Azure 门户上创建 VM(选择资源管理器)时不需要这些详细信息。
如何使用 Java API 获取使用资源管理器创建的 VM(非经典)列表?为什么我们需要租户 ID、客户端 ID 和客户端密钥来创建“com.microsoft.azure.management.compute.ComputeManagementClient”对象?
可以使用订阅 ID 和 Azure 门户凭据来完成吗?azure-mgmt-compute 项目提供的示例需要这些租户 ID、客户端 ID,因为我们在 Azure 门户上创建 VM(选择资源管理器)时不需要这些详细信息。
为什么我们需要租户 ID、客户端 ID 和客户端密钥来创建“com.microsoft.azure.management.compute.ComputeManagementClient”对象?
在幕后,用于执行虚拟机相关操作的com.microsoft.azure.management.compute.ComputeManagementClient消耗。用于身份验证和授权。为了用于此目的,您需要在您的应用程序中创建一个应用程序并授予该应用程序执行权限。您将需要,和其他仅用于此目的的东西。因此,用户通过允许将应用程序安装在他们的. 是用户 Azure AD 中应用程序的唯一 ID。是您的应用程序的唯一 ID。Azure Resource Manager (ARM) REST APIARM APIAzure Active Directory (AD)Azure ADAzure ADAzure Service Management APITenant IdClient IdAzure ADTenant IdClient Id
正确设置所有内容后,为了使用库,用户将针对其 Azure AD 进行身份验证。作为身份验证/授权流程的一部分,用户获得一个令牌,该库利用该令牌向 ARM API 发出经过身份验证的请求以管理虚拟机。
可以使用订阅 ID 和 Azure 门户凭据来完成吗?azure-mgmt-compute 项目提供的示例需要这些租户 ID、客户端 ID,因为我们在 Azure 门户上创建 VM(选择资源管理器)时不需要这些详细信息。
如果您注意到,您首先需要使用您的 Microsoft 帐户或工作/学校帐户登录 Azure 门户。门户软件获取令牌作为登录过程的一部分。之后,它使用租户 ID、客户端 ID 和此令牌来执行所有操作。所以本质上它在做同样的事情,但是你看不到它。
感谢@GauravMantri 的详细解释。
如何使用 Java API 获取使用资源管理器创建的 VM(非经典)列表?
根据 Azure虚拟机 REST参考,您可以使用REST API获取资源组中所有 VM 的列表,这些 VM需要在公共参数和标头中对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();