private void ValidateEndpointResourceId(PSNetworkWatcherConnectionMonitorEndpointObject endpoint)
        {
            if (string.IsNullOrEmpty(endpoint.ResourceId))
            {
                return;
            }

            string[] SplittedName = endpoint.ResourceId.Split('/');

            // Resource ID must be in the format "/subscriptions/00000000-0000-0000-0000-00000000/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/name"
            if (SplittedName.Count() < 9)
            {
                throw new PSArgumentException(Properties.Resources.InvalidEndpointResourceId);
            }

            string resourceType = SplittedName[7];

            if (string.IsNullOrEmpty(resourceType) || (!resourceType.Equals("virtualMachines", StringComparison.OrdinalIgnoreCase) &&
                                                       !resourceType.Equals("virtualMachineScaleSets", StringComparison.OrdinalIgnoreCase) &&
                                                       !resourceType.Equals("workspaces", StringComparison.OrdinalIgnoreCase)))
            {
                throw new PSArgumentException(Properties.Resources.InvalidEndpointResourceType);
            }

            if (resourceType.Equals("workspaces", StringComparison.OrdinalIgnoreCase) && (endpoint.Filter?.Items == null || !endpoint.Filter.Items.Any()))
            {
                throw new PSArgumentException(Properties.Resources.EndpointFilterItemIsMissing);
            }

            if (endpoint.Filter?.Items != null && endpoint.Filter.Items.Any() && !resourceType.Equals("workspaces", StringComparison.OrdinalIgnoreCase))
            {
                throw new PSArgumentException(Properties.Resources.UnsupportedEndpointTypeForEndpointWithFilter);
            }
        }
        private void ValidateEndpointResourceId(PSNetworkWatcherConnectionMonitorEndpointObject endpoint)
        {
            if (string.IsNullOrEmpty(endpoint.ResourceId))
            {
                return;
            }

            string[] splittedName = endpoint.ResourceId.Split('/');

            // Resource ID must be in the format "/subscriptions/00000000-0000-0000-0000-00000000/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/name"
            if (splittedName.Count() < 9)
            {
                throw new PSArgumentException(Properties.Resources.InvalidEndpointResourceId);
            }

            string resourceType = splittedName[7];

            if (string.IsNullOrEmpty(resourceType) || (!resourceType.Equals("virtualMachines", StringComparison.OrdinalIgnoreCase) &&
                                                       !resourceType.Equals("workspaces", StringComparison.OrdinalIgnoreCase) &&
                                                       !resourceType.Equals("virtualNetworks", StringComparison.OrdinalIgnoreCase)))
            {
                throw new PSArgumentException(Properties.Resources.InvalidEndpointResourceType);
            }

            if (string.Equals(endpoint.Type, "AzureVM", StringComparison.OrdinalIgnoreCase))
            {
                if (!resourceType.Equals("virtualMachines", StringComparison.OrdinalIgnoreCase))
                {
                    throw new PSArgumentException(Properties.Resources.InvalidEndpointResourceIdForSpecifiedType, endpoint.Type);
                }
            }
            else if (string.Equals(endpoint.Type, "AzureVNet", StringComparison.OrdinalIgnoreCase))
            {
                if (!resourceType.Equals("virtualNetworks", StringComparison.OrdinalIgnoreCase))
                {
                    throw new PSArgumentException(Properties.Resources.InvalidEndpointResourceIdForSpecifiedType, endpoint.Type);
                }
            }
            else if (string.Equals(endpoint.Type, "AzureSubnet", StringComparison.OrdinalIgnoreCase))
            {
                if (!resourceType.Equals("virtualNetworks", StringComparison.OrdinalIgnoreCase) || splittedName.Count() != 11 ||
                    splittedName[9].Equals("subnet", StringComparison.OrdinalIgnoreCase))
                {
                    throw new PSArgumentException(Properties.Resources.InvalidEndpointResourceIdForSpecifiedType, endpoint.Type);
                }
            }
            else if ((string.Equals(endpoint.Type, "MMAWorkspaceMachine", StringComparison.OrdinalIgnoreCase) || string.Equals(endpoint.Type, "MMAWorkspaceNetwork", StringComparison.OrdinalIgnoreCase)) &&
                     !resourceType.Equals("workspaces", StringComparison.OrdinalIgnoreCase))
            {
                throw new PSArgumentException(Properties.Resources.InvalidEndpointResourceIdForSpecifiedType, endpoint.Type);
            }
        }
        public override void Execute()
        {
            base.Execute();

            if (string.IsNullOrEmpty(this.Name))
            {
                if (!string.IsNullOrEmpty(this.ResourceId))
                {
                    string[] SplittedName = ResourceId.Split('/');
                    // Name is in the form resourceName(ResourceGroupName)
                    this.Name = SplittedName[8] + "(" + SplittedName[4] + ")";
                }
                else if (!string.IsNullOrEmpty(this.Address))
                {
                    this.Name = this.Address;
                }
            }

            PSNetworkWatcherConnectionMonitorEndpointObject endpoint = new PSNetworkWatcherConnectionMonitorEndpointObject()
            {
                Name       = this.Name,
                ResourceId = this.ResourceId,
                Address    = this.Address,
            };

            if (this.FilterItem != null)
            {
                endpoint.Filter = new PSNetworkWatcherConnectionMonitorEndpointFilter()
                {
                    Type = FilterType == null ? "Include" : this.FilterType
                };

                foreach (PSNetworkWatcherConnectionMonitorEndpointFilterItem Item in this.FilterItem)
                {
                    if (endpoint.Filter.Items == null)
                    {
                        endpoint.Filter.Items = new List <PSNetworkWatcherConnectionMonitorEndpointFilterItem>();
                    }

                    endpoint.Filter.Items.Add(new PSNetworkWatcherConnectionMonitorEndpointFilterItem()
                    {
                        Type    = string.IsNullOrEmpty(Item.Type) ? "AgentAddress" : Item.Type,
                        Address = Item.Address
                    });
                }
            }

            this.ValidateEndpoint(endpoint);

            WriteObject(endpoint);
        }
        private void ValidateEndpointType(PSNetworkWatcherConnectionMonitorEndpointObject endpoint)
        {
            if (string.IsNullOrEmpty(endpoint.Type))
            {
                throw new PSArgumentException(Properties.Resources.EmptyEndpointType, endpoint.Name);
            }

            if (!string.Equals(endpoint.Type, "AzureVM", StringComparison.OrdinalIgnoreCase) && !string.Equals(endpoint.Type, "AzureVNet", StringComparison.OrdinalIgnoreCase) &&
                !string.Equals(endpoint.Type, "AzureSubnet", StringComparison.OrdinalIgnoreCase) && !string.Equals(endpoint.Type, "MMAWorkspaceMachine", StringComparison.OrdinalIgnoreCase) &&
                !string.Equals(endpoint.Type, "MMAWorkspaceNetwork", StringComparison.OrdinalIgnoreCase) && !string.Equals(endpoint.Type, "ExternalAddress", StringComparison.OrdinalIgnoreCase))
            {
                throw new PSArgumentException(Properties.Resources.InvalidEndpointType, endpoint.Name);
            }
        }
        public void ValidateEndpoint(PSNetworkWatcherConnectionMonitorEndpointObject endpoint)
        {
            if (string.IsNullOrEmpty(endpoint.Name))
            {
                throw new PSArgumentException(Properties.Resources.ConnectionMonitorEndpointMustHaveName);
            }

            if (string.IsNullOrEmpty(endpoint.ResourceId) && string.IsNullOrEmpty(endpoint.Address))
            {
                throw new PSArgumentException(Properties.Resources.MissedPropertiesInConnectionMonitorEndpoint);
            }

            this.ValidateEndpointType(endpoint);
            this.ValidateEndpointResourceId(endpoint);
        }
