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);
            }
        }
        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);
            }
        }
Ejemplo 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);
            }
        }
Ejemplo 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);
            }
        }
Ejemplo n.º 5
0
        public void UsageTest()
        {
            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);

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

                // Query for usages
                var usagesResponse = networkManagementClient.Usages.List(getNsgResponse.Location.Replace(" ", string.Empty));

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

                foreach (var usage in usagesResponse)
                {
                    Assert.True(usage.Limit > 0);
                    Assert.NotNull(usage.Name);
                    Assert.True(!string.IsNullOrEmpty(usage.Name.LocalizedValue));
                    Assert.True(!string.IsNullOrEmpty(usage.Name.Value));
                }
            }
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
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 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);
            }
        }
Ejemplo n.º 9
0
        public void VirtualNetworkApiTest()
        {
            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, true);
                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.1.0/24",
                        },
                        new Subnet()
                        {
                            Name          = subnet2Name,
                            AddressPrefix = "10.0.2.0/24",
                        }
                    }
                };

                // Put Vnet
                var putVnetResponse = networkManagementClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnetName, vnet);
                Assert.Equal("Succeeded", putVnetResponse.ProvisioningState);

                // Get Vnet
                var getVnetResponse = networkManagementClient.VirtualNetworks.Get(resourceGroupName, vnetName);
                Assert.Equal(vnetName, getVnetResponse.Name);
                Assert.NotNull(getVnetResponse.ResourceGuid);
                Assert.Equal("Succeeded", getVnetResponse.ProvisioningState);
                Assert.Equal("10.1.1.1", getVnetResponse.DhcpOptions.DnsServers[0]);
                Assert.Equal("10.1.2.4", getVnetResponse.DhcpOptions.DnsServers[1]);
                Assert.Equal("10.0.0.0/16", getVnetResponse.AddressSpace.AddressPrefixes[0]);
                Assert.Equal(subnet1Name, getVnetResponse.Subnets[0].Name);
                Assert.Equal(subnet2Name, getVnetResponse.Subnets[1].Name);

                // Get all Vnets
                var getAllVnets = networkManagementClient.VirtualNetworks.List(resourceGroupName);
                Assert.Equal(vnetName, getAllVnets.ElementAt(0).Name);
                Assert.Equal("Succeeded", getAllVnets.ElementAt(0).ProvisioningState);
                Assert.Equal("10.0.0.0/16", getAllVnets.ElementAt(0).AddressSpace.AddressPrefixes[0]);
                Assert.Equal(subnet1Name, getAllVnets.ElementAt(0).Subnets[0].Name);
                Assert.Equal(subnet2Name, getAllVnets.ElementAt(0).Subnets[1].Name);

                // Get all Vnets in a subscription
                var getAllVnetInSubscription = networkManagementClient.VirtualNetworks.ListAll();
                Assert.NotEmpty(getAllVnetInSubscription);

                // Delete Vnet
                networkManagementClient.VirtualNetworks.Delete(resourceGroupName, vnetName);

                // Get all Vnets
                getAllVnets = networkManagementClient.VirtualNetworks.List(resourceGroupName);
                Assert.Empty(getAllVnets);
            }
        }
Ejemplo n.º 10
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);
            }
        }
Ejemplo n.º 11
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);
            }
        }
Ejemplo n.º 12
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);
            }
        }
        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);
            }
        }
Ejemplo n.º 14
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);
            }
        }
