private void remove_Button_Click(object sender, EventArgs e) { var row = GetFirstSelectedRow(); if (row != null) { DomainAccountReservation domainAccountReservation = row.DataBoundItem as DomainAccountReservation; DialogResult dialogResult = MessageBox.Show($"Removing Domain Account Reservation with session id'{domainAccountReservation.SessionId.Trim()}'. Do you want to continue?", "Delete Domain Account Reservation", MessageBoxButtons.YesNo, MessageBoxIcon.Question); SessionSummary status = null; if (dialogResult == DialogResult.Yes) { using (DataLogContext logContext = DbConnect.DataLogContext()) { status = logContext.DbSessions.FirstOrDefault(n => n.SessionId == domainAccountReservation.SessionId && n.Status == "Running"); } using (AssetInventoryContext context = DbConnect.AssetInventoryContext()) { if (status == null) { DomainAccountReservation asset = context.DomainAccountReservations.FirstOrDefault(n => n.SessionId == domainAccountReservation.SessionId); context.DomainAccountReservations.Remove(asset); context.SaveChanges(); } else { MessageBox.Show($"Domain Account Reservation cannot be removed for the session id '{domainAccountReservation.SessionId.Trim()}' as the session is still running", "Session is currently running", MessageBoxButtons.OK, MessageBoxIcon.Error); } } RefreshItems(); } } }
/// <summary> /// Reserves a block of User Ids for the given session. /// </summary> /// <param name="context">The data context.</param> /// <param name="sessionId">The session id.</param> /// <param name="accountPool">The account pool.</param> /// <param name="requestedSize">The size of reservation block being requested.</param> /// <returns>The start index for the reserved block</returns> private static int ReserveBlock(AssetInventoryContext context, string sessionId, DomainAccountPool accountPool, int requestedSize) { if (accountPool == null) { throw new ArgumentNullException("accountPool"); } // Calculate the start index of the new block int startIndex = accountPool.MinimumUserNumber; foreach (DomainAccountReservation reservation in SelectReservationsByKey(context, accountPool.DomainAccountKey)) { // Check to see if there are any users between reservations that we can utilize. // For example, say there were 2 reservations against this DomainAccountKey, but the first one got released // Now there is a gap between the minimum user number and the start index of the reservation we are looking at. // We want to utilize that block, if possible. if ((reservation.StartIndex - startIndex) >= requestedSize) { break; } // Not enough space for the requested size, so check the next one. startIndex = reservation.StartIndex + reservation.Count; } // Now that we have the start index for our new block, we need to see if there is enough room // for the block before we hit the "ceiling" of our domain users. int available = accountPool.MaximumUserNumber - startIndex + 1; if (available < requestedSize) { throw new InsufficientDomainAccountsException($"There were not enough domain accounts available to run this scenario. PoolName: {accountPool.DomainAccountKey} Available: {available} Number Requested: {requestedSize}"); } DomainAccountReservation newReservation = new DomainAccountReservation { SessionId = sessionId, DomainAccountKey = accountPool.DomainAccountKey, StartIndex = startIndex, Count = requestedSize }; context.DomainAccountReservations.Add(newReservation); context.SaveChanges(); return(startIndex); }