Skip to content

Commit ea3798d

Browse files
authored
Merge pull request #81754 from haolingdong-msft/compute-doc-patch
Update virtual machines java SDK Doc to use latest Azure Java SDK Track2
2 parents af30326 + b675c89 commit ea3798d

File tree

1 file changed

+35
-106
lines changed
  • articles/virtual-machines/windows

1 file changed

+35
-106
lines changed

articles/virtual-machines/windows/java.md

Lines changed: 35 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: cynthn
66
ms.service: virtual-machines
77
ms.workload: infrastructure
88
ms.topic: how-to
9-
ms.date: 07/17/2017
9+
ms.date: 10/09/2021
1010
ms.custom: devx-track-java
1111
ms.author: cynthn
1212

@@ -63,117 +63,45 @@ It takes about 20 minutes to do these steps.
6363
6464
```xml
6565
<dependency>
66-
<groupId>com.microsoft.azure</groupId>
67-
<artifactId>azure</artifactId>
68-
<version>1.1.0</version>
66+
<groupId>com.azure</groupId>
67+
<artifactId>azure-identity</artifactId>
68+
<version>1.3.6</version>
6969
</dependency>
7070
<dependency>
71-
<groupId>com.microsoft.azure</groupId>
72-
<artifactId>azure-mgmt-compute</artifactId>
73-
<version>1.1.0</version>
74-
</dependency>
75-
<dependency>
76-
<groupId>com.microsoft.azure</groupId>
77-
<artifactId>azure-mgmt-resources</artifactId>
78-
<version>1.1.0</version>
79-
</dependency>
80-
<dependency>
81-
<groupId>com.microsoft.azure</groupId>
82-
<artifactId>azure-mgmt-network</artifactId>
83-
<version>1.1.0</version>
84-
</dependency>
85-
<dependency>
86-
<groupId>com.squareup.okio</groupId>
87-
<artifactId>okio</artifactId>
88-
<version>1.13.0</version>
89-
</dependency>
90-
<dependency>
91-
<groupId>com.nimbusds</groupId>
92-
<artifactId>nimbus-jose-jwt</artifactId>
93-
<version>3.6</version>
94-
</dependency>
95-
<dependency>
96-
<groupId>net.minidev</groupId>
97-
<artifactId>json-smart</artifactId>
98-
<version>1.0.6.3</version>
99-
</dependency>
100-
<dependency>
101-
<groupId>javax.mail</groupId>
102-
<artifactId>mail</artifactId>
103-
<version>1.4.5</version>
71+
<groupId>com.azure.resourcemanager</groupId>
72+
<artifactId>azure-resourcemanager</artifactId>
73+
<version>2.8.0</version>
10474
</dependency>
10575
```
10676
10777
3. Save the file.
10878
109-
## Create credentials
110-
111-
Before you start this step, make sure that you have access to an [Active Directory service principal](../../active-directory/develop/howto-create-service-principal-portal.md). You should also record the application ID, the authentication key, and the tenant ID that you need in a later step.
79+
## Set up authentication
11280
113-
### Create the authorization file
81+
Learn how to [set up authentication](../azure/developer/java/sdk/get-started#set-up-authentication).
11482
115-
1. Create a file named `azureauth.properties` and add these properties to it:
116-
117-
```
118-
subscription=<subscription-id>
119-
client=<application-id>
120-
key=<authentication-key>
121-
tenant=<tenant-id>
122-
managementURI=https://management.core.windows.net/
123-
baseURL=https://management.azure.com/
124-
authURL=https://login.windows.net/
125-
graphURL=https://graph.microsoft.com/
126-
```
127-
128-
Replace **&lt;subscription-id&gt;** with your subscription identifier, **&lt;application-id&gt;** with the Active Directory application identifier, **&lt;authentication-key&gt;** with the application key, and **&lt;tenant-id&gt;** with the tenant identifier.
129-
130-
2. Save the file.
131-
3. Set an environment variable named AZURE_AUTH_LOCATION in your shell with the full path to the authentication file.
132-
133-
### Create the management client
83+
## Create the management client
13484
13585
1. Open the `App.java` file under `src\main\java\com\fabrikam` and make sure this package statement is at the top:
13686
13787
```java
13888
package com.fabrikam.testAzureApp;
13989
```
14090
141-
2. Under the package statement, add these import statements:
91+
2. Create AzureResourceManager:
14292
14393
```java
144-
import com.microsoft.azure.management.Azure;
145-
import com.microsoft.azure.management.compute.AvailabilitySet;
146-
import com.microsoft.azure.management.compute.AvailabilitySetSkuTypes;
147-
import com.microsoft.azure.management.compute.CachingTypes;
148-
import com.microsoft.azure.management.compute.InstanceViewStatus;
149-
import com.microsoft.azure.management.compute.DiskInstanceView;
150-
import com.microsoft.azure.management.compute.VirtualMachine;
151-
import com.microsoft.azure.management.compute.VirtualMachineSizeTypes;
152-
import com.microsoft.azure.management.network.PublicIPAddress;
153-
import com.microsoft.azure.management.network.Network;
154-
import com.microsoft.azure.management.network.NetworkInterface;
155-
import com.microsoft.azure.management.resources.ResourceGroup;
156-
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
157-
import com.microsoft.azure.management.resources.fluentcore.model.Creatable;
158-
import com.microsoft.rest.LogLevel;
159-
import java.io.File;
160-
import java.util.Scanner;
161-
```
94+
TokenCredential credential = new EnvironmentCredentialBuilder()
95+
.authorityHost(AzureAuthorityHosts.AZURE_PUBLIC_CLOUD)
96+
.build();
16297
163-
2. To create the Active Directory credentials that you need to make requests, add this code to the main method of the App class:
164-
165-
```java
166-
try {
167-
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
168-
Azure azure = Azure.configure()
169-
.withLogLevel(LogLevel.BASIC)
170-
.authenticate(credFile)
171-
.withDefaultSubscription();
172-
} catch (Exception e) {
173-
System.out.println(e.getMessage());
174-
e.printStackTrace();
175-
}
98+
// Please finish 'Set up authentication' step first to set the four environment variables: AZURE_SUBSCRIPTION_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID
99+
AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
176100
101+
AzureResourceManager azureResourceManager = AzureResourceManager.configure()
102+
.withLogLevel(HttpLogDetailLevel.BASIC)
103+
.authenticate(credential, profile)
104+
.withDefaultSubscription();
177105
```
178106
179107
## Create resources
@@ -204,7 +132,6 @@ AvailabilitySet availabilitySet = azure.availabilitySets()
204132
.define("myAvailabilitySet")
205133
.withRegion(Region.US_EAST)
206134
.withExistingResourceGroup("myResourceGroup")
207-
.withSku(AvailabilitySetSkuTypes.MANAGED)
208135
.create();
209136
```
210137
### Create the public IP address
@@ -215,7 +142,7 @@ To create the public IP address for the virtual machine, add this code to the tr
215142

216143
```java
217144
System.out.println("Creating public IP address...");
218-
PublicIPAddress publicIPAddress = azure.publicIPAddresses()
145+
PublicIpAddress publicIPAddress = azure.publicIpAddresses()
219146
.define("myPublicIP")
220147
.withRegion(Region.US_EAST)
221148
.withExistingResourceGroup("myResourceGroup")
@@ -236,7 +163,7 @@ Network network = azure.networks()
236163
.withRegion(Region.US_EAST)
237164
.withExistingResourceGroup("myResourceGroup")
238165
.withAddressSpace("10.0.0.0/16")
239-
.withSubnet("mySubnet","10.0.0.0/24")
166+
.withSubnet("mySubnet", "10.0.0.0/24")
240167
.create();
241168
```
242169

@@ -292,21 +219,22 @@ input.nextLine();
292219
If you want to use an existing disk instead of a marketplace image, use this code:
293220

294221
```java
295-
ManagedDisk managedDisk = azure.disks.define("myosdisk")
222+
Disk managedDisk = azure.disks().define("myosdisk")
296223
.withRegion(Region.US_EAST)
297224
.withExistingResourceGroup("myResourceGroup")
298225
.withWindowsFromVhd("https://mystorage.blob.core.windows.net/vhds/myosdisk.vhd")
226+
.withStorageAccountName("mystorage")
299227
.withSizeInGB(128)
300-
.withSku(DiskSkuTypes.PremiumLRS)
228+
.withSku(DiskSkuTypes.PREMIUM_LRS)
301229
.create();
302230

303-
azure.virtualMachines.define("myVM")
231+
azure.virtualMachines().define("myVM")
304232
.withRegion(Region.US_EAST)
305233
.withExistingResourceGroup("myResourceGroup")
306234
.withExistingPrimaryNetworkInterface(networkInterface)
307-
.withSpecializedOSDisk(managedDisk, OperatingSystemTypes.Windows)
235+
.withSpecializedOSDisk(managedDisk, OperatingSystemTypes.WINDOWS)
308236
.withExistingAvailabilitySet(availabilitySet)
309-
.withSize(VirtualMachineSizeTypes.StandardDS1)
237+
.withSize(VirtualMachineSizeTypes.STANDARD_DS1)
310238
.create();
311239
```
312240

@@ -342,23 +270,24 @@ System.out.println("osProfile");
342270
System.out.println(" computerName: " + vm.osProfile().computerName());
343271
System.out.println(" adminUserName: " + vm.osProfile().adminUsername());
344272
System.out.println(" provisionVMAgent: " + vm.osProfile().windowsConfiguration().provisionVMAgent());
345-
System.out.println(" enableAutomaticUpdates: " + vm.osProfile().windowsConfiguration().enableAutomaticUpdates());
273+
System.out.println(
274+
" enableAutomaticUpdates: " + vm.osProfile().windowsConfiguration().enableAutomaticUpdates());
346275
System.out.println("networkProfile");
347276
System.out.println(" networkInterface: " + vm.primaryNetworkInterfaceId());
348277
System.out.println("vmAgent");
349278
System.out.println(" vmAgentVersion: " + vm.instanceView().vmAgent().vmAgentVersion());
350279
System.out.println(" statuses");
351-
for(InstanceViewStatus status : vm.instanceView().vmAgent().statuses()) {
280+
for (InstanceViewStatus status : vm.instanceView().vmAgent().statuses()) {
352281
System.out.println(" code: " + status.code());
353282
System.out.println(" displayStatus: " + status.displayStatus());
354283
System.out.println(" message: " + status.message());
355284
System.out.println(" time: " + status.time());
356285
}
357286
System.out.println("disks");
358-
for(DiskInstanceView disk : vm.instanceView().disks()) {
287+
for (DiskInstanceView disk : vm.instanceView().disks()) {
359288
System.out.println(" name: " + disk.name());
360289
System.out.println(" statuses");
361-
for(InstanceViewStatus status : disk.statuses()) {
290+
for (InstanceViewStatus status : disk.statuses()) {
362291
System.out.println(" code: " + status.code());
363292
System.out.println(" displayStatus: " + status.displayStatus());
364293
System.out.println(" time: " + status.time());
@@ -370,7 +299,7 @@ System.out.println(" id: " + vm.id());
370299
System.out.println(" name: " + vm.name());
371300
System.out.println(" type: " + vm.type());
372301
System.out.println("VM instance status");
373-
for(InstanceViewStatus status : vm.instanceView().statuses()) {
302+
for (InstanceViewStatus status : vm.instanceView().statuses()) {
374303
System.out.println(" code: " + status.code());
375304
System.out.println(" displayStatus: " + status.displayStatus());
376305
}
@@ -463,4 +392,4 @@ It should take about five minutes for this console application to run completely
463392

464393

465394
## Next steps
466-
* Learn more about using the [Azure libraries for Java](/java/azure/java-sdk-azure-overview).
395+
* Learn more about using the [Azure libraries for Java](/java/azure/java-sdk-azure-overview).

0 commit comments

Comments
 (0)