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
- */
6
-
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 .KnownLinuxVirtualMachineImage ;
12
- import com .microsoft .azure .management .compute .VirtualMachine ;
13
- import com .microsoft .azure .management .compute .VirtualMachineSizeTypes ;
14
- import com .microsoft .azure .management .graphrbac .BuiltInRole ;
15
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
16
- import com .microsoft .azure .management .samples .Utils ;
17
- import com .microsoft .azure .management .storage .StorageAccount ;
18
- import com .microsoft .rest .LogLevel ;
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 .KnownLinuxVirtualMachineImage ;
13
+ import com .azure .resourcemanager .compute .models .VirtualMachine ;
14
+ import com .azure .resourcemanager .compute .models .VirtualMachineSizeTypes ;
15
+ import com .azure .resourcemanager .authorization .models .BuiltInRole ;
16
+ import com .azure .core .management .Region ;
17
+ import com .azure .core .management .profile .AzureProfile ;
18
+ import com .azure .resourcemanager .samples .Utils ;
19
+ import com .azure .resourcemanager .storage .models .StorageAccount ;
19
20
20
- import java .io .File ;
21
21
import java .util .ArrayList ;
22
22
import java .util .List ;
23
23
@@ -33,16 +33,15 @@ public final class ManageStorageFromMSIEnabledVirtualMachine {
33
33
/**
34
34
* Main function which runs the actual sample.
35
35
*
36
- * @param azure instance of the azure client
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 ) {
40
- final String linuxVMName = Utils .createRandomName ( "VM1" );
41
- final String rgName = Utils .createRandomName ( "rgCOMV" );
42
- final String pipName = Utils .createRandomName ( "pip1" );
39
+ public static boolean runSample (AzureResourceManager azureResourceManager ) {
40
+ final String linuxVMName = Utils .randomResourceName ( azureResourceManager , "VM1" , 15 );
41
+ final String rgName = Utils .randomResourceName ( azureResourceManager , "rgCOMV" , 15 );
42
+ final String pipName = Utils .randomResourceName ( azureResourceManager , "pip1" , 15 );
43
43
final String userName = "tirekicker" ;
44
- // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
45
- final String password = "12NewPA$$w0rd!" ;
44
+ final String password = Utils .password ();
46
45
final Region region = Region .US_WEST_CENTRAL ;
47
46
48
47
final String installScript = "https://raw.githubusercontent.com/Azure/azure-libraries-for-java/master/azure-samples/src/main/resources/create_resources_with_msi.sh" ;
@@ -56,7 +55,7 @@ public static boolean runSample(Azure azure) {
56
55
57
56
System .out .println ("Creating a Linux VM with MSI enabled" );
58
57
59
- VirtualMachine virtualMachine = azure .virtualMachines ()
58
+ VirtualMachine virtualMachine = azureResourceManager .virtualMachines ()
60
59
.define (linuxVMName )
61
60
.withRegion (region )
62
61
.withNewResourceGroup (rgName )
@@ -77,7 +76,7 @@ public static boolean runSample(Azure azure) {
77
76
78
77
// Prepare custom script t install az cli that uses MSI to create a storage account
79
78
//
80
- final String stgName = Utils .createRandomName ( "st44" );
79
+ final String stgName = Utils .randomResourceName ( azureResourceManager , "st44" , 15 );
81
80
installCommand = installCommand .replace ("{stgName}" , stgName )
82
81
.replace ("{rgName}" , rgName )
83
82
.replace ("{location}" , region .name ());
@@ -101,26 +100,22 @@ public static boolean runSample(Azure azure) {
101
100
102
101
// Retrieve the storage account created by az cli using MSI credentials
103
102
//
104
- StorageAccount storageAccount = azure .storageAccounts ()
103
+ StorageAccount storageAccount = azureResourceManager .storageAccounts ()
105
104
.getByResourceGroup (rgName , stgName );
106
105
107
106
System .out .println ("Storage account created by az cli using MSI credential" );
108
107
Utils .print (storageAccount );
109
108
return true ;
110
- } catch (Exception f ) {
111
- System .out .println (f .getMessage ());
112
- f .printStackTrace ();
113
109
} finally {
114
110
try {
115
111
System .out .println ("Deleting Resource Group: " + rgName );
116
- azure .resourceGroups ().beginDeleteByName (rgName );
112
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
117
113
} catch (NullPointerException npe ) {
118
114
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
119
115
} catch (Exception g ) {
120
116
g .printStackTrace ();
121
117
}
122
118
}
123
- return false ;
124
119
}
125
120
126
121
/**
@@ -132,17 +127,21 @@ public static void main(String[] args) {
132
127
//=============================================================
133
128
// Authenticate
134
129
135
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
130
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
131
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
132
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
133
+ .build ();
136
134
137
- Azure azure = Azure .configure ()
138
- .withLogLevel (LogLevel .BODY_AND_HEADERS )
139
- .authenticate (credFile )
140
- .withDefaultSubscription ();
135
+ AzureResourceManager azureResourceManager = AzureResourceManager
136
+ .configure ()
137
+ .withLogLevel (HttpLogDetailLevel .BASIC )
138
+ .authenticate (credential , profile )
139
+ .withDefaultSubscription ();
141
140
142
141
// Print selected subscription
143
- System .out .println ("Selected subscription: " + azure .subscriptionId ());
142
+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
144
143
145
- runSample (azure );
144
+ runSample (azureResourceManager );
146
145
} catch (Exception e ) {
147
146
System .out .println (e .getMessage ());
148
147
e .printStackTrace ();
@@ -151,4 +150,4 @@ public static void main(String[] args) {
151
150
152
151
private ManageStorageFromMSIEnabledVirtualMachine () {
153
152
}
154
- }
153
+ }
0 commit comments