Example #1
0
        public static JArray GetRouteTables(string subscriptionId)
        {
            string requestUri = "/subscriptions/" + subscriptionId + "/providers/Microsoft.Network/routeTables?api-version=2019-12-01";
            JArray routeTables = new JArray();
            try
            {

                JObject result = JObject.Parse(WebCalls.Get(requestUri));
                if (result.Property("@odata.nextLink") != null)
                {
                    routeTables.Add(JArray.Parse(result.Property("value").Value.ToString()));
                    while (result.Property("@odata.nextLink") != null)
                    {
                        result = JObject.Parse(WebCalls.Get(result.Property("@odata.nextLink").Value.ToString()));
                        routeTables.Merge(JArray.Parse(result.Property("value").Value.ToString()));
                    }
                }
                else
                {
                    routeTables = JArray.Parse(result.Property("value").Value.ToString());
                }
            }
            catch (Exception e)
            {

            }
            return routeTables;
        }
Example #2
0
 public static void Run([TimerTrigger("0 0 * * * *")]TimerInfo myTimer, ILogger log)
 {
     string apiVersion = GetEnvironmentVariable("apiVersion").Split(": ")[1];
     string subscriptionId = GetEnvironmentVariable("subscriptionId").Split(": ")[1];
     string location = GetEnvironmentVariable("location").Split(": ")[1];
     log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
     List<JObject> serviceTags;
     string requestUri = "/subscriptions/" + subscriptionId + "/providers/Microsoft.Network/locations/" + location + "/serviceTags?api-version=" + apiVersion;
     try {
         serviceTags = JObject.Parse(WebCalls.Get(requestUri)).Property("values").Value.ToObject<List<JObject>>();
     }
     catch (Exception e){
         serviceTags = null;
         Console.WriteLine("Failed to get ServiceTags: " + e.Message);
     }
     if(serviceTags != null)
     {   string Tag = GetEnvironmentVariable("rt-tag")
         JArray routeTables = RouteTables.GetRouteTables(subscriptionId);
         foreach (JToken token in routeTables)
         {
             JObject RouteTable = JObject.Parse(token.ToString());
             JObject RtProperties = JObject.Parse(RouteTable.Property("properties").Value.ToString());
             List<JObject> routes = RtProperties.Property("routes").Value.ToObject<List<JObject>>();
             if (RouteTable.property("name") == GetEnvironmentVariable("rt-name"))
             {
                 string[] requiredServiceTags = GetEnvironmentVariable("rt-tag").Split(",");
                 JObject tagRoutesProperties;
                 foreach (string Tag in requiredServiceTags)
                 {
                     try
                     {
                         tagRoutesProperties = JObject.Parse(serviceTags.Find(x => x.Property("name").Value.ToString() == Tag).Property("properties").Value.ToString());
                     }
                     catch
                     {
                         tagRoutesProperties = null;
                     }
                     if (tagRoutesProperties != null)
                     {
                         int count = 0;
                         string routeName = "AutoRoute-" + Tag + "-" + tagRoutesProperties.Property("changeNumber").Value.ToString() + "-" + count.ToString();
                         routes.RemoveAll(x => x.Property("name").Value.ToString().Contains(Tag + "-"));
                         foreach (string prefix in tagRoutesProperties.Property("addressPrefixes").Value.ToObject<List<string>>())
                         {
                             routes.Add(JObject.Parse("{\"name\": \"" + routeName + "\",\"properties\": {\"addressPrefix\": \"" + prefix + "\",\"nextHopType\": \"Internet\"}}"));
                             count++;
                             routeName = "AutoRoute-" + Tag + "-" + tagRoutesProperties.Property("changeNumber").Value.ToString() + "-" + count.ToString();
                         }
                     }
                     else
                     {
                         //No Service Tag Found
                     }
              }
             else
             {
                 //No Service Tag Found
             }
             string routeTxt = JsonConvert.SerializeObject(routes);
             RtProperties.Remove("routes");
             RtProperties.Add("routes", JArray.Parse(routeTxt));
             RouteTable.Remove("properties");
             RouteTable.Add("properties", RtProperties);
             string RtUpdateUri = RouteTable.Property("id").Value.ToString() + "?api-version=" + apiVersion;
             string testResult = WebCalls.Put(RtUpdateUri, RouteTable.ToString());
         }
     }            
 }