public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { var results = new List <ValidationResult>(); // Subnet name must be unique. if (VirtualNetwork.Subnets.Where(s => s.Name.Equals(Name)).FirstOrDefault() != null) { results.Add(new ValidationResult("Subnet name must be unique within a virtual network.")); } // IpPerInstance must be > 0 for non fixed cidr. if (!Service.FixedSubnetCidr && IpPerInstance <= 0) { results.Add(new ValidationResult("Address/Instance must be greater than 0.")); } // Number of instances need to be between min and max. if (!Service.FixedSubnetCidr && Service.MaxInstances > Service.MinInstances && (ServiceInstances > Service.MaxInstances || ServiceInstances < Service.MinInstances)) { results.Add(new ValidationResult($"The instance must be between {Service.MinInstances} and {Service.MaxInstances}.")); } var ipAddress = IPAddress.Parse(StartIP); IPNetwork network1 = IPNetwork.Parse("10.0.0.0/8"); IPNetwork network2 = IPNetwork.Parse("172.16.0.0/12"); IPNetwork network3 = IPNetwork.Parse("192.168.0.0/16"); if (!network1.Contains(ipAddress) && !network2.Contains(ipAddress) && !network3.Contains(ipAddress)) { results.Add(new ValidationResult($"The start ip is not in the range of RFC1918.")); return(results); } var network = Network; if (IPRangeId < 0) { results.Add(new ValidationResult($"The subnet address range {network} is not contained in the virtual network's address spaces.")); return(results); } // Validate CIDR block. if (!Equals(network.Network, ipAddress)) { var current = IPNetwork.ToBigInteger(network.Network); var next = current + network.Total; var nextIp = IPNetwork.ToIPAddress(next, AddressFamily.InterNetwork); results.Add(new ValidationResult($"{StartIP}/{network.Cidr} is not a valid CIDR block. Try {network.Network} or {nextIp} instead.")); return(results); } // Overlap with other subnets foreach (var subnet in VirtualNetwork.Subnets) { if (network.Overlap(subnet.Network)) { results.Add(new ValidationResult($"The subnet address range {network} overlaps with {subnet.Name} address range {subnet.Network}.")); break; } } return(results); }