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 ;
8
-
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ package com .azure .resourcemanager .resources .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 .resources .models .Deployment ;
13
+ import com .azure .resourcemanager .resources .models .DeploymentMode ;
14
+ import com .azure .resourcemanager .resources .models .DeploymentOperation ;
15
+ import com .azure .core .management .Region ;
16
+ import com .azure .core .management .profile .AzureProfile ;
17
+ import com .azure .resourcemanager .samples .Utils ;
9
18
import com .fasterxml .jackson .core .JsonProcessingException ;
10
19
import com .fasterxml .jackson .databind .JsonNode ;
11
20
import com .fasterxml .jackson .databind .ObjectMapper ;
12
21
import com .fasterxml .jackson .databind .node .ObjectNode ;
13
- import com .microsoft .azure .PagedList ;
14
- import com .microsoft .azure .management .Azure ;
15
- import com .microsoft .azure .management .resources .Deployment ;
16
- import com .microsoft .azure .management .resources .DeploymentMode ;
17
- import com .microsoft .azure .management .resources .DeploymentOperation ;
18
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
19
- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
20
- import com .microsoft .rest .LogLevel ;
21
-
22
- import java .io .File ;
22
+
23
23
import java .io .IOException ;
24
24
import java .io .InputStream ;
25
25
@@ -31,22 +31,23 @@ public final class DeployUsingARMTemplate {
31
31
32
32
/**
33
33
* Main function which runs the actual sample.
34
- * @param azure instance of the azure client
34
+ *
35
+ * @param azureResourceManager instance of the azure client
35
36
* @return true if sample runs successfully
36
37
*/
37
- public static boolean runSample (Azure azure ) {
38
- final String rgName = SdkContext .randomResourceName ("rgRSAT" , 24 );
39
- final String deploymentName = SdkContext .randomResourceName ("dpRSAT" , 24 );
38
+ public static boolean runSample (AzureResourceManager azureResourceManager ) throws IOException , IllegalAccessException {
39
+ final String rgName = Utils .randomResourceName (azureResourceManager , "rgRSAT" , 24 );
40
+ final String deploymentName = Utils .randomResourceName (azureResourceManager , "dpRSAT" , 24 );
40
41
try {
41
- String templateJson = DeployUsingARMTemplate .getTemplate ();
42
+ String templateJson = DeployUsingARMTemplate .getTemplate (azureResourceManager );
42
43
43
44
44
45
//=============================================================
45
46
// Create resource group.
46
47
47
48
System .out .println ("Creating a resource group with name: " + rgName );
48
49
49
- azure .resourceGroups ().define (rgName )
50
+ azureResourceManager .resourceGroups ().define (rgName )
50
51
.withRegion (Region .US_EAST2 )
51
52
.create ();
52
53
@@ -61,7 +62,7 @@ public static boolean runSample(Azure azure) {
61
62
//
62
63
System .out .println ("Starting a deployment for an Azure App Service: " + deploymentName );
63
64
64
- azure .deployments ().define (deploymentName )
65
+ azureResourceManager .deployments ().define (deploymentName )
65
66
.withExistingResourceGroup (rgName )
66
67
.withTemplate (templateJson )
67
68
.withParameters ("{}" )
@@ -70,16 +71,11 @@ public static boolean runSample(Azure azure) {
70
71
71
72
System .out .println ("Started a deployment for an Azure App Service: " + deploymentName );
72
73
return true ;
73
- } catch (Exception f ) {
74
-
75
- System .out .println (f .getMessage ());
76
- f .printStackTrace ();
77
-
78
74
} finally {
79
75
try {
80
- Deployment deployment = azure .deployments ()
76
+ Deployment deployment = azureResourceManager .deployments ()
81
77
.getByResourceGroup (rgName , deploymentName );
82
- PagedList <DeploymentOperation > operations = deployment .deploymentOperations ()
78
+ PagedIterable <DeploymentOperation > operations = deployment .deploymentOperations ()
83
79
.list ();
84
80
85
81
for (DeploymentOperation operation : operations ) {
@@ -101,7 +97,7 @@ public static boolean runSample(Azure azure) {
101
97
102
98
try {
103
99
System .out .println ("Deleting Resource Group: " + rgName );
104
- azure .resourceGroups ().beginDeleteByName (rgName );
100
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
105
101
System .out .println ("Deleted Resource Group: " + rgName );
106
102
} catch (NullPointerException npe ) {
107
103
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
@@ -110,7 +106,6 @@ public static boolean runSample(Azure azure) {
110
106
}
111
107
112
108
}
113
- return false ;
114
109
}
115
110
116
111
/**
@@ -124,39 +119,44 @@ public static void main(String[] args) {
124
119
//=================================================================
125
120
// Authenticate
126
121
127
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
122
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
123
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
124
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
125
+ .build ();
128
126
129
- Azure azure = Azure .configure ()
130
- .withLogLevel (LogLevel .NONE )
131
- .authenticate (credFile )
132
- .withDefaultSubscription ();
127
+ AzureResourceManager azureResourceManager = AzureResourceManager
128
+ .configure ()
129
+ .withLogLevel (HttpLogDetailLevel .BASIC )
130
+ .authenticate (credential , profile )
131
+ .withDefaultSubscription ();
133
132
134
- runSample (azure );
133
+ runSample (azureResourceManager );
135
134
} catch (Exception e ) {
136
135
System .out .println (e .getMessage ());
137
136
e .printStackTrace ();
138
137
}
139
138
}
140
139
141
- private static String getTemplate () throws IllegalAccessException , JsonProcessingException , IOException {
142
- final String hostingPlanName = SdkContext .randomResourceName ("hpRSAT" , 24 );
143
- final String webappName = SdkContext .randomResourceName ("wnRSAT" , 24 );
144
- final InputStream embeddedTemplate ;
145
- embeddedTemplate = DeployUsingARMTemplate .class .getResourceAsStream ("/templateValue.json" );
140
+ private static String getTemplate (AzureResourceManager azureResourceManager ) throws IllegalAccessException , JsonProcessingException , IOException {
141
+ final String hostingPlanName = Utils .randomResourceName (azureResourceManager , "hpRSAT" , 24 );
142
+ final String webappName = Utils .randomResourceName (azureResourceManager , "wnRSAT" , 24 );
146
143
147
- final ObjectMapper mapper = new ObjectMapper ();
148
- final JsonNode tmp = mapper .readTree (embeddedTemplate );
144
+ try (InputStream embeddedTemplate = DeployUsingARMTemplate .class .getResourceAsStream ("/templateValue.json" )) {
149
145
150
- DeployUsingARMTemplate .validateAndAddFieldValue ("string" , hostingPlanName , "hostingPlanName" , null , tmp );
151
- DeployUsingARMTemplate .validateAndAddFieldValue ("string" , webappName , "webSiteName" , null , tmp );
152
- DeployUsingARMTemplate .validateAndAddFieldValue ("string" , "F1" , "skuName" , null , tmp );
153
- DeployUsingARMTemplate .validateAndAddFieldValue ("int" , "1" , "skuCapacity" , null , tmp );
146
+ final ObjectMapper mapper = new ObjectMapper ();
147
+ final JsonNode tmp = mapper .readTree (embeddedTemplate );
154
148
155
- return tmp .toString ();
149
+ DeployUsingARMTemplate .validateAndAddFieldValue ("string" , hostingPlanName , "hostingPlanName" , null , tmp );
150
+ DeployUsingARMTemplate .validateAndAddFieldValue ("string" , webappName , "webSiteName" , null , tmp );
151
+ DeployUsingARMTemplate .validateAndAddFieldValue ("string" , "F1" , "skuName" , null , tmp );
152
+ DeployUsingARMTemplate .validateAndAddFieldValue ("int" , "1" , "skuCapacity" , null , tmp );
153
+
154
+ return tmp .toString ();
155
+ }
156
156
}
157
157
158
158
private static void validateAndAddFieldValue (String type , String fieldValue , String fieldName , String errorMessage ,
159
- JsonNode tmp ) throws IllegalAccessException {
159
+ JsonNode tmp ) throws IllegalAccessException {
160
160
// Add count variable for loop....
161
161
final ObjectMapper mapper = new ObjectMapper ();
162
162
final ObjectNode parameter = mapper .createObjectNode ();
0 commit comments