-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
264 lines (234 loc) · 12.5 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.Azure.Management.AppService.Fluent;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core.ResourceActions;
using Microsoft.Azure.Management.Samples.Common;
using Microsoft.Azure.Management.TrafficManager.Fluent;
using Microsoft.Azure.Management.TrafficManager.Fluent.TrafficManagerProfile.Definition;
using Microsoft.Rest.Azure;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace ManageTrafficManager
{
public class Program
{
private static readonly string certPassword = Utilities.CreatePassword();
private static readonly List<Region> regions = new List<Region>();
/**
* Azure traffic manager sample for managing profiles.
* - Create a domain
* - Create a self-signed certificate for the domain
* - Create 5 app service plans in 5 different regions
* - Create 5 web apps under the each plan, bound to the domain and the certificate
* - Create a traffic manager in front of the web apps
* - Disable an endpoint
* - Delete an endpoint
* - Enable an endpoint
* - Change/configure traffic manager routing method
* - Disable traffic manager profile
* - Enable traffic manager profile
*/
public static void RunSample(IAzure azure)
{
string rgName = SdkContext.RandomResourceName("rgNEMV_", 24);
string domainName = SdkContext.RandomResourceName("jsdkdemo-", 20) + ".com";
string appServicePlanNamePrefix = SdkContext.RandomResourceName("jplan1_", 15);
string webAppNamePrefix = SdkContext.RandomResourceName("webapp1-", 20) + "-";
string tmName = SdkContext.RandomResourceName("jsdktm-", 20);
// The regions in which web app needs to be created
//
regions.Add(Region.USWest2);
regions.Add(Region.USEast2);
regions.Add(Region.AsiaEast);
regions.Add(Region.IndiaWest);
regions.Add(Region.USCentral);
try
{
azure.ResourceGroups.Define(rgName)
.WithRegion(Region.USWest)
.Create();
// ============================================================
// Purchase a domain (will be canceled for a full refund)
Utilities.Log("Purchasing a domain " + domainName + "...");
var domain = azure.AppServices.AppServiceDomains.Define(domainName)
.WithExistingResourceGroup(rgName)
.DefineRegistrantContact()
.WithFirstName("Jon")
.WithLastName("Doe")
.WithEmail("[email protected]")
.WithAddressLine1("123 4th Ave")
.WithCity("Redmond")
.WithStateOrProvince("WA")
.WithCountry(CountryISOCode.UnitedStates)
.WithPostalCode("98052")
.WithPhoneCountryCode(CountryPhoneCode.UnitedStates)
.WithPhoneNumber("4258828080")
.Attach()
.WithDomainPrivacyEnabled(true)
.WithAutoRenewEnabled(false)
.Create();
Utilities.Log("Purchased domain " + domain.Name);
Utilities.Print(domain);
//============================================================
// Create a self-singed SSL certificate
var pfxPath = "webapp_" + nameof(ManageTrafficManager).ToLower() + ".pfx";
Utilities.Log("Creating a self-signed certificate " + pfxPath + "...");
Utilities.CreateCertificate(domainName, pfxPath, certPassword);
Utilities.Log("Created self-signed certificate " + pfxPath);
//============================================================
// Creates app service in 5 different region
var appServicePlans = new List<IAppServicePlan>();
int id = 0;
foreach (var region in regions)
{
var planName = appServicePlanNamePrefix + id;
Utilities.Log("Creating an app service plan " + planName + " in region " + region + "...");
var appServicePlan = azure.AppServices.AppServicePlans
.Define(planName)
.WithRegion(region)
.WithExistingResourceGroup(rgName)
.WithPricingTier(PricingTier.BasicB1)
.WithOperatingSystem(Microsoft.Azure.Management.AppService.Fluent.OperatingSystem.Windows)
.Create();
Utilities.Log("Created app service plan " + planName);
Utilities.Print(appServicePlan);
appServicePlans.Add(appServicePlan);
id++;
}
//============================================================
// Creates websites using previously created plan
var webApps = new List<IWebApp>();
id = 0;
foreach (var appServicePlan in appServicePlans)
{
var webAppName = webAppNamePrefix + id;
Utilities.Log("Creating a web app " + webAppName + " using the plan " + appServicePlan.Name + "...");
var webApp = azure.WebApps.Define(webAppName)
.WithExistingWindowsPlan(appServicePlan)
.WithExistingResourceGroup(rgName)
.WithManagedHostnameBindings(domain, webAppName)
.DefineSslBinding()
.ForHostname(webAppName + "." + domain.Name)
.WithPfxCertificateToUpload(Path.Combine(Utilities.ProjectPath, "Asset", pfxPath), certPassword)
.WithSniBasedSsl()
.Attach()
.DefineSourceControl()
.WithPublicGitRepository("https://github.com/jianghaolu/azure-site-test")
.WithBranch("master")
.Attach()
.Create();
Utilities.Log("Created web app " + webAppName);
Utilities.Print(webApp);
webApps.Add(webApp);
id++;
}
//============================================================
// Creates a traffic manager profile
Utilities.Log("Creating a traffic manager profile " + tmName + " for the web apps...");
IWithEndpoint tmDefinition = azure.TrafficManagerProfiles
.Define(tmName)
.WithExistingResourceGroup(rgName)
.WithLeafDomainLabel(tmName)
.WithPriorityBasedRouting();
ICreatable<ITrafficManagerProfile> tmCreatable = null;
int priority = 1;
foreach (var webApp in webApps)
{
tmCreatable = tmDefinition.DefineAzureTargetEndpoint("endpoint-" + priority)
.ToResourceId(webApp.Id)
.WithRoutingPriority(priority)
.Attach();
priority++;
}
var trafficManagerProfile = tmCreatable.Create();
Utilities.Log("Created traffic manager " + trafficManagerProfile.Name);
Utilities.Print(trafficManagerProfile);
//============================================================
// Disables one endpoint and removes another endpoint
Utilities.Log("Disabling and removing endpoint...");
trafficManagerProfile = trafficManagerProfile.Update()
.UpdateAzureTargetEndpoint("endpoint-1")
.WithTrafficDisabled()
.Parent()
.WithoutEndpoint("endpoint-2")
.Apply();
Utilities.Log("Endpoints updated");
//============================================================
// Enables an endpoint
Utilities.Log("Enabling endpoint...");
trafficManagerProfile = trafficManagerProfile.Update()
.UpdateAzureTargetEndpoint("endpoint-1")
.WithTrafficEnabled()
.Parent()
.Apply();
Utilities.Log("Endpoint updated");
Utilities.Print(trafficManagerProfile);
//============================================================
// Change/configure traffic manager routing method
Utilities.Log("Changing traffic manager profile routing method...");
trafficManagerProfile = trafficManagerProfile.Update()
.WithPerformanceBasedRouting()
.Apply();
Utilities.Log("Changed traffic manager profile routing method");
//============================================================
// Disables the traffic manager profile
Utilities.Log("Disabling traffic manager profile...");
trafficManagerProfile.Update()
.WithProfileStatusDisabled()
.Apply();
Utilities.Log("Traffic manager profile disabled");
//============================================================
// Enables the traffic manager profile
Utilities.Log("Enabling traffic manager profile...");
trafficManagerProfile.Update()
.WithProfileStatusDisabled()
.Apply();
Utilities.Log("Traffic manager profile enabled");
//============================================================
// Deletes the traffic manager profile
Utilities.Log("Deleting the traffic manger profile...");
azure.TrafficManagerProfiles.DeleteById(trafficManagerProfile.Id);
Utilities.Log("Traffic manager profile deleted");
}
finally
{
try
{
Utilities.Log("Deleting Resource Group: " + rgName);
azure.ResourceGroups.BeginDeleteByName(rgName);
Utilities.Log("Deleted Resource Group: " + rgName);
}
catch
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
}
}
public static void Main(string[] args)
{
try
{
//=================================================================
// Authenticate
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
// Print selected subscription
Utilities.Log("Selected subscription: " + azure.SubscriptionId);
RunSample(azure);
}
catch (Exception e)
{
Utilities.Log(e);
}
}
}
}