Browse Source

Open Search action.

pull/927/head
Sebastian 4 years ago
parent
commit
2e3548ec53
  1. 4
      backend/extensions/Squidex.Extensions/Actions/ElasticSearch/ElasticSearchAction.cs
  2. 53
      backend/extensions/Squidex.Extensions/Actions/OpenSearch/OpenSearchAction.cs
  3. 169
      backend/extensions/Squidex.Extensions/Actions/OpenSearch/OpenSearchActionHandler.cs
  4. 21
      backend/extensions/Squidex.Extensions/Actions/OpenSearch/OpenSearchPlugin.cs
  5. 3
      backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj

4
backend/extensions/Squidex.Extensions/Actions/ElasticSearch/ElasticSearchAction.cs

@ -13,10 +13,10 @@ using Squidex.Infrastructure.Validation;
namespace Squidex.Extensions.Actions.ElasticSearch namespace Squidex.Extensions.Actions.ElasticSearch
{ {
[RuleAction( [RuleAction(
Title = "Elasticsearch", Title = "ElasticSearch",
IconImage = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 29 28'><path d='M13.427 17.436H4.163C3.827 16.354 3.636 15.2 3.636 14s.182-2.355.527-3.436h15.245c1.891 0 3.418 1.545 3.418 3.445a3.421 3.421 0 0 1-3.418 3.427h-5.982zm-.436 1.146H4.6a11.508 11.508 0 0 0 4.2 4.982 11.443 11.443 0 0 0 15.827-3.209 5.793 5.793 0 0 0-4.173-1.773H12.99zm7.464-9.164a5.794 5.794 0 0 0 4.173-1.773 11.45 11.45 0 0 0-9.536-5.1c-2.327 0-4.491.7-6.3 1.891a11.554 11.554 0 0 0-4.2 4.982h15.864z'/></svg>", IconImage = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 29 28'><path d='M13.427 17.436H4.163C3.827 16.354 3.636 15.2 3.636 14s.182-2.355.527-3.436h15.245c1.891 0 3.418 1.545 3.418 3.445a3.421 3.421 0 0 1-3.418 3.427h-5.982zm-.436 1.146H4.6a11.508 11.508 0 0 0 4.2 4.982 11.443 11.443 0 0 0 15.827-3.209 5.793 5.793 0 0 0-4.173-1.773H12.99zm7.464-9.164a5.794 5.794 0 0 0 4.173-1.773 11.45 11.45 0 0 0-9.536-5.1c-2.327 0-4.491.7-6.3 1.891a11.554 11.554 0 0 0-4.2 4.982h15.864z'/></svg>",
IconColor = "#1e5470", IconColor = "#1e5470",
Display = "Populate Elasticsearch index", Display = "Populate ElasticSearch index",
Description = "Populate a full text search index in ElasticSearch.", Description = "Populate a full text search index in ElasticSearch.",
ReadMore = "https://www.elastic.co/")] ReadMore = "https://www.elastic.co/")]
public sealed record ElasticSearchAction : RuleAction public sealed record ElasticSearchAction : RuleAction

53
backend/extensions/Squidex.Extensions/Actions/OpenSearch/OpenSearchAction.cs

@ -0,0 +1,53 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.ComponentModel.DataAnnotations;
using Squidex.Domain.Apps.Core.HandleRules;
using Squidex.Domain.Apps.Core.Rules;
using Squidex.Infrastructure.Validation;
namespace Squidex.Extensions.Actions.OpenSearch
{
[RuleAction(
Title = "OpenSearch",
IconImage = "<svg viewBox='0 0 64 64' fill='none' xmlns='http://www.w3.org/2000/svg'><path d='M61.737 23.5a2.263 2.263 0 0 0-2.262 2.263c0 18.618-15.094 33.712-33.712 33.712a2.263 2.263 0 1 0 0 4.525C46.88 64 64 46.88 64 25.763a2.263 2.263 0 0 0-2.263-2.263Z' fill='#fff'/><path d='M48.081 38c2.176-3.55 4.28-8.282 3.866-14.908C51.09 9.367 38.66-1.045 26.921.084c-4.596.441-9.314 4.187-8.895 10.896.182 2.916 1.61 4.637 3.928 5.96 2.208 1.26 5.044 2.057 8.259 2.961 3.883 1.092 8.388 2.32 11.85 4.87 4.15 3.058 6.986 6.603 6.018 13.229Z' fill='#fff'/><path d='M3.919 14C1.743 17.55-.361 22.282.052 28.908.91 42.633 13.342 53.045 25.08 51.916c4.596-.441 9.314-4.187 8.895-10.896-.182-2.916-1.61-4.637-3.928-5.96-2.208-1.26-5.044-2.057-8.259-2.961-3.883-1.092-8.388-2.32-11.85-4.87C5.787 24.17 2.95 20.625 3.919 14Z' fill='#fff'/></svg>",
IconColor = "#005EB8",
Display = "Populate OpenSearch index",
Description = "Populate a full text search index in OpenSearch.",
ReadMore = "https://opensearch.org/")]
public sealed record OpenSearchAction : RuleAction
{
[AbsoluteUrl]
[LocalizedRequired]
[Display(Name = "Server Url", Description = "The url to the elastic search instance or cluster.")]
[Editor(RuleFieldEditor.Url)]
public Uri Host { get; set; }
[LocalizedRequired]
[Display(Name = "Index Name", Description = "The name of the index.")]
[Editor(RuleFieldEditor.Text)]
[Formattable]
public string IndexName { get; set; }
[Display(Name = "Username", Description = "The optional username.")]
[Editor(RuleFieldEditor.Text)]
public string Username { get; set; }
[Display(Name = "Password", Description = "The optional password.")]
[Editor(RuleFieldEditor.Text)]
public string Password { get; set; }
[Display(Name = "Document", Description = "The optional custom document.")]
[Editor(RuleFieldEditor.TextArea)]
[Formattable]
public string Document { get; set; }
[Display(Name = "Deletion", Description = "The condition when to delete the document.")]
[Editor(RuleFieldEditor.Text)]
public string Delete { get; set; }
}
}

169
backend/extensions/Squidex.Extensions/Actions/OpenSearch/OpenSearchActionHandler.cs

@ -0,0 +1,169 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Text.Json.Serialization;
using OpenSearch.Net;
using Squidex.Domain.Apps.Core.HandleRules;
using Squidex.Domain.Apps.Core.Rules.EnrichedEvents;
using Squidex.Domain.Apps.Core.Scripting;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Json;
#pragma warning disable IDE0059 // Value assigned to symbol is never used
#pragma warning disable MA0048 // File name must match type name
namespace Squidex.Extensions.Actions.OpenSearch
{
public sealed class OpenSearchActionHandler : RuleActionHandler<OpenSearchAction, OpenSearchJob>
{
private readonly ClientPool<(Uri Host, string Username, string Password), OpenSearchLowLevelClient> clients;
private readonly IScriptEngine scriptEngine;
private readonly IJsonSerializer serializer;
public OpenSearchActionHandler(RuleEventFormatter formatter, IScriptEngine scriptEngine, IJsonSerializer serializer)
: base(formatter)
{
clients = new ClientPool<(Uri Host, string Username, string Password), OpenSearchLowLevelClient>(key =>
{
var config = new ConnectionConfiguration(key.Host);
if (!string.IsNullOrEmpty(key.Username) && !string.IsNullOrWhiteSpace(key.Password))
{
config = config.BasicAuthentication(key.Username, key.Password);
}
return new OpenSearchLowLevelClient(config);
});
this.scriptEngine = scriptEngine;
this.serializer = serializer;
}
protected override async Task<(string Description, OpenSearchJob Data)> CreateJobAsync(EnrichedEvent @event, OpenSearchAction action)
{
var delete = @event.ShouldDelete(scriptEngine, action.Delete);
string contentId;
if (@event is IEnrichedEntityEvent enrichedEntityEvent)
{
contentId = enrichedEntityEvent.Id.ToString();
}
else
{
contentId = DomainId.NewGuid().ToString();
}
var ruleDescription = string.Empty;
var ruleJob = new OpenSearchJob
{
IndexName = await FormatAsync(action.IndexName, @event),
ServerHost = action.Host.ToString(),
ServerUser = action.Username,
ServerPassword = action.Password,
ContentId = contentId
};
if (delete)
{
ruleDescription = $"Delete entry index: {action.IndexName}";
}
else
{
ruleDescription = $"Upsert to index: {action.IndexName}";
ElasticContent content;
try
{
string jsonString;
if (!string.IsNullOrEmpty(action.Document))
{
jsonString = await FormatAsync(action.Document, @event);
jsonString = jsonString?.Trim();
}
else
{
jsonString = ToJson(@event);
}
content = serializer.Deserialize<ElasticContent>(jsonString);
}
catch (Exception ex)
{
content = new ElasticContent
{
More = new Dictionary<string, object>
{
["error"] = $"Invalid JSON: {ex.Message}"
}
};
}
content.ContentId = contentId;
ruleJob.Content = serializer.Serialize(content, true);
}
return (ruleDescription, ruleJob);
}
protected override async Task<Result> ExecuteJobAsync(OpenSearchJob job,
CancellationToken ct = default)
{
if (string.IsNullOrWhiteSpace(job.ServerHost))
{
return Result.Ignored();
}
var client = await clients.GetClientAsync((new Uri(job.ServerHost, UriKind.Absolute), job.ServerUser, job.ServerPassword));
try
{
if (job.Content != null)
{
var response = await client.IndexAsync<StringResponse>(job.IndexName, job.ContentId, job.Content, ctx: ct);
return Result.SuccessOrFailed(response.OriginalException, response.Body);
}
else
{
var response = await client.DeleteAsync<StringResponse>(job.IndexName, job.ContentId, ctx: ct);
return Result.SuccessOrFailed(response.OriginalException, response.Body);
}
}
catch (OpenSearchClientException ex)
{
return Result.Failed(ex);
}
}
}
public sealed class ElasticContent
{
public string ContentId { get; set; }
[JsonExtensionData]
public Dictionary<string, object> More { get; set; } = new Dictionary<string, object>();
}
public sealed class OpenSearchJob
{
public string ServerHost { get; set; }
public string ServerUser { get; set; }
public string ServerPassword { get; set; }
public string ContentId { get; set; }
public string Content { get; set; }
public string IndexName { get; set; }
}
}

21
backend/extensions/Squidex.Extensions/Actions/OpenSearch/OpenSearchPlugin.cs

@ -0,0 +1,21 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Squidex.Infrastructure.Plugins;
namespace Squidex.Extensions.Actions.OpenSearch
{
public sealed class OpenSearchPlugin : IPlugin
{
public void ConfigureServices(IServiceCollection services, IConfiguration config)
{
services.AddRuleAction<OpenSearchAction, OpenSearchActionHandler>();
}
}
}

3
backend/extensions/Squidex.Extensions/Squidex.Extensions.csproj

@ -16,7 +16,7 @@
<PackageReference Include="Confluent.Kafka" Version="1.8.2" /> <PackageReference Include="Confluent.Kafka" Version="1.8.2" />
<PackageReference Include="Confluent.SchemaRegistry.Serdes" Version="1.3.0" /> <PackageReference Include="Confluent.SchemaRegistry.Serdes" Version="1.3.0" />
<PackageReference Include="CoreTweet" Version="1.0.0.483" /> <PackageReference Include="CoreTweet" Version="1.0.0.483" />
<PackageReference Include="Elasticsearch.Net" Version="7.17.2" /> <PackageReference Include="Elasticsearch.Net" Version="7.17.4" />
<PackageReference Include="Google.Cloud.Diagnostics.Common" Version="4.4.0" /> <PackageReference Include="Google.Cloud.Diagnostics.Common" Version="4.4.0" />
<PackageReference Include="Google.Cloud.Logging.V2" Version="3.6.0" /> <PackageReference Include="Google.Cloud.Logging.V2" Version="3.6.0" />
<PackageReference Include="Meziantou.Analyzer" Version="1.0.702"> <PackageReference Include="Meziantou.Analyzer" Version="1.0.702">
@ -29,6 +29,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.OData.Core" Version="7.11.0" /> <PackageReference Include="Microsoft.OData.Core" Version="7.11.0" />
<PackageReference Include="NodaTime" Version="3.1.2" /> <PackageReference Include="NodaTime" Version="3.1.2" />
<PackageReference Include="OpenSearch.Net" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.3.0" /> <PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.3.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.3.0" /> <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.3.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc8" /> <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc8" />

Loading…
Cancel
Save