4

リソース マネージャーを使用して作成された Java API を使用して、VM (非クラシック) のリストを取得する方法は? 「com.microsoft.azure.management.compute.ComputeManagementClient」オブジェクトを作成するためにテナント ID、クライアント ID、およびクライアント キーが必要なのはなぜですか?

サブスクリプション ID と Azure Portal 資格情報を使用して実行できますか? azure-mgmt-compute プロジェクトで提供されるサンプルには、これらのテナント ID、クライアント ID が必要ですが、Azure Portal で VM を作成する (Resource Manager を選択する) ときはこれらの詳細は必要ありません。

4

2 に答える 2

3

「com.microsoft.azure.management.compute.ComputeManagementClient」オブジェクトを作成するためにテナント ID、クライアント ID、およびクライアント キーが必要なのはなぜですか?

バックグラウンドで、仮想マシン関連の操作を実行するために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 Portal 資格情報を使用して実行できますか? azure-mgmt-compute プロジェクトで提供されるサンプルには、これらのテナント ID、クライアント ID が必要ですが、Azure Portal で VM を作成する (Resource Manager を選択する) ときはこれらの詳細は必要ありません。

お気づきの場合は、最初に Microsoft アカウントまたは職場/学校アカウントを使用して Azure Portal にログインする必要があります。ポータル ソフトウェアは、ログイン プロセスの一部としてトークンを取得します。その後、テナント ID、クライアント ID、およびこのトークンを使用して、すべての操作を実行します。したがって、本質的には同じことを行っていますが、あなたには見えません。

于 2016-03-23T13:21:57.113 に答える
1

@GauravMantri の詳細な説明をありがとう。

リソース マネージャーを使用して作成された Java API を使用して、VM (非クラシック) のリストを取得する方法を教えてください。

Azure Reference for Virtual Machine RESTによると、共通のパラメーターとヘッダーで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();
于 2016-03-25T07:34:09.930 に答える