public async Task <object> Patch(string id, [FromBody] dynamic model)
        {
            // Cut off the notion of uuid from beginning of request
            string name = AppPoolId.CreateFromUuid(id).Name;

            // Set settings
            ApplicationPool appPool = AppPoolHelper.UpdateAppPool(name, model);

            if (appPool == null)
            {
                return(NotFound());
            }

            if (model.identity != null && !await IsAppPoolIdentityAllowed(appPool))
            {
                return(new ForbidResult());
            }

            // Start/Stop
            if (model.status != null)
            {
                Status status = DynamicHelper.To <Status>(model.status);
                try {
                    switch (status)
                    {
                    case Status.Stopped:
                        appPool.Stop();
                        break;

                    case Status.Started:
                        appPool.Start();
                        break;
                    }
                }
                catch (COMException e) {
                    // If pool is fresh and status is still unknown then COMException will be thrown when manipulating status
                    throw new ApiException("Error setting application pool status", e);
                }
            }

            // Update changes
            ManagementUnit.Current.Commit();

            // Refresh data
            appPool = ManagementUnit.ServerManager.ApplicationPools[appPool.Name];

            //
            // Create response
            dynamic pool = AppPoolHelper.ToJsonModel(appPool, Context.Request.GetFields());

            // The Id could change by changing apppool name
            if (pool.id != id)
            {
                return(LocationChanged(AppPoolHelper.GetLocation(pool.id), pool));
            }

            return(pool);
        }
        public object Get(string id)
        {
            // Extract the name of the target app pool from the uuid specified in the request
            string name = AppPoolId.CreateFromUuid(id).Name;

            ApplicationPool pool = AppPoolHelper.GetAppPool(name);

            if (pool == null)
            {
                return(NotFound());
            }

            return(AppPoolHelper.ToJsonModel(pool, Context.Request.GetFields()));
        }
        public void Delete(string id)
        {
            string name = AppPoolId.CreateFromUuid(id).Name;

            ApplicationPool pool = AppPoolHelper.GetAppPool(name);

            if (pool != null)
            {
                AppPoolHelper.DeleteAppPool(pool);
                ManagementUnit.Current.Commit();
            }

            // Sucess
            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
        }
