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 ;
2222import 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