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 ;
918import com .fasterxml .jackson .core .JsonProcessingException ;
1019import com .fasterxml .jackson .databind .JsonNode ;
1120import com .fasterxml .jackson .databind .ObjectMapper ;
1221import 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+
2323import java .io .IOException ;
2424import java .io .InputStream ;
2525
@@ -31,22 +31,23 @@ public final class DeployUsingARMTemplate {
3131
3232 /**
3333 * Main function which runs the actual sample.
34- * @param azure instance of the azure client
34+ *
35+ * @param azureResourceManager instance of the azure client
3536 * @return true if sample runs successfully
3637 */
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 );
4041 try {
41- String templateJson = DeployUsingARMTemplate .getTemplate ();
42+ String templateJson = DeployUsingARMTemplate .getTemplate (azureResourceManager );
4243
4344
4445 //=============================================================
4546 // Create resource group.
4647
4748 System .out .println ("Creating a resource group with name: " + rgName );
4849
49- azure .resourceGroups ().define (rgName )
50+ azureResourceManager .resourceGroups ().define (rgName )
5051 .withRegion (Region .US_EAST2 )
5152 .create ();
5253
@@ -61,7 +62,7 @@ public static boolean runSample(Azure azure) {
6162 //
6263 System .out .println ("Starting a deployment for an Azure App Service: " + deploymentName );
6364
64- azure .deployments ().define (deploymentName )
65+ azureResourceManager .deployments ().define (deploymentName )
6566 .withExistingResourceGroup (rgName )
6667 .withTemplate (templateJson )
6768 .withParameters ("{}" )
@@ -70,16 +71,11 @@ public static boolean runSample(Azure azure) {
7071
7172 System .out .println ("Started a deployment for an Azure App Service: " + deploymentName );
7273 return true ;
73- } catch (Exception f ) {
74-
75- System .out .println (f .getMessage ());
76- f .printStackTrace ();
77-
7874 } finally {
7975 try {
80- Deployment deployment = azure .deployments ()
76+ Deployment deployment = azureResourceManager .deployments ()
8177 .getByResourceGroup (rgName , deploymentName );
82- PagedList <DeploymentOperation > operations = deployment .deploymentOperations ()
78+ PagedIterable <DeploymentOperation > operations = deployment .deploymentOperations ()
8379 .list ();
8480
8581 for (DeploymentOperation operation : operations ) {
@@ -101,7 +97,7 @@ public static boolean runSample(Azure azure) {
10197
10298 try {
10399 System .out .println ("Deleting Resource Group: " + rgName );
104- azure .resourceGroups ().beginDeleteByName (rgName );
100+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
105101 System .out .println ("Deleted Resource Group: " + rgName );
106102 } catch (NullPointerException npe ) {
107103 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) {
110106 }
111107
112108 }
113- return false ;
114109 }
115110
116111 /**
@@ -124,39 +119,44 @@ public static void main(String[] args) {
124119 //=================================================================
125120 // Authenticate
126121
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 ();
128126
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 ();
133132
134- runSample (azure );
133+ runSample (azureResourceManager );
135134 } catch (Exception e ) {
136135 System .out .println (e .getMessage ());
137136 e .printStackTrace ();
138137 }
139138 }
140139
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 );
146143
147- final ObjectMapper mapper = new ObjectMapper ();
148- final JsonNode tmp = mapper .readTree (embeddedTemplate );
144+ try (InputStream embeddedTemplate = DeployUsingARMTemplate .class .getResourceAsStream ("/templateValue.json" )) {
149145
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 );
154148
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+ }
156156 }
157157
158158 private static void validateAndAddFieldValue (String type , String fieldValue , String fieldName , String errorMessage ,
159- JsonNode tmp ) throws IllegalAccessException {
159+ JsonNode tmp ) throws IllegalAccessException {
160160 // Add count variable for loop....
161161 final ObjectMapper mapper = new ObjectMapper ();
162162 final ObjectNode parameter = mapper .createObjectNode ();
0 commit comments