Exemplo n.º 1
0
        public void Delete(string id)
        {
            ProviderId providerId = new ProviderId(id);

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

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

            TraceProviderDefinition provider = ProvidersHelper.GetProviders(site, providerId.Path).Where(r => r.Name.ToString().Equals(providerId.Name)).FirstOrDefault();

            if (provider != null)
            {
                var section = Helper.GetTraceProviderDefinitionSection(site, providerId.Path, ManagementUnit.ResolveConfigScope());

                ProvidersHelper.DeleteProvider(provider, section);
                ManagementUnit.Current.Commit();
            }

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
        }
Exemplo n.º 2
0
        public object Post(dynamic model)
        {
            TraceProviderDefinition provider = null;
            Site site = null;
            HttpRequestTracingId hrtId = null;

            if (model == null)
            {
                throw new ApiArgumentException("model");
            }
            if (model.request_tracing == null)
            {
                throw new ApiArgumentException("request_tracing");
            }
            if (!(model.request_tracing is JObject))
            {
                throw new ApiArgumentException("request_tracing", ApiArgumentException.EXPECTED_OBJECT);
            }
            string hrtUuid = DynamicHelper.Value(model.request_tracing.id);

            if (hrtUuid == null)
            {
                throw new ApiArgumentException("request_tracing.id");
            }

            hrtId = new HttpRequestTracingId(hrtUuid);

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

            string configPath = ManagementUnit.ResolveConfigScope(model);
            var    section    = Helper.GetTraceProviderDefinitionSection(site, hrtId.Path, configPath);

            provider = ProvidersHelper.CreateProvider(model, section);

            ProvidersHelper.AddProvider(provider, section);

            ManagementUnit.Current.Commit();

            dynamic p = ProvidersHelper.ToJsonModel(provider, site, hrtId.Path);

            return(Created(ProvidersHelper.GetLocation(p.id), p));
        }
Exemplo n.º 3
0
        public object Get(string id)
        {
            ProviderId providerId = new ProviderId(id);

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

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

            TraceProviderDefinition provider = ProvidersHelper.GetProviders(site, providerId.Path).Where(p => p.Name.Equals(providerId.Name)).FirstOrDefault();

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

            return(ProvidersHelper.ToJsonModel(provider, site, providerId.Path));
        }
Exemplo n.º 4
0
        public object Patch(string id, dynamic model)
        {
            ProviderId providerId = new ProviderId(id);

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

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

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

            string configPath = ManagementUnit.ResolveConfigScope(model);
            TraceProviderDefinition provider = ProvidersHelper.GetProviders(site, providerId.Path, configPath).Where(p => p.Name.ToString().Equals(providerId.Name)).FirstOrDefault();

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


            provider = ProvidersHelper.UpdateProvider(provider, model, Helper.GetTraceProviderDefinitionSection(site, providerId.Path, configPath));

            ManagementUnit.Current.Commit();

            dynamic prov = ProvidersHelper.ToJsonModel(provider, site, providerId.Path);

            if (prov.id != id)
            {
                return(LocationChanged(ProvidersHelper.GetLocation(prov.id), prov));
            }

            return(prov);
        }
Exemplo n.º 5
0
        public object Get()
        {
            string hrtUuid = Context.Request.Query[Defines.IDENTIFIER];

            if (string.IsNullOrEmpty(hrtUuid))
            {
                return(new StatusCodeResult((int)HttpStatusCode.NotFound));
            }

            HttpRequestTracingId hrtId = new HttpRequestTracingId(hrtUuid);

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

            var providers = ProvidersHelper.GetProviders(site, hrtId.Path);

            this.Context.Response.SetItemsCount(providers.Count());

            Fields fields = Context.Request.GetFields();

            return(new {
                providers = providers.Select(p => ProvidersHelper.ToJsonModelRef(p, site, hrtId.Path, fields))
            });
        }
Exemplo n.º 6
0
        private void ConfigureProviders()
        {
            Environment.Host.RouteBuilder.MapWebApiRoute(Defines.ProvidersResource.Guid, $"{ Defines.PROVIDERS_PATH}/{{id?}}", new { controller = "TraceProviders" });

            Environment.Hal.ProvideLink(Defines.ProvidersResource.Guid, "self", p => new { href = ProvidersHelper.GetLocation(p.id) });

            Environment.Hal.ProvideLink(Defines.Resource.Guid, Defines.ProvidersResource.Name, hrt => new { href = $"/{Defines.PROVIDERS_PATH}?{Defines.IDENTIFIER}={hrt.id}" });
        }
