«

»

Jun 24

Using Pulumi to Create Azure Route Table

I have a class with constants to keep a tight reign on magic strings.

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
public class Constants
{
        public const string SKUSTANDARD = "Standard";
        public const string SKUBASIC = "Basic";
        public const string IPVERSION4 = "IPv4";
        public const string STATIC = "Static";
        public const string DYNAMIC = "Dynamic";
 
        public const string ALL = "*";
        public const string TCP = "TCP";
        public const string UDP = "UDP";
        public const string ALLOW = "Allow";
 
        public const string VIRTUALAPPLIANCE = "VirtualAppliance";
        public const string VIRTUALNETWORKGATEWAY = "VirtualNetworkGateway";
 
        public const string PREMIUM_LRS = "Premium_LRS";
        public const string LRS = "LRS";
 
        public const string STORAGEACCOUNT_BLOB = "BlobStorage";
        public const string STORAGEACCOUNT_BLOCKBLOB = "BlockBlobStorage";
        public const string STORAGEACCOUNT_FILE = "FileStorage";
        public const string STORAGEACCOUNT_STORAGE = "Storage";
        public const string STORAGEACCOUNT_STORAGEV2 = "StorageV2";
        public const string STORAGEACCOUNT_DEFAULT = "StorageV2";
}

This code builds a few routes, then a route table. After the route table is made, it adds associations to subnets.

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
class BuildRouteTables
{
    private readonly string _location;
    private readonly ResourceGroup _resourceGroup;
    private readonly string _dmzSubnetAddress;
    private readonly string _webSubnetAddress;
    private readonly string _dataSubnetAddress;
 
    public Dictionary<string, Subnet> Subnets { get; private set; }
 
    public BuildRouteTables(string location, ResourceGroup resourceGroup)
    {
        _location = location;
        _resourceGroup = resourceGroup;
        Subnets = new Dictionary<string, subnet="">();
 
        var octets = vnetAddressSpace.Split('.');
        const string subnetSize = "24";
        var firstTwoOctets = octets[0] + "." + octets[1];
        _dmzSubnetAddress = firstTwoOctets + ".0.0/" + subnetSize;
        _webSubnetAddress = firstTwoOctets + ".1.0/" + subnetSize;
        _dataSubnetAddress = firstTwoOctets + ".2.0/" + subnetSize;
    }
 
    public void BuildRouteTable(string routeTableName, string firewallIpAddress)
    {
		var dmzRoute = new RouteTableRouteArgs()
		{
			Name = "DMZ",
			AddressPrefix = _dmzSubnetAddress,
			NextHopInIpAddress = firewallIpAddress,
			NextHopType = Constants.VIRTUALAPPLIANCE
		};
 
		var webRoute = new RouteTableRouteArgs()
		{
			Name = "Web",
			AddressPrefix = _webSubnetAddress,
			NextHopInIpAddress = firewallIpAddress,
			NextHopType = Constants.VIRTUALAPPLIANCE
		};
 
		var dataRoute = new RouteTableRouteArgs()
		{
			Name = "Data",
			AddressPrefix = _dataSubnetAddress,
			NextHopInIpAddress = firewallIpAddress,
			NextHopType = Constants.VIRTUALAPPLIANCE
		};
 
		var routes = new InputList{ dmzRoute, webRoute, dataRoute };
 
		var routeTable = new RouteTable(routeTableName, new RouteTableArgs()
		{
			Location = _location,
			ResourceGroupName = _resourceGroup.Name,
			Routes = routes
		});
 
		var dmzAssociation = new SubnetRouteTableAssociation("DmzAssoc", new SubnetRouteTableAssociationArgs()
		{
			RouteTableId = routeTable.Id,
			SubnetId = Subnets["DMZ"].Id
		});
 
		var webAssociation = new SubnetRouteTableAssociation("WebAssoc", new SubnetRouteTableAssociationArgs()
		{
			RouteTableId = routeTable.Id,
			SubnetId = Subnets["Web"].Id
		});
 
		var dataAssociation = new SubnetRouteTableAssociation("DataAssoc", new SubnetRouteTableAssociationArgs()
		{
			RouteTableId = routeTable.Id,
			SubnetId = Subnets["Data"].Id
		});
    }
}