Ejemplo n.º 15
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
            }
        }
        public void PacketCaptureApiTest()
        {
            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()
                    );

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

                //Deploy networkWatcherAgent on VM
                VirtualMachineExtension parameters = new VirtualMachineExtension();
                parameters.Publisher                   = "Microsoft.Azure.NetworkWatcher.Edp";
                parameters.TypeHandlerVersion          = "1.4";
                parameters.VirtualMachineExtensionType = "NetworkWatcherAgentWindows";
                parameters.Location = location;

                var            addExtension       = computeManagementClient.VirtualMachineExtensions.CreateOrUpdate(resourceGroupName, getVm.Name, "NetworkWatcherAgent", parameters);
                string         networkWatcherName = TestUtilities.GenerateName();
                NetworkWatcher properties         = new NetworkWatcher();
                properties.Location = location;

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

                string pcName1 = "pc1";
                string pcName2 = "pc2";

                PacketCapture pcProperties = new PacketCapture();
                pcProperties.Target = getVm.Id;

                pcProperties.StorageLocation = new PacketCaptureStorageLocation
                {
                    FilePath = @"C:\tmp\Capture.cap"
                };

                var createPacketCapture1 = networkManagementClient.PacketCaptures.Create(resourceGroupName, networkWatcherName, pcName1, pcProperties);
                var getPacketCapture     = networkManagementClient.PacketCaptures.Get(resourceGroupName, networkWatcherName, pcName1);
                var queryPC = networkManagementClient.PacketCaptures.GetStatus(resourceGroupName, networkWatcherName, pcName1);

                //Validation
                Assert.Equal(pcName1, createPacketCapture1.Name);
                Assert.Equal(1073741824, createPacketCapture1.TotalBytesPerSession);
                Assert.Equal(0, createPacketCapture1.BytesToCapturePerPacket);
                Assert.Equal(18000, createPacketCapture1.TimeLimitInSeconds);
                Assert.Equal(@"C:\tmp\Capture.cap", createPacketCapture1.StorageLocation.FilePath);
                Assert.Equal("Succeeded", getPacketCapture.ProvisioningState);

                var createPacketCapture2 = networkManagementClient.PacketCaptures.Create(resourceGroupName, networkWatcherName, pcName2, pcProperties);
                var listPCByRg1          = networkManagementClient.PacketCaptures.List(resourceGroupName, networkWatcherName);
                networkManagementClient.PacketCaptures.Stop(resourceGroupName, networkWatcherName, pcName1);
                var queryPCAfterStop = networkManagementClient.PacketCaptures.GetStatus(resourceGroupName, networkWatcherName, pcName1);
                networkManagementClient.PacketCaptures.Delete(resourceGroupName, networkWatcherName, pcName1);
                var listPCByRg2 = networkManagementClient.PacketCaptures.List(resourceGroupName, networkWatcherName);


                //Validation
                Assert.Equal(2, listPCByRg1.Count());
                Assert.Equal("Stopped", queryPCAfterStop.PacketCaptureStatus);
                Assert.Equal("Manual", queryPCAfterStop.StopReason);
                Assert.Single(listPCByRg2);
            }
        }
Ejemplo n.º 17
0
        public void VirtualNetworkPeeringTest()
        {
            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, true);
                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 vnet1Name   = TestUtilities.GenerateName();
                string vnet2Name   = 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 = networkManagementClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnet1Name, vnet);
                Assert.Equal("Succeeded", putVnetResponse.ProvisioningState);

                // Get Vnet
                var getVnetResponse = networkManagementClient.VirtualNetworks.Get(resourceGroupName, vnet1Name);
                Assert.Equal(vnet1Name, getVnetResponse.Name);
                Assert.NotNull(getVnetResponse.ResourceGuid);
                Assert.Equal("Succeeded", getVnetResponse.ProvisioningState);

                // Create vnet2
                var vnet2 = new VirtualNetwork()
                {
                    Location = location,

                    AddressSpace = new AddressSpace()
                    {
                        AddressPrefixes = new List <string>()
                        {
                            "10.1.0.0/16",
                        }
                    },
                    Subnets = new List <Subnet>()
                    {
                        new Subnet()
                        {
                            Name          = subnet1Name,
                            AddressPrefix = "10.1.1.0/24",
                        }
                    }
                };

                // Put Vnet2
                var putVnet2 = networkManagementClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnet2Name, vnet2);
                Assert.Equal("Succeeded", putVnet2.ProvisioningState);

                // Create peering object
                var peering = new VirtualNetworkPeering()
                {
                    AllowForwardedTraffic = true,
                    RemoteVirtualNetwork  = new Microsoft.Azure.Management.Network.Models.SubResource
                    {
                        Id = putVnet2.Id
                    }
                };

                // Create Peering
                networkManagementClient.VirtualNetworkPeerings.CreateOrUpdate(resourceGroupName, vnet1Name, "peer1", peering);

                // Get Peering
                var getPeer = networkManagementClient.VirtualNetworkPeerings.Get(resourceGroupName, vnet1Name, "peer1");
                Assert.Equal("peer1", getPeer.Name);
                Assert.True(getPeer.AllowForwardedTraffic);
                Assert.True(getPeer.AllowVirtualNetworkAccess);
                Assert.False(getPeer.AllowGatewayTransit);
                Assert.NotNull(getPeer.RemoteVirtualNetwork);
                Assert.Equal(putVnet2.Id, getPeer.RemoteVirtualNetwork.Id);

                // List Peering
                var listPeer = networkManagementClient.VirtualNetworkPeerings.List(resourceGroupName, vnet1Name).ToList();
                Assert.Single(listPeer);
                Assert.Equal("peer1", listPeer[0].Name);
                Assert.True(listPeer[0].AllowForwardedTraffic);
                Assert.True(listPeer[0].AllowVirtualNetworkAccess);
                Assert.False(listPeer[0].AllowGatewayTransit);
                Assert.NotNull(listPeer[0].RemoteVirtualNetwork);
                Assert.Equal(putVnet2.Id, listPeer[0].RemoteVirtualNetwork.Id);

                // Get peering from GET vnet
                var peeringVnet = networkManagementClient.VirtualNetworks.Get(resourceGroupName, vnet1Name);
                Assert.Equal(vnet1Name, peeringVnet.Name);
                Assert.Single(peeringVnet.VirtualNetworkPeerings);
                Assert.Equal("peer1", peeringVnet.VirtualNetworkPeerings[0].Name);
                Assert.True(peeringVnet.VirtualNetworkPeerings[0].AllowForwardedTraffic);
                Assert.True(peeringVnet.VirtualNetworkPeerings[0].AllowVirtualNetworkAccess);
                Assert.False(peeringVnet.VirtualNetworkPeerings[0].AllowGatewayTransit);
                Assert.NotNull(peeringVnet.VirtualNetworkPeerings[0].RemoteVirtualNetwork);
                Assert.Equal(putVnet2.Id, peeringVnet.VirtualNetworkPeerings[0].RemoteVirtualNetwork.Id);

                // Delete Peering
                networkManagementClient.VirtualNetworkPeerings.Delete(resourceGroupName, vnet1Name, "peer1");

                listPeer = networkManagementClient.VirtualNetworkPeerings.List(resourceGroupName, vnet1Name).ToList();
                Assert.Empty(listPeer);

                peeringVnet = networkManagementClient.VirtualNetworks.Get(resourceGroupName, vnet1Name);
                Assert.Equal(vnet1Name, peeringVnet.Name);
                Assert.Empty(peeringVnet.VirtualNetworkPeerings);

                // Delete Vnets
                networkManagementClient.VirtualNetworks.Delete(resourceGroupName, vnet1Name);
                networkManagementClient.VirtualNetworks.Delete(resourceGroupName, vnet2Name);
            }
        }
