Azureの小ネタ (改)

~Azureネタを中心に、色々とその他の技術的なことなどを~

Azure SDK for Java 1.0.0 beta

AutoRestに対応したJava SDK 1.0.0 beta がリリースされてます。GitHubのAutoRestブランチを参照してください。

github.com

0.9系のAPIから大幅に変更されているため互換がありません。ただ各言語共通フレームワークからの自動生成なので、各言語間のクラス構成やメソッド名などの差異が(言語差はあるとして)ありません。

pom

READMEのとおりだと、依存が見つからないのでVMを操作するときの最小限の依存関係のpomを張っておきます(備忘録)

  • compute がVMを操作するもの
  • adal4j は Azure Active Directory
  • client authenticationはクライアント認証に必要な補足的なクラス
    <dependencies>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-mgmt-compute</artifactId>
            <version>1.0.0-beta1</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-client-authentication</artifactId>
            <version>1.0.0-beta1</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>adal4j</artifactId>
            <version>1.1.2</version>
        </dependency>
    </dependencies>

使用例

例によってADALでアプリ登録を行い、テナントID、クライアントID、クライアントキーを取得しておき、ApplicationTokenCredentials クラスで認証を行います。 VMを操作するクラスとしてComputeManagementClientを使いますが、サブスクリプションはそこに設定するみたいです。

あとは、Operationオブジェクトを取得してVM情報を取得する流れとなります。

    private static void getVirtualMachine(
            String subscriptionId,
            String tenantId,
            String clientId,
            String clientKey)
            throws CloudException, IOException, InterruptedException {
        
        ApplicationTokenCredentials creds = new ApplicationTokenCredentials(
                clientId, tenantId, clientKey, null);

        ComputeManagementClientImpl client = new ComputeManagementClientImpl(creds);
        client.setSubscriptionId(subscriptionId);

        String rg = "your-resourece-group-name";
        String rs = "your-vm-name";
        ServiceResponse<VirtualMachine> response = client.getVirtualMachinesOperations().get(rg, rs, "instanceview");
        System.out.println(response.getBody().getName());
    }

インスタンスビューを取得するのに、第3引数に"instanceview"を指定するのは,前BLOG ARM API およびSDKでの仮想マシンの状態取得 - Azureの小ネタ (改) のとおりです。