public IActionResult DestroyServer(string serverId) { DatabaseServer targetServer = DocumentSession.GetDatabaseServerById(serverId); if (targetServer == null) { return(NotFound(new { Id = serverId, EntityType = "DatabaseServer", Message = $"No database server found with Id '{serverId}'." })); } if (DocumentSession.DoesServerHaveDatabases(targetServer.Id)) { return(StatusCode(StatusCodes.Status400BadRequest, new { Id = targetServer.Id, TenantId = serverId, EntityType = "DatabaseServer", RequestedAction = ProvisioningAction.Deprovision, Action = targetServer.Action, Status = targetServer.Status, Message = $"Cannot de-provision database server {targetServer.Id} because it still hosts one or more databases. First de-provision these databases and then retry the operation." })); } if (targetServer.Action != ProvisioningAction.None) { return(StatusCode(StatusCodes.Status409Conflict, new { Id = targetServer.Id, TenantId = serverId, EntityType = "DatabaseServer", RequestedAction = ProvisioningAction.Deprovision, Action = targetServer.Action, Status = targetServer.Status, Message = $"Cannot de-provision database server {targetServer.Id} because an action ({targetServer.Action}) is already in progress for this server." })); } targetServer.Action = ProvisioningAction.Deprovision; targetServer.Status = ProvisioningStatus.Pending; targetServer.AddProvisioningEvent($"De-provisioning requested for '${targetServer.Id}'."); DocumentSession.SaveChanges(); return(StatusCode(StatusCodes.Status202Accepted, new { Id = serverId, Message = $"Database server {targetServer.Id} queued for deletion.", EntityType = "DatabaseServer" })); }