Skip to content

Commit 404c82f

Browse files
committed
Release azure-resourcemanager 2.0.0
1 parent a3cd683 commit 404c82f

File tree

5 files changed

+1713
-1460
lines changed

5 files changed

+1713
-1460
lines changed

README.md

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

2828
To run this sample:
2929

30-
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).
30+
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).
3131

3232
git clone https://github.com/Azure-Samples/managed-disk-java-create-virtual-machine-using-custom-image-from-VHD.git
3333

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

3838
## More information ##
3939

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

42-
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
42+
Start to develop applications with Java on Azure [here](http://azure.com/java).
43+
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).
4345

4446
---
4547

pom.xml

Lines changed: 17 additions & 21 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.CreateVirtualMachineUsingCustomImageFromVHD</mainClass>
18+
<mainClass>com.azure.resourcemanager.compute.samples.CreateVirtualMachineUsingCustomImageFromVHD</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.CreateVirtualMachineUsingCustomImageFromVHD.java</mainClass>
44+
<mainClass>com.azure.resourcemanager.compute.samples.CreateVirtualMachineUsingCustomImageFromVHD.java</mainClass>
4445
</manifest>
4546
</archive>
4647
</configuration>
@@ -51,30 +52,25 @@
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>
67-
</dependency>
68-
<dependency>
69-
<groupId>org.apache.commons</groupId>
70-
<artifactId>commons-lang3</artifactId>
71-
<version>3.7</version>
60+
<groupId>com.azure</groupId>
61+
<artifactId>azure-identity</artifactId>
62+
<version>1.1.3</version>
7263
</dependency>
7364
<dependency>
7465
<groupId>com.jcraft</groupId>
7566
<artifactId>jsch</artifactId>
7667
<version>0.1.55</version>
7768
</dependency>
69+
<dependency>
70+
<groupId>commons-net</groupId>
71+
<artifactId>commons-net</artifactId>
72+
<version>3.6</version>
73+
</dependency>
7874
<dependency>
7975
<groupId>com.github.cverges.expect4j</groupId>
8076
<artifactId>expect4j</artifactId>
Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
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-
*/
6-
7-
package com.microsoft.azure.management.compute.samples;
8-
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.KnownLinuxVirtualMachineImage;
12+
import com.azure.resourcemanager.compute.models.VirtualMachine;
13+
import com.azure.core.management.Region;
14+
import com.azure.core.management.profile.AzureProfile;
915
import com.jcraft.jsch.JSchException;
10-
import com.microsoft.azure.management.Azure;
11-
import com.microsoft.azure.management.compute.CachingTypes;
12-
import com.microsoft.azure.management.compute.Disk;
13-
import com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage;
14-
import com.microsoft.azure.management.compute.OperatingSystemStateTypes;
15-
import com.microsoft.azure.management.compute.VirtualMachine;
16-
import com.microsoft.azure.management.compute.VirtualMachineCustomImage;
17-
import com.microsoft.azure.management.compute.VirtualMachineDataDisk;
18-
import com.microsoft.azure.management.compute.VirtualMachineSizeTypes;
19-
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
20-
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
21-
import com.microsoft.azure.management.samples.SSHShell;
22-
import com.microsoft.azure.management.samples.Utils;
23-
import com.microsoft.rest.LogLevel;
24-
25-
import java.io.File;
16+
import com.azure.resourcemanager.compute.models.CachingTypes;
17+
import com.azure.resourcemanager.compute.models.Disk;
18+
import com.azure.resourcemanager.compute.models.OperatingSystemStateTypes;
19+
import com.azure.resourcemanager.compute.models.VirtualMachineCustomImage;
20+
import com.azure.resourcemanager.compute.models.VirtualMachineDataDisk;
21+
import com.azure.resourcemanager.compute.models.VirtualMachineSizeTypes;
22+
import com.azure.resourcemanager.samples.SSHShell;
23+
import com.azure.resourcemanager.samples.Utils;
24+
2625
import java.io.IOException;
2726
import java.util.ArrayList;
2827
import java.util.List;
@@ -42,22 +41,21 @@ public final class CreateVirtualMachineUsingCustomImageFromVHD {
4241

4342
/**
4443
* Main function which runs the actual sample.
45-
* @param azure instance of the azure client
44+
* @param azureResourceManager instance of the azure client
4645
* @return true if sample runs successfully
4746
*/
48-
public static boolean runSample(Azure azure) {
49-
final String linuxVMName1 = SdkContext.randomResourceName("VM1", 10);
50-
final String linuxVMName2 = SdkContext.randomResourceName("VM2", 10);
51-
final String linuxVMName3 = SdkContext.randomResourceName("VM3", 10);
52-
final String customImageName = SdkContext.randomResourceName("img", 10);
53-
final String rgName = SdkContext.randomResourceName("rgCOMV", 15);
54-
final String publicIPDnsLabel = SdkContext.randomResourceName("pip", 10);
47+
public static boolean runSample(AzureResourceManager azureResourceManager) {
48+
final String linuxVMName1 = Utils.randomResourceName(azureResourceManager, "VM1", 10);
49+
final String linuxVMName2 = Utils.randomResourceName(azureResourceManager, "VM2", 10);
50+
final String linuxVMName3 = Utils.randomResourceName(azureResourceManager, "VM3", 10);
51+
final String customImageName = Utils.randomResourceName(azureResourceManager, "img", 10);
52+
final String rgName = Utils.randomResourceName(azureResourceManager, "rgCOMV", 15);
53+
final String publicIPDnsLabel = Utils.randomResourceName(azureResourceManager, "pip", 10);
5554
final String userName = "tirekicker";
56-
// [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
57-
final String password = "12NewPA$$w0rd!";
55+
final String password = Utils.password();
5856
final Region region = Region.US_WEST_CENTRAL;
5957

60-
final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-libraries-for-java/master/azure-samples/src/main/resources/install_apache.sh";
58+
final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/resourcemanager/azure-resourcemanager-samples/src/main/resources/install_apache.sh";
6159
final String apacheInstallCommand = "bash install_apache.sh";
6260
List<String> apacheInstallScriptUris = new ArrayList<>();
6361
apacheInstallScriptUris.add(apacheInstallScript);
@@ -69,7 +67,7 @@ public static boolean runSample(Azure azure) {
6967

7068
System.out.println("Creating a un-managed Linux VM");
7169

72-
VirtualMachine linuxVM = azure.virtualMachines().define(linuxVMName1)
70+
VirtualMachine linuxVM = azureResourceManager.virtualMachines().define(linuxVMName1)
7371
.withRegion(region)
7472
.withNewResourceGroup(rgName)
7573
.withNewPrimaryNetwork("10.0.0.0/28")
@@ -129,7 +127,7 @@ public static boolean runSample(Azure azure) {
129127

130128
System.out.println("Creating virtual machine custom image from un-managed disk VHDs: " + linuxVM.id());
131129

132-
VirtualMachineCustomImage virtualMachineCustomImage = azure.virtualMachineCustomImages()
130+
VirtualMachineCustomImage virtualMachineCustomImage = azureResourceManager.virtualMachineCustomImages()
133131
.define(customImageName)
134132
.withRegion(region)
135133
.withExistingResourceGroup(rgName)
@@ -158,13 +156,13 @@ public static boolean runSample(Azure azure) {
158156

159157
System.out.println("Creating a Linux VM using custom image: " + virtualMachineCustomImage.id());
160158

161-
VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVMName2)
159+
VirtualMachine linuxVM2 = azureResourceManager.virtualMachines().define(linuxVMName2)
162160
.withRegion(region)
163161
.withExistingResourceGroup(rgName)
164162
.withNewPrimaryNetwork("10.0.0.0/28")
165163
.withPrimaryPrivateIPAddressDynamic()
166164
.withoutPrimaryPublicIPAddress()
167-
.withLinuxCustomImage(virtualMachineCustomImage.id())
165+
.withGeneralizedLinuxCustomImage(virtualMachineCustomImage.id())
168166
.withRootUsername(userName)
169167
.withRootPassword(password)
170168
.withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
@@ -177,13 +175,13 @@ public static boolean runSample(Azure azure) {
177175
// Create another Linux VM using custom image and configure the data disks from image and
178176
// add another data disk
179177

180-
VirtualMachine linuxVM3 = azure.virtualMachines().define(linuxVMName3)
178+
VirtualMachine linuxVM3 = azureResourceManager.virtualMachines().define(linuxVMName3)
181179
.withRegion(region)
182180
.withExistingResourceGroup(rgName)
183181
.withNewPrimaryNetwork("10.0.0.0/28")
184182
.withPrimaryPrivateIPAddressDynamic()
185183
.withoutPrimaryPublicIPAddress()
186-
.withLinuxCustomImage(virtualMachineCustomImage.id())
184+
.withGeneralizedLinuxCustomImage(virtualMachineCustomImage.id())
187185
.withRootUsername(userName)
188186
.withRootPassword(password)
189187
.withNewDataDiskFromImage(1, 200, CachingTypes.READ_WRITE)
@@ -208,15 +206,15 @@ public static boolean runSample(Azure azure) {
208206
System.out.println("Getting OS and data disks SAS Uris");
209207

210208
// OS Disk SAS Uri
211-
Disk osDisk = azure.disks().getById(linuxVM3.osDiskId());
209+
Disk osDisk = azureResourceManager.disks().getById(linuxVM3.osDiskId());
212210

213211
String osDiskSasUri = osDisk.grantAccess(24 * 60);
214212

215213
System.out.println("OS disk SAS Uri: " + osDiskSasUri);
216214

217215
// Data Disks SAS Uri
218216
for (VirtualMachineDataDisk disk : linuxVM3.dataDisks().values()) {
219-
Disk dataDisk = azure.disks().getById(disk.id());
217+
Disk dataDisk = azureResourceManager.disks().getById(disk.id());
220218
String dataDiskSasUri = dataDisk.grantAccess(24 * 60);
221219
System.out.println(String.format("Data disk (lun: %d) SAS Uri: %s", disk.lun(), dataDiskSasUri));
222220
}
@@ -225,25 +223,21 @@ public static boolean runSample(Azure azure) {
225223
// Deleting the custom image
226224
System.out.println("Deleting custom Image: " + virtualMachineCustomImage.id());
227225

228-
azure.virtualMachineCustomImages().deleteById(virtualMachineCustomImage.id());
226+
azureResourceManager.virtualMachineCustomImages().deleteById(virtualMachineCustomImage.id());
229227

230228
System.out.println("Deleted custom image");
231229
return true;
232-
} catch (Exception f) {
233-
System.out.println(f.getMessage());
234-
f.printStackTrace();
235230
} finally {
236231
try {
237232
System.out.println("Deleting Resource Group: " + rgName);
238-
azure.resourceGroups().beginDeleteByName(rgName);
233+
azureResourceManager.resourceGroups().beginDeleteByName(rgName);
239234
System.out.println("Deleted Resource Group: " + rgName);
240235
} catch (NullPointerException npe) {
241236
System.out.println("Did not create any resources in Azure. No clean up is necessary");
242237
} catch (Exception g) {
243238
g.printStackTrace();
244239
}
245240
}
246-
return false;
247241
}
248242

249243
/**
@@ -255,17 +249,21 @@ public static void main(String[] args) {
255249
//=============================================================
256250
// Authenticate
257251

258-
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
252+
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
253+
final TokenCredential credential = new DefaultAzureCredentialBuilder()
254+
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
255+
.build();
259256

260-
Azure azure = Azure.configure()
261-
.withLogLevel(LogLevel.BODY)
262-
.authenticate(credFile)
263-
.withDefaultSubscription();
257+
AzureResourceManager azureResourceManager = AzureResourceManager
258+
.configure()
259+
.withLogLevel(HttpLogDetailLevel.BASIC)
260+
.authenticate(credential, profile)
261+
.withDefaultSubscription();
264262

265263
// Print selected subscription
266-
System.out.println("Selected subscription: " + azure.subscriptionId());
264+
System.out.println("Selected subscription: " + azureResourceManager.subscriptionId());
267265

268-
runSample(azure);
266+
runSample(azureResourceManager);
269267
} catch (Exception e) {
270268
System.out.println(e.getMessage());
271269
e.printStackTrace();

0 commit comments

Comments
 (0)