|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.resourcemanager.compute.samples; |
| 5 | + |
| 6 | +import com.azure.core.http.policy.HttpLogDetailLevel; |
| 7 | +import com.azure.core.management.AzureEnvironment; |
| 8 | +import com.azure.core.management.profile.AzureProfile; |
| 9 | +import com.azure.identity.ManagedIdentityCredential; |
| 10 | +import com.azure.identity.ManagedIdentityCredentialBuilder; |
| 11 | +import com.azure.resourcemanager.AzureResourceManager; |
| 12 | +import com.azure.resourcemanager.compute.models.KnownLinuxVirtualMachineImage; |
| 13 | +import com.azure.resourcemanager.compute.models.VirtualMachine; |
| 14 | +import com.azure.core.management.Region; |
| 15 | +import com.azure.resourcemanager.samples.Utils; |
| 16 | + |
| 17 | +/** |
| 18 | + * Azure Compute sample for managing virtual machine from Managed Service Identity (MSI) enabled virtual machine - |
| 19 | + * - Create a virtual machine using MSI credentials from System assigned or User Assigned MSI enabled VM. |
| 20 | + */ |
| 21 | +public final class ManageVirtualMachineFromMSIEnabledVirtualMachine { |
| 22 | + /** |
| 23 | + * Main entry point. |
| 24 | + * |
| 25 | + * @param args the parameters |
| 26 | + */ |
| 27 | + public static void main(String[] args) { |
| 28 | + final Region region = Region.US_WEST_CENTRAL; |
| 29 | + |
| 30 | + // This sample required to be run from a ManagedIdentityCredential (User Assigned or System Assigned) enabled virtual |
| 31 | + // machine with role based contributor access to the resource group specified as the second command line argument. |
| 32 | + // |
| 33 | + // see https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/overview |
| 34 | + // |
| 35 | + |
| 36 | + final String usage = "Usage: mvn clean compile exec:java -Dexec.args=\"<subscription-id> <rg-name> [<client-id>]\""; |
| 37 | + if (args.length < 2) { |
| 38 | + throw new IllegalArgumentException(usage); |
| 39 | + } |
| 40 | + |
| 41 | + final String subscriptionId = args[0]; |
| 42 | + final String resourceGroupName = args[1]; |
| 43 | + final String clientId = args.length > 2 ? args[2] : null; |
| 44 | + final String linuxVMName = "yourVirtualMachineName"; |
| 45 | + final String userName = "tirekicker"; |
| 46 | + final String password = Utils.password(); |
| 47 | + |
| 48 | + //============================================================= |
| 49 | + // ManagedIdentityCredential Authenticate |
| 50 | + |
| 51 | + ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder() |
| 52 | + .clientId(clientId) |
| 53 | + .build(); |
| 54 | + |
| 55 | + AzureProfile profile = new AzureProfile(null, subscriptionId, AzureEnvironment.AZURE); |
| 56 | + |
| 57 | + AzureResourceManager azure = AzureResourceManager.configure() |
| 58 | + .withLogLevel(HttpLogDetailLevel.BASIC) |
| 59 | + .authenticate(credential, profile) |
| 60 | + .withSubscription(subscriptionId); |
| 61 | + |
| 62 | + // Print selected subscription |
| 63 | + System.out.println("Selected subscription: " + azure.subscriptionId()); |
| 64 | + |
| 65 | + //============================================================= |
| 66 | + // Create a Linux VM using MSI credentials |
| 67 | + |
| 68 | + System.out.println("Creating a Linux VM using ManagedIdentityCredential."); |
| 69 | + |
| 70 | + VirtualMachine virtualMachine = azure.virtualMachines() |
| 71 | + .define(linuxVMName) |
| 72 | + .withRegion(region) |
| 73 | + .withExistingResourceGroup(resourceGroupName) |
| 74 | + .withNewPrimaryNetwork("10.0.0.0/28") |
| 75 | + .withPrimaryPrivateIPAddressDynamic() |
| 76 | + .withoutPrimaryPublicIPAddress() |
| 77 | + .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS) |
| 78 | + .withRootUsername(userName) |
| 79 | + .withRootPassword(password) |
| 80 | + .create(); |
| 81 | + |
| 82 | + System.out.println("Created virtual machine using ManagedIdentityCredential."); |
| 83 | + Utils.print(virtualMachine); |
| 84 | + |
| 85 | + System.out.println("Deleting resource group: " + resourceGroupName); |
| 86 | + azure.resourceGroups().deleteByName(resourceGroupName); |
| 87 | + System.out.println("Deleted resource group: " + resourceGroupName); |
| 88 | + } |
| 89 | + |
| 90 | + private ManageVirtualMachineFromMSIEnabledVirtualMachine() { |
| 91 | + } |
| 92 | +} |
0 commit comments