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 .resources .samples ;
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
8
3
9
- import com .microsoft .azure .management .Azure ;
10
- import com .microsoft .azure .management .resources .ResourceGroup ;
11
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
12
- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
13
- import com .microsoft .rest .LogLevel ;
4
+ package com .azure .resourcemanager .resources .samples ;
14
5
15
- import java .io .File ;
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 .resources .models .ResourceGroup ;
12
+ import com .azure .core .management .Region ;
13
+ import com .azure .core .management .profile .AzureProfile ;
14
+ import com .azure .resourcemanager .samples .Utils ;
16
15
17
16
/**
18
17
* Azure Resource sample for managing resource groups -
26
25
public final class ManageResourceGroup {
27
26
/**
28
27
* Main function which runs the actual sample.
29
- * @param azure instance of the azure client
28
+ *
29
+ * @param azureResourceManager instance of the azure client
30
30
* @return true if sample runs successfully
31
31
*/
32
- public static boolean runSample (Azure azure ) {
33
- final String rgName = SdkContext .randomResourceName ("rgRSMA" , 24 );
34
- final String rgName2 = SdkContext .randomResourceName ("rgRSMA" , 24 );
35
- final String resourceTagName = SdkContext .randomResourceName ("rgRSTN" , 24 );
36
- final String resourceTagValue = SdkContext .randomResourceName ("rgRSTV" , 24 );
32
+ public static boolean runSample (AzureResourceManager azureResourceManager ) {
33
+ final String rgName = Utils .randomResourceName (azureResourceManager , "rgRSMA" , 24 );
34
+ final String rgName2 = Utils .randomResourceName (azureResourceManager , "rgRSMA" , 24 );
35
+ final String resourceTagName = Utils .randomResourceName (azureResourceManager , "rgRSTN" , 24 );
36
+ final String resourceTagValue = Utils .randomResourceName (azureResourceManager , "rgRSTV" , 24 );
37
37
try {
38
38
39
39
@@ -42,7 +42,7 @@ public static boolean runSample(Azure azure) {
42
42
43
43
System .out .println ("Creating a resource group with name: " + rgName );
44
44
45
- ResourceGroup resourceGroup = azure .resourceGroups ().define (rgName )
45
+ ResourceGroup resourceGroup = azureResourceManager .resourceGroups ().define (rgName )
46
46
.withRegion (Region .US_WEST )
47
47
.create ();
48
48
@@ -66,7 +66,7 @@ public static boolean runSample(Azure azure) {
66
66
67
67
System .out .println ("Creating another resource group with name: " + rgName2 );
68
68
69
- azure .resourceGroups ().define (rgName2 )
69
+ azureResourceManager .resourceGroups ().define (rgName2 )
70
70
.withRegion (Region .US_WEST )
71
71
.create ();
72
72
@@ -78,7 +78,7 @@ public static boolean runSample(Azure azure) {
78
78
79
79
System .out .println ("Listing all resource groups" );
80
80
81
- for (ResourceGroup rGroup : azure .resourceGroups ().list ()) {
81
+ for (ResourceGroup rGroup : azureResourceManager .resourceGroups ().list ()) {
82
82
System .out .println ("Resource group: " + rGroup .name ());
83
83
}
84
84
@@ -88,25 +88,19 @@ public static boolean runSample(Azure azure) {
88
88
89
89
System .out .println ("Deleting resource group: " + rgName2 );
90
90
91
- azure .resourceGroups ().beginDeleteByName (rgName2 );
91
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName2 );
92
92
return true ;
93
- } catch (Exception f ) {
94
-
95
- System .out .println (f .getMessage ());
96
- f .printStackTrace ();
97
-
98
93
} finally {
99
94
100
95
try {
101
96
System .out .println ("Deleting Resource Group: " + rgName );
102
- azure .resourceGroups ().beginDeleteByName (rgName );
97
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
103
98
} catch (NullPointerException npe ) {
104
99
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
105
100
} catch (Exception g ) {
106
101
g .printStackTrace ();
107
102
}
108
103
}
109
- return false ;
110
104
}
111
105
112
106
/**
@@ -119,14 +113,18 @@ public static void main(String[] args) {
119
113
//=================================================================
120
114
// Authenticate
121
115
122
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
116
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
117
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
118
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
119
+ .build ();
123
120
124
- Azure azure = Azure .configure ()
125
- .withLogLevel (LogLevel .NONE )
126
- .authenticate (credFile )
127
- .withDefaultSubscription ();
121
+ AzureResourceManager azureResourceManager = AzureResourceManager
122
+ .configure ()
123
+ .withLogLevel (HttpLogDetailLevel .BASIC )
124
+ .authenticate (credential , profile )
125
+ .withDefaultSubscription ();
128
126
129
- runSample (azure );
127
+ runSample (azureResourceManager );
130
128
} catch (Exception e ) {
131
129
System .out .println (e .getMessage ());
132
130
e .printStackTrace ();
0 commit comments