Exemple #1
0
        /// <summary>
        /// Sets the VMs Usage state.
        /// </summary>
        /// <param name="hostNames">The host names collection</param>
        /// <param name="usageState">The usage state</param>
        public static void SetUsageState(IEnumerable <string> hostNames, VMUsageState usageState)
        {
            if (hostNames == null)
            {
                throw new ArgumentNullException("hostNames");
            }

            VMPowerState powerState = GetPowerState(usageState);

            using (AssetInventoryContext context = DbConnect.AssetInventoryContext())
            {
                foreach (string hostName in hostNames)
                {
                    SetState(hostName, powerState, usageState, (usageState == VMUsageState.Available), context);
                }
                context.SaveChanges();
            }
        }
Exemple #2
0
        /// <summary>
        /// Selects all <see cref="VirtualMachineReservation"/>s with the specified parameters.
        /// </summary>
        /// <param name="entities">The entities.</param>
        /// <param name="powerState">If specified, filters results to those with the specified <see cref="VMPowerState"/>.</param>
        /// <param name="usageState">If specified, filters results to those with the specified <see cref="VMUsageState"/>.</param>
        /// <param name="sessionId">If specified, filters results to those with the specified session id.</param>
        /// <param name="holdId">If specified, filters results to those with the specified hold id.</param>
        /// <returns></returns>
        public static IQueryable <FrameworkClient> Select
        (
            AssetInventoryContext entities,
            VMPowerState powerState = VMPowerState.None,
            VMUsageState usageState = VMUsageState.None,
            string sessionId        = NoCriteria,
            string holdId           = NoCriteria
        )
        {
            if (entities == null)
            {
                throw new ArgumentNullException("entities");
            }

            IQueryable <FrameworkClient> results = entities.FrameworkClients.Include(n => n.Platforms);

            if (powerState != VMPowerState.None)
            {
                string powerStateValue = EnumUtil.GetDescription(powerState);
                results = results.Where(n => n.PowerState == powerStateValue);
            }

            if (usageState != VMUsageState.None)
            {
                string usageStateValue = EnumUtil.GetDescription(usageState);
                results = results.Where(n => n.UsageState == usageStateValue);
            }

            if (sessionId != NoCriteria)
            {
                results = (string.IsNullOrEmpty(sessionId)) ?
                          results.Where(n => string.IsNullOrEmpty(n.SessionId)) :
                          results.Where(n => n.SessionId == sessionId);
            }

            if (holdId != NoCriteria)
            {
                results = (string.IsNullOrEmpty(holdId)) ?
                          results.Where(n => string.IsNullOrEmpty(n.HoldId)) :
                          results.Where(n => n.HoldId == holdId);
            }

            return(results.OrderBy(n => n.SortOrder));
        }
Exemple #3
0
        /// <summary>
        /// Sets the state of the VM.
        /// </summary>
        /// <param name="hostName">Name of the machine.</param>
        /// <param name="powerState">State of the power.</param>
        /// <param name="usageState">State of the usage.</param>
        /// <param name="clearSession">if set to <c>true</c> [clear session].</param>
        /// <param name="entities">The entities.</param>
        private static void SetState(string hostName, VMPowerState powerState, VMUsageState usageState, bool clearSession, AssetInventoryContext entities)
        {
            FrameworkClient machine = Select(entities, hostName);

            if (machine != null)
            {
                if (clearSession)
                {
                    machine.SessionId   = null;
                    machine.Environment = string.Empty;
                }
                machine.PowerState    = EnumUtil.GetDescription(powerState);
                machine.UsageState    = EnumUtil.GetDescription(usageState);
                machine.PlatformUsage = string.Empty;
                machine.LastUpdated   = DateTime.Now;
            }
            else
            {
                TraceFactory.Logger.Error("{0} not found in database".FormatWith(hostName));
            }
        }
Exemple #4
0
        /// <summary>
        /// Selects all <see cref="VirtualMachine"/>s with the specified parameters.
        /// </summary>
        /// <param name="powerState">If specified, filters results to those with the specified <see cref="VMPowerState"/>.</param>
        /// <param name="usageState">If specified, filters results to those with the specified <see cref="VMUsageState"/>.</param>
        /// <param name="sessionId">If specified, filters results to those with the specified session id.</param>
        /// <param name="holdId">If specified, filters results to those with the specified hold id.</param>
        /// <param name="platform">If specified, filters results to those that have an association with the specified platform.</param>
        /// <returns></returns>
        public static IEnumerable <VirtualMachine> Select
        (
            VMPowerState powerState = VMPowerState.None,
            VMUsageState usageState = VMUsageState.None,
            string sessionId        = NoCriteria,
            string holdId           = NoCriteria,
            string platform         = NoCriteria)
        {
            Collection <VirtualMachine> result = new Collection <VirtualMachine>();

            using (AssetInventoryContext context = DbConnect.AssetInventoryContext())
            {
                foreach (FrameworkClient reservation in Select(context, powerState, usageState, sessionId, holdId))
                {
                    result.Add(new VirtualMachine(reservation));
                }
            }
            if (platform != NoCriteria)
            {
                return(result.Where(v => v.Platforms.Any(p => p.FrameworkClientPlatformId == platform)).OrderBy(n => n.SortOrder));
            }

            return(result.OrderBy(n => n.SortOrder));
        }