public void RemoveTrafficManagerEndpointNonExistingFails()
        {
            // Setup
            ProfileWithDefinition original = GetProfileWithDefinition();

            var existingEndpoint = new TrafficManagerEndpoint
                {
                    DomainName = DomainName,
                    Type = EndpointType.Any,
                    Status = EndpointStatus.Enabled
                };

            original.Endpoints.Add(existingEndpoint);

            // Assert the endpoint exists
            Assert.IsTrue(original.Endpoints.Any(e => e.DomainName == DomainName));

            cmdlet = new RemoveAzureTrafficManagerEndpoint
                {
                    DomainName = DomainName,
                    TrafficManagerProfile = original,
                    CommandRuntime = mockCommandRuntime
                };

            // Action + Assert
            Testing.AssertThrows<Exception>(() => cmdlet.ExecuteCmdlet());
        }
        public void AddTrafficManagerEndpointAlreadyExistsFails()
        {
            // Setup
            ProfileWithDefinition original = GetProfileWithDefinition();

            var existingEndpoint = new TrafficManagerEndpoint
            {
                DomainName = DomainName,
                Type = EndpointType.Any,
                Status = EndpointStatus.Enabled
            };

            original.Endpoints.Add(existingEndpoint);

            cmdlet = new AddAzureTrafficManagerEndpoint
            {
                DomainName = DomainName,
                Type = AnyType,
                TrafficManagerProfile = original,
                CommandRuntime = mockCommandRuntime
            };

            // Action + Assert
            Testing.AssertThrows<Exception>(() => cmdlet.ExecuteCmdlet());
        }
        public void SetTrafficManagerEndpointSucceeds()
        {
            // Setup
            ProfileWithDefinition original = GetProfileWithDefinition();

            var existingEndpoint = new TrafficManagerEndpoint
            {
                DomainName = DomainName,
                Type = EndpointType.Any,
                Status = EndpointStatus.Enabled,
                Weight = 10,
                MinChildEndpoints = 2
            };

            original.Endpoints.Add(existingEndpoint);

            // Assert the endpoint exists
            Assert.IsTrue(original.Endpoints.Any(e => e.DomainName == DomainName));

            cmdlet = new SetAzureTrafficManagerEndpoint
            {
                DomainName = DomainName,
                TrafficManagerProfile = original,
                Weight = Weight,
                MinChildEndpoints = MinChildEndpoints,
                Location = Location,
                CommandRuntime = mockCommandRuntime
            };

            // Action
            cmdlet.ExecuteCmdlet();

            // Assert
            var actual = mockCommandRuntime.OutputPipeline[0] as ProfileWithDefinition;

            // All the properties stay the same except the endpoints
            AssertAllProfilePropertiesDontChangeExceptEndpoints(original, actual);

            // There is an endpoint with the domain name in "actual"
            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.Endpoints.Any(e => e.DomainName == DomainName));
            TrafficManagerEndpoint updatedEndpoint = actual.Endpoints.First(e => e.DomainName == DomainName);

            // Unchanged properties
            Assert.AreEqual(EndpointType.Any, updatedEndpoint.Type);
            Assert.AreEqual(EndpointStatus.Enabled, updatedEndpoint.Status);

            // Updated properties
            Assert.AreEqual(Weight, updatedEndpoint.Weight);
            Assert.AreEqual(Location, updatedEndpoint.Location);
            Assert.AreEqual(MinChildEndpoints, updatedEndpoint.MinChildEndpoints);
        }
        // Commented out because endpoints using this fields will be inconsistent
        // with Portal. This feature hasn't been announced.
