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 .KnownLinuxVirtualMachineImage ;
12
+ import com .azure .resourcemanager .compute .models .VirtualMachineScaleSet ;
13
+ import com .azure .resourcemanager .compute .models .VirtualMachineScaleSetSkuTypes ;
14
+ import com .azure .resourcemanager .network .models .LoadBalancer ;
15
+ import com .azure .resourcemanager .network .models .LoadBalancerSkuType ;
16
+ import com .azure .resourcemanager .network .models .Network ;
17
+ import com .azure .resourcemanager .network .models .PublicIpAddress ;
18
+ import com .azure .resourcemanager .network .models .PublicIPSkuType ;
19
+ import com .azure .resourcemanager .network .models .TransportProtocol ;
20
+ import com .azure .resourcemanager .resources .models .ResourceGroup ;
21
+ import com .azure .resourcemanager .resources .fluentcore .arm .AvailabilityZoneId ;
22
+ import com .azure .core .management .Region ;
23
+ import com .azure .core .management .profile .AzureProfile ;
24
+ import com .azure .resourcemanager .samples .Utils ;
6
25
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 .VirtualMachineScaleSet ;
12
- import com .microsoft .azure .management .compute .VirtualMachineScaleSetSkuTypes ;
13
- import com .microsoft .azure .management .network .LoadBalancer ;
14
- import com .microsoft .azure .management .network .LoadBalancerSkuType ;
15
- import com .microsoft .azure .management .network .Network ;
16
- import com .microsoft .azure .management .network .PublicIPAddress ;
17
- import com .microsoft .azure .management .network .PublicIPSkuType ;
18
- import com .microsoft .azure .management .network .TransportProtocol ;
19
- import com .microsoft .azure .management .resources .ResourceGroup ;
20
- import com .microsoft .azure .management .resources .fluentcore .arm .AvailabilityZoneId ;
21
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
22
- import com .microsoft .azure .management .samples .Utils ;
23
- import com .microsoft .rest .LogLevel ;
24
-
25
- import java .io .File ;
26
26
import java .util .ArrayList ;
27
27
import java .util .List ;
28
28
37
37
public final class ManageZonalVirtualMachineScaleSet {
38
38
/**
39
39
* Main function which runs the actual sample.
40
- * @param azure instance of the azure client
40
+ * @param azureResourceManager instance of the azure client
41
41
* @return true if sample runs successfully
42
42
*/
43
- public static boolean runSample (Azure azure ) {
43
+ public static boolean runSample (AzureResourceManager azureResourceManager ) {
44
44
final Region region = Region .US_EAST2 ;
45
- final String rgName = Utils .createRandomName ( "rgCOMV" );
46
- final String loadBalancerName = Utils .createRandomName ( "extlb" );
45
+ final String rgName = Utils .randomResourceName ( azureResourceManager , "rgCOMV" , 15 );
46
+ final String loadBalancerName = Utils .randomResourceName ( azureResourceManager , "extlb" , 15 );
47
47
final String publicIPName = "pip-" + loadBalancerName ;
48
48
final String frontendName = loadBalancerName + "-FE1" ;
49
49
final String backendPoolName1 = loadBalancerName + "-BAP1" ;
50
50
final String backendPoolName2 = loadBalancerName + "-BAP2" ;
51
51
final String natPoolName1 = loadBalancerName + "-INP1" ;
52
52
final String natPoolName2 = loadBalancerName + "-INP2" ;
53
- final String vmssName1 = Utils .createRandomName ( "vmss1" );
54
- final String vmssName2 = Utils .createRandomName ( "vmss2" );
53
+ final String vmssName1 = Utils .randomResourceName ( azureResourceManager , "vmss1" , 15 );
54
+ final String vmssName2 = Utils .randomResourceName ( azureResourceManager , "vmss2" , 15 );
55
55
56
56
final String userName = "tirekicker" ;
57
- // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
58
- final String password = "12NewPA23w0rd!" ;
57
+ final String password = Utils .password ();
59
58
60
59
try {
61
- ResourceGroup resourceGroup = azure .resourceGroups ()
60
+ ResourceGroup resourceGroup = azureResourceManager .resourceGroups ()
62
61
.define (rgName )
63
62
.withRegion (region )
64
63
.create ();
@@ -68,7 +67,7 @@ public static boolean runSample(Azure azure) {
68
67
69
68
System .out .println ("Creating a zone resilient public ip address" );
70
69
71
- PublicIPAddress publicIPAddress = azure . publicIPAddresses ()
70
+ PublicIpAddress publicIPAddress = azureResourceManager . publicIpAddresses ()
72
71
.define (publicIPName )
73
72
.withRegion (region )
74
73
.withExistingResourceGroup (resourceGroup )
@@ -87,7 +86,7 @@ public static boolean runSample(Azure azure) {
87
86
88
87
System .out .println ("Creating a zone resilient load balancer" );
89
88
90
- LoadBalancer loadBalancer = azure .loadBalancers ()
89
+ LoadBalancer loadBalancer = azureResourceManager .loadBalancers ()
91
90
.define (loadBalancerName )
92
91
.withRegion (region )
93
92
.withExistingResourceGroup (resourceGroup )
@@ -122,7 +121,7 @@ public static boolean runSample(Azure azure) {
122
121
.attach ()
123
122
// Explicitly define the frontend
124
123
.definePublicFrontend (frontendName )
125
- .withExistingPublicIPAddress (publicIPAddress ) // Frontend with PIP means internet-facing load-balancer
124
+ .withExistingPublicIpAddress (publicIPAddress ) // Frontend with PIP means internet-facing load-balancer
126
125
.attach ()
127
126
// Add two probes one per rule
128
127
.defineHttpProbe ("httpProbe" )
@@ -148,7 +147,7 @@ public static boolean runSample(Azure azure) {
148
147
149
148
System .out .println ("Creating network for virtual machine scale sets" );
150
149
151
- Network network = azure
150
+ Network network = azureResourceManager
152
151
.networks ()
153
152
.define ("vmssvnet" )
154
153
.withRegion (region )
@@ -167,7 +166,7 @@ public static boolean runSample(Azure azure) {
167
166
168
167
// HTTP goes to this virtual machine scale set
169
168
//
170
- VirtualMachineScaleSet virtualMachineScaleSet1 = azure .virtualMachineScaleSets ()
169
+ VirtualMachineScaleSet virtualMachineScaleSet1 = azureResourceManager .virtualMachineScaleSets ()
171
170
.define (vmssName1 )
172
171
.withRegion (region )
173
172
.withExistingResourceGroup (resourceGroup )
@@ -183,7 +182,7 @@ public static boolean runSample(Azure azure) {
183
182
.withAvailabilityZone (AvailabilityZoneId .ZONE_1 )
184
183
.create ();
185
184
186
- System .out .println ("Created zone aware virtual machine scale set" );
185
+ System .out .println ("Created zone aware virtual machine scale set: " + virtualMachineScaleSet1 . id () );
187
186
188
187
//=============================================================
189
188
// Create a zone aware virtual machine scale set
@@ -192,7 +191,7 @@ public static boolean runSample(Azure azure) {
192
191
193
192
// HTTPS goes to this virtual machine scale set
194
193
//
195
- VirtualMachineScaleSet virtualMachineScaleSet2 = azure .virtualMachineScaleSets ()
194
+ VirtualMachineScaleSet virtualMachineScaleSet2 = azureResourceManager .virtualMachineScaleSets ()
196
195
.define (vmssName2 )
197
196
.withRegion (region )
198
197
.withExistingResourceGroup (resourceGroup )
@@ -208,26 +207,20 @@ public static boolean runSample(Azure azure) {
208
207
.withAvailabilityZone (AvailabilityZoneId .ZONE_1 )
209
208
.create ();
210
209
211
- System .out .println ("Created zone aware virtual machine scale set" );
210
+ System .out .println ("Created zone aware virtual machine scale set: " + virtualMachineScaleSet2 . id () );
212
211
213
212
return true ;
214
- } catch (Exception f ) {
215
-
216
- System .out .println (f .getMessage ());
217
- f .printStackTrace ();
218
-
219
213
} finally {
220
214
try {
221
215
System .out .println ("Deleting Resource Group: " + rgName );
222
- azure .resourceGroups ().deleteByName (rgName );
216
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
223
217
System .out .println ("Deleted Resource Group: " + rgName );
224
218
} catch (NullPointerException npe ) {
225
219
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
226
220
} catch (Exception g ) {
227
221
g .printStackTrace ();
228
222
}
229
223
}
230
- return false ;
231
224
}
232
225
233
226
/**
@@ -240,17 +233,21 @@ public static void main(String[] args) {
240
233
//=============================================================
241
234
// Authenticate
242
235
243
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
236
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
237
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
238
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
239
+ .build ();
244
240
245
- Azure azure = Azure .configure ()
246
- .withLogLevel (LogLevel .BODY_AND_HEADERS )
247
- .authenticate (credFile )
248
- .withDefaultSubscription ();
241
+ AzureResourceManager azureResourceManager = AzureResourceManager
242
+ .configure ()
243
+ .withLogLevel (HttpLogDetailLevel .BASIC )
244
+ .authenticate (credential , profile )
245
+ .withDefaultSubscription ();
249
246
250
247
// Print selected subscription
251
- System .out .println ("Selected subscription: " + azure .subscriptionId ());
248
+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
252
249
253
- runSample (azure );
250
+ runSample (azureResourceManager );
254
251
} catch (Exception e ) {
255
252
System .out .println (e .getMessage ());
256
253
e .printStackTrace ();
0 commit comments