Exemplo n.º 7
0
        private static void SetRule(TraceRule rule, dynamic model, Site site, string path)
        {
            DynamicHelper.If((object)model.path, v => rule.Path = v);

            DynamicHelper.If <long>((object)model.min_request_execution_time, v => {
                // Setting time taken to 0 turns off the time taken trace trigger
                if (v < 1)
                {
                    throw new ApiArgumentException("min_request_execution_time");
                }
                rule.FailureDefinition.TimeTaken = TimeSpan.FromSeconds(v >= int.MaxValue ? 0 : v);
            });
            DynamicHelper.If <FailureDefinitionVerbosity>((object)model.event_severity, v => rule.FailureDefinition.Verbosity = v);

            // Status codes
            if (model.status_codes != null)
            {
                // Check for status codes and ensure proper format. e.g. 101, 102-103, 104
                if (!(model.status_codes is JArray))
                {
                    throw new ApiArgumentException("model.status_codes", ApiArgumentException.EXPECTED_ARRAY);
                }

                List <string>        statusCodes = new List <string>();
                IEnumerable <string> entries     = (model.status_codes as JArray).ToObject <IEnumerable <string> >();
                long l;

                foreach (var entry in entries)
                {
                    var rangeSplit = entry.Split('-');

                    foreach (var rangeEntry in rangeSplit)
                    {
                        if (!long.TryParse(rangeEntry, out l))
                        {
                            throw new ApiArgumentException("model.status_codes");
                        }
                    }

                    statusCodes.Add(entry.Trim());
                }

                rule.FailureDefinition.StatusCodes = string.Join(",", statusCodes);
            }

            // Custom action
            if (model.custom_action != null)
            {
                if (!(model.custom_action is JObject))
                {
                    throw new ApiArgumentException("custom_action", ApiArgumentException.EXPECTED_OBJECT);
                }

                dynamic customAction = model.custom_action;

                DynamicHelper.If((object)customAction.executable, v => rule.CustomActionExe = v);
                DynamicHelper.If((object)customAction.@params, v => rule.CustomActionParams = v);
                DynamicHelper.If((object)customAction.trigger_limit, 0, 10000, v => rule.CustomActionTriggerLimit = v);
            }

            if (model.traces != null)
            {
                if (!(model.traces is JArray))
                {
                    throw new ApiArgumentException("traces", ApiArgumentException.EXPECTED_ARRAY);
                }

                IEnumerable <dynamic> traces = model.traces;
                rule.TraceAreas.Clear();

                foreach (dynamic ta in traces)
                {
                    if (!(ta is JObject))
                    {
                        throw new ApiArgumentException("traces.item", ApiArgumentException.EXPECTED_OBJECT);
                    }

                    TraceArea traceArea = rule.TraceAreas.CreateElement();

                    //
                    // Ensure provider field is object and the referenced provider exists
                    if (ta.provider == null)
                    {
                        throw new ApiArgumentException("traces.item.provider");
                    }
                    if (!(ta.provider is JObject))
                    {
                        throw new ApiArgumentException("traces.item.provider", ApiArgumentException.EXPECTED_OBJECT);
                    }
                    string providerUuid = DynamicHelper.Value(ta.provider.id);
                    if (string.IsNullOrEmpty(providerUuid))
                    {
                        throw new ApiArgumentException("traces.item.provider.id");
                    }

                    var providerName = new ProviderId(providerUuid).Name;
                    var provider     = ProvidersHelper.GetProviders(site, path).Where(p => p.Name.Equals(providerName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

                    if (provider == null)
                    {
                        throw new NotFoundException("traces.item.provider");
                    }

                    traceArea.Provider = provider.Name;

                    DynamicHelper.If <FailedRequestTracingVerbosity>((object)ta.verbosity, v => traceArea.Verbosity = v);

                    if (ta.allowed_areas != null)
                    {
                        if (!(ta.allowed_areas is JObject))
                        {
                            throw new ApiArgumentException("traces.allowed_areas", ApiArgumentException.EXPECTED_OBJECT);
                        }

                        Dictionary <string, bool> allowedAreas;

                        try {
                            allowedAreas = (ta.allowed_areas as JObject).ToObject <Dictionary <string, bool> >();
                        }
                        catch (JsonSerializationException e) {
                            throw new ApiArgumentException("traces.allowed_areas", e);
                        }

                        List <string> areas = new List <string>();
                        foreach (var key in allowedAreas.Keys)
                        {
                            // Ensure the provider offers the specified area
                            if (!provider.Areas.Any(a => a.Name.Equals(key)))
                            {
                                throw new ApiArgumentException("traces.allowed_areas." + key);
                            }

                            if (allowedAreas[key])
                            {
                                areas.Add(key);
                            }
                        }

                        traceArea.Areas = string.Join(",", areas);
                    }

                    rule.TraceAreas.Add(traceArea);
                }
            }
        }
Exemplo n.º 8
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));
        }