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 .AppServiceDomain ;
11- import  com .microsoft .azure .management .appservice .AppServicePlan ;
12- import  com .microsoft .azure .management .appservice .CustomHostNameDnsRecordType ;
13- import  com .microsoft .azure .management .appservice .PricingTier ;
14- import  com .microsoft .azure .management .appservice .RuntimeStack ;
15- import  com .microsoft .azure .management .appservice .WebApp ;
16- import  com .microsoft .azure .management .resources .fluentcore .arm .CountryIsoCode ;
17- import  com .microsoft .azure .management .resources .fluentcore .arm .CountryPhoneCode ;
18- import  com .microsoft .azure .management .resources .fluentcore .arm .Region ;
19- import  com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
20- import  com .microsoft .azure .management .samples .Utils ;
21- import  com .microsoft .rest .LogLevel ;
22- import  okhttp3 .OkHttpClient ;
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 .AppServiceDomain ;
11+ import  com .azure .resourcemanager .appservice .models .AppServicePlan ;
12+ import  com .azure .resourcemanager .appservice .models .CustomHostnameDnsRecordType ;
13+ import  com .azure .resourcemanager .appservice .models .PricingTier ;
14+ import  com .azure .resourcemanager .appservice .models .RuntimeStack ;
15+ import  com .azure .resourcemanager .appservice .models .WebApp ;
16+ import  com .azure .resourcemanager .resources .fluentcore .arm .CountryIsoCode ;
17+ import  com .azure .resourcemanager .resources .fluentcore .arm .CountryPhoneCode ;
18+ import  com .azure .core .management .Region ;
19+ import  com .azure .core .management .profile .AzureProfile ;
20+ import  com .azure .resourcemanager .samples .Utils ;
21+ import  com .azure .core .http .policy .HttpLogDetailLevel ;
2322
2423import  java .io .File ;
25- import  java .util . concurrent . TimeUnit ;
24+ import  java .io . IOException ;
2625
2726/** 
2827 * Azure App Service sample for managing web apps. 
3635 */ 
3736public  final  class  ManageLinuxWebAppWithDomainSsl  {
3837
39-     private  static  OkHttpClient  httpClient ;
40- 
4138    /** 
4239     * Main function which runs the actual sample. 
43-      * @param azure  instance of the azure client 
40+      * @param azureResourceManager  instance of the azure client 
4441     * @return true if sample runs successfully 
4542     */ 
46-     public  static  boolean  runSample (Azure   azure )  {
43+     public  static  boolean  runSample (AzureResourceManager   azureResourceManager )  throws   IOException  {
4744        // New resources 
48-         final  String  app1Name        = SdkContext .randomResourceName ("webapp1-" , 20 );
49-         final  String  app2Name        = SdkContext .randomResourceName ("webapp2-" , 20 );
50-         final  String  rgName          = SdkContext .randomResourceName ("rgNEMV_" , 24 );
51-         final  String  domainName      = SdkContext .randomResourceName ("jsdkdemo-" , 20 ) + ".com" ;
52-         // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")] 
53-         final  String  certPassword    = "StrongPass!12" ;
45+         final  String  app1Name        = Utils .randomResourceName (azureResourceManager , "webapp1-" , 20 );
46+         final  String  app2Name        = Utils .randomResourceName (azureResourceManager , "webapp2-" , 20 );
47+         final  String  rgName          = Utils .randomResourceName (azureResourceManager , "rgNEMV_" , 24 );
48+         final  String  domainName      = Utils .randomResourceName (azureResourceManager , "jsdkdemo-" , 20 ) + ".com" ;
49+         final  String  certPassword    = Utils .password ();
5450
5551        try  {
5652            //============================================================ 
5753            // Create a web app with a new app service plan 
5854
5955            System .out .println ("Creating web app "  + app1Name  + "..." );
6056
61-             WebApp  app1  = azure .webApps ().define (app1Name )
57+             WebApp  app1  = azureResourceManager .webApps ().define (app1Name )
6258                    .withRegion (Region .US_WEST )
6359                    .withNewResourceGroup (rgName )
6460                    .withNewLinuxPlan (PricingTier .STANDARD_S1 )
65-                     .withBuiltInImage (RuntimeStack .NODEJS_6_9 )
61+                     .withBuiltInImage (RuntimeStack .NODEJS_10_LTS )
6662                    .create ();
6763
6864            System .out .println ("Created web app "  + app1 .name ());
@@ -72,11 +68,11 @@ public static boolean runSample(Azure azure) {
7268            // Create a second web app with the same app service plan 
7369
7470            System .out .println ("Creating another web app "  + app2Name  + "..." );
75-             AppServicePlan  plan  = azure . appServices () .appServicePlans ().getById (app1 .appServicePlanId ());
76-             WebApp  app2  = azure .webApps ().define (app2Name )
71+             AppServicePlan  plan  = azureResourceManager .appServicePlans ().getById (app1 .appServicePlanId ());
72+             WebApp  app2  = azureResourceManager .webApps ().define (app2Name )
7773                    .withExistingLinuxPlan (plan )
7874                    .withExistingResourceGroup (rgName )
79-                     .withBuiltInImage (RuntimeStack .NODEJS_6_9 )
75+                     .withBuiltInImage (RuntimeStack .NODEJS_10_LTS )
8076                    .create ();
8177
8278            System .out .println ("Created web app "  + app2 .name ());
@@ -87,7 +83,7 @@ public static boolean runSample(Azure azure) {
8783
8884            System .out .println ("Purchasing a domain "  + domainName  + "..." );
8985
90-             AppServiceDomain  domain  = azure . appServices (). domains ().define (domainName )
86+             AppServiceDomain  domain  = azureResourceManager . appServiceDomains ().define (domainName )
9187                    .withExistingResourceGroup (rgName )
9288                    .defineRegistrantContact ()
9389                        .withFirstName ("Jon" )
@@ -116,7 +112,7 @@ public static boolean runSample(Azure azure) {
116112                    .defineHostnameBinding ()
117113                        .withAzureManagedDomain (domain )
118114                        .withSubDomain (app1Name )
119-                         .withDnsRecordType (CustomHostNameDnsRecordType .CNAME )
115+                         .withDnsRecordType (CustomHostnameDnsRecordType .CNAME )
120116                        .attach ()
121117                    .apply ();
122118
@@ -126,8 +122,8 @@ public static boolean runSample(Azure azure) {
126122            //============================================================ 
127123            // Create a self-singed SSL certificate 
128124
129-             String  pfxPath  = ManageLinuxWebAppWithDomainSsl .class .getResource ("/" ).getPath () + "webapp_"  + ManageLinuxWebAppWithDomainSsl . class . getSimpleName (). toLowerCase ()  + ".pfx" ;
130-             String  cerPath  = ManageLinuxWebAppWithDomainSsl .class .getResource ("/" ).getPath () + "webapp_"  + ManageLinuxWebAppWithDomainSsl . class . getSimpleName (). toLowerCase ()  + ".cer" ;
125+             String  pfxPath  = ManageLinuxWebAppWithDomainSsl .class .getResource ("/" ).getPath () + "webapp_"  + domainName  + ".pfx" ;
126+             String  cerPath  = ManageLinuxWebAppWithDomainSsl .class .getResource ("/" ).getPath () + "webapp_"  + domainName  + ".cer" ;
131127
132128            System .out .println ("Creating a self-signed certificate "  + pfxPath  + "..." );
133129
@@ -158,7 +154,7 @@ public static boolean runSample(Azure azure) {
158154                    .withManagedHostnameBindings (domain , app2Name )
159155                    .defineSslBinding ()
160156                        .forHostname (app2Name  + "."  + domainName )
161-                         .withExistingCertificate (app1 .hostNameSslStates ().get (app1Name  + "."  + domainName ).thumbprint ())
157+                         .withExistingCertificate (app1 .hostnameSslStates ().get (app1Name  + "."  + domainName ).thumbprint ())
162158                        .withSniBasedSsl ()
163159                        .attach ()
164160                    .apply ();
@@ -167,22 +163,17 @@ public static boolean runSample(Azure azure) {
167163            Utils .print (app2 );
168164
169165            return  true ;
170-         } catch  (Exception  e ) {
171-             System .err .println (e .getMessage ());
172-             e .printStackTrace ();
173166        } finally  {
174167            try  {
175168                System .out .println ("Deleting Resource Group: "  + rgName );
176-                 azure .resourceGroups ().deleteByName (rgName );
169+                 azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
177170                System .out .println ("Deleted Resource Group: "  + rgName );
178171            } catch  (NullPointerException  npe ) {
179172                System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
180173            } catch  (Exception  g ) {
181174                g .printStackTrace ();
182175            }
183176        }
184- 
185-         return  false ;
186177    }
187178    /** 
188179     * Main entry point. 
@@ -196,27 +187,25 @@ public static void main(String[] args) {
196187            //============================================================= 
197188            // Authenticate 
198189
199-             final  File  credFile  = new  File (System .getenv ("AZURE_AUTH_LOCATION" ));
190+             final  AzureProfile  profile  = new  AzureProfile (AzureEnvironment .AZURE );
191+             final  TokenCredential  credential  = new  DefaultAzureCredentialBuilder ()
192+                 .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
193+                 .build ();
200194
201-             Azure  azure  = Azure .configure ()
202-                     .withLogLevel (LogLevel .BODY_AND_HEADERS )
203-                     .authenticate (credFile )
204-                     .withDefaultSubscription ();
195+             AzureResourceManager  azureResourceManager  = AzureResourceManager 
196+                 .configure ()
197+                 .withLogLevel (HttpLogDetailLevel .BASIC )
198+                 .authenticate (credential , profile )
199+                 .withDefaultSubscription ();
205200
206201            // Print selected subscription 
207-             System .out .println ("Selected subscription: "  + azure .subscriptionId ());
202+             System .out .println ("Selected subscription: "  + azureResourceManager .subscriptionId ());
208203
209-             runSample (azure );
204+             runSample (azureResourceManager );
210205
211206        } catch  (Exception  e ) {
212207            System .out .println (e .getMessage ());
213208            e .printStackTrace ();
214209        }
215210    }
216- 
217-     static  {
218-         httpClient  = new  OkHttpClient .Builder ().readTimeout (1 , TimeUnit .MINUTES ).build ();
219-     }
220- 
221- 
222211}
0 commit comments