public void AzureReachabilityReportStateLevelAggregationTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                AzureReachabilityReportParameters parameters = new AzureReachabilityReportParameters();
                parameters.AzureLocations = new List <string>();
                parameters.AzureLocations.Add("West US");

                parameters.ProviderLocation         = new AzureReachabilityReportLocation();
                parameters.ProviderLocation.Country = "United States";
                parameters.ProviderLocation.State   = "washington";

                parameters.StartTime = DateTime.Now.AddDays(-10);
                parameters.EndTime   = DateTime.Now.AddDays(-5);

                var report = networkManagementClient.NetworkWatchers.GetAzureReachabilityReport("NetworkWatcherRG", "NetworkWatcher", parameters);

                //Validation
                Assert.Equal("State", report.AggregationLevel);
                Assert.Equal("United States", report.ProviderLocation.Country);
                Assert.Equal("washington", report.ProviderLocation.State);
                Assert.Equal("West US", report.ReachabilityReport[0].AzureLocation);
            }
        }
        public void AvailableProvidersListAzureLocationCountryStateCitySpecifiedTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                AvailableProvidersListParameters parameters = new AvailableProvidersListParameters();
                parameters.AzureLocations = new List <string>();
                parameters.AzureLocations.Add("West US");
                parameters.Country = "United States";
                parameters.State   = "washington";
                parameters.City    = "seattle";

                var providersList = networkManagementClient.NetworkWatchers.ListAvailableProviders("NetworkWatcherRG", "NetworkWatcher", parameters);

                Assert.Equal("United States", providersList.Countries[0].CountryName);
                Assert.Equal("washington", providersList.Countries[0].States[0].StateName);
                Assert.Equal("seattle", providersList.Countries[0].States[0].Cities[0].CityName);
            }
        }
Esempio n. 3
0
        public void VmssPublicIpAddressApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1, true);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                var    location          = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Compute/virtualMachineScaleSets");
                string resourceGroupName = TestUtilities.GenerateName();
                string deploymentName    = TestUtilities.GenerateName("vmss");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                DeploymentUpdate.CreateVmss(resourcesClient, resourceGroupName, deploymentName);

                string virtualMachineScaleSetName = "vmssip";
                var    vmssListAllPageResult      = networkManagementClient.PublicIPAddresses.ListVirtualMachineScaleSetPublicIPAddresses(resourceGroupName, virtualMachineScaleSetName);
                var    vmssListAllResult          = vmssListAllPageResult.ToList();
                var    firstResult = vmssListAllResult.First();

                Assert.NotNull(vmssListAllResult);
                Assert.Equal("Succeeded", firstResult.ProvisioningState);
                Assert.NotNull(firstResult.ResourceGuid);

                var idItem       = firstResult.Id;
                var vmIndex      = GetNameById(idItem, "virtualMachines");
                var nicName      = GetNameById(idItem, "networkInterfaces");
                var ipConfigName = GetNameById(idItem, "ipConfigurations");
                var ipName       = GetNameById(idItem, "publicIPAddresses");

                var vmssListPageResult = networkManagementClient.PublicIPAddresses.ListVirtualMachineScaleSetVMPublicIPAddresses(
                    resourceGroupName, virtualMachineScaleSetName, vmIndex, nicName, ipConfigName);
                var vmssListResult = vmssListPageResult.ToList();

                Assert.Single(vmssListResult);

                var vmssGetResult = networkManagementClient.PublicIPAddresses.GetVirtualMachineScaleSetPublicIPAddress(
                    resourceGroupName, virtualMachineScaleSetName, vmIndex, nicName, ipConfigName, ipName);

                Assert.NotNull(vmssGetResult);
                Assert.Equal("Succeeded", vmssGetResult.ProvisioningState);
                Assert.NotNull(vmssGetResult.ResourceGuid);

                resourcesClient.ResourceGroups.Delete(resourceGroupName);
            }
        }
Esempio n. 4
0
        public void RouteFilterApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1, true);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);
                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/routefilters");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(
                    resourceGroupName,
                    new ResourceGroup
                {
                    Location = location
                });

                // Create route filter
                string filterName = "filter";
                string ruleName   = "rule";

                var filter = TestHelper.CreateDefaultRouteFilter(resourceGroupName,
                                                                 filterName, location, networkManagementClient);

                Assert.Equal(filter.Name, filterName);
                Assert.Empty(filter.Rules);

                // Update route filter with rule by put on parent resources
                filter = TestHelper.CreateDefaultRouteFilter(resourceGroupName,
                                                             filterName, location, networkManagementClient, true);

                Assert.Equal(filter.Name, filterName);
                Assert.NotEmpty(filter.Rules);

                // Update route filter and delete rules
                filter = TestHelper.CreateDefaultRouteFilter(resourceGroupName,
                                                             filterName, location, networkManagementClient);

                Assert.Equal(filter.Name, filterName);
                Assert.Empty(filter.Rules);

                filter = TestHelper.CreateDefaultRouteFilterRule(resourceGroupName,
                                                                 filterName, ruleName, location, networkManagementClient);

                Assert.Equal(filter.Name, filterName);
                Assert.NotEmpty(filter.Rules);

                resourcesClient.ResourceGroups.Delete(resourceGroupName);
            }
        }
Esempio n. 5
0
        public void ApplicationGatewayApiTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var resourcesClient = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(handler);
                var networkResourceProviderClient = NetworkManagementTestUtilities.GetNetworkResourceProviderClient(handler);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/applicationgateways");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                var vnetName   = TestUtilities.GenerateName();
                var subnetName = TestUtilities.GenerateName();

                var virtualNetwork    = TestHelper.CreateVirtualNetwork(vnetName, subnetName, resourceGroupName, location, networkResourceProviderClient);
                var getSubnetResponse = networkResourceProviderClient.Subnets.Get(resourceGroupName, vnetName, subnetName);
                Console.WriteLine("Virtual Network GatewaySubnet Id: {0}", getSubnetResponse.Subnet.Id);
                var subnet = getSubnetResponse.Subnet;

                var appGw = CreateApplicationGateway(location, subnet, resourceGroupName, networkResourceProviderClient.Credentials.SubscriptionId);

                // Put AppGw
                var putAppGwResponse = networkResourceProviderClient.ApplicationGateways.CreateOrUpdate(resourceGroupName, appGw.Name, appGw);
                Assert.Equal(HttpStatusCode.OK, putAppGwResponse.StatusCode);
                Assert.Equal("Succeeded", putAppGwResponse.Status);

                // Get AppGw
                var getResp = networkResourceProviderClient.ApplicationGateways.Get(resourceGroupName, appGw.Name);
                CompareApplicationGateway(appGw, getResp.ApplicationGateway);

                //Start AppGw
                var startResult = networkResourceProviderClient.ApplicationGateways.Start(resourceGroupName, appGw.Name);
                Assert.Equal(HttpStatusCode.OK, startResult.StatusCode);
                Assert.Equal("Succeeded", startResult.Status);

                //Stop AppGw
                var stopResult = networkResourceProviderClient.ApplicationGateways.Stop(resourceGroupName, appGw.Name);
                Assert.Equal(HttpStatusCode.OK, stopResult.StatusCode);
                Assert.Equal("Succeeded", stopResult.Status);

                // Delete AppGw
                var deleteResult = networkResourceProviderClient.ApplicationGateways.Delete(resourceGroupName, appGw.Name);
                Assert.Equal(HttpStatusCode.OK, deleteResult.StatusCode);
            }
        }
        public void UsageTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var resourcesClient = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(handler);
                var networkResourceProviderClient = NetworkManagementTestUtilities.GetNetworkResourceProviderClient(handler);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/networkSecurityGroups");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                string networkSecurityGroupName = TestUtilities.GenerateName();

                var networkSecurityGroup = new NetworkSecurityGroup()
                {
                    Location = location,
                };

                // Put Nsg
                var putNsgResponse = networkResourceProviderClient.NetworkSecurityGroups.CreateOrUpdate(resourceGroupName, networkSecurityGroupName, networkSecurityGroup);
                Assert.Equal(HttpStatusCode.OK, putNsgResponse.StatusCode);
                Assert.Equal("Succeeded", putNsgResponse.Status);

                var getNsgResponse = networkResourceProviderClient.NetworkSecurityGroups.Get(resourceGroupName, networkSecurityGroupName);
                Assert.Equal(HttpStatusCode.OK, getNsgResponse.StatusCode);

                // Query for usages
                var usagesResponse = networkResourceProviderClient.Usages.List(getNsgResponse.NetworkSecurityGroup.Location.Replace(" ", string.Empty));
                Assert.True(usagesResponse.StatusCode == HttpStatusCode.OK);

                // Verify that the strings are populated
                Assert.NotNull(usagesResponse.Usages);
                Assert.True(usagesResponse.Usages.Any());

                foreach (var usage in usagesResponse.Usages)
                {
                    Assert.True(usage.Limit > 0);
                    Assert.NotNull(usage.Name);
                    Assert.True(!string.IsNullOrEmpty(usage.Name.LocalizedValue));
                    Assert.True(!string.IsNullOrEmpty(usage.Name.Value));
                }
            }
        }
Esempio n. 7
0
        public void ExpressRouteMicrosoftPeeringApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1, true);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);
                var location = "westus";

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(
                    resourceGroupName,
                    new ResourceGroup
                {
                    Location = location
                });

                string circuitName = "circuit";

                var circuit = TestHelper.CreateDefaultExpressRouteCircuit(resourceGroupName,
                                                                          circuitName, location, networkManagementClient);

                Assert.Equal(circuit.Name, circuitName);
                Assert.Equal(circuit.ServiceProviderProperties.BandwidthInMbps, Convert.ToInt32(Circuit_BW));

                circuit = TestHelper.UpdateDefaultExpressRouteCircuitWithMicrosoftPeering(resourceGroupName,
                                                                                          circuitName, networkManagementClient);

                Assert.Equal(circuit.Name, circuitName);
                Assert.Equal(circuit.ServiceProviderProperties.BandwidthInMbps, Convert.ToInt32(Circuit_BW));
                Assert.NotNull(circuit.Peerings);

                resourcesClient.ResourceGroups.Delete(resourceGroupName);
            }
        }
Esempio n. 8
0
        public void BGPCommunityApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);
                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/routefilters");

                var communities = networkManagementClient.BgpServiceCommunities.List();

                Assert.NotNull(communities);
                Assert.True(communities.First().BgpCommunities.First().IsAuthorizedToUse);
            }
        }
        public void CheckDnsAvailabilityTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var resourcesClient = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(handler);
                var networkResourceProviderClient = NetworkManagementTestUtilities.GetNetworkResourceProviderClient(handler);

                var location = GetNrpServiceEndpoint(NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/virtualNetworks"));

                string domainNameLabel = TestUtilities.GenerateName("domainnamelabel");

                var dnsNameAvailability = networkResourceProviderClient.CheckDnsNameAvailability(location, domainNameLabel);

                Assert.True(dnsNameAvailability.DnsNameAvailability);
            }
        }
        public void CheckDnsAvailabilityTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/publicIPAddresses");
                location = location.Replace(" ", "");

                string domainNameLabel = TestUtilities.GenerateName("domainnamelabel");

                var dnsNameAvailability = networkManagementClient.CheckDnsNameAvailability(location, domainNameLabel);

                Assert.True(dnsNameAvailability.Available);
            }
        }
        public void VirtualNetworkApiTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var resourcesClient = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(handler);
                var networkResourceProviderClient = NetworkManagementTestUtilities.GetNetworkResourceProviderClient(handler);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/virtualNetworks");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                string vnetName    = TestUtilities.GenerateName();
                string subnet1Name = TestUtilities.GenerateName();
                string subnet2Name = TestUtilities.GenerateName();

                var vnet = new VirtualNetwork()
                {
                    Location = location,

                    AddressSpace = new AddressSpace()
                    {
                        AddressPrefixes = new List <string>()
                        {
                            "10.0.0.0/16",
                        }
                    },
                    DhcpOptions = new DhcpOptions()
                    {
                        DnsServers = new List <string>()
                        {
                            "10.1.1.1",
                            "10.1.2.4"
                        }
                    },
                    Subnets = new List <Subnet>()
                    {
                        new Subnet()
                        {
                            Name          = subnet1Name,
                            AddressPrefix = "10.0.1.0/24",
                        },
                        new Subnet()
                        {
                            Name          = subnet2Name,
                            AddressPrefix = "10.0.2.0/24",
                        }
                    }
                };

                // Put Vnet
                var putVnetResponse = networkResourceProviderClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnetName, vnet);
                Assert.Equal(HttpStatusCode.OK, putVnetResponse.StatusCode);
                Assert.Equal("Succeeded", putVnetResponse.Status);

                // Get Vnet
                var getVnetResponse = networkResourceProviderClient.VirtualNetworks.Get(resourceGroupName, vnetName);
                Assert.Equal(HttpStatusCode.OK, getVnetResponse.StatusCode);
                Assert.Equal(vnetName, getVnetResponse.VirtualNetwork.Name);
                Assert.NotNull(getVnetResponse.VirtualNetwork.ResourceGuid);
                Assert.Equal(Microsoft.Azure.Management.Resources.Models.ProvisioningState.Succeeded, getVnetResponse.VirtualNetwork.ProvisioningState);
                Assert.Equal("10.1.1.1", getVnetResponse.VirtualNetwork.DhcpOptions.DnsServers[0]);
                Assert.Equal("10.1.2.4", getVnetResponse.VirtualNetwork.DhcpOptions.DnsServers[1]);
                Assert.Equal("10.0.0.0/16", getVnetResponse.VirtualNetwork.AddressSpace.AddressPrefixes[0]);
                Assert.Equal(subnet1Name, getVnetResponse.VirtualNetwork.Subnets[0].Name);
                Assert.Equal(subnet2Name, getVnetResponse.VirtualNetwork.Subnets[1].Name);

                // Get all Vnets
                var getAllVnets = networkResourceProviderClient.VirtualNetworks.List(resourceGroupName);
                Assert.Equal(HttpStatusCode.OK, getAllVnets.StatusCode);
                Assert.Equal(vnetName, getAllVnets.VirtualNetworks[0].Name);
                Assert.Equal(Microsoft.Azure.Management.Resources.Models.ProvisioningState.Succeeded, getAllVnets.VirtualNetworks[0].ProvisioningState);
                Assert.Equal("10.0.0.0/16", getAllVnets.VirtualNetworks[0].AddressSpace.AddressPrefixes[0]);
                Assert.Equal(subnet1Name, getAllVnets.VirtualNetworks[0].Subnets[0].Name);
                Assert.Equal(subnet2Name, getAllVnets.VirtualNetworks[0].Subnets[1].Name);

                // Get all Vnets in a subscription
                var getAllVnetInSubscription = networkResourceProviderClient.VirtualNetworks.ListAll();
                Assert.Equal(HttpStatusCode.OK, getAllVnetInSubscription.StatusCode);
                Assert.Equal(vnetName, getAllVnetInSubscription.VirtualNetworks[0].Name);
                Assert.Equal(Microsoft.Azure.Management.Resources.Models.ProvisioningState.Succeeded, getAllVnetInSubscription.VirtualNetworks[0].ProvisioningState);
                Assert.Equal("10.0.0.0/16", getAllVnetInSubscription.VirtualNetworks[0].AddressSpace.AddressPrefixes[0]);
                Assert.Equal(subnet1Name, getAllVnetInSubscription.VirtualNetworks[0].Subnets[0].Name);
                Assert.Equal(subnet2Name, getAllVnetInSubscription.VirtualNetworks[0].Subnets[1].Name);

                // Delete Vnet
                var deleteVnetResponse = networkResourceProviderClient.VirtualNetworks.Delete(resourceGroupName, vnetName);
                Assert.Equal(HttpStatusCode.OK, deleteVnetResponse.StatusCode);

                // Get all Vnets
                getAllVnets = networkResourceProviderClient.VirtualNetworks.List(resourceGroupName);
                Assert.Equal(HttpStatusCode.OK, getAllVnets.StatusCode);
                Assert.Equal(0, getAllVnets.VirtualNetworks.Count);
            }
        }
