Skip to content

[Backport 8.6] Add missing accessor properties for dictionary responses #7073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.IndexManagement;

public partial class GetAliasResponse
{
/// <summary>
/// A dictionary containing the <see cref="IndexAliases"/> organised by <see cref="IndexName"/>.
/// </summary>
[JsonIgnore]
public IReadOnlyDictionary<IndexName, IndexAliases> Aliases => BackingDictionary;

/// <summary>
/// Checks if a response is functionally valid or not.
/// This is a client abstraction to have a single property to check whether there was something wrong with a request.
/// <para>
/// The aliases endpoint returns a 404 when some of the specified alias names for an index cannot be found. For such partial responses,
/// the client considers the response to be valid.
/// </para>
/// </summary>
public override bool IsValidResponse => base.IsValidResponse || Aliases.Count > 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.IndexManagement;

public partial class GetFieldMappingResponse
{
[JsonIgnore]
public IReadOnlyDictionary<IndexName, TypeFieldMappings> FieldMappings => BackingDictionary;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.IndexManagement;

public partial class GetIndexResponse
{
[JsonIgnore]
public IReadOnlyDictionary<IndexName, IndexState> Indices => BackingDictionary;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Text.Json.Serialization;
using Elastic.Clients.Elasticsearch.Mapping;

namespace Elastic.Clients.Elasticsearch.IndexManagement;

public partial class GetMappingResponse
{
[JsonIgnore]
public IReadOnlyDictionary<IndexName, IndexMappingRecord> Indices => BackingDictionary;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.IndexManagement;

public partial class GetTemplateResponse
{
[JsonIgnore]
public IReadOnlyDictionary<string, TemplateMapping> TemplateMappings => BackingDictionary;
}
17 changes: 17 additions & 0 deletions src/Elastic.Clients.Elasticsearch/Client/IndicesNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Threading.Tasks;
using System.Threading;
using System;

namespace Elastic.Clients.Elasticsearch.IndexManagement;

Expand Down Expand Up @@ -35,4 +36,20 @@ public Task<RefreshResponse> RefreshAsync(Indices indices, CancellationToken can
request.BeforeRequest();
return DoRequestAsync<RefreshRequest, RefreshResponse, RefreshRequestParameters>(request, cancellationToken);
}

public virtual GetAliasResponse GetAlias(Indices indicies, Action<GetAliasRequestDescriptor> configureRequest)
{
var descriptor = new GetAliasRequestDescriptor(indicies);
configureRequest?.Invoke(descriptor);
descriptor.BeforeRequest();
return DoRequest<GetAliasRequestDescriptor, GetAliasResponse, GetAliasRequestParameters>(descriptor);
}

public virtual Task<GetAliasResponse> GetAliasAsync(Indices indicies, Action<GetAliasRequestDescriptor> configureRequest, CancellationToken cancellationToken = default)
{
var descriptor = new GetAliasRequestDescriptor(indicies);
configureRequest?.Invoke(descriptor);
descriptor.BeforeRequest();
return DoRequestAsync<GetAliasRequestDescriptor, GetAliasResponse, GetAliasRequestParameters>(descriptor, cancellationToken);
}
}
2 changes: 1 addition & 1 deletion tests/Tests.Configuration/tests.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
mode: u
# the elasticsearch version that should be started
# Can be a snapshot version of sonatype or "latest" to get the latest snapshot of sonatype
elasticsearch_version: 8.4.3
elasticsearch_version: 8.5.2
# cluster filter allows you to only run the integration tests of a particular cluster (cluster suffix not needed)
# cluster_filter:
# whether we want to forcefully reseed on the node, if you are starting the tests with a node already running
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ public async Task CreateIndicesAsync()
{
aliases = new Dictionary<string, object>
{
{ "projects-alias", new { } },
{ "projects-only", new { filter = new { term = new { join = new { value = "project" }}}} }
{ ProjectsAliasName, new { } },
{ ProjectsAliasFilter, new { filter = new { term = new { join = new { value = "project" }}}} }
},
mappings = new
{
Expand Down
109 changes: 109 additions & 0 deletions tests/Tests/IndexManagement/AliasManagement/GetAliasApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System;
using Elastic.Clients.Elasticsearch.IndexManagement;
using Tests.Core.ManagedElasticsearch.Clusters;
using Tests.Core.ManagedElasticsearch.NodeSeeders;
using Tests.Domain;
using Tests.Framework.EndpointTests;
using Tests.Framework.EndpointTests.TestState;

namespace Tests.IndexManagement.AliasManagement;

public class GetAliasApiTests : ApiIntegrationTestBase<ReadOnlyCluster, GetAliasResponse, GetAliasRequestDescriptor, GetAliasRequest>
{
private static readonly Names Names = Infer.Names(DefaultSeeder.ProjectsAliasName);

public GetAliasApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override bool ExpectIsValid => true;
protected override int ExpectStatusCode => 200;
protected override HttpMethod ExpectHttpMethod => HttpMethod.GET;
protected override bool SupportsDeserialization => false;
protected override string ExpectedUrlPathAndQuery => $"_all/_alias/{DefaultSeeder.ProjectsAliasName}";

protected override GetAliasRequest Initializer => new(Indices.All, Names);
protected override Action<GetAliasRequestDescriptor> Fluent => d => d.Name(Names);

protected override LazyResponses ClientUsage() => Calls(
(client, f) => client.Indices.GetAlias(Indices.All, f),
(client, f) => client.Indices.GetAliasAsync(Indices.All, f),
(client, r) => client.Indices.GetAlias(r),
(client, r) => client.Indices.GetAliasAsync(r)
);

protected override void ExpectResponse(GetAliasResponse response)
{
response.Aliases.Should().NotBeEmpty($"expect to find indices pointing to {DefaultSeeder.ProjectsAliasName}");
var indexAliases = response.Aliases[Infer.Index<Project>()];
indexAliases.Should().NotBeNull("expect to find alias for project");
indexAliases.Aliases.Should().NotBeEmpty("expect to find aliases dictionary definitions for project");
var alias = indexAliases.Aliases[DefaultSeeder.ProjectsAliasName];
alias.Should().NotBeNull();
}
}

// TODO: Support exception information from specification and avoid default error response deserialization in transport

//public class GetAliasPartialMatchApiTests : ApiIntegrationTestBase<ReadOnlyCluster, GetAliasResponse, GetAliasRequestDescriptor, GetAliasRequest>
//{
// private static readonly Names Names = Infer.Names(DefaultSeeder.ProjectsAliasName, "x", "y");

// public GetAliasPartialMatchApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

// protected override bool ExpectIsValid => true;
// protected override int ExpectStatusCode => 404;
// protected override HttpMethod ExpectHttpMethod => HttpMethod.GET;
// protected override bool SupportsDeserialization => false;
// protected override string ExpectedUrlPathAndQuery => $"_all/_alias/{DefaultSeeder.ProjectsAliasName}%2Cx%2Cy";

// protected override GetAliasRequest Initializer => new(Indices.All, Names);
// protected override Action<GetAliasRequestDescriptor> Fluent => d => d.Name(Names);

// protected override LazyResponses ClientUsage() => Calls(
// (client, f) => client.Indices.GetAlias(Indices.All, f),
// (client, f) => client.Indices.GetAliasAsync(Indices.All, f),
// (client, r) => client.Indices.GetAlias(r),
// (client, r) => client.Indices.GetAliasAsync(r)
// );

// protected override void ExpectResponse(GetAliasResponse response)
// {
// response.Aliases.Should().NotBeNull();
// response.Aliases.Count.Should().BeGreaterThan(0);
// }
//}

public class GetAliasNotFoundApiTests : ApiIntegrationTestBase<ReadOnlyCluster, GetAliasResponse, GetAliasRequestDescriptor, GetAliasRequest>
{
private static readonly Names Names = Infer.Names("bad-alias");

public GetAliasNotFoundApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override bool ExpectIsValid => false;
protected override int ExpectStatusCode => 404;
protected override HttpMethod ExpectHttpMethod => HttpMethod.GET;
protected override bool SupportsDeserialization => false;
protected override string ExpectedUrlPathAndQuery => $"_all/_alias/bad-alias";

protected override GetAliasRequest Initializer => new(Indices.All, Names);
protected override Action<GetAliasRequestDescriptor> Fluent => d => d.Name(Names);

protected override LazyResponses ClientUsage() => Calls(
(client, f) => client.Indices.GetAlias(Indices.All, f),
(client, f) => client.Indices.GetAliasAsync(Indices.All, f),
(client, r) => client.Indices.GetAlias(r),
(client, r) => client.Indices.GetAliasAsync(r)
);

protected override void ExpectResponse(GetAliasResponse response)
{
response.ElasticsearchServerError.Should().NotBeNull();
response.ElasticsearchServerError.Error.Reason.Should().Contain("missing");

response.Aliases.Should().NotBeNull();
response.Aliases.Should().BeEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System;
using Elastic.Clients.Elasticsearch.IndexManagement;
using Elastic.Clients.Elasticsearch.Mapping;
using Tests.Core.ManagedElasticsearch.Clusters;
using Tests.Domain;
using Tests.Framework.EndpointTests;
using Tests.Core.Extensions;
using Tests.Framework.EndpointTests.TestState;

namespace Tests.IndexManagement.MappingManagement;

public class GetFieldMappingApiTests : ApiIntegrationTestBase<ReadOnlyCluster, GetFieldMappingResponse, GetFieldMappingRequestDescriptor<Project>, GetFieldMappingRequest>
{
// TODO: Introduce percolation assertions once seeded

private static readonly Fields Fields = Infer.Fields<Project>(p => p.Name, p => p.LeadDeveloper.IpAddress);
private static readonly Field NameField = Infer.Field<Project>(p => p.Name);
private static readonly Indices OnIndices = Infer.Index<Project>()/*.And<ProjectPercolation>()*/;

public GetFieldMappingApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override bool ExpectIsValid => true;
protected override int ExpectStatusCode => 200;
protected override HttpMethod ExpectHttpMethod => HttpMethod.GET;
protected override GetFieldMappingRequest Initializer => new(OnIndices, Fields);
protected override Action<GetFieldMappingRequestDescriptor<Project>> Fluent => d => d.Indices(OnIndices);

protected override string ExpectedUrlPathAndQuery => "/project/_mapping/field/name%2CleadDeveloper.ipAddress";

protected override LazyResponses ClientUsage() => Calls(
(client, f) => client.Indices.GetFieldMapping(Fields, f),
(client, f) => client.Indices.GetFieldMappingAsync(Fields, f),
(client, r) => client.Indices.GetFieldMapping(r),
(client, r) => client.Indices.GetFieldMappingAsync(r)
);

protected override GetFieldMappingRequestDescriptor<Project> NewDescriptor() => new(Fields);

protected override void ExpectResponse(GetFieldMappingResponse response)
{
response.FieldMappings.Should()
.NotBeEmpty("expect indices on the response")
.And.ContainKey(Infer.Index<Project>(), "expect project to return field mappings");
//.And.ContainKey(Infer.Index<ProjectPercolation>(), "expect project percolation to return field mappings");

var projectMappings = response.FieldMappings[Infer.Index<Project>()];
projectMappings.Should().NotBeNull("project mapping value in the dictionary should not point to a null value");

// TODO - Reintroduce this once GetFieldMappingResponse.FieldMappings uses ResolvableReadOnlyDictionaryConverter
//projectMappings.Mappings.Should()
// .NotBeEmpty("project has fields so should contain a type mapping")
// .And.ContainKey(NameField, "project mappings should have 'name'");

//var fieldTypeMapping = projectMappings.Mappings[NameField];
//fieldTypeMapping.Should().NotBeNull("name field mapping should exist");
//fieldTypeMapping.FullName.Should().NotBeNullOrEmpty();
//fieldTypeMapping.Mapping.Should()
// .NotBeEmpty("field type mapping should return a `mapping` with the field information")
// .And.HaveCount(1, "field type mappings only return information from a single field")
// .And.ContainKey(NameField);

//var fieldMapping = fieldTypeMapping.Mapping[NameField];
//AssertNameFieldMapping(fieldMapping);

//fieldMapping = response.MappingFor<Project>(NameField);
//AssertNameFieldMapping(fieldMapping);

//fieldMapping = response.MappingFor<Project>(p => p.Name);
//AssertNameFieldMapping(fieldMapping);
}

private static void AssertNameFieldMapping(FieldMapping fieldMapping)
{
fieldMapping.Should().NotBeNull("expected to find name on field type mapping for project");

//var nameKeyword = fieldMapping as KeywordProperty;
//nameKeyword.Should().NotBeNull("the field type is a keyword mapping");
//nameKeyword.Store.Should().BeTrue("name is keyword field that has store enabled");
//nameKeyword.Fields.Should().NotBeEmpty().And.HaveCount(2);
//nameKeyword.Fields["standard"].Should().NotBeNull();
}
}