예제 #1
0
        public RootModel Parse(string zoneGroup, string region, string environment,
                               string basePath = null)
        {
            if (basePath == null)
            {
                basePath = Directory.GetCurrentDirectory();
            }

            var fullPath = Path.GetFullPath(basePath);

            Console.WriteLine("Starting...");

            //var root = Path.Join(fullPath, $"{zoneGroup}/{region}/{environment}");
            var configPath       = Path.Join(fullPath, zoneGroup, "config.yaml");
            var sampleOutputPath = Directory.GetCurrentDirectory();

            var model = FileParser.LoadRootModelFromConfig(configPath);

            model.ZoneGroup = zoneGroup;

            // iterate regions in config
            foreach (var reg in model.Regions)
            {
                foreach (var env in reg.Environments.Where(e => !e.Ignore))
                {
                    var zonePath = Path.Combine(fullPath, zoneGroup, reg.Id, env.Id, "zones.yaml");
                    var rulePath = Path.Combine(fullPath, zoneGroup, reg.Id, env.Id, "rule");
                    var zones    = FileParser.ParseZonesFile(zonePath);
                    Console.WriteLine($"Got {zones.Count} zones");
                    zones.ForEach(z => z.Cidr = _subnetCalculator.GetSubnetOffset(env.Cidr, (byte)env.ZoneSubnetMaskSize, z.Index));
                    env.Zones = zones;

                    var rules = FileParser.ParseAllInDir <NetworkRule>(rulePath);
                    Console.WriteLine($"Got {rules.Count} rules");
                    env.Rules = new List <Rule>(rules);
                }
            }

            Console.WriteLine("Produced a Network Zone Model");

            try
            {
                model.Validate();
                Console.WriteLine("Zone Model is valid");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(model);
        }
예제 #2
0
        public static RootModel RootModelSample(ISubnetCalculator calculator)
        {
            var model = new RootModel
            {
                ZoneGroup = "Zone Group Id",
                Regions   = new List <Region>()
            };

            var region = new Region {
                Id = "australiaeast", Environments = new List <Model.Environment>()
            };
            var environment = new Model.Environment()
            {
                Id    = "platform-cnry",
                Cidr  = "10.2.0.0/16",
                Rules = new List <Rule> {
                    NetworkRuleSample("rule-id"), new NetworkRule {
                        Id    = "rule-id-24691",
                        From  = "frontend",
                        To    = "backend",
                        Ports = new List <int> {
                            22, 80
                        },
                        IsBidirectional = true,
                        Description     = "Frontend should access backend"
                    }
                },
                Zones = new List <Zone> {
                    ZoneSample("backend", 1), ZoneSample("frontend", 2)
                }
            };
            var hub = new Environment()
            {
                Id     = "hub",
                Cidr   = "10.2.0.0/16",
                Ignore = true
            };

            environment.Zones.ForEach(z =>
                                      z.Cidr = calculator.GetSubnetOffset(environment.Cidr, (byte)environment.ZoneSubnetMaskSize, z.Index));

            region.Environments.Add(environment);
            region.Environments.Add(hub);
            model.Regions.Add(region);

            return(model);
        }