//        [Parameter(Mandatory = false)]
//        public int? Weight { get; set; }

        public override void ExecuteCmdlet()
        {
            ProfileWithDefinition profile = TrafficManagerProfile.GetInstance();

            TrafficManagerEndpoint endpoint = profile.Endpoints.FirstOrDefault(e => e.DomainName == DomainName);

            if (endpoint == null)
            {
                if (String.IsNullOrEmpty(Type) ||
                    String.IsNullOrEmpty(Status) ||
                    String.IsNullOrEmpty(DomainName))
//                    // The weight must be set for endpoints part of a RoundRobin profile
//                    (!Weight.HasValue &&
//                        profile.LoadBalancingMethod == LoadBalancingMethod.RoundRobin))
                {
                    throw new Exception(Resources.SetTrafficManagerEndpointNeedsParameters);
                }

                WriteVerboseWithTimestamp(Resources.SetInexistentTrafficManagerEndpointMessage, Name, DomainName);
                endpoint = new TrafficManagerEndpoint();
                endpoint.DomainName = DomainName;
//                endpoint.Location = Location;
                endpoint.Type = (EndpointType)Enum.Parse(typeof(EndpointType), Type);
//                endpoint.Weight = Weight.HasValue ? Weight.Value : 0;
                endpoint.Status = (EndpointStatus)Enum.Parse(typeof(EndpointStatus), Status);

                // Add it because the endpoint didn't exist
                profile.Endpoints.Add(endpoint);
            }

//            endpoint.Location = Location ?? endpoint.Location;

            endpoint.Type = !String.IsNullOrEmpty(Type)
                ? (EndpointType)Enum.Parse(typeof(EndpointType), Type)
                : endpoint.Type;

//            endpoint.Weight = Weight.HasValue ? Weight.Value : endpoint.Weight;

            endpoint.Status = !String.IsNullOrEmpty(Status)
                ? (EndpointStatus)Enum.Parse(typeof (EndpointStatus), Status)
                : endpoint.Status;

            WriteObject(profile);
        }
        public override void ExecuteCmdlet()
        {
            ProfileWithDefinition profile = TrafficManagerProfile.GetInstance();

            TrafficManagerEndpoint endpoint = profile.Endpoints.FirstOrDefault(e => e.DomainName.Equals(DomainName, StringComparison.InvariantCultureIgnoreCase));

            if (endpoint == null)
            {
                if (String.IsNullOrEmpty(Type) ||
                    String.IsNullOrEmpty(Status) ||
                    String.IsNullOrEmpty(DomainName))
                {
                    throw new Exception(Resources.SetTrafficManagerEndpointNeedsParameters);
                }

                WriteVerboseWithTimestamp(Resources.SetInexistentTrafficManagerEndpointMessage, profile.Name, DomainName);
                endpoint = new TrafficManagerEndpoint();
                endpoint.DomainName = DomainName;
                endpoint.Location = Location;
                endpoint.Type = (EndpointType)Enum.Parse(typeof(EndpointType), Type);
                endpoint.Weight = Weight;
                endpoint.MinChildEndpoints = MinChildEndpoints;
                endpoint.Status = (EndpointStatus)Enum.Parse(typeof(EndpointStatus), Status);

                // Add it because the endpoint didn't exist
                profile.Endpoints.Add(endpoint);
            }

            endpoint.Location = Location ?? endpoint.Location;

            endpoint.Type = !String.IsNullOrEmpty(Type)
                ? (EndpointType)Enum.Parse(typeof(EndpointType), Type)
                : endpoint.Type;

            endpoint.Weight = Weight.HasValue ? Weight.Value : endpoint.Weight;
            endpoint.MinChildEndpoints = MinChildEndpoints.HasValue ? MinChildEndpoints.Value : endpoint.MinChildEndpoints;

            endpoint.Status = !String.IsNullOrEmpty(Status)
                ? (EndpointStatus)Enum.Parse(typeof (EndpointStatus), Status)
                : endpoint.Status;

            WriteObject(profile);
        }
        // Commented out because endpoints using this fields will be inconsistent
        // with Portal. This feature hasn't been announced.
