protected override async Task Handle(DeleteContainerRequest request, CancellationToken cancellationToken) { var containerEntity = await _containerRepository.RetrieveContainerAsync(request.Id); if (containerEntity == null) { throw new StatusException(StatusCodes.Status404NotFound, $"Container with id '{request.Id}' does not exist."); } var container = _mapper.Map <ContainerDto>(containerEntity); var result = await _containerDtoValidator.ValidateAsync(container, cancellationToken); if (!result.IsValid) { throw new StatusException(StatusCodes.Status403Forbidden, "Deleting internal containers is not allowed."); } var success = await _containerRepository.DeleteContainerAsync(request.Id); if (!success) { throw new StatusException(StatusCodes.Status500InternalServerError, $"Container with id '{request.Id}' could not be deleted."); } }
private async Task <bool> HandleShipUndocked(string message) { var receivedShip = JsonSerializer.Deserialize <ShipDockedEventModel>(message); Ship ship = new Ship() { Containers = receivedShip.Containers, Id = receivedShip.ShipId }; foreach (var container in ship.Containers) { await _containerRepository.DeleteContainerAsync(container.Id); } await _shipRepository.DeleteShip(ship.Id); return(true); }
protected override async Task Handle(UpdateImageRequest request, CancellationToken cancellationToken) { var imageEntity = await _imageRepository.RetrieveImageAsync(request.Id); if (imageEntity == null) { throw new StatusException(StatusCodes.Status404NotFound, $"Image with id '{request.Id}' does not exist."); } var image = _mapper.Map <ImageDto>(imageEntity); var containerEntities = await _containerRepository .RetrieveAllContainersAsync(take : int.MaxValue); if (containerEntities == null) { throw new StatusException(StatusCodes.Status500InternalServerError, "Could not retrieve any containers during update."); } var containers = _mapper.Map <IEnumerable <ContainerDto> >(containerEntities) .Where(x => x.Image == image.Name && x.State == "running").ToList(); var networkEntities = await _networkRepository.RetrieveAllNetworksAsync(take : int.MaxValue); if (networkEntities == null) { throw new StatusException(StatusCodes.Status500InternalServerError, "Could not retrieve any networks during update."); } var networks = _mapper.Map <IEnumerable <NetworkDto> >(networkEntities).ToList(); var disconnectedNetworks = new Dictionary <string, IEnumerable <string> >(); foreach (var container in containers) { var containerNetworks = networks.Where(x => x.Containers.Contains(container.Id)).Select(x => x.Id); disconnectedNetworks.Add(container.Id, containerNetworks); var stoppedContainer = await _containerService.StopContainerAsync(container.Id); if (!stoppedContainer) { throw new StatusException(StatusCodes.Status500InternalServerError, $"Could not stop container with id '{container.Id}' during update."); } var deletedContainer = await _containerRepository.DeleteContainerAsync(container.Id); if (!deletedContainer) { throw new StatusException(StatusCodes.Status500InternalServerError, $"Could not delete container with id '{container.Id}' during update."); } } var registry = image.Name.Split('/').First(); var registryCredentials = await _authService.RetrieveCredentialsAsync(registry); var username = registryCredentials?.Item1; var password = registryCredentials?.Item2; var imageParts = image.Name.Split(':'); var imageName = imageParts.First(); var imageTag = imageParts.Last(); imageEntity = await _imageRepository.CreateImageAsync(imageName, imageTag, username, password); if (imageEntity == null) { throw new StatusException(StatusCodes.Status500InternalServerError, $"Could not create image '{image.Name}' during update."); } foreach (var container in containers) { var deploymentEntity = await _deploymentRepository.RetrieveDeploymentAsync(container.Deployment); if (deploymentEntity == null) { throw new StatusException(StatusCodes.Status500InternalServerError, $"Could not retrieve deployment with id '{container.Deployment}' during update."); } var parameters = _mapper.Map <CreateContainerParameters>(deploymentEntity); var containerEntity = await _containerRepository.CreateContainerAsync(parameters); if (containerEntity == null) { throw new StatusException(StatusCodes.Status500InternalServerError, $"Could not create container for deployment with id '{container.Deployment}' during update."); } var reconnectNetworks = disconnectedNetworks .FirstOrDefault(x => x.Key == container.Id).Value; foreach (var id in reconnectNetworks) { await _networkService.ConnectNetworkAsync(id, containerEntity.ID); } var startedContainer = await _containerService.StartContainerAsync(containerEntity.ID); if (!startedContainer) { throw new StatusException(StatusCodes.Status500InternalServerError, $"Could not start container with id '{containerEntity.ID}' during update."); } } }