Skip to content

Instantly share code, notes, and snippets.

@mouadcherkaoui
Last active August 6, 2020 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mouadcherkaoui/4d0942595210ed7f4ee42af638ddbb6c to your computer and use it in GitHub Desktop.
Save mouadcherkaoui/4d0942595210ed7f4ee42af638ddbb6c to your computer and use it in GitHub Desktop.
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