Esempio n. 12
0
        public void PublicIpAddressApiTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var resourcesClient = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(handler);
                var networkResourceProviderClient = NetworkManagementTestUtilities.GetNetworkResourceProviderClient(handler);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/publicIPAddresses");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });


                // Create the parameter for PUT PublicIpAddress
                string publicIpName    = TestUtilities.GenerateName();
                string domainNameLabel = TestUtilities.GenerateName();

                var publicIp = new PublicIpAddress()
                {
                    Location = location,
                    Tags     = new Dictionary <string, string>()
                    {
                        { "key", "value" }
                    },
                    PublicIpAllocationMethod = IpAllocationMethod.Dynamic,
                    DnsSettings = new PublicIpAddressDnsSettings()
                    {
                        DomainNameLabel = domainNameLabel
                    }
                };

                // Put PublicIpAddress
                var putPublicIpAddressResponse = networkResourceProviderClient.PublicIpAddresses.CreateOrUpdate(resourceGroupName, publicIpName, publicIp);
                Assert.Equal(HttpStatusCode.OK, putPublicIpAddressResponse.StatusCode);
                Assert.Equal("Succeeded", putPublicIpAddressResponse.Status);

                // Get PublicIpAddress
                var getPublicIpAddressResponse = networkResourceProviderClient.PublicIpAddresses.Get(resourceGroupName, publicIpName);
                Assert.Equal(HttpStatusCode.OK, getPublicIpAddressResponse.StatusCode);
                Assert.Equal(4, getPublicIpAddressResponse.PublicIpAddress.IdleTimeoutInMinutes);
                Assert.NotNull(getPublicIpAddressResponse.PublicIpAddress.ResourceGuid);

                // Get List of PublicIpAddress
                var getPublicIpAddressListResponse = networkResourceProviderClient.PublicIpAddresses.List(resourceGroupName);
                Assert.Equal(HttpStatusCode.OK, getPublicIpAddressListResponse.StatusCode);
                Assert.Equal(1, getPublicIpAddressListResponse.PublicIpAddresses.Count);
                ArePublicIpAddressesEqual(getPublicIpAddressResponse.PublicIpAddress, getPublicIpAddressListResponse.PublicIpAddresses[0]);

                // Get List of PublicIpAddress in a subscription
                var getPublicIpAddressListSubscriptionResponse = networkResourceProviderClient.PublicIpAddresses.ListAll();
                Assert.Equal(HttpStatusCode.OK, getPublicIpAddressListSubscriptionResponse.StatusCode);
                Assert.Equal(1, getPublicIpAddressListSubscriptionResponse.PublicIpAddresses.Count);
                ArePublicIpAddressesEqual(getPublicIpAddressResponse.PublicIpAddress, getPublicIpAddressListSubscriptionResponse.PublicIpAddresses[0]);

                // Delete PublicIpAddress
                var deletePublicIpAddressResponse = networkResourceProviderClient.PublicIpAddresses.Delete(resourceGroupName, publicIpName);
                Assert.Equal(HttpStatusCode.OK, deletePublicIpAddressResponse.StatusCode);

                // Get PublicIpAddress
                getPublicIpAddressListResponse = networkResourceProviderClient.PublicIpAddresses.List(resourceGroupName);
                Assert.Equal(HttpStatusCode.OK, getPublicIpAddressListResponse.StatusCode);
                Assert.Equal(0, getPublicIpAddressListResponse.PublicIpAddresses.Count);
            }
        }
Esempio n. 13
0
        public void NextHopApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler3 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);
                var computeManagementClient = NetworkManagementTestUtilities.GetComputeManagementClientWithHandler(context, handler3);

                string location = "westcentralus";

                string resourceGroupName = TestUtilities.GenerateName();
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                string virtualMachineName       = TestUtilities.GenerateName();
                string networkSecurityGroupName = virtualMachineName + "-nsg";
                string networkInterfaceName     = TestUtilities.GenerateName();

                //Deploy VM wih VNet,Subnet and Route Table from template
                Deployments.CreateVm(
                    resourcesClient: resourcesClient,
                    resourceGroupName: resourceGroupName,
                    location: location,
                    virtualMachineName: virtualMachineName,
                    storageAccountName: TestUtilities.GenerateName(),
                    networkInterfaceName: networkInterfaceName,
                    networkSecurityGroupName: networkSecurityGroupName,
                    diagnosticsStorageAccountName: TestUtilities.GenerateName(),
                    deploymentName: TestUtilities.GenerateName()
                    );

                string         networkWatcherName = TestUtilities.GenerateName();
                NetworkWatcher properties         = new NetworkWatcher();
                properties.Location = location;

                //Create Network Watcher
                var    createNetworkWatcher = networkManagementClient.NetworkWatchers.CreateOrUpdate(resourceGroupName, networkWatcherName, properties);
                string sourceIPAddress      = networkManagementClient.NetworkInterfaces.Get(resourceGroupName, networkInterfaceName).IpConfigurations.FirstOrDefault().PrivateIPAddress;

                var getVm = computeManagementClient.VirtualMachines.Get(resourceGroupName, virtualMachineName);

                //Use DestinationIPAddress from Route Table
                NextHopParameters nhProperties1 = new NextHopParameters()
                {
                    TargetResourceId     = getVm.Id,
                    SourceIPAddress      = sourceIPAddress,
                    DestinationIPAddress = "10.1.3.6"
                };

                NextHopParameters nhProperties2 = new NextHopParameters()
                {
                    TargetResourceId     = getVm.Id,
                    SourceIPAddress      = sourceIPAddress,
                    DestinationIPAddress = "12.11.12.14"
                };

                var getNextHop1 = networkManagementClient.NetworkWatchers.GetNextHop(resourceGroupName, networkWatcherName, nhProperties1);
                var getNextHop2 = networkManagementClient.NetworkWatchers.GetNextHop(resourceGroupName, networkWatcherName, nhProperties2);

                var routeTable = networkManagementClient.RouteTables.Get(resourceGroupName, resourceGroupName + "RT");

                //Validation
                Assert.Equal("10.0.1.2", getNextHop1.NextHopIpAddress);
                Assert.Equal(routeTable.Id, getNextHop1.RouteTableId);

                Assert.Equal("Internet", getNextHop2.NextHopType);
                Assert.Equal("System Route", getNextHop2.RouteTableId);
            }
        }
