Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Fluent;
namespace AzureManagmentExample
{
public class NsgService
{
Microsoft.Azure.Management.Network.Fluent.NetworkManager networkManager;
IAzure azure;
Dictionary<string, object> nsgsProperties;
public NsgService()
{
azure = Azure.Authenticate("{your authentication file}").WithDefaultSubscription();
}
public string GetNetworkId(string networkName) => azure.Networks.List().FirstOrDefault(n => n.Name == networkName)?.Id;
public Dictionary<string, object> GetSubnetsNSGs(string networkId)
{
nsgsProperties = new Dictionary<string, object>();
foreach (var subnet in azure.Networks.GetById(networkId).Subnets)
{
var nsg = subnet.Value.GetNetworkSecurityGroup();
foreach (var currentRule in nsg.SecurityRules)
{
var dict = new Dictionary<string, object>();
foreach (var property in typeof(INetworkSecurityRule).GetProperties())
{
var value = property.GetValue(currentRule.Value);
dict.Add(property.Name, value);
}
nsgsProperties.Add(nsg.Key, dict);
}
}
return nsgsProperties;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment