Skip to content

Commit ac13ced

Browse files
committed
Release azure-resourcemanager 2.0.0
1 parent c738ada commit ac13ced

File tree

4 files changed

+1666
-1423
lines changed

4 files changed

+1666
-1423
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extensions:
2424

2525
To run this sample:
2626

27-
Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an auth file. See [how to create an auth file](https://github.com/Azure/azure-libraries-for-java/blob/master/AUTH.md).
27+
See [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/identity/azure-identity#defaultazurecredential) and prepare the authentication works best for you. For more details on authentication, please refer to [AUTH.md](https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/resourcemanager/docs/AUTH.md).
2828

2929
git clone https://github.com/Azure-Samples/compute-java-manage-user-assigned-msi-enabled-virtual-machine.git
3030

@@ -34,9 +34,11 @@ Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an aut
3434

3535
## More information ##
3636

37-
[http://azure.com/java](http://azure.com/java)
37+
For general documentation as well as quickstarts on how to use Azure Management Libraries for Java, please see [here](https://aka.ms/azsdk/java/mgmt).
3838

39-
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
39+
Start to develop applications with Java on Azure [here](http://azure.com/java).
40+
41+
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212).
4042

4143
---
4244

pom.xml

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
<artifactId>exec-maven-plugin</artifactId>
1616
<version>1.4.0</version>
1717
<configuration>
18-
<mainClass>com.microsoft.azure.management.compute.samples.ManageUserAssignedMSIEnabledVirtualMachine</mainClass>
18+
<mainClass>com.azure.resourcemanager.compute.samples.ManageUserAssignedMSIEnabledVirtualMachine</mainClass>
19+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
1920
</configuration>
2021
</plugin>
2122
<plugin>
2223
<artifactId>maven-compiler-plugin</artifactId>
23-
<version>3.0</version>
24+
<version>3.8.1</version>
2425
<configuration>
25-
<source>1.7</source>
26-
<target>1.7</target>
26+
<source>1.8</source>
27+
<target>1.8</target>
2728
</configuration>
2829
</plugin>
2930
<plugin>
@@ -40,7 +41,7 @@
4041
</descriptorRefs>
4142
<archive>
4243
<manifest>
43-
<mainClass>com.microsoft.azure.management.compute.samples.ManageUserAssignedMSIEnabledVirtualMachine.java</mainClass>
44+
<mainClass>com.azure.resourcemanager.compute.samples.ManageUserAssignedMSIEnabledVirtualMachine.java</mainClass>
4445
</manifest>
4546
</archive>
4647
</configuration>
@@ -51,24 +52,19 @@
5152
</build>
5253
<dependencies>
5354
<dependency>
54-
<groupId>com.microsoft.azure</groupId>
55-
<artifactId>azure</artifactId>
56-
<version>1.36.3</version>
55+
<groupId>com.azure.resourcemanager</groupId>
56+
<artifactId>azure-resourcemanager</artifactId>
57+
<version>2.0.0</version>
5758
</dependency>
5859
<dependency>
59-
<groupId>commons-net</groupId>
60-
<artifactId>commons-net</artifactId>
61-
<version>3.3</version>
62-
</dependency>
63-
<dependency>
64-
<groupId>commons-lang</groupId>
65-
<artifactId>commons-lang</artifactId>
66-
<version>2.6</version>
60+
<groupId>com.azure</groupId>
61+
<artifactId>azure-identity</artifactId>
62+
<version>1.1.3</version>
6763
</dependency>
6864
<dependency>
69-
<groupId>org.apache.commons</groupId>
70-
<artifactId>commons-lang3</artifactId>
71-
<version>3.7</version>
65+
<groupId>commons-net</groupId>
66+
<artifactId>commons-net</artifactId>
67+
<version>3.6</version>
7268
</dependency>
7369
</dependencies>
7470
</project>
Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
/**
2-
* Copyright (c) Microsoft Corporation. All rights reserved.
3-
* Licensed under the MIT License. See License.txt in the project root for
4-
* license information.
5-
*/
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.credential.TokenCredential;
7+
import com.azure.core.http.policy.HttpLogDetailLevel;
8+
import com.azure.core.http.rest.PagedIterable;
9+
import com.azure.core.management.AzureEnvironment;
10+
import com.azure.identity.DefaultAzureCredentialBuilder;
11+
import com.azure.resourcemanager.AzureResourceManager;
12+
import com.azure.resourcemanager.compute.models.KnownLinuxVirtualMachineImage;
13+
import com.azure.resourcemanager.compute.models.RunCommandInput;
14+
import com.azure.resourcemanager.compute.models.RunCommandResult;
15+
import com.azure.resourcemanager.compute.models.VirtualMachine;
16+
import com.azure.resourcemanager.compute.models.VirtualMachineSizeTypes;
17+
import com.azure.resourcemanager.authorization.models.BuiltInRole;
18+
import com.azure.resourcemanager.msi.models.Identity;
19+
import com.azure.resourcemanager.resources.models.ResourceGroup;
20+
import com.azure.core.management.Region;
21+
import com.azure.core.management.profile.AzureProfile;
22+
import com.azure.resourcemanager.samples.Utils;
623

7-
package com.microsoft.azure.management.compute.samples;
8-
9-
import com.microsoft.azure.PagedList;
10-
import com.microsoft.azure.management.Azure;
11-
import com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage;
12-
import com.microsoft.azure.management.compute.RunCommandInput;
13-
import com.microsoft.azure.management.compute.RunCommandResult;
14-
import com.microsoft.azure.management.compute.VirtualMachine;
15-
import com.microsoft.azure.management.compute.VirtualMachineSizeTypes;
16-
import com.microsoft.azure.management.graphrbac.BuiltInRole;
17-
import com.microsoft.azure.management.msi.Identity;
18-
import com.microsoft.azure.management.resources.ResourceGroup;
19-
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
20-
import com.microsoft.azure.management.samples.Utils;
21-
import com.microsoft.rest.LogLevel;
22-
23-
import java.io.File;
2424
import java.util.ArrayList;
2525
import java.util.List;
2626

@@ -37,18 +37,17 @@ public final class ManageUserAssignedMSIEnabledVirtualMachine {
3737
/**
3838
* Main function which runs the actual sample.
3939
*
40-
* @param azure instance of the azure client
40+
* @param azureResourceManager instance of the azure client
4141
* @return true if sample runs successfully
4242
*/
43-
public static boolean runSample(Azure azure) {
44-
final String rgName1 = Utils.createRandomName("uamsi-rg-1");
45-
final String rgName2 = Utils.createRandomName("uamsi-rg-2");
46-
final String identityName = Utils.createRandomName("id");
47-
final String linuxVMName = Utils.createRandomName("VM1");
48-
final String pipName = Utils.createRandomName("pip1");
43+
public static boolean runSample(AzureResourceManager azureResourceManager) {
44+
final String rgName1 = Utils.randomResourceName(azureResourceManager, "uamsi-rg-1", 15);
45+
final String rgName2 = Utils.randomResourceName(azureResourceManager, "uamsi-rg-2", 15);
46+
final String identityName = Utils.randomResourceName(azureResourceManager, "id", 15);
47+
final String linuxVMName = Utils.randomResourceName(azureResourceManager, "VM1", 15);
48+
final String pipName = Utils.randomResourceName(azureResourceManager, "pip1", 15);
4949
final String userName = "tirekicker";
50-
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
51-
final String password = "12NewPAwX0rd!";
50+
final String password = Utils.password();
5251
final Region region = Region.US_WEST_CENTRAL;
5352

5453

@@ -59,12 +58,12 @@ public static boolean runSample(Azure azure) {
5958

6059
System.out.println("Creating a Resource Group and User Assigned MSI with CONTRIBUTOR access to the resource group");
6160

62-
ResourceGroup resourceGroup1 = azure.resourceGroups()
61+
ResourceGroup resourceGroup1 = azureResourceManager.resourceGroups()
6362
.define(rgName1)
6463
.withRegion(region)
6564
.create();
6665

67-
Identity identity = azure.identities()
66+
Identity identity = azureResourceManager.identities()
6867
.define(identityName)
6968
.withRegion(region)
7069
.withNewResourceGroup(rgName2)
@@ -89,7 +88,7 @@ public static boolean runSample(Azure azure) {
8988

9089
System.out.println("Creating a Linux VM with MSI associated and install Java8, Maven and Git");
9190

92-
VirtualMachine virtualMachine = azure.virtualMachines()
91+
VirtualMachine virtualMachine = azureResourceManager.virtualMachines()
9392
.define(linuxVMName)
9493
.withRegion(region)
9594
.withExistingResourceGroup(rgName2)
@@ -123,9 +122,9 @@ public static boolean runSample(Azure azure) {
123122
List<String> commands = new ArrayList<>();
124123
commands.add("git clone https://github.com/Azure-Samples/compute-java-manage-vm-from-vm-with-msi-credentials.git");
125124
commands.add("cd compute-java-manage-vm-from-vm-with-msi-credentials");
126-
commands.add(String.format("mvn clean compile exec:java -Dexec.args='%s %s %s'", azure.subscriptionId(), resourceGroup1.name(), identity.clientId()));
125+
commands.add(String.format("mvn clean compile exec:java -Dexec.args='%s %s %s'", azureResourceManager.subscriptionId(), resourceGroup1.name(), identity.clientId()));
127126

128-
runCommandOnVM(azure, virtualMachine, commands);
127+
runCommandOnVM(azureResourceManager, virtualMachine, commands);
129128

130129
System.out.println("Java application executed");
131130

@@ -134,36 +133,32 @@ public static boolean runSample(Azure azure) {
134133

135134
System.out.println("Retrieving the virtual machine created from the MSI enabled Linux VM");
136135

137-
PagedList<VirtualMachine> virtualMachines = azure.virtualMachines().listByResourceGroup(resourceGroup1.name());
136+
PagedIterable<VirtualMachine> virtualMachines = azureResourceManager.virtualMachines().listByResourceGroup(resourceGroup1.name());
138137
for (VirtualMachine vm : virtualMachines) {
139138
Utils.print(vm);
140139
}
141140

142141
return true;
143-
} catch (Exception e) {
144-
System.out.println(e.getMessage());
145-
e.printStackTrace();
146142
} finally {
147143
try {
148144
System.out.println("Deleting Resource Group: " + rgName1);
149-
azure.resourceGroups().deleteByName(rgName1);
145+
azureResourceManager.resourceGroups().deleteByName(rgName1);
150146
System.out.println("Deleting Resource Group: " + rgName2);
151-
azure.resourceGroups().deleteByName(rgName2);
147+
azureResourceManager.resourceGroups().deleteByName(rgName2);
152148
} catch (NullPointerException npe) {
153149
System.out.println("Did not create any resources in Azure. No clean up is necessary");
154150
} catch (Exception g) {
155151
g.printStackTrace();
156152
}
157153
}
158-
return false;
159154
}
160155

161-
private static RunCommandResult runCommandOnVM(Azure azure, VirtualMachine virtualMachine, List<String> commands) {
156+
private static RunCommandResult runCommandOnVM(AzureResourceManager azureResourceManager, VirtualMachine virtualMachine, List<String> commands) {
162157
RunCommandInput runParams = new RunCommandInput()
163158
.withCommandId("RunShellScript")
164159
.withScript(commands);
165160

166-
return azure.virtualMachines().runCommand(virtualMachine.resourceGroupName(), virtualMachine.name(), runParams);
161+
return azureResourceManager.virtualMachines().runCommand(virtualMachine.resourceGroupName(), virtualMachine.name(), runParams);
167162
}
168163

169164
/**
@@ -175,17 +170,21 @@ public static void main(String[] args) {
175170
//=============================================================
176171
// Authenticate
177172

178-
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
173+
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
174+
final TokenCredential credential = new DefaultAzureCredentialBuilder()
175+
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
176+
.build();
179177

180-
Azure azure = Azure.configure()
181-
.withLogLevel(LogLevel.BODY_AND_HEADERS)
182-
.authenticate(credFile)
183-
.withDefaultSubscription();
178+
AzureResourceManager azureResourceManager = AzureResourceManager
179+
.configure()
180+
.withLogLevel(HttpLogDetailLevel.BASIC)
181+
.authenticate(credential, profile)
182+
.withDefaultSubscription();
184183

185184
// Print selected subscription
186-
System.out.println("Selected subscription: " + azure.subscriptionId());
185+
System.out.println("Selected subscription: " + azureResourceManager.subscriptionId());
187186

188-
runSample(azure);
187+
runSample(azureResourceManager);
189188
} catch (Exception e) {
190189
System.out.println(e.getMessage());
191190
e.printStackTrace();

0 commit comments

Comments
 (0)