Esempio n. 14
0
        public void ExpandResourceTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/loadBalancers");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(
                    resourceGroupName,
                    new ResourceGroup
                {
                    Location = location
                });

                // Create lbPublicIP
                string lbPublicIpName     = TestUtilities.GenerateName();
                string lbDomaingNameLabel = TestUtilities.GenerateName();

                var lbPublicIp = TestHelper.CreateDefaultPublicIpAddress(
                    lbPublicIpName,
                    resourceGroupName,
                    lbDomaingNameLabel,
                    location,
                    networkManagementClient);

                // Create Vnet
                string vnetName   = TestUtilities.GenerateName();
                string subnetName = TestUtilities.GenerateName();

                var vnet = TestHelper.CreateVirtualNetwork(vnetName, subnetName, resourceGroupName, location,
                                                           networkManagementClient);

                // Create Nics
                string nic1name = TestUtilities.GenerateName();
                string nic2name = TestUtilities.GenerateName();
                string nic3name = TestUtilities.GenerateName();

                var nic1 = TestHelper.CreateNetworkInterface(
                    nic1name,
                    resourceGroupName,
                    null,
                    vnet.Subnets[0].Id,
                    location,
                    "ipconfig",
                    networkManagementClient);

                var nic2 = TestHelper.CreateNetworkInterface(
                    nic2name,
                    resourceGroupName,
                    null,
                    vnet.Subnets[0].Id,
                    location,
                    "ipconfig",
                    networkManagementClient);

                var nic3 = TestHelper.CreateNetworkInterface(
                    nic3name,
                    resourceGroupName,
                    null,
                    vnet.Subnets[0].Id,
                    location,
                    "ipconfig",
                    networkManagementClient);

                // Create the LoadBalancer
                var lbName = TestUtilities.GenerateName();
                var frontendIpConfigName   = TestUtilities.GenerateName();
                var backEndAddressPoolName = TestUtilities.GenerateName();
                var loadBalancingRuleName  = TestUtilities.GenerateName();
                var probeName           = TestUtilities.GenerateName();
                var inboundNatRule1Name = TestUtilities.GenerateName();
                var inboundNatRule2Name = TestUtilities.GenerateName();

                // Populate the loadBalancerCreateOrUpdateParameter
                var loadBalancer = new LoadBalancer()
                {
                    Location = location,
                    FrontendIPConfigurations = new List <FrontendIPConfiguration>()
                    {
                        new FrontendIPConfiguration()
                        {
                            Name            = frontendIpConfigName,
                            PublicIPAddress = new PublicIPAddress()
                            {
                                Id = lbPublicIp.Id
                            }
                        }
                    },
                    BackendAddressPools = new List <BackendAddressPool>()
                    {
                        new BackendAddressPool()
                        {
                            Name = backEndAddressPoolName,
                        }
                    },
                    LoadBalancingRules = new List <LoadBalancingRule>()
                    {
                        new LoadBalancingRule()
                        {
                            Name = loadBalancingRuleName,
                            FrontendIPConfiguration = new Microsoft.Azure.Management.Network.Models.SubResource()
                            {
                                Id = TestHelper.GetChildLbResourceId(networkManagementClient.SubscriptionId,
                                                                     resourceGroupName, lbName, "FrontendIPConfigurations", frontendIpConfigName)
                            },
                            Protocol             = TransportProtocol.Tcp,
                            FrontendPort         = 80,
                            BackendPort          = 80,
                            EnableFloatingIP     = false,
                            IdleTimeoutInMinutes = 15,
                            BackendAddressPool   = new Microsoft.Azure.Management.Network.Models.SubResource()
                            {
                                Id = TestHelper.GetChildLbResourceId(networkManagementClient.SubscriptionId,
                                                                     resourceGroupName, lbName, "backendAddressPools", backEndAddressPoolName)
                            },
                            Probe = new Microsoft.Azure.Management.Network.Models.SubResource()
                            {
                                Id = TestHelper.GetChildLbResourceId(networkManagementClient.SubscriptionId,
                                                                     resourceGroupName, lbName, "probes", probeName)
                            }
                        }
                    },
                    Probes = new List <Probe>()
                    {
                        new Probe()
                        {
                            Name              = probeName,
                            Protocol          = ProbeProtocol.Http,
                            Port              = 80,
                            RequestPath       = "healthcheck.aspx",
                            IntervalInSeconds = 10,
                            NumberOfProbes    = 2
                        }
                    },
                    InboundNatRules = new List <InboundNatRule>()
                    {
                        new InboundNatRule()
                        {
                            Name = inboundNatRule1Name,
                            FrontendIPConfiguration = new Microsoft.Azure.Management.Network.Models.SubResource()
                            {
                                Id = TestHelper.GetChildLbResourceId(networkManagementClient.SubscriptionId,
                                                                     resourceGroupName, lbName, "FrontendIPConfigurations", frontendIpConfigName)
                            },
                            Protocol             = TransportProtocol.Tcp,
                            FrontendPort         = 3389,
                            BackendPort          = 3389,
                            IdleTimeoutInMinutes = 15,
                            EnableFloatingIP     = false
                        },
                        new InboundNatRule()
                        {
                            Name = inboundNatRule2Name,
                            FrontendIPConfiguration = new Microsoft.Azure.Management.Network.Models.SubResource()
                            {
                                Id = TestHelper.GetChildLbResourceId(networkManagementClient.SubscriptionId,
                                                                     resourceGroupName, lbName, "FrontendIPConfigurations", frontendIpConfigName)
                            },
                            Protocol             = TransportProtocol.Tcp,
                            FrontendPort         = 3390,
                            BackendPort          = 3389,
                            IdleTimeoutInMinutes = 15,
                            EnableFloatingIP     = false,
                        }
                    }
                };

                // Create the loadBalancer
                var putLoadBalancer = networkManagementClient.LoadBalancers.CreateOrUpdate(resourceGroupName, lbName, loadBalancer);

                var getLoadBalancer = networkManagementClient.LoadBalancers.Get(resourceGroupName, lbName);

                // Associate the nic with LB
                nic1.IpConfigurations.First().LoadBalancerBackendAddressPools = new List <BackendAddressPool>
                {
                    getLoadBalancer.BackendAddressPools.First()
                };

                nic1.IpConfigurations.First().LoadBalancerInboundNatRules = new List <InboundNatRule>
                {
                    getLoadBalancer.InboundNatRules.First()
                };

                nic2.IpConfigurations.First().LoadBalancerBackendAddressPools = new List <BackendAddressPool>
                {
                    getLoadBalancer.BackendAddressPools.First()
                };

                nic3.IpConfigurations.First().LoadBalancerInboundNatRules = new List <InboundNatRule>
                {
                    getLoadBalancer.InboundNatRules[1]
                };

                // Put Nics
                networkManagementClient.NetworkInterfaces.CreateOrUpdate(resourceGroupName, nic1name, nic1);
                networkManagementClient.NetworkInterfaces.CreateOrUpdate(resourceGroupName, nic2name, nic2);
                networkManagementClient.NetworkInterfaces.CreateOrUpdate(resourceGroupName, nic3name, nic3);

                // Get Nics
                nic1 = networkManagementClient.NetworkInterfaces.Get(resourceGroupName, nic1name);
                nic2 = networkManagementClient.NetworkInterfaces.Get(resourceGroupName, nic2name);
                nic3 = networkManagementClient.NetworkInterfaces.Get(resourceGroupName, nic3name);

                // Get lb with expanded nics from nat rules
                getLoadBalancer = networkManagementClient.LoadBalancers.Get(resourceGroupName, lbName, "InboundNatRules/backendIPConfiguration");

                foreach (var natRule in getLoadBalancer.InboundNatRules)
                {
                    Assert.NotNull(natRule.BackendIPConfiguration);
                    Assert.NotNull(natRule.BackendIPConfiguration.Id);
                    Assert.NotNull(natRule.BackendIPConfiguration.Name);
                    Assert.NotNull(natRule.BackendIPConfiguration.Etag);
                    Assert.Equal(natRule.Id, natRule.BackendIPConfiguration.LoadBalancerInboundNatRules[0].Id);
                }

                // Get lb with expanded nics from pools
                getLoadBalancer = networkManagementClient.LoadBalancers.Get(resourceGroupName, lbName, "BackendAddressPools/backendIPConfigurations");

                foreach (var pool in getLoadBalancer.BackendAddressPools)
                {
                    foreach (var ipconfig in getLoadBalancer.BackendAddressPools.First().BackendIPConfigurations)
                    {
                        Assert.NotNull(ipconfig.Id);
                        Assert.NotNull(ipconfig.Name);
                        Assert.NotNull(ipconfig.Etag);
                        Assert.Equal(pool.Id, ipconfig.LoadBalancerBackendAddressPools[0].Id);
                    }
                }

                // Get lb with expanded publicip
                getLoadBalancer = networkManagementClient.LoadBalancers.Get(resourceGroupName, lbName, "FrontendIPConfigurations/PublicIPAddress");
                foreach (var ipconfig in getLoadBalancer.FrontendIPConfigurations)
                {
                    Assert.NotNull(ipconfig.PublicIPAddress);
                    Assert.NotNull(ipconfig.PublicIPAddress.Id);
                    Assert.NotNull(ipconfig.PublicIPAddress.Name);
                    Assert.NotNull(ipconfig.PublicIPAddress.Etag);
                    Assert.Equal(ipconfig.Id, ipconfig.PublicIPAddress.IpConfiguration.Id);
                }

                // Get NIC with expanded subnet
                nic1 = networkManagementClient.NetworkInterfaces.Get(resourceGroupName, nic1name, "IPConfigurations/Subnet");
                foreach (var ipconfig in nic1.IpConfigurations)
                {
                    Assert.NotNull(ipconfig.Subnet);
                    Assert.NotNull(ipconfig.Subnet.Id);
                    Assert.NotNull(ipconfig.Subnet.Name);
                    Assert.NotNull(ipconfig.Subnet.Etag);
                    Assert.NotEqual(0, ipconfig.Subnet.IpConfigurations.Count());
                }

                // Get subnet with expanded ipconfigurations
                var subnet = networkManagementClient.Subnets.Get(
                    resourceGroupName,
                    vnetName,
                    subnetName,
                    "IPConfigurations");

                foreach (var ipconfig in subnet.IpConfigurations)
                {
                    Assert.NotNull(ipconfig.Name);
                    Assert.NotNull(ipconfig.Id);
                    Assert.NotNull(ipconfig.Etag);
                    Assert.NotNull(ipconfig.PrivateIPAddress);
                }

                // Get publicIPAddress with expanded ipconfigurations
                var publicip = networkManagementClient.PublicIPAddresses.Get(
                    resourceGroupName,
                    lbPublicIpName,
                    "IPConfiguration");

                Assert.NotNull(publicip.IpConfiguration);
                Assert.NotNull(publicip.IpConfiguration.Id);
                Assert.NotNull(publicip.IpConfiguration.Name);
                Assert.NotNull(publicip.IpConfiguration.Etag);

                // Delete LoadBalancer
                networkManagementClient.LoadBalancers.Delete(resourceGroupName, lbName);

                // Verify Delete
                var listLoadBalancer = networkManagementClient.LoadBalancers.List(resourceGroupName);
                Assert.Equal(0, listLoadBalancer.Count());

                // Delete all NetworkInterfaces
                networkManagementClient.NetworkInterfaces.Delete(resourceGroupName, nic1name);
                networkManagementClient.NetworkInterfaces.Delete(resourceGroupName, nic2name);
                networkManagementClient.NetworkInterfaces.Delete(resourceGroupName, nic3name);

                // Delete all PublicIPAddresses
                networkManagementClient.PublicIPAddresses.Delete(resourceGroupName, lbPublicIpName);
            }
        }
        public void NetworkSecurityGroupApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/networkSecurityGroups");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                string networkSecurityGroupName = TestUtilities.GenerateName();

                var networkSecurityGroup = new NetworkSecurityGroup()
                {
                    Location = location,
                };

                // Put Nsg
                var putNsgResponse = networkManagementClient.NetworkSecurityGroups.CreateOrUpdate(resourceGroupName, networkSecurityGroupName, networkSecurityGroup);
                Assert.Equal("Succeeded", putNsgResponse.ProvisioningState);

                // Get NSG
                var getNsgResponse = networkManagementClient.NetworkSecurityGroups.Get(resourceGroupName, networkSecurityGroupName);
                Assert.Equal(networkSecurityGroupName, getNsgResponse.Name);
                Assert.NotNull(getNsgResponse.ResourceGuid);
                Assert.Equal(6, getNsgResponse.DefaultSecurityRules.Count);
                Assert.Equal("AllowVnetInBound", getNsgResponse.DefaultSecurityRules[0].Name);
                Assert.Equal("AllowAzureLoadBalancerInBound", getNsgResponse.DefaultSecurityRules[1].Name);
                Assert.Equal("DenyAllInBound", getNsgResponse.DefaultSecurityRules[2].Name);
                Assert.Equal("AllowVnetOutBound", getNsgResponse.DefaultSecurityRules[3].Name);
                Assert.Equal("AllowInternetOutBound", getNsgResponse.DefaultSecurityRules[4].Name);
                Assert.Equal("DenyAllOutBound", getNsgResponse.DefaultSecurityRules[5].Name);

                // Verify a default security rule
                Assert.Equal(SecurityRuleAccess.Allow, getNsgResponse.DefaultSecurityRules[0].Access);
                Assert.Equal("Allow inbound traffic from all VMs in VNET", getNsgResponse.DefaultSecurityRules[0].Description);
                Assert.Equal("VirtualNetwork", getNsgResponse.DefaultSecurityRules[0].DestinationAddressPrefix);
                Assert.Equal("*", getNsgResponse.DefaultSecurityRules[0].DestinationPortRange);
                Assert.Equal(SecurityRuleDirection.Inbound, getNsgResponse.DefaultSecurityRules[0].Direction);
                Assert.Equal(65000, getNsgResponse.DefaultSecurityRules[0].Priority);
                Assert.Equal(SecurityRuleProtocol.Asterisk, getNsgResponse.DefaultSecurityRules[0].Protocol);
                Assert.Equal("Succeeded", getNsgResponse.DefaultSecurityRules[0].ProvisioningState);
                Assert.Equal("VirtualNetwork", getNsgResponse.DefaultSecurityRules[0].SourceAddressPrefix);
                Assert.Equal("*", getNsgResponse.DefaultSecurityRules[0].SourcePortRange);

                // List NSG
                var listNsgResponse = networkManagementClient.NetworkSecurityGroups.List(resourceGroupName);
                Assert.Single(listNsgResponse);
                Assert.Equal(networkSecurityGroupName, listNsgResponse.First().Name);
                Assert.Equal(6, listNsgResponse.First().DefaultSecurityRules.Count);
                Assert.Equal("AllowVnetInBound", listNsgResponse.First().DefaultSecurityRules[0].Name);
                Assert.Equal("AllowAzureLoadBalancerInBound", listNsgResponse.First().DefaultSecurityRules[1].Name);
                Assert.Equal("DenyAllInBound", listNsgResponse.First().DefaultSecurityRules[2].Name);
                Assert.Equal("AllowVnetOutBound", listNsgResponse.First().DefaultSecurityRules[3].Name);
                Assert.Equal("AllowInternetOutBound", listNsgResponse.First().DefaultSecurityRules[4].Name);
                Assert.Equal("DenyAllOutBound", listNsgResponse.First().DefaultSecurityRules[5].Name);
                Assert.Equal(getNsgResponse.Etag, listNsgResponse.First().Etag);

                // List NSG in a subscription
                var listNsgSubsciptionResponse = networkManagementClient.NetworkSecurityGroups.ListAll();
                Assert.NotEmpty(listNsgSubsciptionResponse);

                // Delete NSG
                networkManagementClient.NetworkSecurityGroups.Delete(resourceGroupName, networkSecurityGroupName);

                // List NSG
                listNsgResponse = networkManagementClient.NetworkSecurityGroups.List(resourceGroupName);
                Assert.Empty(listNsgResponse);
            }
        }
        public void NetworkSecurityGroupWithRulesApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/networkSecurityGroups");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                string networkSecurityGroupName = TestUtilities.GenerateName();
                string securityRule1            = TestUtilities.GenerateName();
                string securityRule2            = TestUtilities.GenerateName();

                string destinationPortRange = "123-3500";

                var networkSecurityGroup = new NetworkSecurityGroup()
                {
                    Location      = location,
                    SecurityRules = new List <SecurityRule>()
                    {
                        new SecurityRule()
                        {
                            Name        = securityRule1,
                            Access      = SecurityRuleAccess.Allow,
                            Description = "Test security rule",
                            DestinationAddressPrefix = "*",
                            DestinationPortRange     = destinationPortRange,
                            Direction           = SecurityRuleDirection.Inbound,
                            Priority            = 500,
                            Protocol            = SecurityRuleProtocol.Tcp,
                            SourceAddressPrefix = "*",
                            SourcePortRange     = "655"
                        }
                    }
                };

                // Put Nsg
                var putNsgResponse = networkManagementClient.NetworkSecurityGroups.CreateOrUpdate(resourceGroupName, networkSecurityGroupName, networkSecurityGroup);
                Assert.Equal("Succeeded", putNsgResponse.ProvisioningState);

                // Get NSG
                var getNsgResponse = networkManagementClient.NetworkSecurityGroups.Get(resourceGroupName, networkSecurityGroupName);
                Assert.Equal(networkSecurityGroupName, getNsgResponse.Name);
                Assert.Equal(6, getNsgResponse.DefaultSecurityRules.Count);
                Assert.Equal("AllowVnetInBound", getNsgResponse.DefaultSecurityRules[0].Name);
                Assert.Equal("AllowAzureLoadBalancerInBound", getNsgResponse.DefaultSecurityRules[1].Name);
                Assert.Equal("DenyAllInBound", getNsgResponse.DefaultSecurityRules[2].Name);
                Assert.Equal("AllowVnetOutBound", getNsgResponse.DefaultSecurityRules[3].Name);
                Assert.Equal("AllowInternetOutBound", getNsgResponse.DefaultSecurityRules[4].Name);
                Assert.Equal("DenyAllOutBound", getNsgResponse.DefaultSecurityRules[5].Name);

                // Verify the security rule
                Assert.Equal(SecurityRuleAccess.Allow, getNsgResponse.SecurityRules[0].Access);
                Assert.Equal("Test security rule", getNsgResponse.SecurityRules[0].Description);
                Assert.Equal("*", getNsgResponse.SecurityRules[0].DestinationAddressPrefix);
                Assert.Equal(destinationPortRange, getNsgResponse.SecurityRules[0].DestinationPortRange);
                Assert.Equal(SecurityRuleDirection.Inbound, getNsgResponse.SecurityRules[0].Direction);
                Assert.Equal(500, getNsgResponse.SecurityRules[0].Priority);
                Assert.Equal(SecurityRuleProtocol.Tcp, getNsgResponse.SecurityRules[0].Protocol);
                Assert.Equal("Succeeded", getNsgResponse.SecurityRules[0].ProvisioningState);
                Assert.Equal("*", getNsgResponse.SecurityRules[0].SourceAddressPrefix);
                Assert.Equal("655", getNsgResponse.SecurityRules[0].SourcePortRange);

                // List NSG
                var listNsgResponse = networkManagementClient.NetworkSecurityGroups.List(resourceGroupName);
                Assert.Single(listNsgResponse);
                Assert.Equal(networkSecurityGroupName, listNsgResponse.First().Name);
                Assert.Equal(6, listNsgResponse.First().DefaultSecurityRules.Count);
                Assert.Equal("AllowVnetInBound", listNsgResponse.First().DefaultSecurityRules[0].Name);
                Assert.Equal("AllowAzureLoadBalancerInBound", listNsgResponse.First().DefaultSecurityRules[1].Name);
                Assert.Equal("DenyAllInBound", listNsgResponse.First().DefaultSecurityRules[2].Name);
                Assert.Equal("AllowVnetOutBound", listNsgResponse.First().DefaultSecurityRules[3].Name);
                Assert.Equal("AllowInternetOutBound", listNsgResponse.First().DefaultSecurityRules[4].Name);
                Assert.Equal("DenyAllOutBound", listNsgResponse.First().DefaultSecurityRules[5].Name);
                Assert.Equal(getNsgResponse.Etag, listNsgResponse.First().Etag);

                // List NSG in a subscription
                var listNsgSubsciptionResponse = networkManagementClient.NetworkSecurityGroups.ListAll();
                Assert.NotEmpty(listNsgSubsciptionResponse);

                // Add a new security rule
                var SecurityRule = new SecurityRule()
                {
                    Name        = securityRule2,
                    Access      = SecurityRuleAccess.Deny,
                    Description = "Test outbound security rule",
                    DestinationAddressPrefix = "*",
                    DestinationPortRange     = destinationPortRange,
                    Direction           = SecurityRuleDirection.Outbound,
                    Priority            = 501,
                    Protocol            = SecurityRuleProtocol.Udp,
                    SourceAddressPrefix = "*",
                    SourcePortRange     = "656",
                };

                networkSecurityGroup.SecurityRules.Add(SecurityRule);

                putNsgResponse = networkManagementClient.NetworkSecurityGroups.CreateOrUpdate(resourceGroupName, networkSecurityGroupName, networkSecurityGroup);

                // Get NSG
                getNsgResponse = networkManagementClient.NetworkSecurityGroups.Get(resourceGroupName, networkSecurityGroupName);

                // Verify the security rule
                Assert.Equal(SecurityRuleAccess.Deny, getNsgResponse.SecurityRules[1].Access);
                Assert.Equal("Test outbound security rule", getNsgResponse.SecurityRules[1].Description);
                Assert.Equal("*", getNsgResponse.SecurityRules[1].DestinationAddressPrefix);
                Assert.Equal(destinationPortRange, getNsgResponse.SecurityRules[1].DestinationPortRange);
                Assert.Equal(SecurityRuleDirection.Outbound, getNsgResponse.SecurityRules[1].Direction);
                Assert.Equal(501, getNsgResponse.SecurityRules[1].Priority);
                Assert.Equal(SecurityRuleProtocol.Udp, getNsgResponse.SecurityRules[1].Protocol);
                Assert.Equal("Succeeded", getNsgResponse.SecurityRules[1].ProvisioningState);
                Assert.Equal("*", getNsgResponse.SecurityRules[1].SourceAddressPrefix);
                Assert.Equal("656", getNsgResponse.SecurityRules[1].SourcePortRange);

                // List Default Security Groups
                var listDefaultSecurityGroups = networkManagementClient.DefaultSecurityRules.List(resourceGroupName, networkSecurityGroupName);
                Assert.NotEmpty(listDefaultSecurityGroups);

                // Get Defaul Security Group
                var getDefaultSecurityGroups = networkManagementClient.DefaultSecurityRules.Get(resourceGroupName, networkSecurityGroupName, listDefaultSecurityGroups.First().Name);
                Assert.Equal(listDefaultSecurityGroups.First().Name, getDefaultSecurityGroups.Name);

                // Delete NSG
                networkManagementClient.NetworkSecurityGroups.Delete(resourceGroupName, networkSecurityGroupName);

                // List NSG
                listNsgResponse = networkManagementClient.NetworkSecurityGroups.List(resourceGroupName);
                Assert.Empty(listNsgResponse);
            }
        }
