This repository was archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
142 lines (131 loc) · 6.62 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.Network.Fluent;
using Microsoft.Azure.Management.Network.Fluent.Models;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Samples.Common;
namespace ManageExpressRoute
{
public class Program
{
private static readonly Region region = Region.USWestCentral;
/**
* Azure Network sample for managing express route circuits.
* - Create Express Route circuit
* - Create Express Route circuit peering. Please note: express route circuit should be provisioned by connectivity provider before this step.
* - Adding authorization to express route circuit
* - Create virtual network to be associated with virtual network gateway
* - Create virtual network gateway
* - Create virtual network gateway connection
*/
public static void RunSample(IAzure azure)
{
string rgName = SdkContext.RandomResourceName("rg", 20);
string ercName = SdkContext.RandomResourceName("erc", 20);
string vnetName = SdkContext.RandomResourceName("vnet", 20);
string gatewayName = SdkContext.RandomResourceName("gtw", 20);
string connectionName = SdkContext.RandomResourceName("con", 20);
try
{
//============================================================
// create Express Route Circuit
Utilities.Log("Creating express route circuit...");
IExpressRouteCircuit erc = azure.ExpressRouteCircuits.Define(ercName)
.WithRegion(region)
.WithNewResourceGroup(rgName)
.WithServiceProvider("Equinix")
.WithPeeringLocation("Silicon Valley")
.WithBandwidthInMbps(50)
.WithSku(ExpressRouteCircuitSkuType.PremiumMeteredData)
.Create();
Utilities.Log("Created express route circuit");
//============================================================
// Create Express Route circuit peering. Please note: express route circuit should be provisioned by connectivity provider before this step.
Utilities.Log("Creating express route circuit peering...");
erc.Peerings.DefineAzurePrivatePeering()
.WithPrimaryPeerAddressPrefix("123.0.0.0/30")
.WithSecondaryPeerAddressPrefix("123.0.0.4/30")
.WithVlanId(200)
.WithPeerAsn(100)
.Create();
Utilities.Log("Created express route circuit peering");
//============================================================
// Adding authorization to express route circuit
erc.Update()
.WithAuthorization("myAuthorization")
.Apply();
//============================================================
// Create virtual network to be associated with virtual network gateway
Utilities.Log("Creating virtual network...");
INetwork network = azure.Networks.Define(vnetName)
.WithRegion(region)
.WithExistingResourceGroup(rgName)
.WithAddressSpace("192.168.0.0/16")
.WithSubnet("GatewaySubnet", "192.168.200.0/26")
.WithSubnet("FrontEnd", "192.168.1.0/24")
.Create();
//============================================================
// Create virtual network gateway
Utilities.Log("Creating virtual network gateway...");
IVirtualNetworkGateway vngw1 = azure.VirtualNetworkGateways.Define(gatewayName)
.WithRegion(region)
.WithNewResourceGroup(rgName)
.WithExistingNetwork(network)
.WithExpressRoute()
.WithSku(VirtualNetworkGatewaySkuName.Standard)
.Create();
Utilities.Log("Created virtual network gateway");
//============================================================
// Create virtual network gateway connection
Utilities.Log("Creating virtual network gateway connection...");
vngw1.Connections.Define(connectionName)
.WithExpressRoute(erc)
// Note: authorization key is required only in case express route circuit and virtual network gateway are in different subscriptions
// .WithAuthorization(erc.Inner.Authorizations.First().AuthorizationKey)
.Create();
Utilities.Log("Created virtual network gateway connection");
}
finally
{
try
{
Utilities.Log("Deleting Resource Group: " + rgName);
azure.ResourceGroups.BeginDeleteByName(rgName);
}
catch (NullReferenceException)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
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 ex)
{
Utilities.Log(ex);
}
}
}
}