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-
9- import com .microsoft . azure .management . Azure ;
10- import com .microsoft . azure .management . compute . KnownLinuxVirtualMachineImage ;
11- import com .microsoft . azure .management .compute .VirtualMachine ;
12- import com .microsoft . azure .management .compute .VirtualMachineSizeTypes ;
13- import com .microsoft . azure .management . resources . fluentcore . arm . Region ;
14- import com .microsoft . azure .management .samples . Utils ;
15- import com .microsoft . rest . LogLevel ;
16- import java . io . File ;
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 .resourcemanager . compute . models . VirtualMachineSizeTypes ;
14+ import com .azure .core . management .Region ;
15+ import com .azure . core . management . profile . AzureProfile ;
16+ import com . azure . resourcemanager . samples . Utils ;
1717
1818/**
1919 * Azure Compute sample for managing virtual machines -
@@ -26,24 +26,23 @@ public final class ConvertVirtualMachineToManagedDisks {
2626 /**
2727 * Main function which runs the actual sample.
2828 *
29- * @param azure instance of the azure client
29+ * @param azureResourceManager instance of the azure client
3030 * @return true if sample runs successfully
3131 */
32- public static boolean runSample (Azure azure ) {
33- final String linuxVMName = Utils .createRandomName ( "VM1" );
34- final String rgName = Utils .createRandomName ( "rgCOMV" );
32+ public static boolean runSample (AzureResourceManager azureResourceManager ) {
33+ final String linuxVMName = Utils .randomResourceName ( azureResourceManager , "VM1" , 15 );
34+ final String rgName = Utils .randomResourceName ( azureResourceManager , "rgCOMV" , 15 );
3535 final String userName = "tirekicker" ;
36- // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
37- final String password = "12NewPA$$w0rd!" ;
38- final Region region = Region .US_WEST_CENTRAL ;
36+ final String password = Utils .password ();
37+ final Region region = Region .US_WEST ;
3938
4039 try {
4140 //=============================================================
4241 // Create a Linux VM using a PIR image with un-managed OS and data disks
4342
4443 System .out .println ("Creating an un-managed Linux VM" );
4544
46- VirtualMachine linuxVM = azure .virtualMachines ().define (linuxVMName )
45+ VirtualMachine linuxVM = azureResourceManager .virtualMachines ().define (linuxVMName )
4746 .withRegion (region )
4847 .withNewResourceGroup (rgName )
4948 .withNewPrimaryNetwork ("10.0.0.0/28" )
@@ -86,22 +85,17 @@ public static boolean runSample(Azure azure) {
8685 Utils .print (linuxVM );
8786
8887 return true ;
89- } catch (Exception f ) {
90- System .out .println (f .getMessage ());
91- f .printStackTrace ();
9288 } finally {
9389 try {
9490 System .out .println ("Deleting Resource Group: " + rgName );
95- azure .resourceGroups ().deleteByName (rgName );
91+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
9692 System .out .println ("Deleted Resource Group: " + rgName );
9793 } catch (NullPointerException npe ) {
9894 System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
9995 } catch (Exception g ) {
10096 g .printStackTrace ();
10197 }
10298 }
103-
104- return false ;
10599 }
106100
107101 /**
@@ -113,17 +107,21 @@ public static void main(String[] args) {
113107 //=============================================================
114108 // Authenticate
115109
116- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
110+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
111+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
112+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
113+ .build ();
117114
118- Azure azure = Azure .configure ()
119- .withLogLevel (LogLevel .BODY )
120- .authenticate (credFile )
121- .withDefaultSubscription ();
115+ AzureResourceManager azureResourceManager = AzureResourceManager
116+ .configure ()
117+ .withLogLevel (HttpLogDetailLevel .BASIC )
118+ .authenticate (credential , profile )
119+ .withDefaultSubscription ();
122120
123121 // Print selected subscription
124- System .out .println ("Selected subscription: " + azure .subscriptionId ());
122+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
125123
126- runSample (azure );
124+ runSample (azureResourceManager );
127125 } catch (Exception e ) {
128126 System .out .println (e .getMessage ());
129127 e .printStackTrace ();
0 commit comments