Exemple #4
0
        internal static object ToJsonModel(ApplicationPool pool, Fields fields = null, bool full = true)
        {
            if (pool == null)
            {
                return(null);
            }

            if (fields == null)
            {
                fields = Fields.All;
            }

            dynamic obj = new ExpandoObject();

            //
            // name
            if (fields.Exists("name"))
            {
                obj.name = pool.Name;
            }

            //
            // id
            obj.id = AppPoolId.CreateFromName(pool.Name).Uuid;

            //
            // status
            if (fields.Exists("status"))
            {
                // Prepare state
                Status state = Status.Unknown;
                try {
                    state = StatusExtensions.FromObjectState(pool.State);
                }
                catch (COMException) {
                    // Problem getting state of app pool. Possible reasons:
                    // 1. App pool's application pool was deleted.
                    // 2. App pool was just created and the status is not accessible yet.
                }
                obj.status = Enum.GetName(typeof(Status), state).ToLower();
            }

            //
            // auto_start
            if (fields.Exists("auto_start"))
            {
                obj.auto_start = pool.AutoStart;
            }

            //
            // pipeline_mode
            if (fields.Exists("pipeline_mode"))
            {
                obj.pipeline_mode = Enum.GetName(typeof(ManagedPipelineMode), pool.ManagedPipelineMode).ToLower();
            }

            //
            // managed_runtime_version
            if (fields.Exists("managed_runtime_version"))
            {
                obj.managed_runtime_version = pool.ManagedRuntimeVersion;
            }

            //
            // enable_32bit_win64
            if (fields.Exists("enable_32bit_win64"))
            {
                obj.enable_32bit_win64 = pool.Enable32BitAppOnWin64;
            }

            //
            // queue_length
            if (fields.Exists("queue_length"))
            {
                obj.queue_length = pool.QueueLength;
            }

            //
            // cpu
            if (fields.Exists("cpu"))
            {
                obj.cpu = new {
                    limit                      = pool.Cpu.Limit,
                    limit_interval             = pool.Cpu.ResetInterval.TotalMinutes,
                    action                     = Enum.GetName(typeof(ProcessorAction), pool.Cpu.Action),
                    processor_affinity_enabled = pool.Cpu.SmpAffinitized,
                    processor_affinity_mask32  = "0x" + pool.Cpu.SmpProcessorAffinityMask.ToString("X"),
                    processor_affinity_mask64  = "0x" + pool.Cpu.SmpProcessorAffinityMask2.ToString("X")
                };
            }


            //
            // process_model
            if (fields.Exists("process_model"))
            {
                dynamic processModel = new ExpandoObject();

                processModel.idle_timeout        = pool.ProcessModel.IdleTimeout.TotalMinutes;
                processModel.max_processes       = pool.ProcessModel.MaxProcesses;
                processModel.pinging_enabled     = pool.ProcessModel.PingingEnabled;
                processModel.ping_interval       = pool.ProcessModel.PingInterval.TotalSeconds;
                processModel.ping_response_time  = pool.ProcessModel.PingResponseTime.TotalSeconds;
                processModel.shutdown_time_limit = pool.ProcessModel.ShutdownTimeLimit.TotalSeconds;
                processModel.startup_time_limit  = pool.ProcessModel.StartupTimeLimit.TotalSeconds;

                if (pool.ProcessModel.Schema.HasAttribute(IdleTimeoutActionAttribute))
                {
                    processModel.idle_timeout_action = Enum.GetName(typeof(IdleTimeoutAction), pool.ProcessModel.IdleTimeoutAction);
                }

                obj.process_model = processModel;
            }

            //
            // identity
            if (fields.Exists("identity"))
            {
                obj.identity = new {
                    // Not changing the casing or adding '_' on the identity type enum because they represent identities and therefore spelling and casing are important
                    identity_type     = Enum.GetName(typeof(ProcessModelIdentityType), pool.ProcessModel.IdentityType),
                    username          = pool.ProcessModel.UserName,
                    load_user_profile = pool.ProcessModel.LoadUserProfile
                };
            }

            //
            // recycling
            if (fields.Exists("recycling"))
            {
                RecyclingLogEventOnRecycle logEvent = pool.Recycling.LogEventOnRecycle;

                Dictionary <string, bool> logEvents = new Dictionary <string, bool>();
                logEvents.Add("time", logEvent.HasFlag(RecyclingLogEventOnRecycle.Time));
                logEvents.Add("requests", logEvent.HasFlag(RecyclingLogEventOnRecycle.Requests));
                logEvents.Add("schedule", logEvent.HasFlag(RecyclingLogEventOnRecycle.Schedule));
                logEvents.Add("memory", logEvent.HasFlag(RecyclingLogEventOnRecycle.Memory));
                logEvents.Add("isapi_unhealthy", logEvent.HasFlag(RecyclingLogEventOnRecycle.IsapiUnhealthy));
                logEvents.Add("on_demand", logEvent.HasFlag(RecyclingLogEventOnRecycle.OnDemand));
                logEvents.Add("config_change", logEvent.HasFlag(RecyclingLogEventOnRecycle.ConfigChange));
                logEvents.Add("private_memory", logEvent.HasFlag(RecyclingLogEventOnRecycle.PrivateMemory));

                obj.recycling = new {
                    disable_overlapped_recycle       = pool.Recycling.DisallowOverlappingRotation,
                    disable_recycle_on_config_change = pool.Recycling.DisallowRotationOnConfigChange,
                    log_events       = logEvents,
                    periodic_restart = new {
                        time_interval  = pool.Recycling.PeriodicRestart.Time.TotalMinutes,
                        private_memory = pool.Recycling.PeriodicRestart.PrivateMemory,
                        request_limit  = pool.Recycling.PeriodicRestart.Requests,
                        virtual_memory = pool.Recycling.PeriodicRestart.Memory,
                        schedule       = pool.Recycling.PeriodicRestart.Schedule.Select(s => s.Time.ToString(@"hh\:mm"))
                    }
                };
            }

            //
            // rapid_fail_protection
            if (fields.Exists("rapid_fail_protection"))
            {
                obj.rapid_fail_protection = new {
                    enabled = pool.Failure.RapidFailProtection,
                    load_balancer_capabilities = Enum.GetName(typeof(LoadBalancerCapabilities), pool.Failure.LoadBalancerCapabilities),
                    interval             = pool.Failure.RapidFailProtectionInterval.TotalMinutes,
                    max_crashes          = pool.Failure.RapidFailProtectionMaxCrashes,
                    auto_shutdown_exe    = pool.Failure.AutoShutdownExe,
                    auto_shutdown_params = pool.Failure.AutoShutdownParams
                };
            }

            //
            // process_orphaning
            if (fields.Exists("process_orphaning"))
            {
                obj.process_orphaning = new {
                    enabled              = pool.Failure.OrphanWorkerProcess,
                    orphan_action_exe    = pool.Failure.OrphanActionExe,
                    orphan_action_params = pool.Failure.OrphanActionParams,
                };
            }

            return(Core.Environment.Hal.Apply(Defines.Resource.Guid, obj, full));
        }