public void ValidateTestGroup(PSNetworkWatcherConnectionMonitorTestGroupObject testGroup)
        {
            if (string.IsNullOrEmpty(testGroup.Name))
            {
                throw new PSArgumentException(Properties.Resources.ConnectionMonitorTestGroupMustHaveName);
            }

            if (testGroup.TestConfigurations == null || !testGroup.TestConfigurations.Any())
            {
                throw new PSArgumentException(Properties.Resources.ConnectionMonitorTestGroupMustHaveTestConfiguration);
            }

            HashSet <string> testConfigurationNames = new HashSet <string>();

            foreach (PSNetworkWatcherConnectionMonitorTestConfigurationObject testConfiguration in testGroup.TestConfigurations)
            {
                this.ValidateTestConfiguration(testConfiguration);

                if (!testConfigurationNames.Add(testConfiguration.Name))
                {
                    throw new PSArgumentException(Properties.Resources.TestConfigurationNamesMustBeUnique);
                }
            }

            if (testGroup.Sources == null || !testGroup.Sources.Any())
            {
                throw new PSArgumentException(Properties.Resources.ConnectionMonitorTestGroupMustHaveSourceEndpoint);
            }

            HashSet <string> sourceEndpointNames = new HashSet <string>();

            foreach (PSNetworkWatcherConnectionMonitorEndpointObject endpoint in testGroup.Sources)
            {
                this.ValidateEndpoint(endpoint);

                if (!sourceEndpointNames.Add(endpoint.Name))
                {
                    throw new PSArgumentException(Properties.Resources.ConnectionMonitorSourceEndpointNamesMustBeUnique);
                }
            }

            if (testGroup.Destinations == null || !testGroup.Destinations.Any())
            {
                throw new PSArgumentException(Properties.Resources.ConnectionMonitorTestGroupMustHaveDestinationEndpoint);
            }

            HashSet <string> destinationEndpointNames = new HashSet <string>();

            foreach (PSNetworkWatcherConnectionMonitorEndpointObject endpoint in testGroup.Destinations)
            {
                this.ValidateEndpoint(endpoint);

                if (!destinationEndpointNames.Add(endpoint.Name))
                {
                    throw new PSArgumentException(Properties.Resources.ConnectionMonitorDestinationEndpointNamesMustBeUnique);
                }
            }
        }
        private void AddSourceEndpointsToConnectionMonitorTestGroup(
            PSNetworkWatcherConnectionMonitorTestGroupObject testGroup,
            ConnectionMonitorTestGroup cmTestGroup,
            ConnectionMonitor connectionMonitor)
        {
            foreach (PSNetworkWatcherConnectionMonitorEndpointObject sourceEndpoint in testGroup.Sources)
            {
                ConnectionMonitorEndpoint cmSourceEndpoint = new ConnectionMonitorEndpoint()
                {
                    Name          = sourceEndpoint.Name,
                    Type          = sourceEndpoint.Type,
                    ResourceId    = sourceEndpoint.ResourceId,
                    Address       = sourceEndpoint.Address,
                    CoverageLevel = sourceEndpoint.CoverageLevel
                };

                // Add ConnectionMonitorEndpointScope
                if (sourceEndpoint.Scope != null)
                {
                    cmSourceEndpoint.Scope = new ConnectionMonitorEndpointScope();

                    if (sourceEndpoint.Scope.Include != null)
                    {
                        cmSourceEndpoint.Scope.Include = new List <ConnectionMonitorEndpointScopeItem>();
                        foreach (PSNetworkWatcherConnectionMonitorEndpointScopeItem item in sourceEndpoint.Scope.Include)
                        {
                            cmSourceEndpoint.Scope.Include.Add(
                                new ConnectionMonitorEndpointScopeItem()
                            {
                                Address = item.Address
                            });
                        }
                    }

                    if (sourceEndpoint.Scope.Exclude != null)
                    {
                        cmSourceEndpoint.Scope.Exclude = new List <ConnectionMonitorEndpointScopeItem>();
                        foreach (PSNetworkWatcherConnectionMonitorEndpointScopeItem item in sourceEndpoint.Scope.Exclude)
                        {
                            cmSourceEndpoint.Scope.Exclude.Add(
                                new ConnectionMonitorEndpointScopeItem()
                            {
                                Address = item.Address
                            });
                        }
                    }
                }

                if (connectionMonitor.Endpoints.Count(x => x.Name == sourceEndpoint.Name) == 0)
                {
                    connectionMonitor.Endpoints.Add(cmSourceEndpoint);
                }

                cmTestGroup.Sources.Add(sourceEndpoint.Name);
            }
        }
        private void AddTestConfigurationsToConnectionMonitorTestGroup(
            PSNetworkWatcherConnectionMonitorTestGroupObject testGroup,
            ConnectionMonitorTestGroup cmTestGroup,
            ConnectionMonitor connectionMonitor)
        {
            foreach (PSNetworkWatcherConnectionMonitorTestConfigurationObject testConfiguration in testGroup.TestConfigurations)
            {
                ConnectionMonitorTestConfiguration cmTestConfiguration = new ConnectionMonitorTestConfiguration()
                {
                    Name = testConfiguration.Name,
                    PreferredIPVersion = testConfiguration.PreferredIPVersion,
                    TestFrequencySec   = testConfiguration.TestFrequencySec,
                    SuccessThreshold   = this.GetSuccessThreshold(testConfiguration)
                };

                if (testConfiguration.ProtocolConfiguration is PSNetworkWatcherConnectionMonitorTcpConfiguration tcpConfiguration)
                {
                    cmTestConfiguration.Protocol         = "Tcp";
                    cmTestConfiguration.TcpConfiguration = new ConnectionMonitorTcpConfiguration()
                    {
                        Port = tcpConfiguration.Port,
                        DisableTraceRoute = tcpConfiguration.DisableTraceRoute
                    };
                }
                else if (testConfiguration.ProtocolConfiguration is PSNetworkWatcherConnectionMonitorHttpConfiguration httpConfiguration)
                {
                    cmTestConfiguration.Protocol          = "Http";
                    cmTestConfiguration.HttpConfiguration = new ConnectionMonitorHttpConfiguration()
                    {
                        Port                  = httpConfiguration.Port,
                        Method                = httpConfiguration.Method,
                        Path                  = httpConfiguration.Path,
                        PreferHTTPS           = httpConfiguration.PreferHTTPS,
                        ValidStatusCodeRanges = httpConfiguration.ValidStatusCodeRanges,
                        RequestHeaders        = this.GetRequestHeaders(httpConfiguration)
                    };
                }
                else if (testConfiguration.ProtocolConfiguration is PSNetworkWatcherConnectionMonitorIcmpConfiguration icmpConfiguration)
                {
                    cmTestConfiguration.Protocol          = "Icmp";
                    cmTestConfiguration.IcmpConfiguration = new ConnectionMonitorIcmpConfiguration()
                    {
                        DisableTraceRoute = icmpConfiguration.DisableTraceRoute
                    };
                }

                if (connectionMonitor.TestConfigurations.Count(x => x.Name == cmTestConfiguration.Name) == 0)
                {
                    connectionMonitor.TestConfigurations.Add(cmTestConfiguration);
                }

                cmTestGroup.TestConfigurations.Add(cmTestConfiguration.Name);
            }
        }
        public override void Execute()
        {
            base.Execute();

            PSNetworkWatcherConnectionMonitorTestGroupObject testGroup = new PSNetworkWatcherConnectionMonitorTestGroupObject()
            {
                Name               = this.Name,
                Disable            = this.Disable.IsPresent,
                TestConfigurations = this.TestConfiguration,
                Sources            = this.Source,
                Destinations       = this.Destination
            };

            this.ValidateTestGroup(testGroup);

            WriteObject(testGroup);
        }
        private void AddDestinationEndpointsToConnectionMonitorTestGroup(
            PSNetworkWatcherConnectionMonitorTestGroupObject testGroup,
            ConnectionMonitorTestGroup cmTestGroup,
            ConnectionMonitor connectionMonitor)
        {
            foreach (PSNetworkWatcherConnectionMonitorEndpointObject destinationEndpoint in testGroup.Destinations)
            {
                ConnectionMonitorEndpoint cmDestinationEndpoint = new ConnectionMonitorEndpoint()
                {
                    Name       = destinationEndpoint.Name,
                    ResourceId = destinationEndpoint.ResourceId,
                    Address    = destinationEndpoint.Address,
                };

                // Add ConnectionMonitorEndpointFilterItem
                if (destinationEndpoint.Filter?.Items != null)
                {
                    cmDestinationEndpoint.Filter = new ConnectionMonitorEndpointFilter()
                    {
                        Type  = string.IsNullOrEmpty(destinationEndpoint.Filter.Type) ? "Include" : destinationEndpoint.Filter.Type,
                        Items = new List <ConnectionMonitorEndpointFilterItem>()
                    };

                    foreach (PSNetworkWatcherConnectionMonitorEndpointFilterItem item in destinationEndpoint.Filter.Items)
                    {
                        cmDestinationEndpoint.Filter.Items.Add(
                            new ConnectionMonitorEndpointFilterItem()
                        {
                            Type    = string.IsNullOrEmpty(item.Type) ? "AgentAddress" : item.Type,
                            Address = item.Address
                        });
                    }
                }

                if (connectionMonitor.Endpoints.Count(x => x.Name == destinationEndpoint.Name) == 0)
                {
                    connectionMonitor.Endpoints.Add(cmDestinationEndpoint);
                }

                cmTestGroup.Destinations.Add(cmDestinationEndpoint.Name);
            }
        }
        public PSConnectionMonitorResultV2 MapConnectionMonitorResultToPSConnectionMonitorResultV2(ConnectionMonitorResult connectionMonitor)
        {
            PSConnectionMonitorResultV2 psConnectionMonitor = new PSConnectionMonitorResultV2()
            {
                Name = connectionMonitor.Name,
                Id   = connectionMonitor.Id,
                Etag = connectionMonitor.Etag,
                ProvisioningState = connectionMonitor.ProvisioningState,
                Type                  = connectionMonitor.Type,
                Location              = connectionMonitor.Location,
                StartTime             = connectionMonitor.StartTime,
                Tags                  = new Dictionary <string, string>(),
                ConnectionMonitorType = connectionMonitor.ConnectionMonitorType,
                Notes                 = connectionMonitor.Notes,
                TestGroups            = new List <PSNetworkWatcherConnectionMonitorTestGroupObject>()
            };

            if (connectionMonitor.Tags != null)
            {
                foreach (KeyValuePair <string, string> KeyValue in connectionMonitor.Tags)
                {
                    psConnectionMonitor.Tags.Add(KeyValue.Key, KeyValue.Value);
                }
            }

            if (connectionMonitor.Outputs != null)
            {
                psConnectionMonitor.Outputs = new List <PSNetworkWatcherConnectionMonitorOutputObject>();
                foreach (ConnectionMonitorOutput output in connectionMonitor.Outputs)
                {
                    psConnectionMonitor.Outputs.Add(
                        new PSNetworkWatcherConnectionMonitorOutputObject()
                    {
                        Type = output.Type,
                        WorkspaceSettings = new PSConnectionMonitorWorkspaceSettings()
                        {
                            WorkspaceResourceId = output.WorkspaceSettings?.WorkspaceResourceId
                        }
                    });
                }
            }

            if (connectionMonitor.TestGroups != null)
            {
                foreach (ConnectionMonitorTestGroup testGroup in connectionMonitor.TestGroups)
                {
                    PSNetworkWatcherConnectionMonitorTestGroupObject testGroupObject = new PSNetworkWatcherConnectionMonitorTestGroupObject()
                    {
                        Name               = testGroup.Name,
                        Disable            = testGroup.Disable,
                        TestConfigurations = new List <PSNetworkWatcherConnectionMonitorTestConfigurationObject>(),
                        Sources            = new List <PSNetworkWatcherConnectionMonitorEndpointObject>(),
                        Destinations       = new List <PSNetworkWatcherConnectionMonitorEndpointObject>()
                    };

                    if (testGroup.Sources != null)
                    {
                        foreach (string sourceEndpointName in testGroup.Sources)
                        {
                            ConnectionMonitorEndpoint sourceEndpoint = GetEndpoinByName(connectionMonitor.Endpoints, sourceEndpointName);

                            PSNetworkWatcherConnectionMonitorEndpointObject EndpointObject =
                                NetworkResourceManagerProfile.Mapper.Map <PSNetworkWatcherConnectionMonitorEndpointObject>(sourceEndpoint);

                            testGroupObject.Sources.Add(EndpointObject);
                        }
                    }

                    if (testGroup.Destinations != null)
                    {
                        foreach (string destinationEndpointName in testGroup.Destinations)
                        {
                            ConnectionMonitorEndpoint destinationEndpoint = GetEndpoinByName(connectionMonitor.Endpoints, destinationEndpointName);

                            PSNetworkWatcherConnectionMonitorEndpointObject EndpointObject =
                                NetworkResourceManagerProfile.Mapper.Map <PSNetworkWatcherConnectionMonitorEndpointObject>(destinationEndpoint);

                            testGroupObject.Destinations.Add(EndpointObject);
                        }
                    }

                    // Test Configuration
                    if (testGroup.TestConfigurations != null)
                    {
                        foreach (string testConfigurationName in testGroup.TestConfigurations)
                        {
                            ConnectionMonitorTestConfiguration testConfiguration = GetTestConfigurationByName(connectionMonitor.TestConfigurations, testConfigurationName);

                            PSNetworkWatcherConnectionMonitorTestConfigurationObject testConfigurationObject = new PSNetworkWatcherConnectionMonitorTestConfigurationObject()
                            {
                                Name = testConfiguration.Name,
                                PreferredIPVersion = testConfiguration.PreferredIPVersion,
                                TestFrequencySec   = testConfiguration.TestFrequencySec,
                                SuccessThreshold   = testConfiguration.SuccessThreshold == null ? null :
                                                     new PSNetworkWatcherConnectionMonitorSuccessThreshold()
                                {
                                    ChecksFailedPercent = testConfiguration.SuccessThreshold.ChecksFailedPercent,
                                    RoundTripTimeMs     = testConfiguration.SuccessThreshold.RoundTripTimeMs
                                },
                                ProtocolConfiguration = this.GetPSProtocolConfiguration(testConfiguration)
                            };

                            testGroupObject.TestConfigurations.Add(testConfigurationObject);
                        }
                    }

                    psConnectionMonitor.TestGroups.Add(testGroupObject);
                }
            }

            return(psConnectionMonitor);
        }