如何使用Java API获取Azure虚拟机列表(非经典/资源管理)

4

如何使用Java API获取使用资源管理器创建的VM(非经典VM)列表?为什么我们需要租户ID、客户端ID和客户端密钥来创建'com.microsoft.azure.management.compute.ComputeManagementClient'对象?

是否可以使用订阅ID和Azure门户凭据完成此操作?与azure-mgmt-compute项目提供的示例需要这些租户ID、客户端ID不同,当我们在Azure门户上创建VM(选择资源管理器)时,我们不需要这些详细信息。

2个回答

3
我们为什么需要租户ID、客户端ID和客户端密钥来创建“com.microsoft.azure.management.compute.ComputeManagementClient”对象?
在幕后,“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的权限。您需要租户ID、客户端ID等信息才能完成此操作。因此,用户通过允许将应用程序安装在其Azure AD中来使用您的应用程序。租户ID是您的应用程序在用户的Azure AD中的唯一ID。客户端ID是您的应用程序的唯一ID。
一旦所有设置都正确地完成,用户就会根据他们的Azure AD进行身份验证。作为身份验证/授权流程的一部分,用户获得一个令牌,这个库利用这个令牌对ARM API进行身份验证请求,以管理虚拟机。
可以使用订阅ID和Azure门户凭据完成吗?azure-mgmt-compute项目提供的示例需要这些租户ID、客户端ID,而我们在Azure门户上创建VM(选择资源管理器)时不需要这些详细信息。
如果您注意到,您首先需要使用Microsoft帐户或工作/学校帐户登录Azure门户。门户软件在登录过程中提取令牌。之后,它使用租户ID、客户端ID和此令牌执行所有操作。因此,本质上它正在做同样的事情,但您看不到它。

感谢Gaurav的详细回复。当您说“为了使用Azure AD进行此操作,您需要在Azure AD中创建一个应用程序,并授予该应用程序执行Azure Service Management API的权限。”时,这个应用程序是否将用于获取所有VM(使用Resource Manager创建)?并且它将由该单个订阅下的任何帐户创建? - Prit
会使用相同的应用程序来获取所有使用资源管理器创建的虚拟机吗? --> 是的。 而且,它将由单个订阅下的任何帐户创建? --> 我不确定我理解了这个问题。您能解释一下吗? - Gaurav Mantri
再次感谢Gaurav!我的意思是,我们可以在AD下创建用户,如果这些用户可以使用资源管理器创建VM,则此单个在AD下创建的应用程序是否可以获取由所有这些用户创建的VM? - Prit
简单的答案是“是”,因为所有虚拟机都是在同一个Azure订阅中创建的。稍微复杂一点的答案是“取决于情况”。这取决于已登录用户在该订阅中的Azure资源上拥有的权限(RBAC)。当列出虚拟机时,用户只能看到她/他可以访问的VMs(请不要将访问与创建混淆;用户也可能可以访问其他VMs)。我建议阅读(或发布另一个问题)关于Azure RBAC的内容。 - Gaurav Mantri
再次感谢你,Gaurav。 - Prit

1

感谢@GauravMantri的详细解释。

如何使用Java API获取创建于资源管理器中的VM(非经典)列表?

根据Virtual Machine REST的Azure参考,您可以使用需要验证Azure Resource Manager请求公共参数和标头REST API获取资源组中所有VM的列表。

以下是使用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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接