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 .network .samples ;
5
+
6
+ import com .azure .core .credential .TokenCredential ;
7
+ import com .azure .core .http .policy .HttpLogDetailLevel ;
8
+ import com .azure .core .http .rest .PagedIterable ;
9
+ import com .azure .core .management .AzureEnvironment ;
10
+ import com .azure .identity .DefaultAzureCredentialBuilder ;
11
+ import com .azure .resourcemanager .AzureResourceManager ;
12
+ import com .azure .resourcemanager .compute .models .KnownWindowsVirtualMachineImage ;
13
+ import com .azure .resourcemanager .compute .models .VirtualMachine ;
14
+ import com .azure .resourcemanager .compute .models .VirtualMachineSizeTypes ;
15
+ import com .azure .resourcemanager .network .models .Network ;
16
+ import com .azure .resourcemanager .network .models .NetworkInterface ;
17
+ import com .azure .core .management .Region ;
18
+ import com .azure .core .management .profile .AzureProfile ;
19
+ import com .azure .resourcemanager .samples .Utils ;
6
20
7
- package com .microsoft .azure .management .network .samples ;
8
-
9
- import com .microsoft .azure .PagedList ;
10
- import com .microsoft .azure .management .Azure ;
11
- import com .microsoft .azure .management .compute .KnownWindowsVirtualMachineImage ;
12
- import com .microsoft .azure .management .compute .VirtualMachine ;
13
- import com .microsoft .azure .management .compute .VirtualMachineSizeTypes ;
14
- import com .microsoft .azure .management .network .Network ;
15
- import com .microsoft .azure .management .network .NetworkInterface ;
16
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
17
- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
18
- import com .microsoft .azure .management .samples .Utils ;
19
- import com .microsoft .rest .LogLevel ;
20
-
21
- import java .io .File ;
22
21
import java .util .Date ;
23
22
24
23
/**
25
24
* Azure Network sample for managing network interfaces -
26
- * - Create a virtual machine with multiple network interfaces
27
- * - Configure a network interface
28
- * - List network interfaces
29
- * - Delete a network interface.
25
+ * - Create a virtual machine with multiple network interfaces
26
+ * - Configure a network interface
27
+ * - List network interfaces
28
+ * - Delete a network interface.
30
29
*/
31
30
32
31
public final class ManageNetworkInterface {
33
32
34
33
/**
35
34
* Main function which runs the actual sample.
36
- * @param azure instance of the azure client
35
+ *
36
+ * @param azureResourceManager instance of the azure client
37
37
* @return true if sample runs successfully
38
38
*/
39
- public static boolean runSample (Azure azure ) {
39
+ public static boolean runSample (AzureResourceManager azureResourceManager ) {
40
40
final Region region = Region .US_NORTH_CENTRAL ;
41
- final String vnetName = SdkContext .randomResourceName ("vnet" , 24 );
42
- final String networkInterfaceName1 = SdkContext .randomResourceName ("nic1" , 24 );
43
- final String networkInterfaceName2 = SdkContext .randomResourceName ("nic2" , 24 );
44
- final String networkInterfaceName3 = SdkContext .randomResourceName ("nic3" , 24 );
45
- final String publicIPAddressLeafDNS1 = SdkContext .randomResourceName ("pip1" , 24 );
46
- final String publicIPAddressLeafDNS2 = SdkContext .randomResourceName ("pip2" , 24 );
47
-
48
- // TODO adjust the length of vm name from 8 to 24
49
- final String vmName = SdkContext .randomResourceName ("vm" , 8 );
50
- final String rgName = SdkContext .randomResourceName ("rgNEMI" , 24 );
41
+ final String vnetName = Utils .randomResourceName (azureResourceManager , "vnet" , 24 );
42
+ final String networkInterfaceName1 = Utils .randomResourceName (azureResourceManager , "nic1" , 24 );
43
+ final String networkInterfaceName2 = Utils .randomResourceName (azureResourceManager , "nic2" , 24 );
44
+ final String networkInterfaceName3 = Utils .randomResourceName (azureResourceManager , "nic3" , 24 );
45
+ final String publicIPAddressLeafDNS1 = Utils .randomResourceName (azureResourceManager , "pip1" , 24 );
46
+ final String publicIPAddressLeafDNS2 = Utils .randomResourceName (azureResourceManager , "pip2" , 24 );
47
+
48
+ // TODO: adjust the length of vm name from 8 to 24
49
+ final String vmName = Utils .randomResourceName (azureResourceManager , "vm" , 8 );
50
+ final String rgName = Utils .randomResourceName (azureResourceManager , "rgNEMI" , 24 );
51
51
final String userName = "tirekicker" ;
52
- // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
53
- final String password = SdkContext .randomResourceName ("Pa5$" , 24 );
52
+ final String password = Utils .password ();
54
53
try {
55
54
56
55
//============================================================
@@ -60,19 +59,19 @@ public static boolean runSample(Azure azure) {
60
59
61
60
System .out .println ("Creating a virtual network ..." );
62
61
63
- Network network = azure .networks ().define (vnetName )
62
+ Network network = azureResourceManager .networks ().define (vnetName )
64
63
.withRegion (region )
65
64
.withNewResourceGroup (rgName )
66
65
.withAddressSpace ("172.16.0.0/16" )
67
66
.defineSubnet ("Front-end" )
68
- .withAddressPrefix ("172.16.1.0/24" )
69
- .attach ()
67
+ .withAddressPrefix ("172.16.1.0/24" )
68
+ .attach ()
70
69
.defineSubnet ("Mid-tier" )
71
- .withAddressPrefix ("172.16.2.0/24" )
72
- .attach ()
70
+ .withAddressPrefix ("172.16.2.0/24" )
71
+ .attach ()
73
72
.defineSubnet ("Back-end" )
74
- .withAddressPrefix ("172.16.3.0/24" )
75
- .attach ()
73
+ .withAddressPrefix ("172.16.3.0/24" )
74
+ .attach ()
76
75
.create ();
77
76
78
77
System .out .println ("Created a virtual network: " + network .id ());
@@ -81,7 +80,7 @@ public static boolean runSample(Azure azure) {
81
80
System .out .println ("Creating multiple network interfaces" );
82
81
System .out .println ("Creating network interface 1" );
83
82
84
- NetworkInterface networkInterface1 = azure .networkInterfaces ().define (networkInterfaceName1 )
83
+ NetworkInterface networkInterface1 = azureResourceManager .networkInterfaces ().define (networkInterfaceName1 )
85
84
.withRegion (region )
86
85
.withExistingResourceGroup (rgName )
87
86
.withExistingPrimaryNetwork (network )
@@ -95,7 +94,7 @@ public static boolean runSample(Azure azure) {
95
94
Utils .print (networkInterface1 );
96
95
System .out .println ("Creating network interface 2" );
97
96
98
- NetworkInterface networkInterface2 = azure .networkInterfaces ().define (networkInterfaceName2 )
97
+ NetworkInterface networkInterface2 = azureResourceManager .networkInterfaces ().define (networkInterfaceName2 )
99
98
.withRegion (region )
100
99
.withExistingResourceGroup (rgName )
101
100
.withExistingPrimaryNetwork (network )
@@ -108,7 +107,7 @@ public static boolean runSample(Azure azure) {
108
107
109
108
System .out .println ("Creating network interface 3" );
110
109
111
- NetworkInterface networkInterface3 = azure .networkInterfaces ().define (networkInterfaceName3 )
110
+ NetworkInterface networkInterface3 = azureResourceManager .networkInterfaces ().define (networkInterfaceName3 )
112
111
.withRegion (region )
113
112
.withExistingResourceGroup (rgName )
114
113
.withExistingPrimaryNetwork (network )
@@ -127,7 +126,7 @@ public static boolean runSample(Azure azure) {
127
126
128
127
Date t1 = new Date ();
129
128
130
- VirtualMachine vm = azure .virtualMachines ().define (vmName )
129
+ VirtualMachine vm = azureResourceManager .virtualMachines ().define (vmName )
131
130
.withRegion (region )
132
131
.withExistingResourceGroup (rgName )
133
132
.withExistingPrimaryNetworkInterface (networkInterface1 )
@@ -162,7 +161,7 @@ public static boolean runSample(Azure azure) {
162
161
// List network interfaces
163
162
164
163
System .out .println ("Walking through network inter4faces in resource group: " + rgName );
165
- PagedList <NetworkInterface > networkInterfaces = azure .networkInterfaces ().listByResourceGroup (rgName );
164
+ PagedIterable <NetworkInterface > networkInterfaces = azureResourceManager .networkInterfaces ().listByResourceGroup (rgName );
166
165
for (NetworkInterface networkinterface : networkInterfaces ) {
167
166
Utils .print (networkinterface );
168
167
}
@@ -173,28 +172,23 @@ public static boolean runSample(Azure azure) {
173
172
174
173
System .out .println ("Deleting a network interface: " + networkInterface2 .id ());
175
174
System .out .println ("First, deleting the vm" );
176
- azure .virtualMachines ().deleteById (vm .id ());
175
+ azureResourceManager .virtualMachines ().deleteById (vm .id ());
177
176
System .out .println ("Second, deleting the network interface" );
178
- azure .networkInterfaces ().deleteById (networkInterface2 .id ());
177
+ azureResourceManager .networkInterfaces ().deleteById (networkInterface2 .id ());
179
178
System .out .println ("Deleted network interface" );
180
179
181
180
System .out .println ("============================================================" );
182
181
System .out .println ("Remaining network interfaces are ..." );
183
- networkInterfaces = azure .networkInterfaces ().listByResourceGroup (rgName );
182
+ networkInterfaces = azureResourceManager .networkInterfaces ().listByResourceGroup (rgName );
184
183
for (NetworkInterface networkinterface : networkInterfaces ) {
185
184
Utils .print (networkinterface );
186
185
}
187
186
return true ;
188
- } catch (Exception f ) {
189
-
190
- System .out .println (f .getMessage ());
191
- f .printStackTrace ();
192
-
193
187
} finally {
194
188
195
189
try {
196
190
System .out .println ("Deleting Resource Group: " + rgName );
197
- azure .resourceGroups ().deleteByName (rgName );
191
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
198
192
System .out .println ("Deleted Resource Group: " + rgName );
199
193
} catch (NullPointerException npe ) {
200
194
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
@@ -203,11 +197,11 @@ public static boolean runSample(Azure azure) {
203
197
}
204
198
205
199
}
206
- return false ;
207
200
}
208
201
209
202
/**
210
203
* Main entry point.
204
+ *
211
205
* @param args the parameters
212
206
*/
213
207
public static void main (String [] args ) {
@@ -216,17 +210,21 @@ public static void main(String[] args) {
216
210
//=============================================================
217
211
// Authenticate
218
212
219
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
213
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
214
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
215
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
216
+ .build ();
220
217
221
- Azure azure = Azure .configure ()
222
- .withLogLevel (LogLevel .BASIC )
223
- .authenticate (credFile )
224
- .withDefaultSubscription ();
218
+ AzureResourceManager azureResourceManager = AzureResourceManager
219
+ .configure ()
220
+ .withLogLevel (HttpLogDetailLevel .BASIC )
221
+ .authenticate (credential , profile )
222
+ .withDefaultSubscription ();
225
223
226
224
// Print selected subscription
227
- System .out .println ("Selected subscription: " + azure .subscriptionId ());
225
+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
228
226
229
- runSample (azure );
227
+ runSample (azureResourceManager );
230
228
} catch (Exception e ) {
231
229
System .out .println (e .getMessage ());
232
230
e .printStackTrace ();
0 commit comments