Exemple #1
0
        public async Task Put()
        {
            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
            {
                var input = await context.ReadForMemoryAsync(RequestBodyStream(), "Analyzers");

                if (input.TryGet("Analyzers", out BlittableJsonReaderArray analyzers) == false)
                {
                    ThrowRequiredPropertyNameInRequest("Analyzers");
                }

                var commands = new List <PutServerWideAnalyzerCommand>();
                foreach (var analyzerToAdd in analyzers)
                {
                    var analyzerDefinition = JsonDeserializationServer.AnalyzerDefinition((BlittableJsonReaderObject)analyzerToAdd);
                    analyzerDefinition.Name = analyzerDefinition.Name?.Trim();

                    if (LoggingSource.AuditLog.IsInfoEnabled)
                    {
                        var clientCert = GetCurrentCertificate();

                        var auditLog = LoggingSource.AuditLog.GetLogger("Server", "Audit");
                        auditLog.Info($"Analyzer {analyzerDefinition.Name} PUT by {clientCert?.Subject} {clientCert?.Thumbprint} with definition: {analyzerToAdd}");
                    }

                    analyzerDefinition.Validate();

                    // check if analyzer is compilable
                    AnalyzerCompiler.Compile(analyzerDefinition.Name, analyzerDefinition.Code);

                    var command = new PutServerWideAnalyzerCommand(analyzerDefinition, GetRaftRequestIdFromQuery());

                    commands.Add(command);
                }

                var index = 0L;
                foreach (var command in commands)
                {
                    index = (await ServerStore.SendToLeaderAsync(command)).Index;
                }

                await ServerStore.WaitForCommitIndexChange(RachisConsensus.CommitIndexModification.GreaterOrEqual, index);

                NoContentStatus(HttpStatusCode.Created);
            }
        }
Exemple #2
0
        public async Task Get()
        {
            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (context.OpenReadTransaction())
                {
                    var analyzers = new Dictionary <string, AnalyzerDefinition>();

                    foreach (var item in ServerStore.Cluster.ItemsStartingWith(context, PutServerWideAnalyzerCommand.Prefix, 0, long.MaxValue))
                    {
                        var analyzerDefinition = JsonDeserializationServer.AnalyzerDefinition(item.Value);

                        analyzers.Add(item.ItemName.Substring(PutServerWideAnalyzerCommand.Prefix.Length), analyzerDefinition);
                    }

                    var namesOnly = GetBoolValueQueryString("namesOnly", false) ?? false;

                    await using (var writer = new AsyncBlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        writer.WriteStartObject();

                        writer.WriteArray(context, "Analyzers", analyzers.Values, (w, c, analyzer) =>
                        {
                            w.WriteStartObject();

                            w.WritePropertyName(nameof(AnalyzerDefinition.Name));
                            w.WriteString(analyzer.Name);

                            if (namesOnly == false)
                            {
                                w.WriteComma();
                                w.WritePropertyName(nameof(AnalyzerDefinition.Code));
                                w.WriteString(analyzer.Code);
                            }

                            w.WriteEndObject();
                        });

                        writer.WriteEndObject();
                    }
                }
        }
        public async Task Put()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                var input = await context.ReadForMemoryAsync(RequestBodyStream(), "Analyzers");

                if (input.TryGet("Analyzers", out BlittableJsonReaderArray analyzers) == false)
                {
                    ThrowRequiredPropertyNameInRequest("Analyzers");
                }

                var command = new PutAnalyzersCommand(Database.Name, GetRaftRequestIdFromQuery());
                foreach (var analyzerToAdd in analyzers)
                {
                    var analyzerDefinition = JsonDeserializationServer.AnalyzerDefinition((BlittableJsonReaderObject)analyzerToAdd);
                    analyzerDefinition.Name = analyzerDefinition.Name?.Trim();

                    if (LoggingSource.AuditLog.IsInfoEnabled)
                    {
                        var clientCert = GetCurrentCertificate();

                        var auditLog = LoggingSource.AuditLog.GetLogger(Database.Name, "Audit");
                        auditLog.Info($"Analyzer {analyzerDefinition.Name} PUT by {clientCert?.Subject} {clientCert?.Thumbprint} with definition: {analyzerToAdd}");
                    }

                    analyzerDefinition.Validate();

                    // check if analyzer is compilable
                    AnalyzerCompiler.Compile(analyzerDefinition.Name, analyzerDefinition.Code);

                    command.Analyzers.Add(analyzerDefinition);
                }

                var index = (await ServerStore.SendToLeaderAsync(command)).Index;

                await Database.RachisLogIndexNotifications.WaitForIndexNotification(index, ServerStore.Engine.OperationTimeout);

                NoContentStatus(HttpStatusCode.Created);
            }
        }