Skip to content

Instantly share code, notes, and snippets.

@writeameer
Last active December 2, 2016 02:12
Show Gist options
  • Save writeameer/d7d23f560addb317042cee47fa23aa7d to your computer and use it in GitHub Desktop.
Save writeameer/d7d23f560addb317042cee47fa23aa7d to your computer and use it in GitHub Desktop.
app2.cs
using Microsoft.Azure.Management.ResourceManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Rest;
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
namespace ConsoleApplication1
{
public class AzureStuff
{
public static TokenCredentials credential;
public AzureStuff()
{
credential = Util.GetCredential(Lookup.subscriptionId, Lookup.clientId, Lookup.clientSecret, Lookup.tenantId);
}
public static void DoSomething()
{
var credential = Util.GetCredential(Lookup.subscriptionId, Lookup.clientId, Lookup.clientSecret, Lookup.tenantId);
var armClient = new ResourceManagementClient(credential) { SubscriptionId = Lookup.subscriptionId };
armClient.ResourceGroups.List().ToList().ForEach(x => Console.WriteLine(x.Name));
}
public void AppGateway()
{
var resourceGroup = "blah";
var location = "Australia East";
var netClient = new NetworkManagementClient(credential) { SubscriptionId = Lookup.subscriptionId };
var subscriptionId = Lookup.subscriptionId;
var gwName = "test111";
var frontEndIpConfigName = "appGatewayFrontendIP";
var frontEndPortName = "appGatewayFrontendPort";
var backEndAddressPoolName = "appGatewayBackendPool";
var backendHttpSettingName = "appGatewayBackendHttpSettings";
var httpListenerName = "appGatewayHttpListener";
// Using steps here: https://docs.microsoft.com/en-us/azure/application-gateway/application-gateway-create-gateway-arm#create-a-virtual-network-and-a-subnet-for-the-application-gateway
// Get subnet Id from existing vnets
var devNet = netClient.VirtualNetworks.List("blah")
.Where(x => x.Name.Equals("development")).FirstOrDefault();
var tier1Subnet = devNet.Subnets.Where(sn => sn.Name.Equals("Tier1")).FirstOrDefault();
// Create IP for gateway
var ipParams = new PublicIPAddress{Location = location ,PublicIPAllocationMethod = "Dynamic" };
var newIp = netClient.PublicIPAddresses.CreateOrUpdate(resourceGroup, "appGatewayFrontendIP", ipParams);
// Create AppGateway Config Object, Step 1
var gatewayIpConfig = new ApplicationGatewayIPConfiguration { Name = "appGatewayIpConfig",
Subnet = new SubResource { Id = tier1Subnet.Id },
};
// Create AppGateway Config Object, Step 2
var backEndAddressPoolId = $"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Network/applicationGateways/{gwName}/backendAddressPools/{backEndAddressPoolName}";
var backEndAddressPool = new ApplicationGatewayBackendAddressPool
{
Name = backEndAddressPoolName,
BackendAddresses = new List<ApplicationGatewayBackendAddress> { new ApplicationGatewayBackendAddress { IpAddress = "1.1.1.1" } },
Id = backEndAddressPoolId
};
// Create AppGateway Config Object, Step 3
var backendHttpSettingId = $"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Network/applicationGateways/{gwName}/backendHttpSettingsCollection/{backendHttpSettingName}";
var backendHttpSetting = new ApplicationGatewayBackendHttpSettings {
Id = backendHttpSettingId,
Name = backendHttpSettingName,
Protocol = "http", Port = 80,
CookieBasedAffinity = "Disabled"
};
// Create AppGateway Config Object, Step 4
var frontEndPortId = $"/subscriptions/{Lookup.subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Network/applicationGateways/{gwName}/frontendPorts/{frontEndPortName}";
var frontEndPort = new ApplicationGatewayFrontendPort { Name = "appGatewayFrontendPort", Port = 80, Id = frontEndPortId };
// Create AppGateway Config Object, Step 5
// FrontEnd IP Config
var frontEndIpConfigId = $"/subscriptions/{Lookup.subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Network/applicationGateways/{gwName}/frontendIpConfigurations/{frontEndIpConfigName}";
var frontEndIpConfig = new ApplicationGatewayFrontendIPConfiguration
{
Name = frontEndIpConfigName,
PublicIPAddress = new SubResource { Id = newIp.Id },
Id = frontEndIpConfigId
};
// Create AppGateway Config Object, Step 6
var httpListenerId = $"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.Network/applicationGateways/{gwName}/httpListeners/{httpListenerName}";
var httpListener = new ApplicationGatewayHttpListener
{
Id= httpListenerId,
Name = httpListenerName,
FrontendIPConfiguration = new SubResource { Id = frontEndIpConfig.Id },
FrontendPort = new SubResource { Id = frontEndPort.Id },
Protocol = "http"
};
// Create AppGateway Config Object, Step 7
var requestRoutingRule = new ApplicationGatewayRequestRoutingRule
{
Name = "rule1",
RuleType = "Basic",
HttpListener = httpListener,
BackendAddressPool = backEndAddressPool,
BackendHttpSettings = backendHttpSetting
};
// Create Gateway
var parameters = new ApplicationGateway()
{
Location = location,
Sku = new ApplicationGatewaySku { Name = ApplicationGatewaySkuName.StandardSmall, Capacity = 1, Tier = "Standard" },
FrontendPorts = new List<ApplicationGatewayFrontendPort> { frontEndPort },
FrontendIPConfigurations = new List<ApplicationGatewayFrontendIPConfiguration>() { frontEndIpConfig },
GatewayIPConfigurations = new List<ApplicationGatewayIPConfiguration> { gatewayIpConfig },
BackendAddressPools = new List<ApplicationGatewayBackendAddressPool>() { backEndAddressPool },
BackendHttpSettingsCollection = new List<ApplicationGatewayBackendHttpSettings>() { backendHttpSetting },
HttpListeners = new List<ApplicationGatewayHttpListener> { httpListener },
RequestRoutingRules = new List<ApplicationGatewayRequestRoutingRule> { requestRoutingRule}
};
netClient.ApplicationGateways.CreateOrUpdate(resourceGroup, "test111", parameters);
}
public void GetAppgatewayStatus()
{
var netClient = new NetworkManagementClient(credential) { SubscriptionId = Lookup.subscriptionId };
var myGateway= netClient.ApplicationGateways.Get("blah", "test111");
Console.WriteLine(myGateway.ProvisioningState);
}
}
}
@writeameer
Copy link
Author

Needed to manually generate those magic strings ( For e.g. frontEndPortId, backEndAddressPoolId , backendHttpSettingId etc ) and inject them into the request for the API call to pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment