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 .WebApp ;
15- import com .microsoft .azure .management .resources .fluentcore .arm .CountryIsoCode ;
16- import com .microsoft .azure .management .resources .fluentcore .arm .CountryPhoneCode ;
17- import com .microsoft .azure .management .resources .fluentcore .arm .Region ;
18- import com .microsoft .azure .management .resources .fluentcore .utils .SdkContext ;
19- import com .microsoft .azure .management .samples .Utils ;
20- import com .microsoft .rest .LogLevel ;
21- 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 .WebApp ;
15+ import com .azure .resourcemanager .resources .fluentcore .arm .CountryIsoCode ;
16+ import com .azure .resourcemanager .resources .fluentcore .arm .CountryPhoneCode ;
17+ import com .azure .core .management .Region ;
18+ import com .azure .core .management .profile .AzureProfile ;
19+ import com .azure .resourcemanager .samples .Utils ;
20+ import com .azure .core .http .policy .HttpLogDetailLevel ;
2221
2322import java .io .File ;
24- import java .util . concurrent . TimeUnit ;
23+ import java .io . IOException ;
2524
2625/**
2726 * Azure App Service sample for managing web apps.
3534 */
3635public final class ManageWebAppWithDomainSsl {
3736
38- private static OkHttpClient httpClient ;
39-
4037 /**
4138 * Main function which runs the actual sample.
42- * @param azure instance of the azure client
39+ * @param azureResourceManager instance of the azure client
4340 * @return true if sample runs successfully
4441 */
45- public static boolean runSample (Azure azure ) {
42+ public static boolean runSample (AzureResourceManager azureResourceManager ) throws IOException {
4643 // New resources
47- final String app1Name = SdkContext .randomResourceName ("webapp1-" , 20 );
48- final String app2Name = SdkContext .randomResourceName ("webapp2-" , 20 );
49- final String rgName = SdkContext .randomResourceName ("rgNEMV_" , 24 );
50- final String domainName = SdkContext .randomResourceName ("jsdkdemo-" , 20 ) + ".com" ;
51- // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
52- final String certPassword = "StrongPass!12" ;
44+ final String app1Name = Utils .randomResourceName (azureResourceManager , "webapp1-" , 20 );
45+ final String app2Name = Utils .randomResourceName (azureResourceManager , "webapp2-" , 20 );
46+ final String rgName = Utils .randomResourceName (azureResourceManager , "rgNEMV_" , 24 );
47+ final String domainName = Utils .randomResourceName (azureResourceManager , "jsdkdemo-" , 20 ) + ".com" ;
48+ final String certPassword = Utils .password ();
5349
5450 try {
5551 //============================================================
5652 // Create a web app with a new app service plan
5753
5854 System .out .println ("Creating web app " + app1Name + "..." );
5955
60- WebApp app1 = azure .webApps ().define (app1Name )
56+ WebApp app1 = azureResourceManager .webApps ().define (app1Name )
6157 .withRegion (Region .US_WEST )
6258 .withNewResourceGroup (rgName )
6359 .withNewWindowsPlan (PricingTier .STANDARD_S1 )
@@ -70,8 +66,8 @@ public static boolean runSample(Azure azure) {
7066 // Create a second web app with the same app service plan
7167
7268 System .out .println ("Creating another web app " + app2Name + "..." );
73- AppServicePlan plan = azure . appServices () .appServicePlans ().getById (app1 .appServicePlanId ());
74- WebApp app2 = azure .webApps ().define (app2Name )
69+ AppServicePlan plan = azureResourceManager .appServicePlans ().getById (app1 .appServicePlanId ());
70+ WebApp app2 = azureResourceManager .webApps ().define (app2Name )
7571 .withExistingWindowsPlan (plan )
7672 .withExistingResourceGroup (rgName )
7773 .create ();
@@ -84,7 +80,7 @@ public static boolean runSample(Azure azure) {
8480
8581 System .out .println ("Purchasing a domain " + domainName + "..." );
8682
87- AppServiceDomain domain = azure . appServices (). domains ().define (domainName )
83+ AppServiceDomain domain = azureResourceManager . appServiceDomains ().define (domainName )
8884 .withExistingResourceGroup (rgName )
8985 .defineRegistrantContact ()
9086 .withFirstName ("Jon" )
@@ -113,7 +109,7 @@ public static boolean runSample(Azure azure) {
113109 .defineHostnameBinding ()
114110 .withAzureManagedDomain (domain )
115111 .withSubDomain (app1Name )
116- .withDnsRecordType (CustomHostNameDnsRecordType .CNAME )
112+ .withDnsRecordType (CustomHostnameDnsRecordType .CNAME )
117113 .attach ()
118114 .apply ();
119115
@@ -123,8 +119,8 @@ public static boolean runSample(Azure azure) {
123119 //============================================================
124120 // Create a self-singed SSL certificate
125121
126- String pfxPath = ManageWebAppWithDomainSsl .class .getResource ("/" ).getPath () + "webapp_" + ManageWebAppWithDomainSsl . class . getSimpleName (). toLowerCase () + ".pfx" ;
127- String cerPath = ManageWebAppWithDomainSsl .class .getResource ("/" ).getPath () + "webapp_" + ManageWebAppWithDomainSsl . class . getSimpleName (). toLowerCase () + ".cer" ;
122+ String pfxPath = ManageWebAppWithDomainSsl .class .getResource ("/" ).getPath () + "webapp_" + domainName + ".pfx" ;
123+ String cerPath = ManageWebAppWithDomainSsl .class .getResource ("/" ).getPath () + "webapp_" + domainName + ".cer" ;
128124
129125 System .out .println ("Creating a self-signed certificate " + pfxPath + "..." );
130126
@@ -155,7 +151,7 @@ public static boolean runSample(Azure azure) {
155151 .withManagedHostnameBindings (domain , app2Name )
156152 .defineSslBinding ()
157153 .forHostname (app2Name + "." + domainName )
158- .withExistingCertificate (app1 .hostNameSslStates ().get (app1Name + "." + domainName ).thumbprint ())
154+ .withExistingCertificate (app1 .hostnameSslStates ().get (app1Name + "." + domainName ).thumbprint ())
159155 .withSniBasedSsl ()
160156 .attach ()
161157 .apply ();
@@ -164,56 +160,48 @@ public static boolean runSample(Azure azure) {
164160 Utils .print (app2 );
165161
166162 return true ;
167- } catch (Exception e ) {
168- System .err .println (e .getMessage ());
169- e .printStackTrace ();
170163 } finally {
171164 try {
172165 System .out .println ("Deleting Resource Group: " + rgName );
173- azure .resourceGroups ().deleteByName (rgName );
166+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
174167 System .out .println ("Deleted Resource Group: " + rgName );
175168 } catch (NullPointerException npe ) {
176169 System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
177170 } catch (Exception g ) {
178171 g .printStackTrace ();
179172 }
180173 }
181-
182- return false ;
183174 }
184175 /**
185176 * Main entry point.
186177 * @param args the parameters
187178 */
188179 public static void main (String [] args ) {
189180
190-
191181 try {
192182
193183 //=============================================================
194184 // Authenticate
195185
196- final File credFile = new File (System .getenv ("AZURE_AUTH_LOCATION" ));
186+ final AzureProfile profile = new AzureProfile (AzureEnvironment .AZURE );
187+ final TokenCredential credential = new DefaultAzureCredentialBuilder ()
188+ .authorityHost (profile .getEnvironment ().getActiveDirectoryEndpoint ())
189+ .build ();
197190
198- Azure azure = Azure .configure ()
199- .withLogLevel (LogLevel .BODY )
200- .authenticate (credFile )
201- .withDefaultSubscription ();
191+ AzureResourceManager azureResourceManager = AzureResourceManager
192+ .configure ()
193+ .withLogLevel (HttpLogDetailLevel .BASIC )
194+ .authenticate (credential , profile )
195+ .withDefaultSubscription ();
202196
203197 // Print selected subscription
204- System .out .println ("Selected subscription: " + azure .subscriptionId ());
198+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
205199
206- runSample (azure );
200+ runSample (azureResourceManager );
207201
208202 } catch (Exception e ) {
209203 System .out .println (e .getMessage ());
210204 e .printStackTrace ();
211205 }
212206 }
213-
214- static {
215- httpClient = new OkHttpClient .Builder ().readTimeout (1 , TimeUnit .MINUTES ).build ();
216- }
217-
218-
219207}
0 commit comments