public async Task <string> Handle(CreateImageRequest request, CancellationToken cancellationToken) { var deploymentEntity = await _deploymentRepository.RetrieveDeploymentAsync(request.Image.Deployment); if (deploymentEntity == null) { throw new StatusException(StatusCodes.Status400BadRequest, $"Deployment with id '{request.Image.Deployment}' does not exist."); } var imageEntity = await _imageRepository.RetrieveImageAsync(deploymentEntity.Image); if (imageEntity != null) { return(imageEntity.ID.Remove(0, 7)); } var imageParts = deploymentEntity.Image.Split(":"); var imageName = imageParts.First(); var imageTag = imageParts.Last(); var imageResponse = await _imageRepository.CreateImageAsync(imageName, imageTag, request.Username, request.Password); return(imageResponse switch { null when request.Username != null && request.Password == null => throw new StatusException( StatusCodes.Status403Forbidden, $"Image '{deploymentEntity.Image}' could not be created. A password must be provided."), null when request.Username == null && request.Password != null => throw new StatusException( StatusCodes.Status403Forbidden, $"Image '{deploymentEntity.Image}' could not be created. A username must be provided."), null => throw new StatusException(StatusCodes.Status400BadRequest, $"Image '{deploymentEntity.Image}' could not be created. Make sure that the image exists and can be accessed anonymously."), _ => imageResponse.ID.Remove(0, 7) });
public async Task <DeploymentDto> Handle(RetrieveDeploymentRequest request, CancellationToken cancellationToken) { var deploymentEntity = await _deploymentRepository.RetrieveDeploymentAsync(request.Id); if (deploymentEntity == null) { throw new StatusException(StatusCodes.Status404NotFound, $"Deployment with id '{request.Id}' does not exist."); } var deployment = _mapper.Map <DeploymentDto>(deploymentEntity); return(deployment); }
public async Task <string> Handle(CreateContainerRequest request, CancellationToken cancellationToken) { var containerEntity = await _containerRepository.RetrieveContainerAsync(request.Container.Deployment); if (containerEntity != null) { throw new StatusException(StatusCodes.Status400BadRequest, $"Container for deployment with id '{request.Container.Deployment}' does already exist."); } var deploymentEntity = await _deploymentRepository.RetrieveDeploymentAsync(request.Container.Deployment); if (deploymentEntity == null) { throw new StatusException(StatusCodes.Status400BadRequest, $"Deployment with id '{request.Container.Deployment}' does not exist."); } var imageEntity = await _imageRepository.RetrieveImageAsync(deploymentEntity.Image); if (imageEntity == null) { throw new StatusException(StatusCodes.Status400BadRequest, $"Image '{deploymentEntity.Image}' does not exist and needs to be pulled first."); } var parameters = _mapper.Map <CreateContainerParameters>(deploymentEntity); var entity = await _containerRepository.CreateContainerAsync(parameters); if (entity == null) { throw new StatusException(StatusCodes.Status500InternalServerError, $"Container for deployment with id '{request.Container.Deployment}' could not be created."); } return(entity.ID); }
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."); } } }