ご存じの方も多いと思うが Microsoft Azure には動作モードに ARM(Azure Resource Manager) と ASM(Azure Service Manager) という二つの動作モードが存在する。昨今、Java 版 ARM SDK の Preview 版が発表されたので利用してみた。
SDK のソースコードは GitHub にて公開されているので、全体が閲覧可能になっている。こちらのREADMEによれば Authenticating a service principal with Azure Resource Manager を参照してサービス プリンシパルの認証を設定する必要がある。
上記の記事を参考に実行したコマンドは以下になる。Authenticating a service principal with Azure Resource Manager の記事を参照すれば特に問題ないが、New-AzureRoleAssignment コマンド実行時に Role を Owner 等(少なくとも Reader ではダメ)にしないと後で SDK を利用したリソース作成ができない点に注意だ。
# ARM モードにして認証する Switch-AzureMode AzureResourceManager Add-AzureAccount # Azure Active Directory アプリケーションのサービス プリンシパルを作成 $azureAdApplication = New-AzureADApplication -DisplayName "ARM Java SDK" -HomePage "<home page>" -IdentifierUris "<identifieruri>" -Password "<password>" New-AzureADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId # ApplicationId か IdentifierUri をユーザ名として利用する $creds = Get-Credential Add-AzureAccount -Credential $creds -ServicePrincipal -Tenant $subscription.TenantId # リソースへのアクセス権を付与する New-AzureRoleAssignment -RoleDefinitionName Owner -ServicePrincipalName $azureAdApplication.ApplicationId
上記でサービスプリンシパル認証の設定が完了したので、Gradle を設定して依存関係を解決する。念のため省略記法では記載していないが、com.microsoft.azure の group は SDK を稼働させる際に必須になるので注意してほしい。
apply plugin: 'java' apply plugin: 'eclipse' sourceCompatibility = 1.8 version = '1.0' jar { manifest { attributes 'Implementation-Title': 'Hello Azure ARM SDK Preview', 'Implementation-Version': version } } repositories { mavenCentral() } dependencies { compile group: 'com.microsoft.azure', name: 'azure-core', version: '0.8.0' compile group: 'com.microsoft.azure', name: 'azure-mgmt-resources', version: '0.8.0' compile group: 'com.microsoft.azure', name: 'azure-mgmt-storage', version: '0.8.0' compile group: 'com.microsoft.azure', name: 'azure-mgmt-network', version: '0.8.0' compile group: 'com.microsoft.azure', name: 'azure-mgmt-compute', version: '0.8.0' compile group: 'com.microsoft.azure', name: 'azure-mgmt-utility', version: '0.8.0' compile group: 'com.microsoft.azure', name: 'adal4j', version: '1.0.0' compile group: 'commons-logging', name: 'commons-logging', version: '1.2' compile group: 'org.slf4j', name: 'slf4j-jdk14', version: '1.5.6' testCompile group: 'junit', name: 'junit', version: '4.+' } test { systemProperties 'property': 'value' } uploadArchives { repositories { flatDir { dirs 'repos' } } }
MVN Repository の Home » com.microsoft.azure » azure-management-compute を参照したところ、0.8.0 が最新だったので上記のバージョンを設定しているが、適宜変更すること。
準備が整ったので以下のソースコードを実行するが、プロジェクト全体はGitHub/normalian/HelloAzureARMSDK を参照すること。
public static void main(String[] args) throws Exception { Configuration config = createConfiguration(); ResourceManagementClient resourceManagementClient = ResourceManagementService .create(config); StorageManagementClient storageManagementClient = StorageManagementService .create(config); ComputeManagementClient computeManagementClient = ComputeManagementService .create(config); NetworkResourceProviderClient networkResourceProviderClient = NetworkResourceProviderService .create(config); String resourceGroupName = "java-armsdk-group"; // Get-AzureLocation で確認 String region = "EastAsia"; ResourceContext context = new ResourceContext(region, resourceGroupName, subscriptionId, false); System.out.println("Start create vm..."); // VM 作成 try { VirtualMachine vm = ComputeHelper.createVM( resourceManagementClient, computeManagementClient, networkResourceProviderClient, storageManagementClient, context, "javaSampleVM", "normalian", "P@ssw0rd2015") .getVirtualMachine(); System.out.println(vm.getName() + " is created"); } catch (Exception ex) { ex.printStackTrace(); } // リソースグループごと仮想マシンを削除、残す場合はコメントアウトする resourceManagementClient.getResourceGroupsOperations().beginDeleting( context.getResourceGroupName()); System.out.println("end"); } public static Configuration createConfiguration() throws Exception { return ManagementConfiguration.configure( null, new URI(armUrl), subscriptionId, AuthHelper.getAccessTokenFromServicePrincipalCredentials( managementUri, armAadUrl, armTenant, armClientId, armClientkey).getAccessToken()); } }
上記を見て分かる通り、ソースコード自体は非常にシンプルだ。次回以降で ARM SDK を利用して細かな制御を確認したいと思う。