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 ;
23
22
24
23
import java .io .File ;
25
- import java .util . concurrent . TimeUnit ;
24
+ import java .io . IOException ;
26
25
27
26
/**
28
27
* Azure App Service sample for managing web apps.
36
35
*/
37
36
public final class ManageLinuxWebAppWithDomainSsl {
38
37
39
- private static OkHttpClient httpClient ;
40
-
41
38
/**
42
39
* Main function which runs the actual sample.
43
- * @param azure instance of the azure client
40
+ * @param azureResourceManager instance of the azure client
44
41
* @return true if sample runs successfully
45
42
*/
46
- public static boolean runSample (Azure azure ) {
43
+ public static boolean runSample (AzureResourceManager azureResourceManager ) throws IOException {
47
44
// 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 ();
54
50
55
51
try {
56
52
//============================================================
57
53
// Create a web app with a new app service plan
58
54
59
55
System .out .println ("Creating web app " + app1Name + "..." );
60
56
61
- WebApp app1 = azure .webApps ().define (app1Name )
57
+ WebApp app1 = azureResourceManager .webApps ().define (app1Name )
62
58
.withRegion (Region .US_WEST )
63
59
.withNewResourceGroup (rgName )
64
60
.withNewLinuxPlan (PricingTier .STANDARD_S1 )
65
- .withBuiltInImage (RuntimeStack .NODEJS_6_9 )
61
+ .withBuiltInImage (RuntimeStack .NODEJS_10_LTS )
66
62
.create ();
67
63
68
64
System .out .println ("Created web app " + app1 .name ());
@@ -72,11 +68,11 @@ public static boolean runSample(Azure azure) {
72
68
// Create a second web app with the same app service plan
73
69
74
70
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 )
77
73
.withExistingLinuxPlan (plan )
78
74
.withExistingResourceGroup (rgName )
79
- .withBuiltInImage (RuntimeStack .NODEJS_6_9 )
75
+ .withBuiltInImage (RuntimeStack .NODEJS_10_LTS )
80
76
.create ();
81
77
82
78
System .out .println ("Created web app " + app2 .name ());
@@ -87,7 +83,7 @@ public static boolean runSample(Azure azure) {
87
83
88
84
System .out .println ("Purchasing a domain " + domainName + "..." );
89
85
90
- AppServiceDomain domain = azure . appServices (). domains ().define (domainName )
86
+ AppServiceDomain domain = azureResourceManager . appServiceDomains ().define (domainName )
91
87
.withExistingResourceGroup (rgName )
92
88
.defineRegistrantContact ()
93
89
.withFirstName ("Jon" )
@@ -116,7 +112,7 @@ public static boolean runSample(Azure azure) {
116
112
.defineHostnameBinding ()
117
113
.withAzureManagedDomain (domain )
118
114
.withSubDomain (app1Name )
119
- .withDnsRecordType (CustomHostNameDnsRecordType .CNAME )
115
+ .withDnsRecordType (CustomHostnameDnsRecordType .CNAME )
120
116
.attach ()
121
117
.apply ();
122
118
@@ -126,8 +122,8 @@ public static boolean runSample(Azure azure) {
126
122
//============================================================
127
123
// Create a self-singed SSL certificate
128
124
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" ;
131
127
132
128
System .out .println ("Creating a self-signed certificate " + pfxPath + "..." );
133
129
@@ -158,7 +154,7 @@ public static boolean runSample(Azure azure) {
158
154
.withManagedHostnameBindings (domain , app2Name )
159
155
.defineSslBinding ()
160
156
.forHostname (app2Name + "." + domainName )
161
- .withExistingCertificate (app1 .hostNameSslStates ().get (app1Name + "." + domainName ).thumbprint ())
157
+ .withExistingCertificate (app1 .hostnameSslStates ().get (app1Name + "." + domainName ).thumbprint ())
162
158
.withSniBasedSsl ()
163
159
.attach ()
164
160
.apply ();
@@ -167,22 +163,17 @@ public static boolean runSample(Azure azure) {
167
163
Utils .print (app2 );
168
164
169
165
return true ;
170
- } catch (Exception e ) {
171
- System .err .println (e .getMessage ());
172
- e .printStackTrace ();
173
166
} finally {
174
167
try {
175
168
System .out .println ("Deleting Resource Group: " + rgName );
176
- azure .resourceGroups ().deleteByName (rgName );
169
+ azureResourceManager .resourceGroups ().beginDeleteByName (rgName );
177
170
System .out .println ("Deleted Resource Group: " + rgName );
178
171
} catch (NullPointerException npe ) {
179
172
System .out .println ("Did not create any resources in Azure. No clean up is necessary" );
180
173
} catch (Exception g ) {
181
174
g .printStackTrace ();
182
175
}
183
176
}
184
-
185
- return false ;
186
177
}
187
178
/**
188
179
* Main entry point.
@@ -196,27 +187,25 @@ public static void main(String[] args) {
196
187
//=============================================================
197
188
// Authenticate
198
189
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 ();
200
194
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 ();
205
200
206
201
// Print selected subscription
207
- System .out .println ("Selected subscription: " + azure .subscriptionId ());
202
+ System .out .println ("Selected subscription: " + azureResourceManager .subscriptionId ());
208
203
209
- runSample (azure );
204
+ runSample (azureResourceManager );
210
205
211
206
} catch (Exception e ) {
212
207
System .out .println (e .getMessage ());
213
208
e .printStackTrace ();
214
209
}
215
210
}
216
-
217
- static {
218
- httpClient = new OkHttpClient .Builder ().readTimeout (1 , TimeUnit .MINUTES ).build ();
219
- }
220
-
221
-
222
211
}
0 commit comments