//        [Parameter(Mandatory = false)]
//        public int Weight { get; set; }

        public override void ExecuteCmdlet()
        {
            TrafficManagerEndpoint endpoint = new TrafficManagerEndpoint();
            endpoint.DomainName = DomainName;
//            endpoint.Location = Location;
            endpoint.Status = (EndpointStatus)Enum.Parse(typeof(EndpointStatus), Status);
            endpoint.Type = (EndpointType)Enum.Parse(typeof(EndpointType), Type);
//            endpoint.Weight = Weight;
            ProfileWithDefinition profile = TrafficManagerProfile.GetInstance();

            if (profile.Endpoints.Any(e => e.DomainName == endpoint.DomainName))
            {
                throw new Exception(
                    string.Format(Resources.AddTrafficManagerEndpointFailed, profile.Name, endpoint.DomainName));
            }

            profile.Endpoints.Add(endpoint);
            WriteObject(TrafficManagerProfile);
        }
        public override void ExecuteCmdlet()
        {
            TrafficManagerEndpoint endpoint = new TrafficManagerEndpoint();
            endpoint.DomainName = DomainName;
            endpoint.Location = Location;
            endpoint.Status = (EndpointStatus)Enum.Parse(typeof(EndpointStatus), Status);
            endpoint.Type = (EndpointType)Enum.Parse(typeof(EndpointType), Type);
            endpoint.Weight = Weight.HasValue ? Weight.Value : 1;
            endpoint.MinChildEndpoints = MinChildEndpoints.HasValue ? MinChildEndpoints.Value : 1;
            ProfileWithDefinition profile = TrafficManagerProfile.GetInstance();

            if (profile.Endpoints.Any(e => e.DomainName.Equals(endpoint.DomainName, StringComparison.InvariantCultureIgnoreCase)))
            {
                throw new Exception(
                    string.Format(Resources.AddTrafficManagerEndpointFailed, profile.Name, endpoint.DomainName));
            }

            profile.Endpoints.Add(endpoint);
            WriteObject(TrafficManagerProfile);
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            TrafficManagerEndpoint endpoint = obj as TrafficManagerEndpoint;

            if (endpoint == null)
            {
                return(false);
            }

            return(DomainName == endpoint.DomainName &&
                   Location == endpoint.Location &&
                   Type == endpoint.Type &&
                   Status == endpoint.Status &&
                   MonitorStatus == endpoint.MonitorStatus &&
                   Weight == endpoint.Weight);
        }
        public void RemoveTrafficManagerEndpointSucceeds()
        {
            // Setup
            ProfileWithDefinition original = GetProfileWithDefinition();

            var existingEndpoint = new TrafficManagerEndpoint
                {
                    DomainName = DomainName,
                    Type = EndpointType.Any,
                    Status = EndpointStatus.Enabled
                };

            original.Endpoints.Add(existingEndpoint);

            // Assert the endpoint exists
            Assert.IsTrue(original.Endpoints.Any(e => e.DomainName == DomainName));

            cmdlet = new RemoveAzureTrafficManagerEndpoint
                {
                    DomainName = DomainName.ToUpper(),
                    TrafficManagerProfile = original,
                    CommandRuntime = mockCommandRuntime
                };

            // Action
            cmdlet.ExecuteCmdlet();

            // Assert
            var actual = mockCommandRuntime.OutputPipeline[0] as ProfileWithDefinition;

            // All the properties stay the same except the endpoints
            AssertAllProfilePropertiesDontChangeExceptEndpoints(original, actual);

            // There is a new endpoint with the new domain name in "actual"
            Assert.IsNotNull(actual);
            Assert.IsFalse(actual.Endpoints.Any(e => e.DomainName == DomainName));
        }
        public void SetTrafficManagerEndpointNotExisting()
        {
            // Setup
            ProfileWithDefinition original = GetProfileWithDefinition();
            TrafficManagerEndpoint expectedEndpoint = new TrafficManagerEndpoint()
            {
                DomainName = DomainName,
                Type = EndpointType.Any,
                Status = EndpointStatus.Enabled,
                Weight = Weight,
                MinChildEndpoints = MinChildEndpoints,
                Location = Location
            };

            cmdlet = new SetAzureTrafficManagerEndpoint
            {
                DomainName = DomainName,
                TrafficManagerProfile = original,
                Type = EndpointType.Any.ToString(),
                Weight = Weight,
                MinChildEndpoints = MinChildEndpoints,
                Location = Location,
                Status = EndpointStatus.Enabled.ToString(),
                CommandRuntime = mockCommandRuntime
            };

            // Assert the endpoint doesn't exist
            Assert.IsFalse(original.Endpoints.Any(e => e.DomainName == DomainName));

            // Action
            cmdlet.ExecuteCmdlet();

            var actual = mockCommandRuntime.OutputPipeline[0] as ProfileWithDefinition;

            // There is a new endpoint with the domain name in "actual"
            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.Endpoints.Any(e => e.DomainName == DomainName));
            TrafficManagerEndpoint newEndpoint = actual.Endpoints.First(e => e.DomainName == DomainName);
            Assert.AreEqual(expectedEndpoint, newEndpoint);
        }