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);
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
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 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);
            }
        }
        public void CheckConnectivityVmToInternetTest()
        {
            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, true);
                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 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();
                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);

                ConnectivityParameters connectivityParameters = new ConnectivityParameters();
                connectivityParameters.Source              = new ConnectivitySource();
                connectivityParameters.Source.ResourceId   = getVm.Id;
                connectivityParameters.Destination         = new ConnectivityDestination();
                connectivityParameters.Destination.Address = "bing.com";
                connectivityParameters.Destination.Port    = 80;

                var connectivityCheck = networkManagementClient.NetworkWatchers.CheckConnectivity(resourceGroupName, networkWatcherName, connectivityParameters);

                //Validation
                Assert.Equal("Reachable", connectivityCheck.ConnectionStatus);
                Assert.Equal(0, connectivityCheck.ProbesFailed);
                Assert.Equal("Source", connectivityCheck.Hops.FirstOrDefault().Type);
                Assert.Equal("Internet", connectivityCheck.Hops.LastOrDefault().Type);
            }
        }
Exemplo n.º 8
0
        public void VerifyIpFlowApiTest()
        {
            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 = "eastus";

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

                string virtualMachineName1      = TestUtilities.GenerateName();
                string networkInterfaceName1    = TestUtilities.GenerateName();
                string networkSecurityGroupName = virtualMachineName1 + "-nsg";

                //Deploy VM with a template
                Deployments.CreateVm(
                    resourcesClient: resourcesClient,
                    resourceGroupName: resourceGroupName,
                    location: location,
                    virtualMachineName: virtualMachineName1,
                    storageAccountName: TestUtilities.GenerateName(),
                    networkInterfaceName: networkInterfaceName1,
                    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    getVm1         = computeManagementClient.VirtualMachines.Get(resourceGroupName, virtualMachineName1);
                string localIPAddress = networkManagementClient.NetworkInterfaces.Get(resourceGroupName, networkInterfaceName1).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);

                VerificationIPFlowParameters ipFlowProperties = new VerificationIPFlowParameters()
                {
                    TargetResourceId = getVm1.Id,
                    Direction        = "Outbound",
                    Protocol         = "TCP",
                    LocalPort        = "80",
                    RemotePort       = "80",
                    LocalIPAddress   = localIPAddress,
                    RemoteIPAddress  = "12.11.12.14"
                };

                //Verify IP flow from a VM to a location given the configured  rule
                var verifyIpFlow = networkManagementClient.NetworkWatchers.VerifyIPFlow(resourceGroupName, networkWatcherName, ipFlowProperties);

                //Verify validity of the result
                Assert.Equal("Deny", verifyIpFlow.Access);
                Assert.Equal("securityRules/" + securityRule1, verifyIpFlow.RuleName);
            }
        }
Exemplo n.º 9
0
        public void TopologyApiTest()
        {
            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 = "eastus";

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

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

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

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

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

                //Create NetworkWatcher
                var createNetworkWatcher = networkManagementClient.NetworkWatchers.CreateOrUpdate(resourceGroupName2, networkWatcherName, properties);

                TopologyParameters tpProperties = new TopologyParameters()
                {
                    TargetResourceGroupName = resourceGroupName1
                };

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

                //Get the current network topology of the resourceGroupName1
                var getTopology = networkManagementClient.NetworkWatchers.GetTopology(resourceGroupName2, networkWatcherName, tpProperties);


                //Getting infromation about VM from topology
                TopologyResource vmResource = getTopology.Resources[2];

                //Verify that topology contain right number of resources (9 resources from template)
                Assert.Equal(9, getTopology.Resources.Count);

                //Verify that topology contain information about acreated VM
                Assert.Equal(virtualMachineName, vmResource.Name);
                Assert.Equal(getVm.Id, vmResource.Id);
                Assert.Equal(networkInterfaceName, vmResource.Associations.FirstOrDefault().Name);
                Assert.Equal(getVm.NetworkProfile.NetworkInterfaces.FirstOrDefault().Id, vmResource.Associations.FirstOrDefault().ResourceId);
                Assert.Equal("Contains", vmResource.Associations.FirstOrDefault().AssociationType);
            }
        }