Esempio n. 17
0
        public void SecurityRuleWithRulesApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/networkSecurityGroups");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                string networkSecurityGroupName = TestUtilities.GenerateName();
                string securityRule1            = TestUtilities.GenerateName();
                string securityRule2            = TestUtilities.GenerateName();

                string destinationPortRange = "123-3500";

                var networkSecurityGroup = new NetworkSecurityGroup()
                {
                    Location      = location,
                    SecurityRules = new List <SecurityRule>()
                    {
                        new SecurityRule()
                        {
                            Name        = securityRule1,
                            Access      = SecurityRuleAccess.Allow,
                            Description = "Test security rule",
                            DestinationAddressPrefix = "*",
                            DestinationPortRange     = destinationPortRange,
                            Direction           = SecurityRuleDirection.Inbound,
                            Priority            = 500,
                            Protocol            = SecurityRuleProtocol.Tcp,
                            SourceAddressPrefix = "*",
                            SourcePortRange     = "655"
                        }
                    }
                };

                // Put Nsg
                var putNsgResponse = networkManagementClient.NetworkSecurityGroups.CreateOrUpdate(resourceGroupName, networkSecurityGroupName, networkSecurityGroup);
                Assert.Equal("Succeeded", putNsgResponse.ProvisioningState);

                // Get NSG
                var getNsgResponse = networkManagementClient.NetworkSecurityGroups.Get(resourceGroupName, networkSecurityGroupName);
                Assert.Equal(networkSecurityGroupName, getNsgResponse.Name);

                // Get SecurityRule
                var getSecurityRuleResponse = networkManagementClient.SecurityRules.Get(resourceGroupName, networkSecurityGroupName, securityRule1);
                Assert.Equal(securityRule1, getSecurityRuleResponse.Name);

                this.CompareSecurityRule(getNsgResponse.SecurityRules[0], getSecurityRuleResponse);

                // Add a new security rule
                var SecurityRule = new SecurityRule()
                {
                    Name        = securityRule2,
                    Access      = SecurityRuleAccess.Deny,
                    Description = "Test outbound security rule",
                    DestinationAddressPrefix = "*",
                    DestinationPortRange     = destinationPortRange,
                    Direction           = SecurityRuleDirection.Outbound,
                    Priority            = 501,
                    Protocol            = SecurityRuleProtocol.Udp,
                    SourceAddressPrefix = "*",
                    SourcePortRange     = "656",
                };

                var putSecurityRuleResponse = networkManagementClient.SecurityRules.CreateOrUpdate(resourceGroupName, networkSecurityGroupName, securityRule2, SecurityRule);
                Assert.Equal("Succeeded", putSecurityRuleResponse.ProvisioningState);

                // Get NSG
                getNsgResponse = networkManagementClient.NetworkSecurityGroups.Get(resourceGroupName, networkSecurityGroupName);

                // Get the SecurityRule2
                var getSecurityRule2Response = networkManagementClient.SecurityRules.Get(resourceGroupName, networkSecurityGroupName, securityRule2);
                Assert.Equal(securityRule2, getSecurityRule2Response.Name);

                // Verify the security rule
                Assert.Equal(SecurityRuleAccess.Deny, getSecurityRule2Response.Access);
                Assert.Equal("Test outbound security rule", getSecurityRule2Response.Description);
                Assert.Equal("*", getSecurityRule2Response.DestinationAddressPrefix);
                Assert.Equal(destinationPortRange, getSecurityRule2Response.DestinationPortRange);
                Assert.Equal(SecurityRuleDirection.Outbound, getSecurityRule2Response.Direction);
                Assert.Equal(501, getSecurityRule2Response.Priority);
                Assert.Equal(SecurityRuleProtocol.Udp, getSecurityRule2Response.Protocol);
                Assert.Equal("Succeeded", getSecurityRule2Response.ProvisioningState);
                Assert.Equal("*", getSecurityRule2Response.SourceAddressPrefix);
                Assert.Equal("656", getSecurityRule2Response.SourcePortRange);

                this.CompareSecurityRule(getNsgResponse.SecurityRules[1], getSecurityRule2Response);

                // List all SecurityRules
                var getsecurityRules = networkManagementClient.SecurityRules.List(resourceGroupName, networkSecurityGroupName);

                Assert.Equal(2, getsecurityRules.Count());
                this.CompareSecurityRule(getNsgResponse.SecurityRules[0], getsecurityRules.ElementAt(0));
                this.CompareSecurityRule(getNsgResponse.SecurityRules[1], getsecurityRules.ElementAt(1));

                // Delete a SecurityRule
                networkManagementClient.SecurityRules.Delete(resourceGroupName, networkSecurityGroupName, securityRule2);

                getsecurityRules = networkManagementClient.SecurityRules.List(resourceGroupName, networkSecurityGroupName);

                Assert.Single(getsecurityRules);

                // Delete NSG
                networkManagementClient.NetworkSecurityGroups.Delete(resourceGroupName, networkSecurityGroupName);

                // List NSG
                var listNsgResponse = networkManagementClient.NetworkSecurityGroups.List(resourceGroupName);
                Assert.Empty(listNsgResponse);
            }
        }
Esempio n. 18
0
        public void DeleteConnectionMonitorTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler3 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1, true);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);
                var computeManagementClient = NetworkManagementTestUtilities.GetComputeManagementClientWithHandler(context, handler3);

                string location = "centraluseuap";

                string resourceGroupName = TestUtilities.GenerateName();
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                string virtualMachineName       = TestUtilities.GenerateName();
                string networkInterfaceName     = TestUtilities.GenerateName();
                string networkSecurityGroupName = virtualMachineName + "-nsg";

                //Deploy VM with a template
                DeploymentUpdate.CreateVm(
                    resourcesClient: resourcesClient,
                    resourceGroupName: resourceGroupName,
                    location: location,
                    virtualMachineName: virtualMachineName,
                    storageAccountName: TestUtilities.GenerateName(),
                    networkInterfaceName: networkInterfaceName,
                    networkSecurityGroupName: networkSecurityGroupName,
                    diagnosticsStorageAccountName: TestUtilities.GenerateName(),
                    deploymentName: TestUtilities.GenerateName()
                    );

                var getVm = computeManagementClient.VirtualMachines.Get(resourceGroupName, virtualMachineName);

                //Deploy networkWatcherAgent on VM
                VirtualMachineExtension parameters = new VirtualMachineExtension
                {
                    Publisher                   = "Microsoft.Azure.NetworkWatcher",
                    TypeHandlerVersion          = "1.4",
                    VirtualMachineExtensionType = "NetworkWatcherAgentWindows",
                    Location = location
                };

                var addExtension = computeManagementClient.VirtualMachineExtensions.CreateOrUpdate(resourceGroupName, getVm.Name, "NetworkWatcherAgent", parameters);

                string         networkWatcherName = TestUtilities.GenerateName();
                NetworkWatcher properties         = new NetworkWatcher
                {
                    Location = location
                };

                //Create network Watcher
                var createNetworkWatcher = networkManagementClient.NetworkWatchers.CreateOrUpdate(resourceGroupName, networkWatcherName, properties);

                string            connectionMonitorName1 = TestUtilities.GenerateName();
                string            connectionMonitorName2 = TestUtilities.GenerateName();
                ConnectionMonitor cm = new ConnectionMonitor
                {
                    Location = location,
                    Source   = new ConnectionMonitorSource
                    {
                        ResourceId = getVm.Id
                    },
                    Destination = new ConnectionMonitorDestination
                    {
                        Address = "bing.com",
                        Port    = 80
                    },
                    MonitoringIntervalInSeconds = 30,
                    AutoStart = false
                };

                var connectionMonitor1 = networkManagementClient.ConnectionMonitors.CreateOrUpdate(resourceGroupName, networkWatcherName, connectionMonitorName1, cm);
                var connectionMonitor2 = networkManagementClient.ConnectionMonitors.CreateOrUpdate(resourceGroupName, networkWatcherName, connectionMonitorName2, cm);

                var getConnectionMonitors1 = networkManagementClient.ConnectionMonitors.List(resourceGroupName, networkWatcherName);
                Assert.Equal(2, getConnectionMonitors1.Count());

                networkManagementClient.ConnectionMonitors.Delete(resourceGroupName, networkWatcherName, connectionMonitorName2);
                var getConnectionMonitors2 = networkManagementClient.ConnectionMonitors.List(resourceGroupName, networkWatcherName);
                Assert.Single(getConnectionMonitors2);
            }
        }
        public void PublicIpAddressApiTestWithIdletTimeoutAndReverseFqdn()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                var location = ResourcesManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/publicIPAddresses");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                // Create the parameter for PUT PublicIPAddress
                string publicIpName    = TestUtilities.GenerateName();
                string domainNameLabel = TestUtilities.GenerateName();
                string reverseFqdn;

                var publicIp = new PublicIPAddress()
                {
                    Location = location,
                    Tags     = new Dictionary <string, string>()
                    {
                        { "key", "value" }
                    },
                    PublicIPAllocationMethod = IPAllocationMethod.Dynamic,
                    DnsSettings = new PublicIPAddressDnsSettings()
                    {
                        DomainNameLabel = domainNameLabel,
                    },
                    IdleTimeoutInMinutes = 16,
                };

                // Put PublicIPAddress
                var putPublicIpAddressResponse = networkManagementClient.PublicIPAddresses.CreateOrUpdate(resourceGroupName, publicIpName, publicIp);
                Assert.Equal("Succeeded", putPublicIpAddressResponse.ProvisioningState);

                // Get PublicIPAddress
                var getPublicIpAddressResponse = networkManagementClient.PublicIPAddresses.Get(resourceGroupName, publicIpName);

                // Add Reverse FQDN
                reverseFqdn = getPublicIpAddressResponse.DnsSettings.Fqdn;
                getPublicIpAddressResponse.DnsSettings.ReverseFqdn = reverseFqdn;

                putPublicIpAddressResponse = networkManagementClient.PublicIPAddresses.CreateOrUpdate(resourceGroupName, publicIpName, getPublicIpAddressResponse);
                Assert.Equal("Succeeded", putPublicIpAddressResponse.ProvisioningState);

                // Get PublicIPAddress
                getPublicIpAddressResponse = networkManagementClient.PublicIPAddresses.Get(resourceGroupName, publicIpName);
                Assert.Equal(16, getPublicIpAddressResponse.IdleTimeoutInMinutes);
                Assert.Equal(reverseFqdn, getPublicIpAddressResponse.DnsSettings.ReverseFqdn);

                // Get List of PublicIPAddress
                var getPublicIpAddressListResponse = networkManagementClient.PublicIPAddresses.List(resourceGroupName);
                Assert.Single(getPublicIpAddressListResponse);
                ArePublicIpAddressesEqual(getPublicIpAddressResponse, getPublicIpAddressListResponse.First());

                // Get List of PublicIPAddress in a subscription
                var getPublicIpAddressListSubscriptionResponse = networkManagementClient.PublicIPAddresses.ListAll();
                Assert.NotEmpty(getPublicIpAddressListSubscriptionResponse);

                // Delete PublicIPAddress
                networkManagementClient.PublicIPAddresses.Delete(resourceGroupName, publicIpName);

                // Get PublicIPAddress
                getPublicIpAddressListResponse = networkManagementClient.PublicIPAddresses.List(resourceGroupName);
                Assert.Empty(getPublicIpAddressListResponse);
            }
        }
