public void Delete(string id)
        {
            RuleId ruleId = new RuleId(id);

            Site        site = ruleId.SiteId == null ? null : SiteHelper.GetSite(ruleId.SiteId.Value);
            Application app  = ApplicationHelper.GetApplication(ruleId.AppPath, site);

            if (ruleId.SiteId != null && site == null)
            {
                Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
                return;
            }

            TraceRule rule = RulesHelper.GetRules(site, ruleId.AppPath).Where(r => r.Path.ToString().Equals(ruleId.Path)).FirstOrDefault();

            if (rule != null)
            {
                var section = Helper.GetTraceFailedRequestsSection(site, ruleId.AppPath, ManagementUnit.ResolveConfigScope());

                RulesHelper.DeleteRule(rule, section);
                ManagementUnit.Current.Commit();
            }

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
        }
        public object Get(string id)
        {
            RuleId ruleId = new RuleId(id);

            Site site = ruleId.SiteId == null ? null : SiteHelper.GetSite(ruleId.SiteId.Value);

            if (ruleId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            TraceRule rule = RulesHelper.GetRules(site, ruleId.AppPath).Where(r => r.Path.Equals(ruleId.Path)).FirstOrDefault();

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

            return(RulesHelper.ToJsonModel(rule, site, ruleId.AppPath));
        }
        public object Patch(string id, dynamic model)
        {
            RuleId ruleId = new RuleId(id);

            Site site = ruleId.SiteId == null ? null : SiteHelper.GetSite(ruleId.SiteId.Value);

            if (ruleId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            string    configPath = ManagementUnit.ResolveConfigScope(model);
            TraceRule rule       = RulesHelper.GetRules(site, ruleId.AppPath, configPath).Where(r => r.Path.ToString().Equals(ruleId.Path)).FirstOrDefault();

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


            rule = RulesHelper.UpdateRule(rule, model, site, ruleId.AppPath, configPath);

            ManagementUnit.Current.Commit();

            dynamic rle = RulesHelper.ToJsonModel(rule, site, ruleId.AppPath);

            if (rle.id != id)
            {
                return(LocationChanged(RulesHelper.GetLocation(rle.id), rle));
            }

            return(rle);
        }
Exemplo n.º 4
0
        internal static object ToJsonModel(TraceRule rule, Site site, string path, Fields fields = null, bool full = true)
        {
            if (rule == null)
            {
                return(null);
            }

            RuleId ruleId = new RuleId(site?.Id, path, rule.Path);

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

            dynamic obj = new ExpandoObject();

            //
            // path
            if (fields.Exists("path"))
            {
                obj.path = rule.Path;
            }

            //
            // id
            obj.id = new RuleId(site?.Id, path, rule.Path).Uuid;

            //
            // status_codes
            if (fields.Exists("status_codes"))
            {
                var statusCodes = rule.FailureDefinition.StatusCodes.Split(',')
                                  .Select(statusCode => statusCode.Trim())
                                  .Where(statusCode => !string.IsNullOrEmpty(statusCode));

                obj.status_codes = statusCodes;
            }

            //
            // min_request_execution_time
            if (fields.Exists("min_request_execution_time"))
            {
                // 0 turns off the time taken trigger, display this value as int max
                var totalSeconds = rule.FailureDefinition.TimeTaken.TotalSeconds;
                obj.min_request_execution_time = totalSeconds == 0 ? int.MaxValue : totalSeconds;
            }

            //
            // event_severity
            if (fields.Exists("event_severity"))
            {
                obj.event_severity = Enum.GetName(rule.FailureDefinition.Verbosity.GetType(), rule.FailureDefinition.Verbosity).ToLower();
            }

            //
            // custom_action
            if (fields.Exists("custom_action"))
            {
                obj.custom_action = new {
                    executable    = rule.CustomActionExe,
                    @params       = rule.CustomActionParams,
                    trigger_limit = rule.CustomActionTriggerLimit
                };
            }

            //
            // traces
            if (fields.Exists("traces"))
            {
                // It is possible that a trace rule was created and the provider that was providing the areas was removed
                // We do not want to error out in this case, instead display a null provider

                obj.traces = rule.TraceAreas.Select(ta => {
                    var areas = ta.Areas.Split(',')
                                .Select(area => area.Trim())
                                .Where(area => !string.IsNullOrEmpty(area));

                    var provider = ProvidersHelper.GetProviders(site, path).Where(p => p.Name.Equals(ta.Provider, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                    Dictionary <string, bool> allowedAreas = new Dictionary <string, bool>();
                    if (provider != null)
                    {
                        foreach (var area in provider.Areas)
                        {
                            allowedAreas.Add(area.Name, false);
                        }
                        foreach (var area in areas)
                        {
                            allowedAreas[area] = true;
                        }
                    }

                    return(new {
                        allowed_areas = allowedAreas,
                        provider = ProvidersHelper.ToJsonModelRef(provider, site, path),
                        verbosity = Enum.GetName(ta.Verbosity.GetType(), ta.Verbosity).ToLower()
                    });
                });
            }

            //
            // request_tracing
            if (fields.Exists("request_tracing"))
            {
                obj.request_tracing = Helper.ToJsonModelRef(site, path);
            }

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