Ejemplo n.º 18
0
        public void NetworkWatcherApiTest()
        {
            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 = "eastus";

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

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

                properties.Location = location;
                //Create Network Watcher in the resource group
                var createNetworkWatcher = networkManagementClient.NetworkWatchers.CreateOrUpdate(resourceGroupName, networkWatcherName, properties);

                //Get Network Watcher by name in the resource group
                var getNetworkWatcherByName = networkManagementClient.NetworkWatchers.Get(resourceGroupName, networkWatcherName);

                //Get all Network Watchers in the resource group
                var getNetworkWatchersByResourceGroup = networkManagementClient.NetworkWatchers.List(resourceGroupName);

                //Get all Network Watchers in the subscription
                var getNetworkWatchersBySubscription = networkManagementClient.NetworkWatchers.ListAll();

                //Delete Network Watcher
                networkManagementClient.NetworkWatchers.Delete(resourceGroupName, networkWatcherName);

                //Get all Network Watchers in the subscription
                var getNetworkWatcherBySubscriptionAfterDeleting = networkManagementClient.NetworkWatchers.ListAll();

                //Verify name of the created Network Watcher
                Assert.Equal(networkWatcherName, getNetworkWatcherByName.Name);

                //Verify provisioning state
                Assert.Equal("Succeeded", getNetworkWatcherByName.ProvisioningState);

                //Verify the number of Network Watchers in the resource group (should be 1)
                Assert.Single(getNetworkWatchersByResourceGroup);

                //Verify the number of Network Watchers in the subscription
                Assert.Equal(2, getNetworkWatchersBySubscription.Count());

                //Verify the number of Network Watchers in the subscription after deleting one which was created in the test
                Assert.Single(getNetworkWatcherBySubscriptionAfterDeleting);
            }
        }
Ejemplo n.º 19
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);
            }
        }
Ejemplo n.º 20
0
        public void VirtualNetworkUsageTest()
        {
            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, true);
                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 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.1.0/24",
                        },
                    }
                };

                // Put Vnet
                var putVnetResponse = networkManagementClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnetName, vnet);
                Assert.Equal("Succeeded", putVnetResponse.ProvisioningState);

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

                // Get Vnet usage
                var listUsageResponse = networkManagementClient.VirtualNetworks.ListUsage(resourceGroupName, vnetName).ToList();
                Assert.Equal(0.0, listUsageResponse[0].CurrentValue);

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

                var nicParameters = new NetworkInterface()
                {
                    Location = location,
                    Tags     = new Dictionary <string, string>()
                    {
                        { "key", "value" }
                    },
                    IpConfigurations = new List <NetworkInterfaceIPConfiguration>()
                    {
                        new NetworkInterfaceIPConfiguration()
                        {
                            Name = ipConfigName,
                            PrivateIPAllocationMethod = IPAllocationMethod.Static,
                            PrivateIPAddress          = "10.0.1.9",
                            Subnet = new Subnet()
                            {
                                Id = getSubnetResponse.Id
                            }
                        }
                    }
                };

                var putNicResponse = networkManagementClient.NetworkInterfaces.CreateOrUpdate(resourceGroupName, nicName, nicParameters);

                // Get Vnet usage again
                listUsageResponse = networkManagementClient.VirtualNetworks.ListUsage(resourceGroupName, vnetName).ToList();
                Assert.Equal(1.0, listUsageResponse[0].CurrentValue);

                // Delete Vnet and Nic
                networkManagementClient.NetworkInterfaces.Delete(resourceGroupName, nicName);
                networkManagementClient.VirtualNetworks.Delete(resourceGroupName, vnetName);
            }
        }
