Skip to content

Commit 1cdd51b

Browse files
committed
Release azure-resourcemanager 2.0.0
1 parent 3e45504 commit 1cdd51b

File tree

4 files changed

+1667
-1426
lines changed

4 files changed

+1667
-1426
lines changed

README.md

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

3030
To run this sample:
3131

32-
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).
32+
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).
3333

3434
git clone https://github.com/Azure-Samples/compute-java-manage-vm.git
3535

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

4040
## More information ##
4141

42-
[http://azure.com/java](http://azure.com/java)
42+
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).
4343

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

4648
---
4749

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.ManageVirtualMachine</mainClass>
18+
<mainClass>com.azure.resourcemanager.compute.samples.ManageVirtualMachine</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.ManageVirtualMachine.java</mainClass>
44+
<mainClass>com.azure.resourcemanager.compute.samples.ManageVirtualMachine.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: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
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.management.AzureEnvironment;
9+
import com.azure.identity.DefaultAzureCredentialBuilder;
10+
import com.azure.resourcemanager.AzureResourceManager;
11+
import com.azure.resourcemanager.compute.models.Disk;
12+
import com.azure.resourcemanager.compute.models.KnownLinuxVirtualMachineImage;
13+
import com.azure.resourcemanager.compute.models.KnownWindowsVirtualMachineImage;
14+
import com.azure.resourcemanager.compute.models.VirtualMachine;
15+
import com.azure.resourcemanager.compute.models.VirtualMachineSizeTypes;
16+
import com.azure.resourcemanager.network.models.Network;
17+
import com.azure.core.management.Region;
18+
import com.azure.resourcemanager.resources.fluentcore.model.Creatable;
19+
import com.azure.core.management.profile.AzureProfile;
20+
import com.azure.resourcemanager.samples.Utils;
621

7-
package com.microsoft.azure.management.compute.samples;
8-
9-
import com.microsoft.azure.management.Azure;
10-
import com.microsoft.azure.management.compute.Disk;
11-
import com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage;
12-
import com.microsoft.azure.management.compute.KnownWindowsVirtualMachineImage;
13-
import com.microsoft.azure.management.compute.VirtualMachine;
14-
import com.microsoft.azure.management.compute.VirtualMachineSizeTypes;
15-
import com.microsoft.azure.management.network.Network;
16-
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
17-
import com.microsoft.azure.management.resources.fluentcore.model.Creatable;
18-
import com.microsoft.azure.management.samples.Utils;
19-
import com.microsoft.rest.LogLevel;
20-
21-
import java.io.File;
2222
import java.util.Date;
2323

