private static void ReadVnetConfig()
        {
            // Read the vnetconfig file and get the names of local networks, virtual networks and affinity groups.
            XDocument vnetconfigxml = XDocument.Load(vnetConfigFilePath);

            foreach (XElement el in vnetconfigxml.Descendants())
            {
                switch (el.Name.LocalName)
                {
                case "LocalNetworkSite":
                    LocalNets.Add(el.FirstAttribute.Value);
                    List <XElement> elements   = el.Elements().ToList();
                    var             prefixlist = new AddressPrefixList();
                    prefixlist.Add(elements[0].Elements().First().Value);
                    LocalNetworkSites.Add(new LocalNetworkSite()
                    {
                        Name = el.FirstAttribute.Value,
                        VpnGatewayAddress = elements[1].Value,
                        AddressSpace      = new AddressSpace()
                        {
                            AddressPrefixes = prefixlist
                        }
                    });
                    break;

                case "VirtualNetworkSite":
                    VirtualNets.Add(el.Attribute("name").Value);
                    AffinityGroups.Add(el.Attribute("AffinityGroup").Value);
                    break;

                case "DnsServer":
                    DnsServers.Add(new DnsServer()
                    {
                        Name = el.Attribute("name").Value, Address = el.Attribute("IPAddress").Value
                    });
                    break;
                }
            }

            foreach (string aff in AffinityGroups)
            {
                if (Utilities.CheckRemove(vmPowershellCmdlets.GetAzureAffinityGroup, aff))
                {
                    vmPowershellCmdlets.NewAzureAffinityGroup(aff, locationName, null, null);
                }
            }
        }
        private static void ReadVnetConfig()
        {
            // Read the vnetconfig file and get the names of local networks, virtual networks and affinity groups.
            XDocument vnetconfigxml = XDocument.Load(vnetConfigFilePath);

            foreach (XElement el in vnetconfigxml.Descendants())
            {
                switch (el.Name.LocalName)
                {
                    case "LocalNetworkSite":
                        LocalNets.Add(el.FirstAttribute.Value);
                        List<XElement> elements = el.Elements().ToList();
                        var prefixlist = new AddressPrefixList();
                        prefixlist.Add(elements[0].Elements().First().Value);
                        LocalNetworkSites.Add(new LocalNetworkSite()
                        {
                            Name = el.FirstAttribute.Value,
                            VpnGatewayAddress = elements[1].Value,
                            AddressSpace = new AddressSpace() {AddressPrefixes = prefixlist}
                        });
                        break;
                    case "VirtualNetworkSite":
                        VirtualNets.Add(el.Attribute("name").Value);
                        AffinityGroups.Add(el.Attribute("AffinityGroup").Value);
                        break;
                    case "DnsServer":
                        DnsServers.Add(new DnsServer() { Name = el.Attribute("name").Value, Address = el.Attribute("IPAddress").Value });
                        break;
                }
            }

            foreach (string aff in AffinityGroups)
            {
                if (Utilities.CheckRemove(vmPowershellCmdlets.GetAzureAffinityGroup, aff))
                {
                    vmPowershellCmdlets.NewAzureAffinityGroup(aff, locationName, null, null);
                }
            }
        }
        public void VNetTest()
        {
            StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);

            string newAzureQuickVMName = Utilities.GetUniqueShortName(vmNamePrefix);
            if (string.IsNullOrEmpty(imageName))
                imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows" }, false);

            // Read the vnetconfig file and get the names of local networks, virtual networks and affinity groups.
            XDocument vnetconfigxml = XDocument.Load(vnetConfigFilePath);
            List<string> localNets = new List<string>();
            List<string> virtualNets = new List<string>();
            HashSet<string> affinityGroups = new HashSet<string>();
            Dictionary<string, string> dns = new Dictionary<string, string>();
            List<LocalNetworkSite> localNetworkSites = new List<LocalNetworkSite>();
            AddressPrefixList prefixlist = null;

            foreach (XElement el in vnetconfigxml.Descendants())
            {
                switch (el.Name.LocalName)
                {
                    case "LocalNetworkSite":
                        {
                            localNets.Add(el.FirstAttribute.Value);
                            List<XElement> elements = el.Elements().ToList<XElement>();
                            prefixlist = new AddressPrefixList();
                            prefixlist.Add(elements[0].Elements().First().Value);
                            localNetworkSites.Add(new LocalNetworkSite()
                            {
                                VpnGatewayAddress = elements[1].Value,
                                AddressSpace = new AddressSpace() { AddressPrefixes = prefixlist }
                            }
                            );
                        }
                        break;
                    case "VirtualNetworkSite":
                        virtualNets.Add(el.Attribute("name").Value);
                        affinityGroups.Add(el.Attribute("AffinityGroup").Value);
                        break;
                    case "DnsServer":
                        {
                            dns.Add(el.Attribute("name").Value, el.Attribute("IPAddress").Value);
                            break;
                        }
                    default:
                        break;
                }
            }

            foreach (string aff in affinityGroups)
            {
                if (Utilities.CheckRemove(vmPowershellCmdlets.GetAzureAffinityGroup, aff))
                {

                    vmPowershellCmdlets.NewAzureAffinityGroup(aff, locationName, null, null);
                }
            }

            string vnet1 = virtualNets[0];
            string lnet1 = localNets[0];

            try
            {
                vmPowershellCmdlets.NewAzureQuickVM(OS.Windows, newAzureQuickVMName, serviceName, imageName, username, password, locationName); // New-AzureQuickVM
                Console.WriteLine("VM is created successfully: -Name {0} -ServiceName {1}", newAzureQuickVMName, serviceName);

                vmPowershellCmdlets.SetAzureVNetConfig(vnetConfigFilePath);

                foreach (VirtualNetworkSiteContext site in vmPowershellCmdlets.GetAzureVNetSite(null))
                {
                    Console.WriteLine("Name: {0}, AffinityGroup: {1}", site.Name, site.AffinityGroup);
                    Console.WriteLine("Name: {0}, Location: {1}", site.Name, site.Location);
                }

                foreach (string vnet in virtualNets)
                {
                    VirtualNetworkSiteContext vnetsite = vmPowershellCmdlets.GetAzureVNetSite(vnet)[0];
                    Assert.AreEqual(vnet, vnetsite.Name);

                    //Verify DNS and IPAddress
                    Assert.AreEqual(1, vnetsite.DnsServers.Count());
                    Assert.IsTrue(dns.ContainsKey(vnetsite.DnsServers.First().Name));
                    Assert.AreEqual(dns[vnetsite.DnsServers.First().Name], vnetsite.DnsServers.First().Address);

                    //Verify the Gateway sites
                    Assert.AreEqual(1, vnetsite.GatewaySites.Count);
                    Assert.AreEqual(localNetworkSites[0].VpnGatewayAddress, vnetsite.GatewaySites[0].VpnGatewayAddress);
                    Assert.IsTrue(localNetworkSites[0].AddressSpace.AddressPrefixes.All(c => vnetsite.GatewaySites[0].AddressSpace.AddressPrefixes.Contains(c)));

                    Assert.AreEqual(Microsoft.WindowsAzure.Commands.ServiceManagement.Network.ProvisioningState.NotProvisioned, vmPowershellCmdlets.GetAzureVNetGateway(vnet)[0].State);
                }

                vmPowershellCmdlets.NewAzureVNetGateway(vnet1);

                Assert.IsTrue(GetVNetState(vnet1, Microsoft.WindowsAzure.Commands.ServiceManagement.Network.ProvisioningState.Provisioned, 12, 60));

                // Set-AzureVNetGateway -Connect Test
                vmPowershellCmdlets.SetAzureVNetGateway("connect", vnet1, lnet1);

                foreach (Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Gateway.Model.GatewayConnectionContext connection in vmPowershellCmdlets.GetAzureVNetConnection(vnet1))
                {
                    Console.WriteLine("Connectivity: {0}, LocalNetwork: {1}", connection.ConnectivityState, connection.LocalNetworkSiteName);
                    Assert.IsFalse(connection.ConnectivityState.ToLowerInvariant().Contains("notconnected"));
                }

                // Get-AzureVNetGatewayKey
                Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Gateway.Model.SharedKeyContext result = vmPowershellCmdlets.GetAzureVNetGatewayKey(vnet1,
                    vmPowershellCmdlets.GetAzureVNetConnection(vnet1).First().LocalNetworkSiteName);
                Console.WriteLine("Gateway Key: {0}", result.Value);


                // Set-AzureVNetGateway -Disconnect
                vmPowershellCmdlets.SetAzureVNetGateway("disconnect", vnet1, lnet1);

                foreach (Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Gateway.Model.GatewayConnectionContext connection in vmPowershellCmdlets.GetAzureVNetConnection(vnet1))
                {
                    Console.WriteLine("Connectivity: {0}, LocalNetwork: {1}", connection.ConnectivityState, connection.LocalNetworkSiteName);
                }

                // Remove-AzureVnetGateway
                vmPowershellCmdlets.RemoveAzureVNetGateway(vnet1);

                foreach (string vnet in virtualNets)
                {
                    Microsoft.WindowsAzure.Commands.ServiceManagement.Network.VirtualNetworkGatewayContext gateway = vmPowershellCmdlets.GetAzureVNetGateway(vnet)[0];

                    Console.WriteLine("State: {0}, VIP: {1}", gateway.State.ToString(), gateway.VIPAddress);
                    if (vnet.Equals(vnet1))
                    {
                        if (gateway.State != Microsoft.WindowsAzure.Commands.ServiceManagement.Network.ProvisioningState.Deprovisioning &&
                            gateway.State != Microsoft.WindowsAzure.Commands.ServiceManagement.Network.ProvisioningState.NotProvisioned)
                        {
                            Assert.Fail("The state of the gateway is neither Deprovisioning nor NotProvisioned!");
                        }
                    }
                    else
                    {
                        Assert.AreEqual(Microsoft.WindowsAzure.Commands.ServiceManagement.Network.ProvisioningState.NotProvisioned, gateway.State);
                    }

                }

                Utilities.RetryActionUntilSuccess(() => vmPowershellCmdlets.RemoveAzureVNetConfig(), "in use", 10, 30);

                pass = true;

            }
            catch (Exception e)
            {
                pass = false;
                if (cleanupIfFailed)
                {
                    try
                    {
                        vmPowershellCmdlets.RemoveAzureVNetGateway(vnet1);
                    }
                    catch { }
                    Utilities.RetryActionUntilSuccess(() => vmPowershellCmdlets.RemoveAzureVNetConfig(), "in use", 10, 30);
                }
                Assert.Fail("Exception occurred: {0}", e.ToString());
            }
            finally
            {
                foreach (string aff in affinityGroups)
                {
                    try
                    {
                        vmPowershellCmdlets.RemoveAzureAffinityGroup(aff);
                    }
                    catch
                    {
                        // Some service uses the affinity group, so it cannot be deleted.  Just leave it.
                    }
                }
            }
        }