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 .appservice .samples ;
8-
9- import com .microsoft .azure .management .Azure ;
10- import com .microsoft .azure .management .appservice .AppServicePlan ;
11- import com .microsoft .azure .management .appservice .PricingTier ;
12- import com .microsoft .azure .management .appservice .PublishingProfile ;
13- import com .microsoft .azure .management .appservice .RuntimeStack ;
14- import com .microsoft .azure .management .appservice .WebApp ;
15- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
16- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
17- import com .microsoft .azure .management .samples .Utils ;
18- import com .microsoft .rest .LogLevel ;
19- import okhttp3 .OkHttpClient ;
20- import okhttp3 .Request ;
1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT License.
3+
4+ package com .azure .resourcemanager .appservice .samples ;
5+
6+ import com .azure .core .credential .TokenCredential ;
7+ import com .azure .core .management .AzureEnvironment ;
8+ import com .azure .identity .DefaultAzureCredentialBuilder ;
9+ import com .azure .resourcemanager .AzureResourceManager ;
10+ import com .azure .resourcemanager .appservice .models .AppServicePlan ;
11+ import com .azure .resourcemanager .appservice .models .PricingTier ;
12+ import com .azure .resourcemanager .appservice .models .PublishingProfile ;
13+ import com .azure .resourcemanager .appservice .models .RuntimeStack ;
14+ import com .azure .resourcemanager .appservice .models .WebApp ;
15+ import com .azure .core .management .Region ;
16+ import com .azure .core .management .profile .AzureProfile ;
17+ import com .azure .resourcemanager .resources .fluentcore .utils .ResourceManagerUtils ;
18+ import com .azure .resourcemanager .samples .Utils ;
19+ import com .azure .core .http .policy .HttpLogDetailLevel ;
2120import org .eclipse .jgit .api .Git ;
2221import org .eclipse .jgit .api .PushCommand ;
22+ import org .eclipse .jgit .api .errors .GitAPIException ;
2323import org .eclipse .jgit .transport .RefSpec ;
2424import org .eclipse .jgit .transport .UsernamePasswordCredentialsProvider ;
2525
2626import java .io .File ;
27- import java .io . IOException ;
28- import java . util . concurrent . TimeUnit ;
27+ import java .time . Duration ;
28+
2929
3030/**
3131 * Azure App Service basic sample for managing web apps.
3737 */
3838public final class ManageLinuxWebAppSourceControl {
3939
40- private static OkHttpClient httpClient ;
41-
4240 /**
4341 * Main function which runs the actual sample.
44- * @param azure instance of the azure client
42+ * @param azureResourceManager instance of the azure client
4543 * @return true if sample runs successfully
4644 */
47- public static boolean runSample (Azure azure ) {
45+ public static boolean runSample (AzureResourceManager azureResourceManager ) throws GitAPIException {
4846 // New resources
4947 final String suffix = ".azurewebsites.net" ;
50- final String app1Name = SdkContext .randomResourceName ("webapp1-" , 20 );
51- final String app2Name = SdkContext .randomResourceName ("webapp2-" , 20 );
52- final String app3Name = SdkContext .randomResourceName ("webapp3-" , 20 );
53- final String app4Name = SdkContext .randomResourceName ("webapp4-" , 20 );
48+ final String app1Name = Utils .randomResourceName (azureResourceManager , "webapp1-" , 20 );
49+ final String app2Name = Utils .randomResourceName (azureResourceManager , "webapp2-" , 20 );
50+ final String app3Name = Utils .randomResourceName (azureResourceManager , "webapp3-" , 20 );
51+ final String app4Name = Utils .randomResourceName (azureResourceManager , "webapp4-" , 20 );
5452 final String app1Url = app1Name + suffix ;
5553 final String app2Url = app2Name + suffix ;
5654 final String app3Url = app3Name + suffix ;
5755 final String app4Url = app4Name + suffix ;
58- final String rgName = SdkContext .randomResourceName ("rg1NEMV_" , 24 );
56+ final String rgName = Utils .randomResourceName (azureResourceManager , "rg1NEMV_" , 24 );
5957
6058 try {
6159
@@ -65,7 +63,7 @@ public static boolean runSample(Azure azure) {
6563
6664 System .out .println ("Creating web app " + app1Name + " in resource group " + rgName + "..." );
6765
68- WebApp app1 = azure .webApps ().define (app1Name )
66+ WebApp app1 = azureResourceManager .webApps ().define (app1Name )
6967 .withRegion (Region .US_WEST )
7068 .withNewResourceGroup (rgName )
7169 .withNewLinuxPlan (PricingTier .STANDARD_S1 )
@@ -80,24 +78,24 @@ public static boolean runSample(Azure azure) {
8078
8179 System .out .println ("Deploying helloworld.war to " + app1Name + " through FTP..." );
8280
83- Utils .uploadFileToWebApp (app1 .getPublishingProfile (), "helloworld.war" , ManageLinuxWebAppSourceControl .class .getResourceAsStream ("/helloworld.war" ));
81+ Utils .uploadFileViaFtp (app1 .getPublishingProfile (), "helloworld.war" , ManageLinuxWebAppSourceControl .class .getResourceAsStream ("/helloworld.war" ));
8482
8583 System .out .println ("Deployment helloworld.war to web app " + app1 .name () + " completed" );
8684 Utils .print (app1 );
8785
8886 // warm up
8987 System .out .println ("Warming up " + app1Url + "/helloworld..." );
90- curl ("http://" + app1Url + "/helloworld" );
91- SdkContext .sleep (5000 );
88+ Utils . sendGetRequest ("http://" + app1Url + "/helloworld/ " );
89+ ResourceManagerUtils .sleep (Duration . ofSeconds ( 5 ) );
9290 System .out .println ("CURLing " + app1Url + "/helloworld..." );
93- System .out .println (curl ("http://" + app1Url + "/helloworld" ));
91+ System .out .println (Utils . sendGetRequest ("http://" + app1Url + "/helloworld/ " ));
9492
9593 //============================================================
9694 // Create a second web app with local git source control
9795
9896 System .out .println ("Creating another web app " + app2Name + " in resource group " + rgName + "..." );
99- AppServicePlan plan = azure . appServices () .appServicePlans ().getById (app1 .appServicePlanId ());
100- WebApp app2 = azure .webApps ().define (app2Name )
97+ AppServicePlan plan = azureResourceManager .appServicePlans ().getById (app1 .appServicePlanId ());
98+ WebApp app2 = azureResourceManager .webApps ().define (app2Name )
10199 .withExistingLinuxPlan (plan )
102100 .withExistingResourceGroup (rgName )
103101 .withBuiltInImage (RuntimeStack .TOMCAT_8_5_JRE8 )
@@ -131,16 +129,16 @@ public static boolean runSample(Azure azure) {
131129
132130 // warm up
133131 System .out .println ("Warming up " + app2Url + "/helloworld..." );
134- curl ("http://" + app2Url + "/helloworld" );
135- SdkContext .sleep (5000 );
132+ Utils . sendGetRequest ("http://" + app2Url + "/helloworld/ " );
133+ ResourceManagerUtils .sleep (Duration . ofSeconds ( 5 ) );
136134 System .out .println ("CURLing " + app2Url + "/helloworld..." );
137- System .out .println (curl ("http://" + app2Url + "/helloworld" ));
135+ System .out .println (Utils . sendGetRequest ("http://" + app2Url + "/helloworld/ " ));
138136
139137 //============================================================
140138 // Create a 3rd web app with a public GitHub repo in Azure-Samples
141139
142140 System .out .println ("Creating another web app " + app3Name + "..." );
143- WebApp app3 = azure .webApps ().define (app3Name )
141+ WebApp app3 = azureResourceManager .webApps ().define (app3Name )
144142 .withExistingLinuxPlan (plan )
145143 .withNewResourceGroup (rgName )
146144 .withBuiltInImage (RuntimeStack .TOMCAT_8_5_JRE8 )
@@ -155,16 +153,16 @@ public static boolean runSample(Azure azure) {
155153
156154 // warm up
157155 System .out .println ("Warming up " + app3Url + "..." );
158- curl ("http://" + app3Url );
159- SdkContext .sleep (5000 );
156+ Utils . sendGetRequest ("http://" + app3Url );
157+ ResourceManagerUtils .sleep (Duration . ofSeconds ( 5 ) );
160158 System .out .println ("CURLing " + app3Url + "..." );
161- System .out .println (curl ("http://" + app3Url ));
159+ System .out .println (Utils . sendGetRequest ("http://" + app3Url ));
162160
163161 //============================================================
164162 // Create a 4th web app with a personal GitHub repo and turn on continuous integration
165163
166164 System .out .println ("Creating another web app " + app4Name + "..." );
167- WebApp app4 = azure .webApps ()
165+ WebApp app4 = azureResourceManager .webApps ()
168166 .define (app4Name )
169167 .withExistingLinuxPlan (plan )
170168 .withExistingResourceGroup (rgName )
@@ -182,27 +180,23 @@ public static boolean runSample(Azure azure) {
182180
183181 // warm up
184182 System .out .println ("Warming up " + app4Url + "..." );
185- curl ("http://" + app4Url );
186- SdkContext .sleep (5000 );
183+ Utils . sendGetRequest ("http://" + app4Url );
184+ ResourceManagerUtils .sleep (Duration . ofSeconds ( 5 ) );
187185 System .out .println ("CURLing " + app4Url + "..." );
188- System .out .println (curl ("http://" + app4Url ));
186+ System .out .println (Utils . sendGetRequest ("http://" + app4Url ));
189187
190188 return true ;
191- } catch (Exception e ) {
192- System .err .println (e .getMessage ());
193- e .printStackTrace ();
194189 } finally {
195190 try {
196191 System .out .println ("Deleting Resource Group: " + rgName );
197- azure .resourceGroups ().beginDeleteByName (rgName );
192+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
198193 System .out .println ("Deleted Resource Group: " + rgName );
199194 } catch (NullPointerException npe ) {
200195 System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
201196 } catch (Exception g ) {
202197 g .printStackTrace ();
203198 }
204199 }
205- return false ;
206200 }
207201 /**
208202 * Main entry point.
@@ -214,34 +208,24 @@ public static void main(String[] args) {
214208 //=============================================================
215209 // Authenticate
216210
217- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
211+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
212+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
213+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
214+ .build ();
218215
219- Azure azure = Azure
220- .configure ()
221- .withLogLevel (LogLevel .BASIC )
222- .authenticate (credFile )
223- .withDefaultSubscription ();
216+ AzureResourceManager azureResourceManager = AzureResourceManager
217+ .configure ()
218+ .withLogLevel (HttpLogDetailLevel .BASIC )
219+ .authenticate (credential , profile )
220+ .withDefaultSubscription ();
224221
225222 // Print selected subscription
226- System .out .println ("Selected subscription: " + azure .subscriptionId ());
223+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
227224
228- runSample (azure );
225+ runSample (azureResourceManager );
229226 } catch (Exception e ) {
230227 System .out .println (e .getMessage ());
231228 e .printStackTrace ();
232229 }
233230 }
234-
235- private static String curl (String url ) {
236- Request request = new Request .Builder ().url (url ).get ().build ();
237- try {
238- return httpClient .newCall (request ).execute ().body ().string ();
239- } catch (IOException e ) {
240- return null ;
241- }
242- }
243-
244- static {
245- httpClient = new OkHttpClient .Builder ().readTimeout (1 , TimeUnit .MINUTES ).build ();
246- }
247231}
0 commit comments