コード例 #1
0
        public static EndpointViewModel ToEndpointViewModel(this Endpoint endpoint)
        {
            EndpointViewModel vm;

            if (endpoint.Kind == Endpoint.EndpointKind.Http)
            {
                vm = new HttpEndpointViewModel
                {
                    Location      = endpoint.GetHttpLocation() ?? "",
                    ResponseMatch = endpoint.GetHttpResponseMatch() ?? "",
                    StatusCodes   = string.Join(',', endpoint.GetHttpStatusCodes()?.Select(c => c.ToString()) ?? new string[0]),
                    IgnoreCerts   = endpoint.GetHttpIgnoreTlsCerts(),
                    CustomCert    = endpoint.GetHttpCustomTlsCert() ?? ""
                };
            }
            else if (endpoint.Kind == Endpoint.EndpointKind.Icmp)
            {
                vm = new IcmpEndpointViewModel
                {
                    Address = endpoint.GetIcmpAddress()
                };
            }
            else
            {
                throw new NotImplementedException();
            }

            vm.Id      = endpoint.Id;
            vm.Kind    = endpoint.Kind;
            vm.Enabled = endpoint.Enabled;
            vm.Name    = endpoint.Name;

            return(vm);
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id, HttpEndpointViewModel vm)
        {
            if (id != vm.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var endpoint    = vm.ToEndpoint();
                    var oldEndpoint = _context.Endpoints.AsNoTracking().First(e => e.Id == endpoint.Id);
                    _context.Update(endpoint);
                    await _context.SaveChangesAsync();

                    NotifyEditEvent(oldEndpoint, endpoint);
                    _stateService.RemoveStatus(endpoint.Id);
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EndpointExists(vm.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(List)));
            }
            return(View(nameof(Edit), vm));
        }
コード例 #3
0
        public void ShouldConvertHttpViewModelToEndpoint()
        {
            var vm1 = new HttpEndpointViewModel {
                Id = 5, Enabled = true, Kind = Endpoint.EndpointKind.Http, Name = "fdsa", Location = "asdf", ResponseMatch = "qwerty", StatusCodes = " 201, 500", IgnoreCerts = true, CustomCert = "zxcvbnm"
            };
            var ep1 = vm1.ToEndpoint();

            Assert.AreEqual(5, ep1.Id);
            Assert.IsTrue(ep1.Enabled);
            Assert.AreEqual(Endpoint.EndpointKind.Http, ep1.Kind);
            Assert.AreEqual("fdsa", ep1.Name);
            Assert.AreEqual("asdf", ep1.GetHttpLocation());
            Assert.AreEqual("qwerty", ep1.GetHttpResponseMatch());
            CollectionAssert.AreEquivalent(new List <int> {
                201, 500
            }, ep1.GetHttpStatusCodes());
            Assert.IsTrue(ep1.GetHttpIgnoreTlsCerts());
            Assert.AreEqual("zxcvbnm", ep1.GetHttpCustomTlsCert());
            Assert.IsTrue(DateTimeOffset.UtcNow.AddMinutes(-1) < ep1.LastUpdated);

            var vm2 = new HttpEndpointViewModel {
                Kind = Endpoint.EndpointKind.Http
            };
            var ep2 = vm2.ToEndpoint();

            Assert.AreEqual(Endpoint.EndpointKind.Http, ep2.Kind);
            Assert.IsTrue(DateTimeOffset.UtcNow.AddMinutes(-1) < ep2.LastUpdated);
        }
コード例 #4
0
        public async Task <IActionResult> Create(HttpEndpointViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var endpoint = vm.ToEndpoint();
                endpoint.Id = 0;
                _context.Add(endpoint);
                await _context.SaveChangesAsync();

                NotifyEvent(endpoint, NotificationType.EndpointAdded);
                return(RedirectToAction(nameof(List)));
            }
            return(View(nameof(Create), vm));
        }