Last active
August 6, 2020 09:54
-
-
Save mouadcherkaoui/4d0942595210ed7f4ee42af638ddbb6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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