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 .CachingTypes ;
12+ import com .azure .resourcemanager .compute .models .Disk ;
13+ import com .azure .resourcemanager .compute .models .DiskSkuTypes ;
14+ import com .azure .resourcemanager .compute .models .KnownLinuxVirtualMachineImage ;
15+ import com .azure .resourcemanager .compute .models .OperatingSystemTypes ;
16+ import com .azure .resourcemanager .compute .models .VirtualMachine ;
17+ import com .azure .resourcemanager .compute .models .VirtualMachineDataDisk ;
18+ import com .azure .resourcemanager .compute .models .VirtualMachineSizeTypes ;
19+ import com .azure .resourcemanager .compute .models .VirtualMachineUnmanagedDataDisk ;
20+ import com .azure .core .management .Region ;
21+ import com .azure .core .management .profile .AzureProfile ;
22+ import com .azure .resourcemanager .samples .Utils ;
623
7- package com .microsoft .azure .management .compute .samples ;
8-
9- import com .microsoft .azure .management .Azure ;
10- import com .microsoft .azure .management .compute .CachingTypes ;
11- import com .microsoft .azure .management .compute .Disk ;
12- import com .microsoft .azure .management .compute .DiskSkuTypes ;
13- import com .microsoft .azure .management .compute .KnownLinuxVirtualMachineImage ;
14- import com .microsoft .azure .management .compute .OperatingSystemTypes ;
15- import com .microsoft .azure .management .compute .VirtualMachine ;
16- import com .microsoft .azure .management .compute .VirtualMachineDataDisk ;
17- import com .microsoft .azure .management .compute .VirtualMachineSizeTypes ;
18- import com .microsoft .azure .management .compute .VirtualMachineUnmanagedDataDisk ;
19- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
20- import com .microsoft .azure .management .samples .Utils ;
21- import com .microsoft .rest .LogLevel ;
22-
23- import java .io .File ;
2424import java .util .ArrayList ;
2525import java .util .List ;
2626
3434public class CreateVirtualMachineUsingSpecializedDiskFromVhd {
3535 /**
3636 * Main function which runs the actual sample.
37- * @param azure instance of the azure client
37+ * @param azureResourceManager instance of the azure client
3838 * @return true if sample runs successfully
3939 */
40- public static boolean runSample (Azure azure ) {
41- final String linuxVMName1 = Utils .createRandomName ( "VM1" );
42- final String linuxVMName2 = Utils .createRandomName ( "VM2" );
43- final String managedOSDiskName = Utils .createRandomName ( "ds-os-" );
44- final String managedDataDiskNamePrefix = Utils .createRandomName ( "ds-data-" );
45- final String rgName = Utils .createRandomName ( "rgCOMV" );
46- final String publicIpDnsLabel = Utils .createRandomName ( "pip" );
47- final String storageAccountName = Utils .createRandomName ( "stg" );
40+ public static boolean runSample (AzureResourceManager azureResourceManager ) {
41+ final String linuxVMName1 = Utils .randomResourceName ( azureResourceManager , "VM1" , 15 );
42+ final String linuxVMName2 = Utils .randomResourceName ( azureResourceManager , "VM2" , 15 );
43+ final String managedOSDiskName = Utils .randomResourceName ( azureResourceManager , "ds-os-" , 15 );
44+ final String managedDataDiskNamePrefix = Utils .randomResourceName ( azureResourceManager , "ds-data-" , 15 );
45+ final String rgName = Utils .randomResourceName ( azureResourceManager , "rgCOMV" , 15 );
46+ final String publicIpDnsLabel = Utils .randomResourceName ( azureResourceManager , "pip" , 15 );
47+ final String storageAccountName = Utils .randomResourceName ( azureResourceManager , "stg" , 15 );
4848 final String userName = "tirekicker" ;
49- // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
50- final String password = "12NewPA$$w0rd!" ;
49+ final String password = Utils .password ();
5150 final Region region = Region .US_WEST_CENTRAL ;
5251
53- final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-libraries -for-java/master/azure-samples/src/main/resources/install_apache.sh" ;
52+ final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk -for-java/master/sdk/resourcemanager/ azure-resourcemanager -samples/src/main/resources/install_apache.sh" ;
5453 final String apacheInstallCommand = "bash install_apache.sh" ;
5554 List <String > apacheInstallScriptUris = new ArrayList <>();
5655 apacheInstallScriptUris .add (apacheInstallScript );
@@ -60,7 +59,7 @@ public static boolean runSample(Azure azure) {
6059
6160 System .out .println ("Creating a un-managed Linux VM" );
6261
63- VirtualMachine linuxVM = azure .virtualMachines ().define (linuxVMName1 )
62+ VirtualMachine linuxVM = azureResourceManager .virtualMachines ().define (linuxVMName1 )
6463 .withRegion (region )
6564 .withNewResourceGroup (rgName )
6665 .withNewPrimaryNetwork ("10.0.0.0/28" )
@@ -105,7 +104,7 @@ public static boolean runSample(Azure azure) {
105104 // Delete the virtual machine
106105 System .out .println ("Deleting VM: " + linuxVM .id ());
107106
108- azure .virtualMachines ().deleteById (linuxVM .id ());
107+ azureResourceManager .virtualMachines ().deleteById (linuxVM .id ());
109108
110109 System .out .println ("Deleted the VM" );
111110
@@ -114,7 +113,7 @@ public static boolean runSample(Azure azure) {
114113
115114 System .out .println (String .format ("Creating managed disk from the specialized OS VHD: %s " , specializedOSVhdUri ));
116115
117- Disk osDisk = azure .disks ().define (managedOSDiskName )
116+ Disk osDisk = azureResourceManager .disks ().define (managedOSDiskName )
118117 .withRegion (region )
119118 .withExistingResourceGroup (rgName )
120119 .withLinuxFromVhd (specializedOSVhdUri )
@@ -123,7 +122,7 @@ public static boolean runSample(Azure azure) {
123122 .create ();
124123
125124 System .out .println ("Created managed disk holding OS: " + osDisk .id ());
126- // Utils .print(osDisk); TODO
125+ // ResourceManagerUtils .print(osDisk); TODO
127126
128127 //=============================================================
129128 // Create Managed disks from the Data VHDs
@@ -133,7 +132,7 @@ public static boolean runSample(Azure azure) {
133132 for (String dataVhdUri : dataVhdUris ) {
134133 System .out .println (String .format ("Creating managed disk from the Data VHD: %s " , dataVhdUri ));
135134
136- Disk dataDisk = azure .disks ().define (managedDataDiskNamePrefix + "-" + i )
135+ Disk dataDisk = azureResourceManager .disks ().define (managedDataDiskNamePrefix + "-" + i )
137136 .withRegion (region )
138137 .withExistingResourceGroup (rgName )
139138 .withData ()
@@ -145,7 +144,7 @@ public static boolean runSample(Azure azure) {
145144 dataDisks .add (dataDisk );
146145
147146 System .out .println ("Created managed disk holding data: " + dataDisk .id ());
148- // Utils .print(dataDisk); TODO
147+ // ResourceManagerUtils .print(dataDisk); TODO
149148 i ++;
150149 }
151150
@@ -154,7 +153,7 @@ public static boolean runSample(Azure azure) {
154153
155154 System .out .println ("Creating a Linux VM using specialized OS and data disks" );
156155
157- VirtualMachine linuxVM2 = azure .virtualMachines ().define (linuxVMName2 )
156+ VirtualMachine linuxVM2 = azureResourceManager .virtualMachines ().define (linuxVMName2 )
158157 .withRegion (region )
159158 .withExistingResourceGroup (rgName )
160159 .withNewPrimaryNetwork ("10.0.0.0/28" )
@@ -190,27 +189,22 @@ public static boolean runSample(Azure azure) {
190189 System .out .println ("Getting data disks SAS Uris" );
191190
192191 for (String diskId : dataDiskIds ) {
193- Disk dataDisk = azure .disks ().getById (diskId );
192+ Disk dataDisk = azureResourceManager .disks ().getById (diskId );
194193 String dataDiskSasUri = dataDisk .grantAccess (24 * 60 );
195194 System .out .println (String .format ("Data disk SAS Uri: %s" , dataDiskSasUri ));
196195 }
197196 return true ;
198- } catch (Exception f ) {
199- System .out .println (f .getMessage ());
200- f .printStackTrace ();
201-
202197 } finally {
203198 try {
204199 System .out .println ("Deleting Resource Group: " + rgName );
205- azure .resourceGroups ().beginDeleteByName (rgName );
200+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
206201 System .out .println ("Deleted Resource Group: " + rgName );
207202 } catch (NullPointerException npe ) {
208203 System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
209204 } catch (Exception g ) {
210205 g .printStackTrace ();
211206 }
212207 }
213- return false ;
214208 }
215209
216210 /**
@@ -222,18 +216,21 @@ public static void main(String[] args) {
222216 //=============================================================
223217 // Authenticate
224218
225- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
219+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
220+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
221+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
222+ .build ();
226223
227- Azure azure = Azure
228- .configure ()
229- .withLogLevel (LogLevel . BODY )
230- .authenticate (credFile )
231- .withDefaultSubscription ();
224+ AzureResourceManager azureResourceManager = AzureResourceManager
225+ .configure ()
226+ .withLogLevel (HttpLogDetailLevel . BASIC )
227+ .authenticate (credential , profile )
228+ .withDefaultSubscription ();
232229
233230 // Print selected subscription
234- System .out .println ("Selected subscription: " + azure .subscriptionId ());
231+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
235232
236- runSample (azure );
233+ runSample (azureResourceManager );
237234 } catch (Exception e ) {
238235 System .out .println (e .getMessage ());
239236 e .printStackTrace ();
0 commit comments