-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
168 lines (150 loc) · 7.1 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
// 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.Fluent;
using Microsoft.Azure.Management.Network.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Samples.Common;
using System;
using System.Diagnostics;
using System.IO;
namespace ManageSimpleApplicationGateway
{
public class Program
{
/**
* Azure network sample for managing application gateways.
*
* - CREATE an application gateway for load balancing
* HTTP/HTTPS requests to backend server pools of virtual machines
*
* This application gateway serves traffic for multiple
* domain names
*
* Routing Rule 1
* Hostname 1 = None
* Backend server pool 1 = 4 virtual machines with IP addresses
* Backend server pool 1 settings = HTTP:8080
* Front end port 1 = HTTP:80
* Listener 1 = HTTP
* Routing rule 1 = HTTP listener 1 => backend server pool 1
* (round-robin load distribution)
*
* - MODIFY the application gateway - re-configure the Routing Rule 1 for SSL offload and
* add a host name, www.contoso.com
*
* Change listener 1 from HTTP to HTTPS
* Add SSL certificate to the listener
* Update front end port 1 to HTTPS:1443
* Add a host name, www.contoso.com
* Enable cookie-based affinity
*
* Modified Routing Rule 1
* Hostname 1 = www.contoso.com
* Backend server pool 1 = 4 virtual machines with IP addresses
* Backend server pool 1 settings = HTTP:8080
* Front end port 1 = HTTPS:1443
* Listener 1 = HTTPS
* Routing rule 1 = HTTPS listener 1 => backend server pool 1
* (round-robin load distribution)
*
*/
public static void RunSample(IAzure azure)
{
string rgName = SdkContext.RandomResourceName("rgNEAGS", 15);
string pipName = SdkContext.RandomResourceName("pip" + "-", 18);
try
{
//=======================================================================
// Create an application gateway
Utilities.Log("================= CREATE ======================");
Utilities.Log("Creating an application gateway... (this can take about 20 min)");
Stopwatch t = Stopwatch.StartNew();
IApplicationGateway applicationGateway = azure.ApplicationGateways.Define("myFirstAppGateway")
.WithRegion(Region.USEast)
.WithNewResourceGroup(rgName)
// Request routing rule for HTTP from public 80 to public 8080
.DefineRequestRoutingRule("HTTP-80-to-8080")
.FromPublicFrontend()
.FromFrontendHttpPort(80)
.ToBackendHttpPort(8080)
.ToBackendIPAddress("11.1.1.1")
.ToBackendIPAddress("11.1.1.2")
.ToBackendIPAddress("11.1.1.3")
.ToBackendIPAddress("11.1.1.4")
.Attach()
.WithNewPublicIPAddress()
.Create();
t.Stop();
Utilities.Log("Application gateway created: (took " + (t.ElapsedMilliseconds / 1000) + " seconds)");
Utilities.PrintAppGateway(applicationGateway);
//=======================================================================
// Update an application gateway
// configure the first routing rule for SSL offload
Utilities.Log("================= UPDATE ======================");
Utilities.Log("Updating the application gateway");
t = Stopwatch.StartNew();
applicationGateway.Update()
.WithoutRequestRoutingRule("HTTP-80-to-8080")
.DefineRequestRoutingRule("HTTPs-1443-to-8080")
.FromPublicFrontend()
.FromFrontendHttpsPort(1443)
.WithSslCertificateFromPfxFile(
new FileInfo(
Utilities.GetCertificatePath("NetworkTestCertificate1.pfx")))
.WithSslCertificatePassword("Abc123")
.ToBackendHttpPort(8080)
.ToBackendIPAddress("11.1.1.1")
.ToBackendIPAddress("11.1.1.2")
.ToBackendIPAddress("11.1.1.3")
.ToBackendIPAddress("11.1.1.4")
.WithHostName("www.contoso.com")
.WithCookieBasedAffinity()
.Attach()
.Apply();
t.Stop();
Utilities.Log("Application gateway updated: (took " + (t.ElapsedMilliseconds / 1000) + " seconds)");
Utilities.PrintAppGateway(applicationGateway);
}
finally
{
try
{
Utilities.Log("Deleting Resource Group: " + rgName);
azure.ResourceGroups.DeleteByName(rgName);
Utilities.Log("Deleted Resource Group: " + rgName);
}
catch (NullReferenceException)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
catch (Exception e)
{
Utilities.Log(e.Message);
Utilities.Log(e.StackTrace);
}
}
}
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.Message);
Utilities.Log(e.StackTrace);
}
}
}
}