Esempio n. 6
0
        public override void Execute()
        {
            base.Execute();

            PSNetworkWatcherConnectionMonitorEndpointObject endpoint = new PSNetworkWatcherConnectionMonitorEndpointObject()
            {
                Name          = this.Name,
                Type          = this.GetEndpointType(),
                ResourceId    = this.ResourceId,
                Address       = this.Address,
                CoverageLevel = this.CoverageLevel
            };

            if (this.IncludeItem != null || this.ExcludeItem != null)
            {
                endpoint.Scope = new PSNetworkWatcherConnectionMonitorEndpointScope();

                if (this.IncludeItem != null)
                {
                    endpoint.Scope.Include = new List <PSNetworkWatcherConnectionMonitorEndpointScopeItem>();
                    foreach (PSNetworkWatcherConnectionMonitorEndpointScopeItem item in this.IncludeItem)
                    {
                        endpoint.Scope.Include.Add(new PSNetworkWatcherConnectionMonitorEndpointScopeItem()
                        {
                            Address = item.Address
                        });
                    }
                }

                if (this.ExcludeItem != null)
                {
                    endpoint.Scope.Exclude = new List <PSNetworkWatcherConnectionMonitorEndpointScopeItem>();
                    foreach (PSNetworkWatcherConnectionMonitorEndpointScopeItem item in this.ExcludeItem)
                    {
                        endpoint.Scope.Exclude.Add(new PSNetworkWatcherConnectionMonitorEndpointScopeItem()
                        {
                            Address = item.Address
                        });
                    }
                }
            }

            this.ValidateEndpoint(endpoint);

            WriteObject(endpoint);
        }
        private void ValidateEndpointFilter(PSNetworkWatcherConnectionMonitorEndpointObject endpoint)
        {
            if (endpoint.Filter == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(endpoint.Filter.Type) && !endpoint.Filter.Type.Equals("Include", StringComparison.OrdinalIgnoreCase))
            {
                throw new PSArgumentException(Properties.Resources.UnsupportedEndpointFilterType);
            }

            if (!string.IsNullOrEmpty(endpoint.Filter.Type) && (endpoint.Filter.Items == null || (endpoint.Filter.Items != null && !endpoint.Filter.Items.Any())))
            {
                throw new PSArgumentException(Properties.Resources.EndpointWilthFilterTypeMustHaveFilterItem);
            }

            if (endpoint.Filter.Items != null && endpoint.Filter.Items.Any() && string.IsNullOrEmpty(endpoint.ResourceId))
            {
                throw new PSArgumentException(Properties.Resources.ResourceIDIsMissingInEndpointWithFilter);
            }

            this.ValidateEndpointFilterItemList(endpoint.Filter?.Items);
        }
        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);
        }