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 .management .AzureEnvironment ;
9
+ import com .azure .identity .DefaultAzureCredentialBuilder ;
10
+ import com .azure .resourcemanager .AzureResourceManager ;
11
+ import com .azure .resourcemanager .resources .models .Deployment ;
12
+ import com .azure .resourcemanager .resources .models .DeploymentMode ;
13
+ import com .azure .core .management .Region ;
14
+ import com .azure .core .management .profile .AzureProfile ;
15
+ import com .azure .resourcemanager .resources .fluentcore .utils .ResourceManagerUtils ;
16
+ import com .azure .resourcemanager .samples .Utils ;
9
17
import com .fasterxml .jackson .core .JsonProcessingException ;
10
18
import com .fasterxml .jackson .databind .JsonNode ;
11
19
import com .fasterxml .jackson .databind .ObjectMapper ;
12
20
import com .fasterxml .jackson .databind .node .ObjectNode ;
13
- import com .microsoft .azure .management .Azure ;
14
- import com .microsoft .azure .management .resources .Deployment ;
15
- import com .microsoft .azure .management .resources .DeploymentMode ;
16
- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
17
- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
18
- import com .microsoft .rest .LogLevel ;
19
-
20
- import java .io .File ;
21
+
21
22
import java .io .IOException ;
22
23
import java .io .InputStream ;
24
+ import java .time .Duration ;
23
25
24
26
/**
25
27
* Azure Resource sample for deploying resources using an ARM template and
@@ -30,21 +32,22 @@ public final class DeployUsingARMTemplateWithProgress {
30
32
31
33
/**
32
34
* Main function which runs the actual sample.
33
- * @param azure instance of the azure client
35
+ *
36
+ * @param azureResourceManager instance of the azure client
34
37
* @return true if sample runs successfully
35
38
*/
36
- public static boolean runSample (Azure azure ) {
37
- final String rgName = SdkContext .randomResourceName ("rgRSAP" , 24 );
38
- final String deploymentName = SdkContext .randomResourceName ("dpRSAP" , 24 );
39
+ public static boolean runSample (AzureResourceManager azureResourceManager ) throws IOException , IllegalAccessException {
40
+ final String rgName = Utils .randomResourceName (azureResourceManager , "rgRSAP" , 24 );
41
+ final String deploymentName = Utils .randomResourceName (azureResourceManager , "dpRSAP" , 24 );
39
42
try {
40
- String templateJson = DeployUsingARMTemplateWithProgress .getTemplate ();
43
+ String templateJson = DeployUsingARMTemplateWithProgress .getTemplate (azureResourceManager );
41
44
42
45
//=============================================================
43
46
// Create resource group.
44
47
45
48
System .out .println ("Creating a resource group with name: " + rgName );
46
49
47
- azure .resourceGroups ().define (rgName )
50
+ azureResourceManager .resourceGroups ().define (rgName )
48
51
.withRegion (Region .US_WEST )
49
52
.create ();
50
53
@@ -57,7 +60,7 @@ public static boolean runSample(Azure azure) {
57
60
58
61
System .out .println ("Starting a deployment for an Azure App Service: " + deploymentName );
59
62
60
- azure .deployments ().define (deploymentName )
63
+ azureResourceManager .deployments ().define (deploymentName )
61
64
.withExistingResourceGroup (rgName )
62
65
.withTemplate (templateJson )
63
66
.withParameters ("{}" )
@@ -66,26 +69,21 @@ public static boolean runSample(Azure azure) {
66
69
67
70
System .out .println ("Started a deployment for an Azure App Service: " + deploymentName );
68
71
69
- Deployment deployment = azure .deployments ().getByResourceGroup (rgName , deploymentName );
72
+ Deployment deployment = azureResourceManager .deployments ().getByResourceGroup (rgName , deploymentName );
70
73
System .out .println ("Current deployment status : " + deployment .provisioningState ());
71
74
72
75
while (!(deployment .provisioningState ().equalsIgnoreCase ("Succeeded" )
73
76
|| deployment .provisioningState ().equalsIgnoreCase ("Failed" )
74
77
|| deployment .provisioningState ().equalsIgnoreCase ("Cancelled" ))) {
75
- SdkContext .sleep (10000 );
76
- deployment = azure .deployments ().getByResourceGroup (rgName , deploymentName );
78
+ ResourceManagerUtils .sleep (Duration . ofSeconds ( 10 ) );
79
+ deployment = azureResourceManager .deployments ().getByResourceGroup (rgName , deploymentName );
77
80
System .out .println ("Current deployment status : " + deployment .provisioningState ());
78
81
}
79
82
return true ;
80
- } catch (Exception f ) {
81
-
82
- System .out .println (f .getMessage ());
83
- f .printStackTrace ();
84
-
85
83
} finally {
86
84
try {
87
85
System .out .println ("Deleting Resource Group: " + rgName );
88
- azure .resourceGroups ().beginDeleteByName (rgName );
86
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
89
87
System .out .println ("Deleted Resource Group: " + rgName );
90
88
} catch (NullPointerException npe ) {
91
89
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
@@ -94,49 +92,53 @@ public static boolean runSample(Azure azure) {
94
92
}
95
93
96
94
}
97
- return false ;
98
95
}
99
96
100
97
/**
101
98
* Main entry point.
99
+ *
102
100
* @param args the parameters
103
101
*/
104
102
public static void main (String [] args ) {
105
103
try {
106
104
//=================================================================
107
105
// Authenticate
108
106
109
- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
107
+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
108
+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
109
+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
110
+ .build ();
110
111
111
- Azure azure = Azure .configure ()
112
- .withLogLevel (LogLevel .BASIC )
113
- .authenticate (credFile )
114
- .withDefaultSubscription ();
112
+ AzureResourceManager azureResourceManager = AzureResourceManager
113
+ .configure ()
114
+ .withLogLevel (HttpLogDetailLevel .BASIC )
115
+ .authenticate (credential , profile )
116
+ .withDefaultSubscription ();
115
117
116
- runSample (azure );
117
- }
118
- catch (Exception e ) {
118
+ runSample (azureResourceManager );
119
+ } catch (Exception e ) {
119
120
System .out .println (e .getMessage ());
120
121
e .printStackTrace ();
121
122
}
122
123
123
124
}
124
125
125
- private static String getTemplate () throws IllegalAccessException , JsonProcessingException , IOException {
126
- final String hostingPlanName = SdkContext .randomResourceName ("hpRSAT" , 24 );
127
- final String webappName = SdkContext .randomResourceName ("wnRSAT" , 24 );
128
- final InputStream embeddedTemplate ;
129
- embeddedTemplate = DeployUsingARMTemplateWithProgress .class .getResourceAsStream ("/templateValue.json" );
126
+ private static String getTemplate (AzureResourceManager azureResourceManager ) throws IllegalAccessException , JsonProcessingException , IOException {
127
+ final String hostingPlanName = Utils .randomResourceName (azureResourceManager , "hpRSAT" , 24 );
128
+ final String webappName = Utils .randomResourceName (azureResourceManager , "wnRSAT" , 24 );
130
129
131
- final ObjectMapper mapper = new ObjectMapper ();
132
- final JsonNode tmp = mapper .readTree (embeddedTemplate );
130
+ try (InputStream embeddedTemplate = DeployUsingARMTemplateWithProgress .class .getResourceAsStream ("/templateValue.json" )) {
133
131
134
- DeployUsingARMTemplateWithProgress .validateAndAddFieldValue ("string" , hostingPlanName , "hostingPlanName" , null , tmp );
135
- DeployUsingARMTemplateWithProgress .validateAndAddFieldValue ("string" , webappName , "webSiteName" , null , tmp );
136
- DeployUsingARMTemplateWithProgress .validateAndAddFieldValue ("string" , "F1" , "skuName" , null , tmp );
137
- DeployUsingARMTemplateWithProgress .validateAndAddFieldValue ("int" , "1" , "skuCapacity" , null , tmp );
132
+ final ObjectMapper mapper = new ObjectMapper ();
133
+ final JsonNode tmp = mapper .readTree (embeddedTemplate );
138
134
139
- return tmp .toString ();
135
+ DeployUsingARMTemplateWithProgress .validateAndAddFieldValue ("string" , hostingPlanName , "hostingPlanName" , null , tmp );
136
+ DeployUsingARMTemplateWithProgress .validateAndAddFieldValue ("string" , webappName , "webSiteName" , null , tmp );
137
+ DeployUsingARMTemplateWithProgress .validateAndAddFieldValue ("string" , "F1" , "skuName" , null , tmp );
138
+ DeployUsingARMTemplateWithProgress .validateAndAddFieldValue ("int" , "1" , "skuCapacity" , null , tmp );
139
+
140
+ return tmp .toString ();
141
+ }
140
142
}
141
143
142
144
private static void validateAndAddFieldValue (String type , String fieldValue , String fieldName , String errorMessage ,
0 commit comments