Example #1
0
        public static string SetASG(string resourceId)
        {
            JObject networkInterface = GetNetworkInterface(resourceId);
            JArray  ipConfigurations;
            string  result = "";
            string  uri;

            if (networkInterface.ContainsKey("tags"))
            {
                if (networkInterface.Property("tags").Value.ToObject <JObject>().ContainsKey("ASG"))
                {
                    string ASGArray = "[{\"id\": \"" + ApplicationSecurityGroups.GetASGId(networkInterface.Property("tags").Value.ToObject <JObject>().Property("ASG").Value.ToString()) + "\"}]";
                    ipConfigurations = networkInterface["properties"].Value <JArray>("ipConfigurations");
                    JProperty asgArray    = new JProperty("applicationSecurityGroups", JArray.Parse(ASGArray));
                    string    thisTest    = networkInterface["properties"]["ipConfigurations"][0]["properties"].Last.ToString();
                    bool      appSecurity = false;
                    try{
                        appSecurity = networkInterface["properties"]["ipConfigurations"][0]["properties"]["applicationSecurityGroups"].HasValues;
                    }
                    catch (Exception e)
                    {
                    }
                    if (appSecurity)
                    {
                        networkInterface["properties"]["ipConfigurations"][0]["properties"]["applicationSecurityGroups"] = JArray.Parse(ASGArray);
                    }
                    else
                    {
                        networkInterface["properties"]["ipConfigurations"][0]["properties"].Last.AddAfterSelf(asgArray);
                    }
                    uri    = resourceId + "?api-version=" + ASG_Management.GetEnvironmentVariable("apiVersion").Split(": ")[1];
                    result = WebCalls.Put(uri, networkInterface.ToString());
                    return("SUCCESS");
                }
                else
                {
                    return("No ASG Tag");
                }
            }
            else
            {
                return("No ASG Tag");
            }
        }
Example #2
0
        private static JObject GetNetworkInterface(string resourceId)
        {
            string  requestUri = resourceId + "?api-version=" + ASG_Management.GetEnvironmentVariable("apiVersion").Split(": ")[1];
            JObject networkInterface;

            try
            {
                networkInterface = JObject.Parse(WebCalls.Get(requestUri));
                if (networkInterface.ContainsKey("error"))
                {
                    networkInterface = null;
                }
            }
            catch (Exception e)
            {
                networkInterface = null;
            }
            return(networkInterface);
        }
Example #3
0
        public static string SetASGFromVM(string vmResourceId)
        {
            bool    success      = true;
            string  vmApiVersion = "2019-07-01";
            string  uri          = vmResourceId + "?api-version=" + vmApiVersion;
            JObject vm           = new JObject();

            try {
                vm = JObject.Parse(WebCalls.Get(uri));
            }
            catch (Exception e) {
                success = false;
            }
            if (success)
            {
                if (vm.ContainsKey("tags"))
                {
                    if (vm.Property("tags").Value.ToObject <JObject>().ContainsKey("ASG"))
                    {
                        string    ASGArray         = "[{\"id\": \"" + ApplicationSecurityGroups.GetASGId(vm["tags"]["ASG"].ToString()) + "\"}]";
                        JProperty asgArray         = new JProperty("applicationSecurityGroups", JArray.Parse(ASGArray));
                        JObject   networkInterface = new JObject();
                        try{
                            networkInterface = GetNetworkInterface(vm["properties"]["networkProfile"]["networkInterfaces"][0]["id"].ToString());
                        }
                        catch (Exception e) {
                            return("Failed to find Network Interface");
                        }
                        bool appSecurity = false;
                        try
                        {
                            appSecurity = networkInterface["properties"]["ipConfigurations"][0]["properties"]["applicationSecurityGroups"].HasValues;
                        }
                        catch (Exception e)
                        {
                        }
                        if (appSecurity)
                        {
                            networkInterface["properties"]["ipConfigurations"][0]["properties"]["applicationSecurityGroups"] = JArray.Parse(ASGArray);
                        }
                        else
                        {
                            networkInterface["properties"]["ipConfigurations"][0]["properties"].Last.AddAfterSelf(asgArray);
                        }
                        uri = networkInterface["id"].ToString() + "?api-version=" + ASG_Management.GetEnvironmentVariable("apiVersion").Split(": ")[1];
                        try{
                            WebCalls.Put(uri, networkInterface.ToString());
                            return("SUCCESS");
                        }
                        catch (Exception e)
                        {
                            return("Failed to set Network Interface");
                        }
                    }
                    else
                    {
                        return("No ASG Tag");
                    }
                }
                else
                {
                    return("No ASG Tag");
                }
            }
            else
            {
                return("Failed to find VM");
            }
        }