Esempio n. 20
0
        public void ExpressRouteCircuitWithPeeringApiTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var resourcesClient = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(handler);
                var networkResourceProviderClient = NetworkManagementTestUtilities.GetNetworkResourceProviderClient(handler);
                // var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/expressRouteCircuits");

                var location = "brazilsouth";

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(
                    resourceGroupName,
                    new ResourceGroup
                {
                    Location = location
                });

                string circuitName = TestUtilities.GenerateName();

                var circuit = new ExpressRouteCircuit()
                {
                    Location = location,
                    Sku      =
                        new ExpressRouteCircuitSku()
                    {
                        Name   = "Standard_MeteredData",
                        Tier   = ExpressRouteCircuitSkuTier.Standard,
                        Family = ExpressRouteCircuitSkuFamily.MeteredData
                    },
                    BillingType = ExpressRouteCircuitBillingType.ServiceProviderType,
                    ServiceProviderProperties =
                        new ExpressRouteCircuitServiceProviderProperties()
                    {
                        ServiceProviderName = "Equinix",
                        PeeringLocation     = "Silicon Valley",
                        BandwidthInMbps     = 1000
                    },
                    Peerings = new List <ExpressRouteCircuitPeering>()
                    {
                        new ExpressRouteCircuitPeering()
                        {
                            Name        = "AzurePrivatePeering",
                            PeeringType = ExpressRouteCircuitPeeringType.AzurePrivatePeering,
                            PeerASN     = 100,
                            PrimaryPeerAddressPrefix   = "192.168.1.0/30",
                            SecondaryPeerAddressPrefix = "192.168.2.0/30",
                            VlanId = 200
                        }
                    }
                };

                // Create the circuit
                var putCircuit = networkResourceProviderClient.ExpressRouteCircuits.CreateOrUpdate(resourceGroupName, circuitName, circuit);
                Assert.Equal(HttpStatusCode.OK, putCircuit.StatusCode);

                var getCircuit = networkResourceProviderClient.ExpressRouteCircuits.Get(resourceGroupName, circuitName);

                // Verify properties
                Assert.Equal(HttpStatusCode.OK, getCircuit.StatusCode);

                Assert.NotNull(getCircuit.ExpressRouteCircuit);
                Assert.NotNull(getCircuit.ExpressRouteCircuit.Sku);
                Assert.Equal("Standard_MeteredData", getCircuit.ExpressRouteCircuit.Sku.Name);
                Assert.Equal(ExpressRouteCircuitSkuTier.Standard, getCircuit.ExpressRouteCircuit.Sku.Tier);
                Assert.Equal(ExpressRouteCircuitSkuFamily.MeteredData, getCircuit.ExpressRouteCircuit.Sku.Family);

                Assert.NotNull(getCircuit.ExpressRouteCircuit.ServiceProviderProperties);
                Assert.Equal("Equinix", getCircuit.ExpressRouteCircuit.ServiceProviderProperties.ServiceProviderName);
                Assert.Equal("Silicon Valley", getCircuit.ExpressRouteCircuit.ServiceProviderProperties.PeeringLocation);
                Assert.Equal(1000, getCircuit.ExpressRouteCircuit.ServiceProviderProperties.BandwidthInMbps);

                // Verify peering
                Assert.NotNull(getCircuit.ExpressRouteCircuit.Peerings);
                Assert.Equal(1, getCircuit.ExpressRouteCircuit.Peerings.Count);
                Assert.Equal("AzurePrivatePeering", getCircuit.ExpressRouteCircuit.Peerings[0].Name);
                Assert.Equal(ExpressRouteCircuitPeeringType.AzurePrivatePeering, getCircuit.ExpressRouteCircuit.Peerings[0].PeeringType);
                Assert.Equal(100, getCircuit.ExpressRouteCircuit.Peerings[0].PeerASN);
                Assert.Equal("192.168.1.0/30", getCircuit.ExpressRouteCircuit.Peerings[0].PrimaryPeerAddressPrefix);
                Assert.Equal("192.168.2.0/30", getCircuit.ExpressRouteCircuit.Peerings[0].SecondaryPeerAddressPrefix);
                Assert.Equal(200, getCircuit.ExpressRouteCircuit.Peerings[0].VlanId);

                // Get peering alone
                var getPeering = networkResourceProviderClient.ExpressRouteCircuitPeerings.Get(resourceGroupName, circuitName, "AzurePrivatePeering");
                Assert.Equal(HttpStatusCode.OK, getPeering.StatusCode);
                Assert.Equal("AzurePrivatePeering", getPeering.Peering.Name);
                Assert.Equal(ExpressRouteCircuitPeeringType.AzurePrivatePeering, getPeering.Peering.PeeringType);
                Assert.Equal(100, getPeering.Peering.PeerASN);
                Assert.Equal("192.168.1.0/30", getPeering.Peering.PrimaryPeerAddressPrefix);
                Assert.Equal("192.168.2.0/30", getPeering.Peering.SecondaryPeerAddressPrefix);
                Assert.Equal(200, getPeering.Peering.VlanId);
                Assert.Null(getPeering.Peering.MicrosoftPeeringConfig);

                // list all peerings
                var listPeering = networkResourceProviderClient.ExpressRouteCircuitPeerings.List(resourceGroupName, circuitName);
                Assert.Equal(HttpStatusCode.OK, listPeering.StatusCode);
                Assert.Equal(1, listPeering.Peerings.Count);
                Assert.Equal("AzurePrivatePeering", listPeering.Peerings[0].Name);
                Assert.Equal(ExpressRouteCircuitPeeringType.AzurePrivatePeering, listPeering.Peerings[0].PeeringType);
                Assert.Equal(100, listPeering.Peerings[0].PeerASN);
                Assert.Equal("192.168.1.0/30", listPeering.Peerings[0].PrimaryPeerAddressPrefix);
                Assert.Equal("192.168.2.0/30", listPeering.Peerings[0].SecondaryPeerAddressPrefix);
                Assert.Equal(200, listPeering.Peerings[0].VlanId);

                // Add a new peering
                var peering = new ExpressRouteCircuitPeering()
                {
                    PeeringType = ExpressRouteCircuitPeeringType.MicrosoftPeering,
                    PeerASN     = 100,
                    PrimaryPeerAddressPrefix   = "192.168.1.0/30",
                    SecondaryPeerAddressPrefix = "192.168.2.0/30",
                    VlanId = 200,
                    MicrosoftPeeringConfig = new ExpressRouteCircuitPeeringConfig()
                    {
                        AdvertisedPublicPrefixes = new List <string>()
                        {
                            "11.2.3.4/30", "12.2.3.4/30"
                        },
                        CustomerASN         = 1000,
                        RoutingRegistryName = "AFRINIC"
                    }
                };

                var putPeering = networkResourceProviderClient.ExpressRouteCircuitPeerings.CreateOrUpdate(resourceGroupName, circuitName, "MicrosoftPeering", peering);
                Assert.Equal(HttpStatusCode.OK, putPeering.StatusCode);

                getPeering = networkResourceProviderClient.ExpressRouteCircuitPeerings.Get(resourceGroupName, circuitName, "MicrosoftPeering");
                Assert.Equal(HttpStatusCode.OK, getPeering.StatusCode);

                Assert.Equal("MicrosoftPeering", getPeering.Peering.Name);
                Assert.Equal(ExpressRouteCircuitPeeringType.MicrosoftPeering, getPeering.Peering.PeeringType);
                Assert.Equal(100, getPeering.Peering.PeerASN);
                Assert.Equal("192.168.1.0/30", getPeering.Peering.PrimaryPeerAddressPrefix);
                Assert.Equal("192.168.2.0/30", getPeering.Peering.SecondaryPeerAddressPrefix);
                Assert.Equal(200, getPeering.Peering.VlanId);
                Assert.NotNull(getPeering.Peering.MicrosoftPeeringConfig);
                Assert.Equal(2, getPeering.Peering.MicrosoftPeeringConfig.AdvertisedPublicPrefixes.Count);
                Assert.NotNull(getPeering.Peering.MicrosoftPeeringConfig.AdvertisedPublicPrefixesState);
                Assert.Equal(1000, getPeering.Peering.MicrosoftPeeringConfig.CustomerASN);
                Assert.Equal("AFRINIC", getPeering.Peering.MicrosoftPeeringConfig.RoutingRegistryName);

                listPeering = networkResourceProviderClient.ExpressRouteCircuitPeerings.List(resourceGroupName, circuitName);
                Assert.Equal(HttpStatusCode.OK, listPeering.StatusCode);
                Assert.Equal(2, listPeering.Peerings.Count);

                // Delete Peering
                var deletePeering = networkResourceProviderClient.ExpressRouteCircuitPeerings.Delete(resourceGroupName, circuitName, "MicrosoftPeering");
                Assert.Equal(HttpStatusCode.OK, deletePeering.StatusCode);

                listPeering = networkResourceProviderClient.ExpressRouteCircuitPeerings.List(resourceGroupName, circuitName);
                Assert.Equal(HttpStatusCode.OK, listPeering.StatusCode);
                Assert.Equal(1, listPeering.Peerings.Count);

                // Verify Delete circuit
                var deleteCircuit = networkResourceProviderClient.ExpressRouteCircuits.Delete(resourceGroupName, circuitName);
                Assert.Equal(HttpStatusCode.OK, deleteCircuit.StatusCode);

                var listCircuit = networkResourceProviderClient.ExpressRouteCircuits.List(resourceGroupName);
                Assert.Equal(0, listCircuit.ExpressRouteCircuits.Count);
            }
        }
        public void EmptyRouteTableTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/routeTables");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(
                    resourceGroupName,
                    new ResourceGroup {
                    Location = location
                });

                string routeTableName = TestUtilities.GenerateName();

                var routeTable = new RouteTable()
                {
                    Location = location,
                };

                // Put RouteTable
                var putRouteTableResponse =
                    networkManagementClient.RouteTables.CreateOrUpdate(
                        resourceGroupName,
                        routeTableName,
                        routeTable);

                Assert.Equal("Succeeded", putRouteTableResponse.ProvisioningState);

                // Get RouteTable
                var getRouteTableResponse = networkManagementClient.RouteTables.Get(
                    resourceGroupName,
                    routeTableName);

                Assert.Equal(routeTableName, getRouteTableResponse.Name);
                Assert.False(getRouteTableResponse.Routes.Any());

                // List RouteTable
                var listRouteTableResponse = networkManagementClient.RouteTables.List(resourceGroupName);

                Assert.Single(listRouteTableResponse);
                Assert.Equal(getRouteTableResponse.Name, listRouteTableResponse.First().Name);
                Assert.Equal(getRouteTableResponse.Id, listRouteTableResponse.First().Id);

                // Delete RouteTable
                networkManagementClient.RouteTables.Delete(resourceGroupName, routeTableName);

                // Verify delete
                listRouteTableResponse = networkManagementClient.RouteTables.List(resourceGroupName);

                Assert.Empty(listRouteTableResponse);
            }
        }
