Beispiel #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;
        }
Beispiel #2
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));
        }
Beispiel #3
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);
        }
Beispiel #4
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);
                }
            }
        }