protected override async Task Handle(ConnectNetworkRequest request, CancellationToken cancellationToken) { var networkId = request.Id; var containerId = request.Network.Container; var networkEntity = await _networkRepository.RetrieveNetworkAsync(networkId); if (networkEntity == null) { throw new StatusException(StatusCodes.Status404NotFound, $"Network with id '{request.Id}' does not exist."); } var containerEntity = await _containerRepository.RetrieveContainerAsync(containerId); if (containerEntity == null) { throw new StatusException(StatusCodes.Status404NotFound, $"Container with id '{request.Network.Container}' does not exist."); } var success = await _networkService.ConnectNetworkAsync(networkId, containerId); if (!success) { throw new StatusException(StatusCodes.Status500InternalServerError, $"Could not connect container '{containerId}' to network with id '{networkId}'."); } }
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."); } }
public async Task <ContainerDto> Handle(RetrieveContainerRequest 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 = _containerDtoValidator.Validate(container); if (!result.IsValid) { throw new StatusException(StatusCodes.Status403Forbidden, "Retrieving internal containers is not allowed."); } return(container); }
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(StartContainerRequest 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 success = await _containerService.StartContainerAsync(request.Id); if (!success && containerEntity.State.Running) { throw new StatusException(StatusCodes.Status400BadRequest, $"Container with id '{request.Id}' is already running."); } if (!success) { throw new StatusException(StatusCodes.Status500InternalServerError, $"Container with id '{request.Id}' could not be started."); } }