Esempio n. 22
0
        public void TroubleshootApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler3 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler4 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);
                var computeManagementClient = NetworkManagementTestUtilities.GetComputeManagementClientWithHandler(context, handler3);
                var storageManagementClient = NetworkManagementTestUtilities.GetStorageManagementClientWithHandler(context, handler4);

                string location = "westcentralus";

                string resourceGroupName = TestUtilities.GenerateName();
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                // CreateVirtualNetworkGateway API
                // Prerequisite:- Create PublicIPAddress(Gateway Ip) using Put PublicIPAddress API
                string publicIpName    = TestUtilities.GenerateName();
                string domainNameLabel = TestUtilities.GenerateName();

                var nic1publicIp = TestHelper.CreateDefaultPublicIpAddress(publicIpName, resourceGroupName, domainNameLabel, location, networkManagementClient);

                //Prerequisite:-Create Virtual Network using Put VirtualNetwork API
                string vnetName   = TestUtilities.GenerateName();
                string subnetName = "GatewaySubnet";

                var virtualNetwork = TestHelper.CreateVirtualNetwork(vnetName, subnetName, resourceGroupName, location, networkManagementClient);

                var getSubnetResponse = networkManagementClient.Subnets.Get(resourceGroupName, vnetName, subnetName);

                // CreateVirtualNetworkGateway API
                string virtualNetworkGatewayName = TestUtilities.GenerateName();
                string ipConfigName = TestUtilities.GenerateName();

                var virtualNetworkGateway = new VirtualNetworkGateway()
                {
                    Location = location,
                    Tags     = new Dictionary <string, string>()
                    {
                        { "key", "value" }
                    },
                    EnableBgp          = false,
                    GatewayDefaultSite = null,
                    GatewayType        = VirtualNetworkGatewayType.Vpn,
                    VpnType            = VpnType.RouteBased,
                    IpConfigurations   = new List <VirtualNetworkGatewayIPConfiguration>()
                    {
                        new VirtualNetworkGatewayIPConfiguration()
                        {
                            Name = ipConfigName,
                            PrivateIPAllocationMethod = IPAllocationMethod.Dynamic,
                            PublicIPAddress           = new SubResource()
                            {
                                Id = nic1publicIp.Id
                            },
                            Subnet = new SubResource()
                            {
                                Id = getSubnetResponse.Id
                            }
                        }
                    },
                    Sku = new VirtualNetworkGatewaySku()
                    {
                        Name = VirtualNetworkGatewaySkuName.Basic,
                        Tier = VirtualNetworkGatewaySkuTier.Basic
                    }
                };

                var putVirtualNetworkGatewayResponse = networkManagementClient.VirtualNetworkGateways.CreateOrUpdate(resourceGroupName, virtualNetworkGatewayName, virtualNetworkGateway);

                // GetVirtualNetworkGateway API
                var getVirtualNetworkGatewayResponse = networkManagementClient.VirtualNetworkGateways.Get(resourceGroupName, virtualNetworkGatewayName);

                string         networkWatcherName = TestUtilities.GenerateName();
                NetworkWatcher properties         = new NetworkWatcher();
                properties.Location = location;

                //Create network Watcher
                var createNetworkWatcher = networkManagementClient.NetworkWatchers.CreateOrUpdate(resourceGroupName, networkWatcherName, properties);


                //Create storage
                string storageName = TestUtilities.GenerateName();

                var storageParameters = new StorageAccountCreateParameters()
                {
                    Location = location,
                    Kind     = Kind.Storage,
                    Sku      = new Sku
                    {
                        Name = SkuName.StandardLRS
                    }
                };

                var account = storageManagementClient.StorageAccounts.Create(resourceGroupName, storageName, storageParameters);

                TroubleshootingParameters parameters = new TroubleshootingParameters()
                {
                    TargetResourceId = getVirtualNetworkGatewayResponse.Id,
                    StorageId        = account.Id,
                    StoragePath      = "https://nwtestdbdzq4xsvskrei6.blob.core.windows.net/vhds",
                };

                //Get troubleshooting
                var troubleshoot = networkManagementClient.NetworkWatchers.GetTroubleshooting(resourceGroupName, networkWatcherName, parameters);

                QueryTroubleshootingParameters qParameters = new QueryTroubleshootingParameters()
                {
                    TargetResourceId = getVirtualNetworkGatewayResponse.Id
                };

                //Query last troubleshoot
                var queryTroubleshoot = networkManagementClient.NetworkWatchers.GetTroubleshootingResult(resourceGroupName, networkWatcherName, qParameters);

                //TO DO: make verification once fixed for troubleshoot API deployed
            }
        }
        public void ViewNsgRuleApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler3 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);
                var computeManagementClient = NetworkManagementTestUtilities.GetComputeManagementClientWithHandler(context, handler3);

                string location = "westcentralus";

                string resourceGroupName = TestUtilities.GenerateName();
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                string virtualMachineName       = TestUtilities.GenerateName();
                string networkInterfaceName     = TestUtilities.GenerateName();
                string networkSecurityGroupName = virtualMachineName + "-nsg";

                //Deploy VM with template
                Deployments.CreateVm(
                    resourcesClient: resourcesClient,
                    resourceGroupName: resourceGroupName,
                    location: location,
                    virtualMachineName: virtualMachineName,
                    storageAccountName: TestUtilities.GenerateName(),
                    networkInterfaceName: networkInterfaceName,
                    networkSecurityGroupName: networkSecurityGroupName,
                    diagnosticsStorageAccountName: TestUtilities.GenerateName(),
                    deploymentName: TestUtilities.GenerateName()
                    );

                string         networkWatcherName = TestUtilities.GenerateName();
                NetworkWatcher properties         = new NetworkWatcher();
                properties.Location = location;

                //Create network Watcher
                var createNetworkWatcher = networkManagementClient.NetworkWatchers.CreateOrUpdate(resourceGroupName, networkWatcherName, properties);

                var    getVm          = computeManagementClient.VirtualMachines.Get(resourceGroupName, virtualMachineName);
                string localIPAddress = networkManagementClient.NetworkInterfaces.Get(resourceGroupName, networkInterfaceName).IpConfigurations.FirstOrDefault().PrivateIPAddress;

                string securityRule1 = TestUtilities.GenerateName();

                // Add a security rule
                var SecurityRule = new SecurityRule()
                {
                    Name        = securityRule1,
                    Access      = SecurityRuleAccess.Deny,
                    Description = "Test outbound security rule",
                    DestinationAddressPrefix = "*",
                    DestinationPortRange     = "80",
                    Direction           = SecurityRuleDirection.Outbound,
                    Priority            = 501,
                    Protocol            = SecurityRuleProtocol.Tcp,
                    SourceAddressPrefix = "*",
                    SourcePortRange     = "*",
                };

                var nsg = networkManagementClient.NetworkSecurityGroups.Get(resourceGroupName, networkSecurityGroupName);
                nsg.SecurityRules.Add(SecurityRule);
                networkManagementClient.NetworkSecurityGroups.CreateOrUpdate(resourceGroupName, networkSecurityGroupName, nsg);

                SecurityGroupViewParameters sgvProperties = new SecurityGroupViewParameters()
                {
                    TargetResourceId = getVm.Id
                };

                //Get view security group rules
                var viewNSGRules = networkManagementClient.NetworkWatchers.GetVMSecurityRules(resourceGroupName, networkWatcherName, sgvProperties);

                //Verify effective security rule defined earlier
                var getEffectiveSecurityRule = viewNSGRules.NetworkInterfaces.FirstOrDefault().SecurityRuleAssociations.EffectiveSecurityRules.Where(x => x.Name == "UserRule_" + securityRule1);
                Assert.Equal("Tcp", getEffectiveSecurityRule.FirstOrDefault().Protocol);
                Assert.Equal(501, getEffectiveSecurityRule.FirstOrDefault().Priority);
                Assert.Equal("Deny", getEffectiveSecurityRule.FirstOrDefault().Access);
                Assert.Equal("Outbound", getEffectiveSecurityRule.FirstOrDefault().Direction);
                Assert.Equal("0.0.0.0/0", getEffectiveSecurityRule.FirstOrDefault().DestinationAddressPrefix);
                Assert.Equal("80-80", getEffectiveSecurityRule.FirstOrDefault().DestinationPortRange);
                Assert.Equal("0.0.0.0/0", getEffectiveSecurityRule.FirstOrDefault().SourceAddressPrefix);
                Assert.Equal("0-65535", getEffectiveSecurityRule.FirstOrDefault().SourcePortRange);

                //Verify 6 default rules
                var getDefaultSecurityRule1 = viewNSGRules.NetworkInterfaces.FirstOrDefault().SecurityRuleAssociations.DefaultSecurityRules.Where(x => x.Name == "AllowVnetInBound");
                Assert.Equal("*", getDefaultSecurityRule1.FirstOrDefault().Protocol);
                Assert.Equal(65000, getDefaultSecurityRule1.FirstOrDefault().Priority);
                Assert.Equal("Allow", getDefaultSecurityRule1.FirstOrDefault().Access);
                Assert.Equal("Inbound", getDefaultSecurityRule1.FirstOrDefault().Direction);
                Assert.Equal("VirtualNetwork", getDefaultSecurityRule1.FirstOrDefault().DestinationAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule1.FirstOrDefault().DestinationPortRange);
                Assert.Equal("VirtualNetwork", getDefaultSecurityRule1.FirstOrDefault().SourceAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule1.FirstOrDefault().SourcePortRange);

                var getDefaultSecurityRule2 = viewNSGRules.NetworkInterfaces.FirstOrDefault().SecurityRuleAssociations.DefaultSecurityRules.Where(x => x.Name == "AllowAzureLoadBalancerInBound");
                Assert.Equal("*", getDefaultSecurityRule2.FirstOrDefault().Protocol);
                Assert.Equal(65001, getDefaultSecurityRule2.FirstOrDefault().Priority);
                Assert.Equal("Allow", getDefaultSecurityRule2.FirstOrDefault().Access);
                Assert.Equal("Inbound", getDefaultSecurityRule2.FirstOrDefault().Direction);
                Assert.Equal("*", getDefaultSecurityRule2.FirstOrDefault().DestinationAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule2.FirstOrDefault().DestinationPortRange);
                Assert.Equal("AzureLoadBalancer", getDefaultSecurityRule2.FirstOrDefault().SourceAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule2.FirstOrDefault().SourcePortRange);

                var getDefaultSecurityRule3 = viewNSGRules.NetworkInterfaces.FirstOrDefault().SecurityRuleAssociations.DefaultSecurityRules.Where(x => x.Name == "DenyAllInBound");
                Assert.Equal("*", getDefaultSecurityRule3.FirstOrDefault().Protocol);
                Assert.Equal(65500, getDefaultSecurityRule3.FirstOrDefault().Priority);
                Assert.Equal("Deny", getDefaultSecurityRule3.FirstOrDefault().Access);
                Assert.Equal("Inbound", getDefaultSecurityRule3.FirstOrDefault().Direction);
                Assert.Equal("*", getDefaultSecurityRule3.FirstOrDefault().DestinationAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule3.FirstOrDefault().DestinationPortRange);
                Assert.Equal("*", getDefaultSecurityRule3.FirstOrDefault().SourceAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule3.FirstOrDefault().SourcePortRange);

                var getDefaultSecurityRule4 = viewNSGRules.NetworkInterfaces.FirstOrDefault().SecurityRuleAssociations.DefaultSecurityRules.Where(x => x.Name == "AllowVnetOutBound");
                Assert.Equal("*", getDefaultSecurityRule4.FirstOrDefault().Protocol);
                Assert.Equal(65000, getDefaultSecurityRule4.FirstOrDefault().Priority);
                Assert.Equal("Allow", getDefaultSecurityRule4.FirstOrDefault().Access);
                Assert.Equal("Outbound", getDefaultSecurityRule4.FirstOrDefault().Direction);
                Assert.Equal("VirtualNetwork", getDefaultSecurityRule4.FirstOrDefault().DestinationAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule4.FirstOrDefault().DestinationPortRange);
                Assert.Equal("VirtualNetwork", getDefaultSecurityRule4.FirstOrDefault().SourceAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule4.FirstOrDefault().SourcePortRange);

                var getDefaultSecurityRule5 = viewNSGRules.NetworkInterfaces.FirstOrDefault().SecurityRuleAssociations.DefaultSecurityRules.Where(x => x.Name == "AllowInternetOutBound");
                Assert.Equal("*", getDefaultSecurityRule5.FirstOrDefault().Protocol);
                Assert.Equal(65001, getDefaultSecurityRule5.FirstOrDefault().Priority);
                Assert.Equal("Allow", getDefaultSecurityRule5.FirstOrDefault().Access);
                Assert.Equal("Outbound", getDefaultSecurityRule5.FirstOrDefault().Direction);
                Assert.Equal("Internet", getDefaultSecurityRule5.FirstOrDefault().DestinationAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule5.FirstOrDefault().DestinationPortRange);
                Assert.Equal("*", getDefaultSecurityRule5.FirstOrDefault().SourceAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule5.FirstOrDefault().SourcePortRange);

                var getDefaultSecurityRule6 = viewNSGRules.NetworkInterfaces.FirstOrDefault().SecurityRuleAssociations.DefaultSecurityRules.Where(x => x.Name == "DenyAllOutBound");
                Assert.Equal("*", getDefaultSecurityRule6.FirstOrDefault().Protocol);
                Assert.Equal(65500, getDefaultSecurityRule6.FirstOrDefault().Priority);
                Assert.Equal("Deny", getDefaultSecurityRule6.FirstOrDefault().Access);
                Assert.Equal("Outbound", getDefaultSecurityRule6.FirstOrDefault().Direction);
                Assert.Equal("*", getDefaultSecurityRule6.FirstOrDefault().DestinationAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule6.FirstOrDefault().DestinationPortRange);
                Assert.Equal("*", getDefaultSecurityRule6.FirstOrDefault().SourceAddressPrefix);
                Assert.Equal("*", getDefaultSecurityRule6.FirstOrDefault().SourcePortRange);
            }
        }
