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 .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 .ImageReference ;
13
+ import com .azure .resourcemanager .compute .models .VirtualMachineScaleSetVM ;
14
+ import com .azure .resourcemanager .network .models .LoadBalancerInboundNatRule ;
15
+ import com .azure .resourcemanager .network .models .Network ;
16
+ import com .azure .resourcemanager .network .models .PublicIpAddress ;
17
+ import com .azure .resourcemanager .network .models .LoadBalancer ;
18
+ import com .azure .resourcemanager .network .models .TransportProtocol ;
19
+ import com .azure .resourcemanager .compute .models .VirtualMachineScaleSet ;
20
+ import com .azure .resourcemanager .compute .models .VirtualMachineScaleSetSkuTypes ;
21
+ import com .azure .resourcemanager .network .models .VirtualMachineScaleSetNetworkInterface ;
22
+ import com .azure .resourcemanager .network .models .VirtualMachineScaleSetNicIpConfiguration ;
23
+ import com .azure .core .management .Region ;
24
+ import com .azure .core .management .profile .AzureProfile ;
25
+ import com .azure .resourcemanager .samples .Utils ;
6
26
7
- package com .microsoft .azure .management .compute .samples ;
8
-
9
- import com .microsoft .azure .PagedList ;
10
- import com .microsoft .azure .management .Azure ;
11
- import com .microsoft .azure .management .compute .ImageReference ;
12
- import com .microsoft .azure .management .compute .VirtualMachineScaleSetVM ;
13
- import com .microsoft .azure .management .network .LoadBalancerInboundNatRule ;
14
- import com .microsoft .azure .management .network .Network ;
15
- import com .microsoft .azure .management .network .PublicIPAddress ;
16
- import com .microsoft .azure .management .network .LoadBalancer ;
17
- import com .microsoft .azure .management .network .TransportProtocol ;
18
- import com .microsoft .azure .management .compute .VirtualMachineScaleSet ;
19
- import com .microsoft .azure .management .compute .VirtualMachineScaleSetSkuTypes ;
20
- import com .microsoft .azure .management .network .VirtualMachineScaleSetNetworkInterface ;
21
- import com .microsoft .azure .management .network .VirtualMachineScaleSetNicIPConfiguration ;
22
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
23
- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
24
- import com .microsoft .azure .management .samples .Utils ;
25
- import com .microsoft .rest .LogLevel ;
26
-
27
- import java .io .File ;
28
27
import java .util .ArrayList ;
29
28
import java .util .Date ;
30
29
import java .util .List ;
@@ -45,14 +44,14 @@ public final class ManageVirtualMachineScaleSetWithUnmanagedDisks {
45
44
46
45
/**
47
46
* Main function which runs the actual sample.
48
- * @param azure instance of the azure client
47
+ * @param azureResourceManager instance of the azure client
49
48
* @return true if sample runs successfully
50
49
*/
51
- public static boolean runSample (Azure azure ) {
50
+ public static boolean runSample (AzureResourceManager azureResourceManager ) {
52
51
final Region region = Region .US_EAST2 ;
53
- final String rgName = SdkContext .randomResourceName ("rgCOVS" , 15 );
54
- final String vnetName = SdkContext .randomResourceName ("vnet" , 24 );
55
- final String loadBalancerName1 = SdkContext .randomResourceName ("intlb" + "-" , 18 );
52
+ final String rgName = Utils .randomResourceName (azureResourceManager , "rgCOVS" , 15 );
53
+ final String vnetName = Utils .randomResourceName (azureResourceManager , "vnet" , 24 );
54
+ final String loadBalancerName1 = Utils .randomResourceName (azureResourceManager , "intlb" + "-" , 18 );
56
55
final String publicIpName = "pip-" + loadBalancerName1 ;
57
56
final String frontendName = loadBalancerName1 + "-FE1" ;
58
57
final String backendPoolName1 = loadBalancerName1 + "-BAP1" ;
@@ -65,10 +64,10 @@ public static boolean runSample(Azure azure) {
65
64
final String natPool50XXto22 = "natPool50XXto22" ;
66
65
final String natPool60XXto23 = "natPool60XXto23" ;
67
66
68
- final String vmssName = SdkContext .randomResourceName ("vmss" , 24 );
69
- final String storageAccountName1 = SdkContext .randomResourceName ("stg1" , 24 );
70
- final String storageAccountName2 = SdkContext .randomResourceName ("stg2" , 24 );
71
- final String storageAccountName3 = SdkContext .randomResourceName ("stg3" , 24 );
67
+ final String vmssName = Utils .randomResourceName (azureResourceManager , "vmss" , 24 );
68
+ final String storageAccountName1 = Utils .randomResourceName (azureResourceManager , "stg1" , 24 );
69
+ final String storageAccountName2 = Utils .randomResourceName (azureResourceManager , "stg2" , 24 );
70
+ final String storageAccountName3 = Utils .randomResourceName (azureResourceManager , "stg3" , 24 );
72
71
73
72
final String userName = "tirekicker" ;
74
73
final String sshKey =
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD [email protected] " ;
@@ -82,7 +81,7 @@ public static boolean runSample(Azure azure) {
82
81
// Create a virtual network with a frontend subnet
83
82
System .out .println ("Creating virtual network with a frontend subnet ..." );
84
83
85
- Network network = azure .networks ().define (vnetName )
84
+ Network network = azureResourceManager .networks ().define (vnetName )
86
85
.withRegion (region )
87
86
.withNewResourceGroup (rgName )
88
87
.withAddressSpace ("172.16.0.0/16" )
@@ -99,7 +98,7 @@ public static boolean runSample(Azure azure) {
99
98
// Create a public IP address
100
99
System .out .println ("Creating a public IP address..." );
101
100
102
- PublicIPAddress publicIPAddress = azure . publicIPAddresses ().define (publicIpName )
101
+ PublicIpAddress publicIPAddress = azureResourceManager . publicIpAddresses ().define (publicIpName )
103
102
.withRegion (region )
104
103
.withExistingResourceGroup (rgName )
105
104
.withLeafDomainLabel (publicIpName )
@@ -134,7 +133,7 @@ public static boolean runSample(Azure azure) {
134
133
+ " balancer to a port for a specific virtual machine in the backend address pool\n "
135
134
+ " - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23" );
136
135
137
- LoadBalancer loadBalancer1 = azure .loadBalancers ().define (loadBalancerName1 )
136
+ LoadBalancer loadBalancer1 = azureResourceManager .loadBalancers ().define (loadBalancerName1 )
138
137
.withRegion (region )
139
138
.withExistingResourceGroup (rgName )
140
139
@@ -171,7 +170,7 @@ public static boolean runSample(Azure azure) {
171
170
172
171
// Explicitly define the frontend
173
172
.definePublicFrontend (frontendName )
174
- .withExistingPublicIPAddress (publicIPAddress )
173
+ .withExistingPublicIpAddress (publicIPAddress )
175
174
.attach ()
176
175
177
176
// Add two probes one per rule
@@ -206,7 +205,7 @@ public static boolean runSample(Azure azure) {
206
205
.withSku ("18.04-LTS" )
207
206
.withVersion ("latest" );
208
207
209
- VirtualMachineScaleSet virtualMachineScaleSet = azure .virtualMachineScaleSets ().define (vmssName )
208
+ VirtualMachineScaleSet virtualMachineScaleSet = azureResourceManager .virtualMachineScaleSets ().define (vmssName )
210
209
.withRegion (region )
211
210
.withExistingResourceGroup (rgName )
212
211
.withSku (VirtualMachineScaleSetSkuTypes .STANDARD_D3_V2 )
@@ -241,13 +240,13 @@ public static boolean runSample(Azure azure) {
241
240
System .out .println ();
242
241
243
242
// Print virtual machine scale set details
244
- // Utils .print(virtualMachineScaleSet);
243
+ // ResourceManagerUtils .print(virtualMachineScaleSet);
245
244
246
245
//=============================================================
247
246
// List virtual machine scale set network interfaces
248
247
249
248
System .out .println ("Listing scale set network interfaces ..." );
250
- PagedList <VirtualMachineScaleSetNetworkInterface > vmssNics = virtualMachineScaleSet .listNetworkInterfaces ();
249
+ PagedIterable <VirtualMachineScaleSetNetworkInterface > vmssNics = virtualMachineScaleSet .listNetworkInterfaces ();
251
250
for (VirtualMachineScaleSetNetworkInterface vmssNic : vmssNics ) {
252
251
System .out .println (vmssNic .id ());
253
252
}
@@ -259,10 +258,10 @@ public static boolean runSample(Azure azure) {
259
258
for (VirtualMachineScaleSetVM instance : virtualMachineScaleSet .virtualMachines ().list ()) {
260
259
System .out .println ("Scale set virtual machine instance #" + instance .instanceId ());
261
260
System .out .println (instance .id ());
262
- PagedList <VirtualMachineScaleSetNetworkInterface > networkInterfaces = instance .listNetworkInterfaces ();
261
+ PagedIterable <VirtualMachineScaleSetNetworkInterface > networkInterfaces = instance .listNetworkInterfaces ();
263
262
// Pick the first NIC
264
- VirtualMachineScaleSetNetworkInterface networkInterface = networkInterfaces .get ( 0 );
265
- for (VirtualMachineScaleSetNicIPConfiguration ipConfig :networkInterface .ipConfigurations ().values ()) {
263
+ VirtualMachineScaleSetNetworkInterface networkInterface = networkInterfaces .iterator (). next ( );
264
+ for (VirtualMachineScaleSetNicIpConfiguration ipConfig :networkInterface .ipConfigurations ().values ()) {
266
265
if (ipConfig .isPrimary ()) {
267
266
List <LoadBalancerInboundNatRule > natRules = ipConfig .listAssociatedLoadBalancerInboundNatRules ();
268
267
for (LoadBalancerInboundNatRule natRule : natRules ) {
@@ -315,23 +314,17 @@ public static boolean runSample(Azure azure) {
315
314
System .out .println ("re-started virtual machine scale set" );
316
315
317
316
return true ;
318
- } catch (Exception f ) {
319
-
320
- System .out .println (f .getMessage ());
321
- f .printStackTrace ();
322
-
323
317
} finally {
324
318
try {
325
319
System .out .println ("Deleting Resource Group: " + rgName );
326
- azure .resourceGroups ().deleteByName (rgName );
320
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
327
321
System .out .println ("Deleted Resource Group: " + rgName );
328
322
} catch (NullPointerException npe ) {
329
323
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
330
324
} catch (Exception g ) {
331
325
g .printStackTrace ();
332
326
}
333
327
}
334
- return false ;
335
328
}
336
329
337
330
/**
@@ -345,18 +338,21 @@ public static void main(String[] args) {
345
338
//=============================================================
346
339
// Authenticate
347
340
348
- System .out .println (System .getenv ("AZURE_AUTH_LOCATION" ));
349
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
341
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
342
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
343
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
344
+ .build ();
350
345
351
- Azure azure = Azure .configure ()
352
- .withLogLevel (LogLevel .BASIC )
353
- .authenticate (credFile )
354
- .withDefaultSubscription ();
346
+ AzureResourceManager azureResourceManager = AzureResourceManager
347
+ .configure ()
348
+ .withLogLevel (HttpLogDetailLevel .BASIC )
349
+ .authenticate (credential , profile )
350
+ .withDefaultSubscription ();
355
351
356
352
// Print selected subscription
357
- System .out .println ("Selected subscription: " + azure .subscriptionId ());
353
+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
358
354
359
- runSample (azure );
355
+ runSample (azureResourceManager );
360
356
361
357
} catch (Exception e ) {
362
358
System .out .println (e .getMessage ());
0 commit comments