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 ;
6
23
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 ;
24
24
import java .util .ArrayList ;
25
25
import java .util .List ;
26
26
34
34
public class CreateVirtualMachineUsingSpecializedDiskFromVhd {
35
35
/**
36
36
* Main function which runs the actual sample.
37
- * @param azure instance of the azure client
37
+ * @param azureResourceManager instance of the azure client
38
38
* @return true if sample runs successfully
39
39
*/
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 );
48
48
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 ();
51
50
final Region region = Region .US_WEST_CENTRAL ;
52
51
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" ;
54
53
final String apacheInstallCommand = "bash install_apache.sh" ;
55
54
List <String > apacheInstallScriptUris = new ArrayList <>();
56
55
apacheInstallScriptUris .add (apacheInstallScript );
@@ -60,7 +59,7 @@ public static boolean runSample(Azure azure) {
60
59
61
60
System .out .println ("Creating a un-managed Linux VM" );
62
61
63
- VirtualMachine linuxVM = azure .virtualMachines ().define (linuxVMName1 )
62
+ VirtualMachine linuxVM = azureResourceManager .virtualMachines ().define (linuxVMName1 )
64
63
.withRegion (region )
65
64
.withNewResourceGroup (rgName )
66
65
.withNewPrimaryNetwork ("10.0.0.0/28" )
@@ -105,7 +104,7 @@ public static boolean runSample(Azure azure) {
105
104
// Delete the virtual machine
106
105
System .out .println ("Deleting VM: " + linuxVM .id ());
107
106
108
- azure .virtualMachines ().deleteById (linuxVM .id ());
107
+ azureResourceManager .virtualMachines ().deleteById (linuxVM .id ());
109
108
110
109
System .out .println ("Deleted the VM" );
111
110
@@ -114,7 +113,7 @@ public static boolean runSample(Azure azure) {
114
113
115
114
System .out .println (String .format ("Creating managed disk from the specialized OS VHD: %s " , specializedOSVhdUri ));
116
115
117
- Disk osDisk = azure .disks ().define (managedOSDiskName )
116
+ Disk osDisk = azureResourceManager .disks ().define (managedOSDiskName )
118
117
.withRegion (region )
119
118
.withExistingResourceGroup (rgName )
120
119
.withLinuxFromVhd (specializedOSVhdUri )
@@ -123,7 +122,7 @@ public static boolean runSample(Azure azure) {
123
122
.create ();
124
123
125
124
System .out .println ("Created managed disk holding OS: " + osDisk .id ());
126
- // Utils .print(osDisk); TODO
125
+ // ResourceManagerUtils .print(osDisk); TODO
127
126
128
127
//=============================================================
129
128
// Create Managed disks from the Data VHDs
@@ -133,7 +132,7 @@ public static boolean runSample(Azure azure) {
133
132
for (String dataVhdUri : dataVhdUris ) {
134
133
System .out .println (String .format ("Creating managed disk from the Data VHD: %s " , dataVhdUri ));
135
134
136
- Disk dataDisk = azure .disks ().define (managedDataDiskNamePrefix + "-" + i )
135
+ Disk dataDisk = azureResourceManager .disks ().define (managedDataDiskNamePrefix + "-" + i )
137
136
.withRegion (region )
138
137
.withExistingResourceGroup (rgName )
139
138
.withData ()
@@ -145,7 +144,7 @@ public static boolean runSample(Azure azure) {
145
144
dataDisks .add (dataDisk );
146
145
147
146
System .out .println ("Created managed disk holding data: " + dataDisk .id ());
148
- // Utils .print(dataDisk); TODO
147
+ // ResourceManagerUtils .print(dataDisk); TODO
149
148
i ++;
150
149
}
151
150
@@ -154,7 +153,7 @@ public static boolean runSample(Azure azure) {
154
153
155
154
System .out .println ("Creating a Linux VM using specialized OS and data disks" );
156
155
157
- VirtualMachine linuxVM2 = azure .virtualMachines ().define (linuxVMName2 )
156
+ VirtualMachine linuxVM2 = azureResourceManager .virtualMachines ().define (linuxVMName2 )
158
157
.withRegion (region )
159
158
.withExistingResourceGroup (rgName )
160
159
.withNewPrimaryNetwork ("10.0.0.0/28" )
@@ -190,27 +189,22 @@ public static boolean runSample(Azure azure) {
190
189
System .out .println ("Getting data disks SAS Uris" );
191
190
192
191
for (String diskId : dataDiskIds ) {
193
- Disk dataDisk = azure .disks ().getById (diskId );
192
+ Disk dataDisk = azureResourceManager .disks ().getById (diskId );
194
193
String dataDiskSasUri = dataDisk .grantAccess (24 * 60 );
195
194
System .out .println (String .format ("Data disk SAS Uri: %s" , dataDiskSasUri ));
196
195
}
197
196
return true ;
198
- } catch (Exception f ) {
199
- System .out .println (f .getMessage ());
200
- f .printStackTrace ();
201
-
202
197
} finally {
203
198
try {
204
199
System .out .println ("Deleting Resource Group: " + rgName );
205
- azure .resourceGroups ().beginDeleteByName (rgName );
200
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
206
201
System .out .println ("Deleted Resource Group: " + rgName );
207
202
} catch (NullPointerException npe ) {
208
203
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
209
204
} catch (Exception g ) {
210
205
g .printStackTrace ();
211
206
}
212
207
}
213
- return false ;
214
208
}
215
209
216
210
/**
@@ -222,18 +216,21 @@ public static void main(String[] args) {
222
216
//=============================================================
223
217
// Authenticate
224
218
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 ();
226
223
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 ();
232
229
233
230
// Print selected subscription
234
- System .out .println ("Selected subscription: " + azure .subscriptionId ());
231
+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
235
232
236
- runSample (azure );
233
+ runSample (azureResourceManager );
237
234
} catch (Exception e ) {
238
235
System .out .println (e .getMessage ());
239
236
e .printStackTrace ();
0 commit comments