Ejemplo n.º 21
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 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 = "West US";

                var a = HttpMockServer.Mode;


                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);

                // Get available WAF rule sets (validate first result set/group)
                var availableWAFRuleSets = networkManagementClient.ApplicationGateways.ListAvailableWafRuleSets();
                Assert.NotNull(availableWAFRuleSets);
                Assert.NotEmpty(availableWAFRuleSets.Value);
                Assert.NotNull(availableWAFRuleSets.Value[0].Name);
                Assert.NotNull(availableWAFRuleSets.Value[0].RuleSetType);
                Assert.NotNull(availableWAFRuleSets.Value[0].RuleSetVersion);
                Assert.NotEmpty(availableWAFRuleSets.Value[0].RuleGroups);
                Assert.NotNull(availableWAFRuleSets.Value[0].RuleGroups[0].RuleGroupName);
                Assert.NotEmpty(availableWAFRuleSets.Value[0].RuleGroups[0].Rules);
                // Assert.NotNull(availableWAFRuleSets.Value[0].RuleGroups[0].Rules[0].RuleId);

                // Get availalbe SSL options
                var sslOptions = networkManagementClient.ApplicationGateways.ListAvailableSslOptions();
                Assert.NotNull(sslOptions.DefaultPolicy);
                Assert.NotNull(sslOptions.AvailableCipherSuites);
                Assert.NotNull(sslOptions.AvailableCipherSuites[20]);

                var policies   = networkManagementClient.ApplicationGateways.ListAvailableSslPredefinedPolicies();
                var enumerator = policies.GetEnumerator();
                Assert.True(enumerator.MoveNext());
                Assert.NotNull(enumerator.Current.Name);

                var policy = networkManagementClient.ApplicationGateways.GetSslPredefinedPolicy(ApplicationGatewaySslPolicyName.AppGwSslPolicy20150501);
                Assert.NotNull(policy.MinProtocolVersion);
                Assert.NotNull(policy.CipherSuites);
                Assert.NotNull(policy.CipherSuites[20]);

                // 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);

                // Get AppGw backend health
                var backendHealth = networkManagementClient.ApplicationGateways.BackendHealth(resourceGroupName, appGwName, "true");
                Assert.Equal(2, backendHealth.BackendAddressPools.Count);
                Assert.Equal(1, backendHealth.BackendAddressPools[0].BackendHttpSettingsCollection.Count);
                Assert.Equal(1, backendHealth.BackendAddressPools[1].BackendHttpSettingsCollection.Count);
                Assert.True(backendHealth.BackendAddressPools[1].BackendAddressPool.BackendIPConfigurations.IsAny());


                //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);
            }
        }
        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);
            }
        }
Ejemplo n.º 24
0
        public void SubnetRouteTableTest()
        {
            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();
                string route1Name     = TestUtilities.GenerateName();

                var routeTable = new RouteTable()
                {
                    Location = location,
                };
                routeTable.Routes = new List <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);

                // Verify that the subnet reference is null
                Assert.Null(getRouteTableResponse.Subnets);

                // Create Vnet with subnet and add a route table
                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",
                            RouteTable    = new RouteTable()
                            {
                                Id = getRouteTableResponse.Id,
                            }
                        }
                    }
                };

                var putVnetResponse = networkManagementClient.VirtualNetworks.CreateOrUpdate(resourceGroupName, vnetName, vnet);
                Assert.Equal("Succeeded", putVnetResponse.ProvisioningState);

                var getSubnetResponse = networkManagementClient.Subnets.Get(resourceGroupName, vnetName, subnetName);
                Assert.Equal(getSubnetResponse.RouteTable.Id, getRouteTableResponse.Id);

                // Get RouteTable
                getRouteTableResponse = networkManagementClient.RouteTables.Get(
                    resourceGroupName,
                    routeTableName);
                Assert.Equal(1, getRouteTableResponse.Subnets.Count);
                Assert.Equal(getSubnetResponse.Id, getRouteTableResponse.Subnets[0].Id);
            }
        }
Ejemplo n.º 25
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);
            }
        }
Ejemplo n.º 26
0
        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);
            }
        }
Ejemplo n.º 27
0
        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);
            }
        }
Ejemplo n.º 28
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 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);
            }
        }
Ejemplo n.º 30
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());
            }
        }