Esempio n. 1
0
        private static void EdgeGeneralTests(WebClient webClient)
        {
            // Test the Authenticate method.
            AuthenticateResponse authenticateResponse = webClient.Authenticate(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]);

            EdgeOS.API.Types.Configuration.Configuration exampleConfiguration = new EdgeOS.API.Types.Configuration.Configuration()
            {
                Firewall = new Firewall()
                {
                    Group = new FirewallGroup()
                    {
                        AddressGroup = new Dictionary <string, AddressGroupEntry>()
                        {
                            {
                                "APITestAddresses", new AddressGroupEntry()
                                {
                                    Address = new[] { "4.3.2.1" }
                                }
                            }
                        }
                    }
                }
            };

            // Add an IP to a FirewallAddressGroup (creating it if it doesn't already exist).
            ConfigurationSettingsBatchResponse configurationSettingsBatchResponse = webClient.ConfigurationSettingsBatch(new ConfigurationSettingsBatchRequest
            {
                // Add an IP address group.
                Set = exampleConfiguration,

                // Cut down on the amount of bytes being returned by selecting a hopefully empty node.
                Get = new EdgeOS.API.Types.Configuration.Configuration()
                {
                    CustomAttribute = new[] { "" }
                }
            });

            // Delete APITestAddresses.
            exampleConfiguration.Firewall.Group.AddressGroup["APITestAddresses"] = null;

            // Remove the FirewallAddressGroup.
            ConfigurationSettingsDeleteResponse configurationSettingsDeleteResponse = webClient.ConfigurationSettingsDelete(exampleConfiguration);

            // Get predefined config list.
            ConfigurationSettingsGetResponse configurationSettingsGetPredefinedListResponse = webClient.ConfigurationSettingsGetPredefinedList();

            // Get sections of partial config.
            ConfigurationSettingsGetResponse configurationSettingsGetSectionsResponse = webClient.ConfigurationSettingsGetSections("{\"firewall\":null, \"protocols\":null}");

            // Get tree config.
            ConfigurationSettingsGetTreeResponse configurationSettingsGetTreeResponse = webClient.ConfigurationSettingsGetTree(new[] { "system", "ntp" });

            configurationSettingsGetTreeResponse = webClient.ConfigurationSettingsGetTree(new[] { "system" });
            configurationSettingsGetTreeResponse = webClient.ConfigurationSettingsGetTree(new[] { "firewall", "group", "address-group" });

            // Set a FirewallAddressGroup.
            ConfigurationSettingsSetResponse configurationSettingsSetResponse = webClient.ConfigurationSettingsSet(exampleConfiguration);

            // Data methods.
            DataDefaultConfigurationStatusResponse dataDefaultConfigurationStatusResponse = webClient.DataDefaultConfigurationStatus();
            DataDHCPLeasesResponse         dataDHCPLeasesResponse         = webClient.DataDHCPLeases();
            DataDHCPStatisticsResponse     dataDHCPStatisticsResponse     = webClient.DataDHCPStatistics();
            DataFirewallStatisticsResponse dataFirewallStatisticsResponse = webClient.DataFirewallStatistics();
            DataNATStatisticsResponse      dataNATStatisticsResponse      = webClient.DataNATStatistics();
            DataRoutesResponse             dataRoutesResponse             = webClient.DataRoutes();
            DataSystemInformationResponse  dataSystemInformationResponse  = webClient.DataSystemInformation();

            // Heartbeat.
            webClient.Heartbeat();

            // Firmware update.
            UpgradeResponse upgradeFirmware = webClient.UpgradeFirmware("EdgeOS.API.dll");

            upgradeFirmware = webClient.UpgradeFirmware(new Uri("http://localhost/firmware.bin"));

            //TODO: Wizard Feature.
            //TODO: Wizard Setup.
        }