Esempio n. 24
0
        public void ApplicationGatewayApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/applicationgateways");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                var vnetName     = TestUtilities.GenerateName();
                var gwSubnetName = TestUtilities.GenerateName();
                var subnet2Name  = TestUtilities.GenerateName();
                var appGwName    = TestUtilities.GenerateName();

                var vnet = new VirtualNetwork()
                {
                    Location = location,

                    AddressSpace = new AddressSpace()
                    {
                        AddressPrefixes = new List <string>()
                        {
                            "10.0.0.0/16",
                        }
                    },
                    DhcpOptions = new DhcpOptions()
                    {
                        DnsServers = new List <string>()
                        {
                            "10.1.1.1",
                            "10.1.2.4"
                        }
                    },
                    Subnets = new List <Subnet>()
                    {
                        new Subnet()
                        {
                            Name          = gwSubnetName,
                            AddressPrefix = "10.0.0.0/24",
                        },
                        new Subnet()
                        {
                            Name          = subnet2Name,
                            AddressPrefix = "10.0.1.0/24",
                        }
                    }
                };

                var putVnetResponse   = networkManagementClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnetName, vnet);
                var getVnetResponse   = networkManagementClient.VirtualNetworks.Get(resourceGroupName, vnetName);
                var getSubnetResponse = networkManagementClient.Subnets.Get(resourceGroupName, vnetName, gwSubnetName);
                Console.WriteLine("Virtual Network GatewaySubnet Id: {0}", getSubnetResponse.Id);
                var gwSubnet = getSubnetResponse;

                var appGw = CreateApplicationGateway(location, gwSubnet, resourceGroupName, appGwName, networkManagementClient.SubscriptionId);

                // Put AppGw
                var putAppGwResponse = networkManagementClient.ApplicationGateways.CreateOrUpdate(resourceGroupName, appGwName, appGw);
                Assert.Equal("Succeeded", putAppGwResponse.ProvisioningState);

                // Get AppGw
                var getGateway = networkManagementClient.ApplicationGateways.Get(resourceGroupName, appGwName);
                Assert.Equal(appGwName, getGateway.Name);
                CompareApplicationGateway(appGw, getGateway);

                // Create Nics
                string nic1name = TestUtilities.GenerateName();
                string nic2name = TestUtilities.GenerateName();

                var nic1 = TestHelper.CreateNetworkInterface(
                    nic1name,
                    resourceGroupName,
                    null,
                    getVnetResponse.Subnets[1].Id,
                    location,
                    "ipconfig",
                    networkManagementClient);

                var nic2 = TestHelper.CreateNetworkInterface(
                    nic2name,
                    resourceGroupName,
                    null,
                    getVnetResponse.Subnets[1].Id,
                    location,
                    "ipconfig",
                    networkManagementClient);

                // Add NIC to application gateway backend address pool.
                nic1.IpConfigurations[0].ApplicationGatewayBackendAddressPools = new List <ApplicationGatewayBackendAddressPool>
                {
                    getGateway.BackendAddressPools[1]
                };
                nic2.IpConfigurations[0].ApplicationGatewayBackendAddressPools = new List <ApplicationGatewayBackendAddressPool>
                {
                    getGateway.BackendAddressPools[1]
                };
                // Put Nics
                networkManagementClient.NetworkInterfaces.CreateOrUpdate(resourceGroupName, nic1name, nic1);
                networkManagementClient.NetworkInterfaces.CreateOrUpdate(resourceGroupName, nic2name, nic2);

                //Start AppGw
                networkManagementClient.ApplicationGateways.Start(resourceGroupName, appGwName);

                // Get AppGw and make sure nics are added to backend
                getGateway = networkManagementClient.ApplicationGateways.Get(resourceGroupName, appGwName);
                Assert.Equal(2, getGateway.BackendAddressPools[1].BackendIPConfigurations.Count);

                //Stop AppGw
                networkManagementClient.ApplicationGateways.Stop(resourceGroupName, appGwName);

                // Delete AppGw
                networkManagementClient.ApplicationGateways.Delete(resourceGroupName, appGwName);
            }
        }
Esempio n. 25
0
        public void FlowLogApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler3 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler4 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler5 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient                     = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient             = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);
                var computeManagementClient             = NetworkManagementTestUtilities.GetComputeManagementClientWithHandler(context, handler3);
                var storageManagementClient             = NetworkManagementTestUtilities.GetStorageManagementClientWithHandler(context, handler4);
                var operationalInsightsManagementClient = NetworkManagementTestUtilities.GetOperationalInsightsManagementClientWithHandler(context, handler5);

                string location          = "eastus2euap";
                string workspaceLocation = "East US";

                string resourceGroupName = TestUtilities.GenerateName();
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                //Create network security group
                string networkSecurityGroupName = TestUtilities.GenerateName();

                var networkSecurityGroup = new NetworkSecurityGroup()
                {
                    Location = location,
                };

                // Put Nsg
                var putNsgResponse = networkManagementClient.NetworkSecurityGroups.CreateOrUpdate(resourceGroupName, networkSecurityGroupName, networkSecurityGroup);

                // Get NSG
                var getNsgResponse = networkManagementClient.NetworkSecurityGroups.Get(resourceGroupName, networkSecurityGroupName);

                string         networkWatcherName = TestUtilities.GenerateName();
                NetworkWatcher properties         = new NetworkWatcher();
                properties.Location = location;

                //Create network Watcher
                var createNetworkWatcher = networkManagementClient.NetworkWatchers.CreateOrUpdate(resourceGroupName, networkWatcherName, properties);

                //Create storage
                string storageName = TestUtilities.GenerateName();

                var storageParameters = new StorageAccountCreateParameters()
                {
                    Location = location,
                    Kind     = Kind.Storage,
                    Sku      = new Sku
                    {
                        Name = SkuName.StandardLRS
                    }
                };

                var storageAccount = storageManagementClient.StorageAccounts.Create(resourceGroupName, storageName, storageParameters);

                //create workspace
                string workspaceName = TestUtilities.GenerateName();

                var workSpaceParameters = new Workspace()
                {
                    Location = workspaceLocation
                };

                var workspace = operationalInsightsManagementClient.Workspaces.CreateOrUpdate(resourceGroupName, workspaceName, workSpaceParameters);

                FlowLogInformation configParameters = new FlowLogInformation()
                {
                    TargetResourceId = getNsgResponse.Id,
                    Enabled          = true,
                    StorageId        = storageAccount.Id,
                    RetentionPolicy  = new RetentionPolicyParameters
                    {
                        Days    = 5,
                        Enabled = true
                    },
                    FlowAnalyticsConfiguration = new TrafficAnalyticsProperties()
                    {
                        NetworkWatcherFlowAnalyticsConfiguration = new TrafficAnalyticsConfigurationProperties()
                        {
                            Enabled             = true,
                            WorkspaceId         = workspace.CustomerId,
                            WorkspaceRegion     = workspace.Location,
                            WorkspaceResourceId = workspace.Id
                        }
                    }
                };


                //configure flowlog and TA
                var configureFlowLog1 = networkManagementClient.NetworkWatchers.SetFlowLogConfiguration(resourceGroupName, networkWatcherName, configParameters);

                FlowLogStatusParameters flowLogParameters = new FlowLogStatusParameters()
                {
                    TargetResourceId = getNsgResponse.Id
                };

                var queryFlowLogStatus1 = networkManagementClient.NetworkWatchers.GetFlowLogStatus(resourceGroupName, networkWatcherName, flowLogParameters);

                //check both flowlog and TA config and enabled status
                Assert.Equal(queryFlowLogStatus1.TargetResourceId, configParameters.TargetResourceId);
                Assert.True(queryFlowLogStatus1.Enabled);
                Assert.Equal(queryFlowLogStatus1.StorageId, configParameters.StorageId);
                Assert.Equal(queryFlowLogStatus1.RetentionPolicy.Days, configParameters.RetentionPolicy.Days);
                Assert.Equal(queryFlowLogStatus1.RetentionPolicy.Enabled, configParameters.RetentionPolicy.Enabled);
                Assert.True(queryFlowLogStatus1.FlowAnalyticsConfiguration.NetworkWatcherFlowAnalyticsConfiguration.Enabled);
                Assert.Equal(queryFlowLogStatus1.FlowAnalyticsConfiguration.NetworkWatcherFlowAnalyticsConfiguration.WorkspaceId,
                             configParameters.FlowAnalyticsConfiguration.NetworkWatcherFlowAnalyticsConfiguration.WorkspaceId);
                Assert.Equal(queryFlowLogStatus1.FlowAnalyticsConfiguration.NetworkWatcherFlowAnalyticsConfiguration.WorkspaceRegion,
                             configParameters.FlowAnalyticsConfiguration.NetworkWatcherFlowAnalyticsConfiguration.WorkspaceRegion);
                Assert.Equal(queryFlowLogStatus1.FlowAnalyticsConfiguration.NetworkWatcherFlowAnalyticsConfiguration.WorkspaceResourceId,
                             configParameters.FlowAnalyticsConfiguration.NetworkWatcherFlowAnalyticsConfiguration.WorkspaceResourceId);

                //disable TA
                configParameters.FlowAnalyticsConfiguration.NetworkWatcherFlowAnalyticsConfiguration.Enabled = false;
                var configureFlowLog2   = networkManagementClient.NetworkWatchers.SetFlowLogConfiguration(resourceGroupName, networkWatcherName, configParameters);
                var queryFlowLogStatus2 = networkManagementClient.NetworkWatchers.GetFlowLogStatus(resourceGroupName, networkWatcherName, flowLogParameters);

                //check TA disabled and ensure flowlog config is unchanged
                Assert.Equal(queryFlowLogStatus2.TargetResourceId, configParameters.TargetResourceId);
                Assert.True(queryFlowLogStatus2.Enabled);
                Assert.Equal(queryFlowLogStatus2.StorageId, configParameters.StorageId);
                Assert.Equal(queryFlowLogStatus2.RetentionPolicy.Days, configParameters.RetentionPolicy.Days);
                Assert.Equal(queryFlowLogStatus2.RetentionPolicy.Enabled, configParameters.RetentionPolicy.Enabled);
                Assert.False(queryFlowLogStatus2.FlowAnalyticsConfiguration.NetworkWatcherFlowAnalyticsConfiguration.Enabled);

                //disable flowlog (and TA)
                configParameters.Enabled = false;
                var configureFlowLog3   = networkManagementClient.NetworkWatchers.SetFlowLogConfiguration(resourceGroupName, networkWatcherName, configParameters);
                var queryFlowLogStatus3 = networkManagementClient.NetworkWatchers.GetFlowLogStatus(resourceGroupName, networkWatcherName, flowLogParameters);

                //check both flowlog and TA disabled
                Assert.False(queryFlowLogStatus3.Enabled);
                Assert.False(queryFlowLogStatus3.FlowAnalyticsConfiguration.NetworkWatcherFlowAnalyticsConfiguration.Enabled);
            }
        }
Esempio n. 26
0
        public void RoutesHopTypeTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/routeTables");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(
                    resourceGroupName,
                    new ResourceGroup {
                    Location = location
                });

                string routeTableName = TestUtilities.GenerateName();
                string route1Name     = TestUtilities.GenerateName();
                string route2Name     = TestUtilities.GenerateName();
                string route3Name     = TestUtilities.GenerateName();
                string route4Name     = TestUtilities.GenerateName();

                var routeTable = new RouteTable()
                {
                    Location = location,
                };
                routeTable.Routes = new List <Route>();

                // Add a route
                var route1 = new Route()
                {
                    AddressPrefix    = "192.168.1.0/24",
                    Name             = route1Name,
                    NextHopIpAddress = "23.108.1.1",
                    NextHopType      = RouteNextHopType.VirtualAppliance
                };

                routeTable.Routes.Add(route1);

                // Put RouteTable
                var putRouteTableResponse =
                    networkManagementClient.RouteTables.CreateOrUpdate(
                        resourceGroupName,
                        routeTableName,
                        routeTable);

                Assert.Equal("Succeeded", putRouteTableResponse.ProvisioningState);

                // Get RouteTable
                var getRouteTableResponse = networkManagementClient.RouteTables.Get(
                    resourceGroupName,
                    routeTableName);

                Assert.Equal(routeTableName, getRouteTableResponse.Name);
                Assert.Equal(1, getRouteTableResponse.Routes.Count);
                Assert.Equal(route1Name, getRouteTableResponse.Routes[0].Name);

                // Add another route
                var route2 = new Route()
                {
                    AddressPrefix = "10.0.1.0/24",
                    Name          = route2Name,
                    NextHopType   = RouteNextHopType.VnetLocal
                };

                networkManagementClient.Routes.CreateOrUpdate(
                    resourceGroupName,
                    routeTableName,
                    route2Name,
                    route2);

                // Add another route
                var route3 = new Route()
                {
                    AddressPrefix = "0.0.0.0/0",
                    Name          = route3Name,
                    NextHopType   = RouteNextHopType.Internet
                };

                networkManagementClient.Routes.CreateOrUpdate(
                    resourceGroupName,
                    routeTableName,
                    route3Name,
                    route3);

                // Add another route
                var route4 = new Route()
                {
                    AddressPrefix = "10.0.2.0/24",
                    Name          = route4Name,
                    NextHopType   = RouteNextHopType.None
                };

                networkManagementClient.Routes.CreateOrUpdate(
                    resourceGroupName,
                    routeTableName,
                    route4Name,
                    route4);

                getRouteTableResponse = networkManagementClient.RouteTables.Get(
                    resourceGroupName,
                    routeTableName);
                Assert.Equal(routeTableName, getRouteTableResponse.Name);
                Assert.Equal(4, getRouteTableResponse.Routes.Count);
                Assert.Equal(route2Name, getRouteTableResponse.Routes[1].Name);
                Assert.Equal(RouteNextHopType.VirtualAppliance, getRouteTableResponse.Routes[0].NextHopType);
                Assert.Equal(RouteNextHopType.VnetLocal, getRouteTableResponse.Routes[1].NextHopType);
                Assert.Equal(RouteNextHopType.Internet, getRouteTableResponse.Routes[2].NextHopType);
                Assert.Equal(RouteNextHopType.None, getRouteTableResponse.Routes[3].NextHopType);

                // Delete RouteTable
                networkManagementClient.RouteTables.Delete(resourceGroupName, routeTableName);

                // Verify delete
                var listRouteTableResponse = networkManagementClient.RouteTables.List(resourceGroupName);

                Assert.Equal(0, listRouteTableResponse.Count());
            }
        }
