Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaAPI获取Azure虚拟机列表(非经典/资源管理)_Java_Azure_Azure Virtual Machine_Azure Resource Manager_Azure Java Sdk - Fatal编程技术网

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

如何使用JavaAPI获取Azure虚拟机列表(非经典/资源管理),java,azure,azure-virtual-machine,azure-resource-manager,azure-java-sdk,Java,Azure,Azure Virtual Machine,Azure Resource Manager,Azure Java Sdk,如何使用JavaAPI获取VM(非经典)列表,这些VM是使用资源管理器创建的?为什么我们需要租户id、客户端id和客户端密钥来创建“com.microsoft.azure.management.compute.ComputeManagementClient”对象 可以使用订阅id和Azure门户凭据来完成吗? azure mgmt compute项目提供的示例需要这些租户id、客户端id,因为在azure门户上创建VM(选择资源管理器)时,我们不需要这些详细信息 为什么我们需要租户id、客户端i

如何使用JavaAPI获取VM(非经典)列表,这些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资源管理器(ARM)REST API来执行与虚拟机相关的操作
ARMAPI
使用Azure Active Directory(AD)
进行身份验证和授权。为了将
Azure AD
用于此目的,您需要在
Azure AD
中创建一个应用程序,并授予该应用程序执行
Azure服务管理API
的权限。您需要
租户Id
客户Id
和其他仅用于此目的的东西。因此,用户通过允许应用程序安装在他们的
Azure AD
中来使用您的应用程序
租户Id
是用户Azure AD中应用程序的唯一Id。
客户端Id
是应用程序的唯一Id

一旦一切都正确设置,为了使用库,用户将根据其Azure AD进行身份验证。作为身份验证/授权流的一部分,用户将获得一个令牌,此库将使用此令牌对ARM API发出身份验证请求,以管理虚拟机

可以使用订阅id和Azure门户凭据来完成吗? azure管理计算项目提供的示例需要这些租户id, 客户端id,因为我们在创建VM时不需要这些详细信息 (正在选择Azure门户上的资源管理器)


如果您注意到,您首先需要使用您的Microsoft帐户或工作/学校帐户登录Azure Portal。门户软件获取令牌作为登录过程的一部分。之后,它使用租户id、客户端id和该令牌来执行所有操作。因此,本质上它是在做同样的事情,但是你看不见。

感谢@GauravMantri的详细解释

如何使用JavaAPI获取VM(非经典)列表,这些VM是使用资源管理器创建的

根据的Azure参考,您可以使用中需要的资源组中所有VM的列表

下面是使用JavaAPI的示例代码

// 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();
//参数包括clientId、clientSecret、tenantId、subscriptionId和resourceGroupName。
私有静态最终字符串clientId=“”;
私有静态最终字符串clientSecret=“”;
私有静态最终字符串tenantId=“”;
私有静态最终字符串subscriptionId=“”;
私有静态最终字符串ResourceGroupName=“”;
//用于通过类AuthenticationResult获取访问令牌的函数
私有静态身份验证结果getAccessTokenFromServicePrincipalCredentials()
抛出ServiceUnavailableException、MalformedUrlexException、ExecutionException、InterruptedException{
认证上下文;
AuthenticationResult=null;
ExecutorService=null;
试一试{
服务=Executors.newFixedThreadPool(1);
//TODO:添加您的租户id
上下文=新的AuthenticationContext(“https://login.windows.net/“+租户,虚假,服务);
//TODO:添加您的客户端id和客户端密码
ClientCredential cred=新的ClientCredential(clientId,clientSecret);
Future=context.acquireToken(“https://management.azure.com/“,cred,null);
结果=future.get();
}最后{
service.shutdown();
}
如果(结果==null){
抛出新的ServiceUnavailableException(“身份验证结果为空”);
}
返回结果;
}
//获取资源组中VM列表的过程
Configuration config=ManagementConfiguration.configure(空,新URI(“https://management.core.windows.net"),
订阅ID,
getAccessTokenFromServicePrincipalCredentials().getAccessToken());
ComputeManagementClient=ComputeManagementService.create(config);
VirtualMachine响应listResponse=client.getVirtualMachine操作().list(resourceGroupName);
ArrayList=listResponse.getVirtualMachine();

感谢Gaurav的详细回复。当您说“为了使用Azure AD实现此目的,您需要在Azure AD中创建一个应用程序,并授予该应用程序执行Azure服务管理API的权限。”此应用程序是否将用于获取所有VM(使用资源管理器创建)?尽管它将由该单一订阅下的任何帐户创建?
此应用程序是否将用于获取所有虚拟机(使用资源管理器创建)?
-->是<代码>虽然它将由该单一订阅下的任何帐户创建?-->我不确定我是否理解这一点。请你解释一下好吗?再次感谢高拉夫!我的意思是,我们可以在AD下创建用户,如果这些用户可以使用资源管理器创建VM,那么在AD下创建的这个应用程序能否获取这些所有用户创建的VM?简单的答案是“是”,因为所有虚拟机都是在同一Azure订阅中创建的。一个有点复杂的答案是“视情况而定”。这取决于登录用户对该订阅中Azure资源的权限(RBAC)。在列出虚拟机时,用户只能看到她/他有权访问的虚拟机(请不要将访问与创建混淆;使用