2424
/**
@@ -38,17 +38,16 @@ public final class ManageVirtualMachine {
3838

3939
/**
4040
* Main function which runs the actual sample.
41-
* @param azure instance of the azure client
41+
* @param azureResourceManager instance of the azure client
4242
* @return true if sample runs successfully
4343
*/
44-
public static boolean runSample(Azure azure) {
44+
public static boolean runSample(AzureResourceManager azureResourceManager) {
4545
final Region region = Region.US_WEST_CENTRAL;
46-
final String windowsVMName = Utils.createRandomName("wVM");
47-
final String linuxVMName = Utils.createRandomName("lVM");
48-
final String rgName = Utils.createRandomName("rgCOMV");
46+
final String windowsVMName = Utils.randomResourceName(azureResourceManager, "wVM", 15);
47+
final String linuxVMName = Utils.randomResourceName(azureResourceManager, "lVM", 15);
48+
final String rgName = Utils.randomResourceName(azureResourceManager, "rgCOMV", 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 = "12NewPA$$w0rd!";
50+
final String password = Utils.password();
5251

5352
try {
5453

@@ -57,16 +56,16 @@ public static boolean runSample(Azure azure) {
5756

5857
// Prepare a creatable data disk for VM
5958
//
60-
Creatable<Disk> dataDiskCreatable = azure.disks().define(Utils.createRandomName("dsk-"))
59+
Creatable<Disk> dataDiskCreatable = azureResourceManager.disks().define(Utils.randomResourceName(azureResourceManager, "dsk-", 15))
6160
.withRegion(region)
6261
.withExistingResourceGroup(rgName)
6362
.withData()
6463
.withSizeInGB(100);
6564

6665
// Create a data disk to attach to VM
6766
//
68-
Disk dataDisk = azure.disks()
69-
.define(Utils.createRandomName("dsk-"))
67+
Disk dataDisk = azureResourceManager.disks()
68+
.define(Utils.randomResourceName(azureResourceManager, "dsk-", 15))
7069
.withRegion(region)
7170
.withNewResourceGroup(rgName)
7271
.withData()
@@ -77,7 +76,7 @@ public static boolean runSample(Azure azure) {
7776

7877
Date t1 = new Date();
7978

80-
VirtualMachine windowsVM = azure.virtualMachines()
79+
VirtualMachine windowsVM = azureResourceManager.virtualMachines()
8180
.define(windowsVMName)
8281
.withRegion(region)
8382
.withNewResourceGroup(rgName)
@@ -113,9 +112,9 @@ public static boolean runSample(Azure azure) {
113112
//=============================================================
114113
// Update - Add data disk
115114

116-
windowsVM.update()
117-
.withNewDataDisk(10)
118-
.apply();
115+
windowsVM.update()
116+
.withNewDataDisk(10)
117+
.apply();
119118

120119

121120
System.out.println("Added a data disk to VM" + windowsVM.id());
@@ -125,9 +124,9 @@ public static boolean runSample(Azure azure) {
125124
//=============================================================
126125
// Update - detach data disk
127126

128-
windowsVM.update()
129-
.withoutDataDisk(0)
130-
.apply();
127+
windowsVM.update()
128+
.withoutDataDisk(0)
129+
.apply();
131130

132131
System.out.println("Detached data disk at lun 0 from VM " + windowsVM.id());
133132

@@ -160,7 +159,7 @@ public static boolean runSample(Azure azure) {
160159

161160
System.out.println("Creating a Linux VM in the network");
162161

163-
VirtualMachine linuxVM = azure.virtualMachines()
162+
VirtualMachine linuxVM = azureResourceManager.virtualMachines()
164163
.define(linuxVMName)
165164
.withRegion(region)
166165
.withExistingResourceGroup(rgName)
@@ -184,36 +183,30 @@ public static boolean runSample(Azure azure) {
184183

185184
System.out.println("Printing list of VMs =======");
186185

187-
for (VirtualMachine virtualMachine : azure.virtualMachines().listByResourceGroup(resourceGroupName)) {
186+
for (VirtualMachine virtualMachine : azureResourceManager.virtualMachines().listByResourceGroup(resourceGroupName)) {
188187
Utils.print(virtualMachine);
189188
}
190189

191190
//=============================================================
192191
// Delete the virtual machine
193192
System.out.println("Deleting VM: " + windowsVM.id());
194193

195-
azure.virtualMachines().deleteById(windowsVM.id());
194+
azureResourceManager.virtualMachines().deleteById(windowsVM.id());
196195

197196
System.out.println("Deleted VM: " + windowsVM.id());
198197
return true;
199-
} catch (Exception f) {
200-
201-
System.out.println(f.getMessage());
202-
f.printStackTrace();
203-
204198
} finally {
205199

206200
try {
207201
System.out.println("Deleting Resource Group: " + rgName);
208-
azure.resourceGroups().deleteByName(rgName);
202+
azureResourceManager.resourceGroups().beginDeleteByName(rgName);
209203
System.out.println("Deleted Resource Group: " + rgName);
210204
} catch (NullPointerException npe) {
211205
System.out.println("Did not create any resources in Azure. No clean up is necessary");
212206
} catch (Exception g) {
213207
g.printStackTrace();
214208
}
215209
}
216-
return false;
217210
}
218211

219212
/**
@@ -226,17 +219,21 @@ public static void main(String[] args) {
226219
//=============================================================
227220
// Authenticate
228221

229-
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
222+
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
223+
final TokenCredential credential = new DefaultAzureCredentialBuilder()
224+
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
225+
.build();
230226

231-
Azure azure = Azure.configure()
232-
.withLogLevel(LogLevel.BASIC)
233-
.authenticate(credFile)
234-
.withDefaultSubscription();
227+
AzureResourceManager azureResourceManager = AzureResourceManager
228+
.configure()
229+
.withLogLevel(HttpLogDetailLevel.BASIC)
230+
.authenticate(credential, profile)
231+
.withDefaultSubscription();
235232

236233
// Print selected subscription
237-
System.out.println("Selected subscription: " + azure.subscriptionId());
234+
System.out.println("Selected subscription: " + azureResourceManager.subscriptionId());
238235

239-
runSample(azure);
236+
runSample(azureResourceManager);
240237
} catch (Exception e) {
241238
System.out.println(e.getMessage());
242239
e.printStackTrace();
@@ -246,4 +243,4 @@ public static void main(String[] args) {
246243
private ManageVirtualMachine() {
247244

248245
}
249-
}
246+
}

0 commit comments

Comments
 (0)