Esempio n. 27
0
        public void ExpressRouteCircuitApiTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var resourcesClient = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(handler);
                var networkResourceProviderClient = NetworkManagementTestUtilities.GetNetworkResourceProviderClient(handler);
                // var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/expressRouteCircuits");

                var location = "brazilsouth";

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(
                    resourceGroupName,
                    new ResourceGroup
                {
                    Location = location
                });

                string circuitName = TestUtilities.GenerateName();

                var circuit = new ExpressRouteCircuit()
                {
                    Location = location,
                    Sku      =
                        new ExpressRouteCircuitSku()
                    {
                        Name   = "Standard_MeteredData",
                        Tier   = ExpressRouteCircuitSkuTier.Standard,
                        Family = ExpressRouteCircuitSkuFamily.MeteredData
                    },
                    BillingType = ExpressRouteCircuitBillingType.ServiceProviderType,
                    ServiceProviderProperties =
                        new ExpressRouteCircuitServiceProviderProperties()
                    {
                        ServiceProviderName = "Equinix",
                        PeeringLocation     = "Silicon Valley",
                        BandwidthInMbps     = 1000
                    }
                };
                // Create the circuit
                var putCircuit = networkResourceProviderClient.ExpressRouteCircuits.CreateOrUpdate(resourceGroupName, circuitName, circuit);
                Assert.Equal(HttpStatusCode.OK, putCircuit.StatusCode);

                var getCircuit = networkResourceProviderClient.ExpressRouteCircuits.Get(resourceGroupName, circuitName);

                // Verify properties
                Assert.Equal(HttpStatusCode.OK, getCircuit.StatusCode);

                Assert.NotNull(getCircuit.ExpressRouteCircuit);
                Assert.NotNull(getCircuit.ExpressRouteCircuit.Sku);
                Assert.NotNull(getCircuit.ExpressRouteCircuit.Etag);
                Assert.Equal("Standard_MeteredData", getCircuit.ExpressRouteCircuit.Sku.Name);
                Assert.Equal(ExpressRouteCircuitSkuTier.Standard, getCircuit.ExpressRouteCircuit.Sku.Tier);
                Assert.Equal(ExpressRouteCircuitSkuFamily.MeteredData, getCircuit.ExpressRouteCircuit.Sku.Family);
                //Assert.Equal(ExpressRouteCircuitBillingType.ServiceProviderType, getCircuit.ExpressRouteCircuit.BillingType);

                Assert.NotNull(getCircuit.ExpressRouteCircuit.ServiceProviderProperties);
                Assert.Equal("Equinix", getCircuit.ExpressRouteCircuit.ServiceProviderProperties.ServiceProviderName);
                Assert.Equal("Silicon Valley", getCircuit.ExpressRouteCircuit.ServiceProviderProperties.PeeringLocation);
                Assert.Equal(1000, getCircuit.ExpressRouteCircuit.ServiceProviderProperties.BandwidthInMbps);

                // Verify List
                var listCircuit = networkResourceProviderClient.ExpressRouteCircuits.List(resourceGroupName);
                Assert.Equal(HttpStatusCode.OK, listCircuit.StatusCode);
                Assert.NotNull(listCircuit.ExpressRouteCircuits);
                Assert.Equal(1, listCircuit.ExpressRouteCircuits.Count);
                Assert.Equal(listCircuit.ExpressRouteCircuits[0].Etag, getCircuit.ExpressRouteCircuit.Etag);
                Assert.Equal(listCircuit.ExpressRouteCircuits[0].Sku.Name, getCircuit.ExpressRouteCircuit.Sku.Name);
                Assert.Equal(listCircuit.ExpressRouteCircuits[0].Sku.Tier, getCircuit.ExpressRouteCircuit.Sku.Tier);
                Assert.Equal(listCircuit.ExpressRouteCircuits[0].Sku.Family, getCircuit.ExpressRouteCircuit.Sku.Family);

                Assert.NotNull(getCircuit.ExpressRouteCircuit.ServiceProviderProperties);
                Assert.Equal(listCircuit.ExpressRouteCircuits[0].ServiceProviderProperties.ServiceProviderName, getCircuit.ExpressRouteCircuit.ServiceProviderProperties.ServiceProviderName);
                Assert.Equal(listCircuit.ExpressRouteCircuits[0].ServiceProviderProperties.PeeringLocation, getCircuit.ExpressRouteCircuit.ServiceProviderProperties.PeeringLocation);
                Assert.Equal(listCircuit.ExpressRouteCircuits[0].ServiceProviderProperties.BandwidthInMbps, getCircuit.ExpressRouteCircuit.ServiceProviderProperties.BandwidthInMbps);

                // Verify Delete
                var deleteCircuit = networkResourceProviderClient.ExpressRouteCircuits.Delete(resourceGroupName, circuitName);
                Assert.Equal(HttpStatusCode.OK, deleteCircuit.StatusCode);

                listCircuit = networkResourceProviderClient.ExpressRouteCircuits.List(resourceGroupName);
                Assert.Equal(0, listCircuit.ExpressRouteCircuits.Count);
            }
        }
Esempio n. 28
0
        public void SubnetResourceNavigationLinksTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler3 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);
                var redisClient             = RedisCacheManagementTestUtilities.GetRedisManagementClientWithHandler(context, handler3);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient,
                                                                                  "Microsoft.Network/virtualNetworks");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                string vnetName   = TestUtilities.GenerateName();
                string subnetName = TestUtilities.GenerateName();
                string redisName  = TestUtilities.GenerateName();

                var vnet = new VirtualNetwork()
                {
                    Location = location,

                    AddressSpace = new AddressSpace()
                    {
                        AddressPrefixes = new List <string>()
                        {
                            "10.0.0.0/16",
                        }
                    },
                    DhcpOptions = new DhcpOptions()
                    {
                        DnsServers = new List <string>()
                        {
                            "10.1.1.1",
                            "10.1.2.4"
                        }
                    },
                    Subnets = new List <Subnet>()
                    {
                        new Subnet()
                        {
                            Name          = subnetName,
                            AddressPrefix = "10.0.0.0/24",
                        }
                    }
                };

                var putVnetResponse = networkManagementClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnetName, vnet);

                var getSubnetResponse = networkManagementClient.Subnets.Get(resourceGroupName, vnetName, subnetName);
                Assert.Null(getSubnetResponse.ResourceNavigationLinks);

                redisClient.Redis.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, redisName, parameters: new RedisCreateOrUpdateParameters
                {
                    Location = location,
                    Sku      = new Sku()
                    {
                        Name     = SkuName.Premium,
                        Family   = SkuFamily.P,
                        Capacity = 1
                    },
                    SubnetId = getSubnetResponse.Id
                }).Wait();

                // wait for maximum 30 minutes for cache to create
                for (int i = 0; i < 60; i++)
                {
                    TestUtilities.Wait(new TimeSpan(0, 0, 30));
                    RedisResource responseGet = redisClient.Redis.Get(resourceGroupName: resourceGroupName, name: redisName);
                    if ("succeeded".Equals(responseGet.ProvisioningState, StringComparison.OrdinalIgnoreCase))
                    {
                        break;
                    }
                    Assert.False(i == 60, "Cache is not in succeeded state even after 30 min.");
                }

                getSubnetResponse = networkManagementClient.Subnets.Get(resourceGroupName, vnetName, subnetName);
                Assert.Equal(1, getSubnetResponse.ResourceNavigationLinks.Count);
            }
        }
        public void NetworkInterfaceEnableIPForwardingTest()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (var context = UndoContext.Current)
            {
                context.Start();
                var resourcesClient = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(handler);
                var networkResourceProviderClient = NetworkManagementTestUtilities.GetNetworkResourceProviderClient(handler);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient, "Microsoft.Network/networkInterfaces");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                // Create Vnet
                // Populate parameter for Put Vnet
                string vnetName   = TestUtilities.GenerateName();
                string subnetName = TestUtilities.GenerateName();

                var vnet = new VirtualNetwork()
                {
                    Location = location,

                    AddressSpace = new AddressSpace()
                    {
                        AddressPrefixes = new List <string>()
                        {
                            "10.0.0.0/16",
                        }
                    },
                    DhcpOptions = new DhcpOptions()
                    {
                        DnsServers = new List <string>()
                        {
                            "10.1.1.1",
                            "10.1.2.4"
                        }
                    },
                    Subnets = new List <Subnet>()
                    {
                        new Subnet()
                        {
                            Name          = subnetName,
                            AddressPrefix = "10.0.0.0/24",
                        }
                    }
                };

                var putVnetResponse = networkResourceProviderClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnetName, vnet);
                Assert.Equal(HttpStatusCode.OK, putVnetResponse.StatusCode);

                var getSubnetResponse = networkResourceProviderClient.Subnets.Get(resourceGroupName, vnetName, subnetName);

                // Create Nic
                string nicName      = TestUtilities.GenerateName();
                string ipConfigName = TestUtilities.GenerateName();

                var nicParameters = new NetworkInterface()
                {
                    Location = location,
                    Name     = nicName,
                    Tags     = new Dictionary <string, string>()
                    {
                        { "key", "value" }
                    },
                    IpConfigurations = new List <NetworkInterfaceIpConfiguration>()
                    {
                        new NetworkInterfaceIpConfiguration()
                        {
                            Name = ipConfigName,
                            PrivateIpAllocationMethod = IpAllocationMethod.Dynamic,
                            Subnet = new ResourceId()
                            {
                                Id = getSubnetResponse.Subnet.Id
                            }
                        }
                    },
                    EnableIPForwarding = false,
                };

                // Test NIC apis
                var putNicResponse = networkResourceProviderClient.NetworkInterfaces.CreateOrUpdate(resourceGroupName, nicName, nicParameters);
                Assert.Equal(HttpStatusCode.OK, putNicResponse.StatusCode);

                var getNicResponse = networkResourceProviderClient.NetworkInterfaces.Get(resourceGroupName, nicName);
                Assert.Equal(getNicResponse.NetworkInterface.Name, nicName);
                Assert.Equal(getNicResponse.NetworkInterface.ProvisioningState, Microsoft.Azure.Management.Resources.Models.ProvisioningState.Succeeded);
                Assert.Null(getNicResponse.NetworkInterface.VirtualMachine);
                Assert.Null(getNicResponse.NetworkInterface.MacAddress);
                Assert.Equal(1, getNicResponse.NetworkInterface.IpConfigurations.Count);
                Assert.Equal(ipConfigName, getNicResponse.NetworkInterface.IpConfigurations[0].Name);
                Assert.False(getNicResponse.NetworkInterface.EnableIPForwarding);

                getNicResponse.NetworkInterface.EnableIPForwarding = true;
                networkResourceProviderClient.NetworkInterfaces.CreateOrUpdate(resourceGroupName, nicName, getNicResponse.NetworkInterface);
                getNicResponse = networkResourceProviderClient.NetworkInterfaces.Get(resourceGroupName, nicName);
                Assert.Equal(getNicResponse.NetworkInterface.Name, nicName);
                Assert.True(getNicResponse.NetworkInterface.EnableIPForwarding);

                // Delete Nic
                var deleteNicResponse = networkResourceProviderClient.NetworkInterfaces.Delete(resourceGroupName, nicName);
                Assert.Equal(HttpStatusCode.OK, deleteNicResponse.StatusCode);

                var getListNicResponse = networkResourceProviderClient.NetworkInterfaces.List(resourceGroupName);
                Assert.Equal(0, getListNicResponse.NetworkInterfaces.Count);

                // Delete VirtualNetwork
                var deleteVnetResponse = networkResourceProviderClient.VirtualNetworks.Delete(resourceGroupName, vnetName);
                Assert.Equal(HttpStatusCode.OK, deleteVnetResponse.StatusCode);
            }
        }
Esempio n. 30
0
        public void SubnetApiTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                var resourcesClient         = ResourcesManagementTestUtilities.GetResourceManagementClientWithHandler(context, handler1);
                var networkManagementClient = NetworkManagementTestUtilities.GetNetworkManagementClientWithHandler(context, handler2);

                var location = NetworkManagementTestUtilities.GetResourceLocation(resourcesClient,
                                                                                  "Microsoft.Network/virtualNetworks");

                string resourceGroupName = TestUtilities.GenerateName("csmrg");
                resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName,
                                                              new ResourceGroup
                {
                    Location = location
                });

                string vnetName    = TestUtilities.GenerateName();
                string subnet1Name = TestUtilities.GenerateName();
                string subnet2Name = TestUtilities.GenerateName();

                var vnet = new VirtualNetwork()
                {
                    Location = location,

                    AddressSpace = new AddressSpace()
                    {
                        AddressPrefixes = new List <string>()
                        {
                            "10.0.0.0/16",
                        }
                    },
                    DhcpOptions = new DhcpOptions()
                    {
                        DnsServers = new List <string>()
                        {
                            "10.1.1.1",
                            "10.1.2.4"
                        }
                    },
                    Subnets = new List <Subnet>()
                    {
                        new Subnet()
                        {
                            Name          = subnet1Name,
                            AddressPrefix = "10.0.0.0/24",
                        }
                    }
                };

                var putVnetResponse = networkManagementClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnetName, vnet);

                // Create a Subnet
                // Populate paramters for a Subnet
                var subnet = new Subnet()
                {
                    Name          = subnet2Name,
                    AddressPrefix = "10.0.1.0/24",
                };

                #region Verification

                var putSubnetResponse = networkManagementClient.Subnets.CreateOrUpdate(resourceGroupName, vnetName, subnet2Name, subnet);

                var getVnetResponse = networkManagementClient.VirtualNetworks.Get(resourceGroupName, vnetName);
                Assert.Equal(2, getVnetResponse.Subnets.Count());

                var getSubnetResponse = networkManagementClient.Subnets.Get(resourceGroupName, vnetName, subnet2Name);

                // Verify the getSubnetResponse
                Assert.True(AreSubnetsEqual(getVnetResponse.Subnets[1], getSubnetResponse));

                var getSubnetListResponse = networkManagementClient.Subnets.List(resourceGroupName, vnetName);

                // Verify ListSubnets
                Assert.True(AreSubnetsEqual(getVnetResponse.Subnets, getSubnetListResponse));

                // Delete the subnet "subnet1"
                networkManagementClient.Subnets.Delete(resourceGroupName, vnetName, subnet2Name);

                // Verify that the deletion was successful
                getSubnetListResponse = networkManagementClient.Subnets.List(resourceGroupName, vnetName);

                Assert.Equal(1, getSubnetListResponse.Count());
                Assert.Equal(subnet1Name, getSubnetListResponse.ElementAt(0).Name);

                #endregion
            }
        }