Esempio n. 2
0
        private static void CreateClass(WebClient webClient, string[] startKey)
        {
            // Get the tree.
            ConfigurationSettingsGetTreeResponse configurationSettingsGetTreeResponse = webClient.ConfigurationSettingsGetTree(startKey);

            // We remember further sections or tags for later rather than process immediately because we want to finish writing the current class before moving onto them.
            List <string[]> furtherSectionsOrTags = new List <string[]>();

            // There may not be any further definitions.
            if (configurationSettingsGetTreeResponse.Get.Definitions != null)
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendLine("using Newtonsoft.Json;");

                foreach (KeyValuePair <string, ConfigurationSettingsGetTreeResponse.GetConfig.Definition> definition in configurationSettingsGetTreeResponse.Get.Definitions)
                {
                    if (definition.Value.Tag)
                    {
                        stringBuilder.AppendLine("using System.Collections.Generic;");
                        break;
                    }
                }

                stringBuilder.AppendLine();
                stringBuilder.AppendLine("namespace EdgeOS.API.Types.Configuration");
                stringBuilder.AppendLine("{");
                stringBuilder.AppendLine("    /// <summary>A class representing an EdgeOS " + GetNamingConventionString(startKey != null ? string.Join(" ", startKey) + " " : "") + "configuration tree.</summary>");
                stringBuilder.AppendLine("    public class " + GetClassNameFromStartKey(startKey));
                stringBuilder.AppendLine("    {");

                // To work out when to add new lines in the class once a previous item has been added.
                bool alreadyAddedItem = false;

                // Take each of the definitions.
                foreach (KeyValuePair <string, ConfigurationSettingsGetTreeResponse.GetConfig.Definition> definition in configurationSettingsGetTreeResponse.Get.Definitions)
                {
                    // Write the separator to the class if this is not the first iteration.
                    if (alreadyAddedItem)
                    {
                        stringBuilder.AppendLine();
                    }

                    // Write the metadata to the class.
                    stringBuilder.AppendLine("        /// <summary>" + definition.Value.Help.TrimEnd() + "</summary>");
                    stringBuilder.AppendLine("        [JsonProperty(PropertyName = \"" + definition.Key + "\")]");

                    string namingConventionKey = GetNamingConventionString(definition.Key);

                    // Is this a list of further values (will not have a type declared)?
                    if (definition.Value.Type == ConfigurationSettingsGetTreeResponse.GetConfig.Definition.ValueType.Unknown)
                    {
                        // Get the full path to this key.
                        string[] newArray = AppendArray(startKey, definition.Key);

                        // Write out the section path.
                        //Console.WriteLine("Section: " + string.Join(" -> ", newArray));

                        // Write this section to the class.
                        stringBuilder.AppendLine("        public " + GetNamingConventionString(startKey == null ? definition.Key : startKey[startKey.Length - 1] + '-' + definition.Key) + " " + namingConventionKey + ";");

                        // We will recursively call this method again (but after we have finished processing all Definitions for this class).
                        furtherSectionsOrTags.Add(newArray);
                    }
                    // User defined tags are present.
                    else if (definition.Value.Tag)
                    {
                        // Get the full path to this key.
                        string[] newArray = AppendArray(startKey, definition.Key);

                        // Write out the tag path.
                        //Console.WriteLine("Tag: " + string.Join(" -> ", newArray));

                        // Write this tag to the class.
                        stringBuilder.AppendLine("        public Dictionary<string, " + namingConventionKey + "Entry> " + namingConventionKey + ";");

                        // Query an undefined tag to get the definitions.
                        newArray = AppendArray(newArray, "Entry");

                        // We will recursively call this method again (but after we have finished processing all Definitions for this class).
                        furtherSectionsOrTags.Add(newArray);
                    }
                    // This is a setting
                    else
                    {
                        // Write out the value.
                        //Console.WriteLine("Value: " + definition.Key);

                        // Write this value to the class.
                        stringBuilder.AppendLine("        public " + GetTypeString(definition.Value) + " " + namingConventionKey + ";");
                    }

                    // Mark that we have now added an item.
                    alreadyAddedItem = true;
                }

                // Write the class footer.
                stringBuilder.AppendLine("    }");
                stringBuilder.Append("}");

                // Create a path and filename for this class.
                string fileName = @"Configuration\" + (startKey != null ? GetFilepathString(startKey) : "Configuration") + ".cs";

                // Get just the directory
                string directory = Path.GetDirectoryName(fileName);
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                // Write this class to a file.
                File.WriteAllText(fileName, stringBuilder.ToString());

                // Update the user on the progress.
                Console.WriteLine(fileName);

                // More children (tags and sections) to resolve.
                foreach (string[] furtherSectionOrTag in furtherSectionsOrTags)
                {
                    CreateClass(webClient, furtherSectionOrTag);
                }
            }
        }