Skip to content

Commit 0c2f0ae

Browse files
committed
fix change in GetAlias behavior, since 5.5 now returns 404 when some aliases suplied are not found
1 parent 173aeb6 commit 0c2f0ae

File tree

9 files changed

+97
-22
lines changed

9 files changed

+97
-22
lines changed

src/Nest/Aggregations/AggregateJsonConverter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ private IAggregate GetGeoCentroidAggregate(JsonReader reader, JsonSerializer ser
234234
reader.Read();
235235
var geoCentroid = new GeoCentroidAggregate {Location = serializer.Deserialize<GeoLocation>(reader)};
236236
reader.Read();
237+
if (reader.TokenType == JsonToken.PropertyName && (string) reader.Value == Parser.Count)
238+
{
239+
reader.Read();
240+
geoCentroid.Count = (long) reader.Value;
241+
reader.Read();
242+
}
237243
return geoCentroid;
238244
}
239245

src/Nest/Aggregations/Metric/GeoCentroid/GeoCentroidAggregate.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
public class GeoCentroidAggregate : MetricAggregateBase
44
{
55
public GeoLocation Location { get; set; }
6+
7+
//TODO non nullable in 6.0, introduced in 5.5
8+
public long? Count { get; set; }
69
}
710
}

src/Nest/Indices/AliasManagement/GetAlias/ElasticClient-GetAlias.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public IGetAliasResponse GetAlias(Func<GetAliasDescriptor, IGetAliasRequest> sel
4040
/// <inheritdoc/>
4141
public IGetAliasResponse GetAlias(IGetAliasRequest request) =>
4242
this.Dispatcher.Dispatch<IGetAliasRequest, GetAliasRequestParameters, GetAliasResponse>(
43-
request,
43+
this.ForceConfiguration<IGetAliasRequest, GetAliasRequestParameters>(request, c => c.AllowedStatusCodes = new[] { -1 }),
4444
(p, d) => this.LowLevelDispatch.IndicesGetAliasDispatch<GetAliasResponse>(p)
4545
);
4646

@@ -53,7 +53,7 @@ public Task<IGetAliasResponse> GetAliasAsync(
5353
/// <inheritdoc/>
5454
public Task<IGetAliasResponse> GetAliasAsync(IGetAliasRequest request, CancellationToken cancellationToken = default(CancellationToken)) =>
5555
this.Dispatcher.DispatchAsync<IGetAliasRequest, GetAliasRequestParameters, GetAliasResponse, IGetAliasResponse>(
56-
request,
56+
this.ForceConfiguration<IGetAliasRequest, GetAliasRequestParameters>(request, c => c.AllowedStatusCodes = new[] { -1 }),
5757
cancellationToken,
5858
(p, d, c) => this.LowLevelDispatch.IndicesGetAliasDispatchAsync<GetAliasResponse>(p, c)
5959
);

src/Nest/Indices/AliasManagement/GetAlias/GetAliasResponse.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Linq.Expressions;
5+
using Elasticsearch.Net;
46
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
58

69
namespace Nest
710
{
811
[JsonConverter(typeof(GetAliasResponseConverter))]
912
public interface IGetAliasResponse : IResponse
1013
{
1114
IReadOnlyDictionary<string, IReadOnlyList<AliasDefinition>> Indices { get; }
15+
/// <summary>
16+
/// Starting from elasticsearch 5.5 if only some indices are not found an additonal error message will be returned.
17+
/// </summary>
18+
string Error { get; }
19+
int? StatusCode { get; }
1220
}
1321

1422
public class GetAliasResponse : ResponseBase, IGetAliasResponse
1523
{
1624
public IReadOnlyDictionary<string, IReadOnlyList<AliasDefinition>> Indices { get; internal set; } = EmptyReadOnly<string, IReadOnlyList<AliasDefinition>>.Dictionary;
25+
26+
27+
public override bool IsValid => this.Indices.Count > 0 ? true : false;
28+
public string Error { get; internal set; }
29+
public int? StatusCode { get; internal set; }
1730
}
1831

1932
internal class GetAliasResponseConverter : JsonConverter
@@ -27,7 +40,24 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
2740

2841
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
2942
{
30-
var dict = serializer.Deserialize<Dictionary<string, Dictionary<string, Dictionary<string, AliasDefinition>>>>(reader);
43+
var j = JObject.Load(reader);
44+
var errorProperty =j.Property("error");
45+
string error = null;
46+
if (errorProperty?.Value?.Type == JTokenType.String)
47+
{
48+
error = errorProperty.Value.Value<string>();
49+
errorProperty.Remove();
50+
}
51+
var statusProperty =j.Property("status");
52+
int? statusCode = null;
53+
if (statusProperty?.Value?.Type == JTokenType.Integer)
54+
{
55+
statusCode = statusProperty.Value.Value<int>();
56+
statusProperty.Remove();
57+
}
58+
59+
//Read the remaining properties as aliases
60+
var dict = serializer.Deserialize<Dictionary<string, Dictionary<string, Dictionary<string, AliasDefinition>>>>(j.CreateReader());
3161
var indices = new Dictionary<string, IReadOnlyList<AliasDefinition>>();
3262

3363
foreach (var kv in dict)
@@ -49,7 +79,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
4979
indices.Add(indexDict, aliases);
5080
}
5181

52-
return new GetAliasResponse { Indices = indices };
82+
return new GetAliasResponse { Indices = indices, Error = error, StatusCode = statusCode};
5383
}
5484

5585
public override bool CanConvert(Type objectType) => true;

src/Tests/ClientConcepts/Certificates/SslAndKpiXPackCluster.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,21 @@ public abstract class SslAndKpiXPackCluster : XPackCluster
3131
"xpack.security.http.ssl.enabled=true",
3232
};
3333

34-
private static int _port = 9200;
35-
private int? _desiredPort;
36-
public override int DesiredPort
37-
{
38-
get
39-
{
40-
if (!this._desiredPort.HasValue)
41-
this._desiredPort = ++_port;
42-
return this._desiredPort.Value;
43-
}
44-
}
34+
// private static readonly object _lock = new object();
35+
// private static int _port = 9200;
36+
// private int? _desiredPort;
37+
// public override int DesiredPort
38+
// {
39+
// get
40+
// {
41+
// lock (_lock)
42+
// {
43+
// if (!this._desiredPort.HasValue)
44+
// this._desiredPort = ++_port;
45+
// return this._desiredPort.Value;
46+
// }
47+
// }
48+
// }
4549

4650
public override ConnectionSettings ClusterConnectionSettings(ConnectionSettings s) =>
4751
this.ConnectionSettings(Authenticate(s));

src/Tests/ClientConcepts/Certificates/WorkingWithCertificates.doc.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Tests.ClientConcepts.Certificates
2424
* `ServicePointManager.ServerCertificateValidationCallback`. Most examples you will find doing this this will simply return `true` from the
2525
* validation callback and merrily whistle off into the sunset. **This is not advisable** as it allows *any* HTTPS traffic through in the
2626
* current `AppDomain` *without* any validation. Here's a concrete example:
27+
*
2728
*/
2829
public class WorkingWithCertificates
2930
{
@@ -65,9 +66,7 @@ protected override ConnectionSettings ConnectionSettings(ConnectionSettings s) =
6566
[IntegrationOnly]
6667
public class DenyAllSslCertificatesApiTests : ConnectionErrorTestBase<DenyAllCertificatesCluster>
6768
{
68-
public DenyAllSslCertificatesApiTests(DenyAllCertificatesCluster cluster, EndpointUsage usage) : base(cluster, usage)
69-
{
70-
}
69+
public DenyAllSslCertificatesApiTests(DenyAllCertificatesCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
7170

7271
[I]
7372
public async Task UsedHttps() => await AssertOnAllResponses(r => r.ApiCall.Uri.Scheme.Should().Be("https"));

src/Tests/Framework/ManagedElasticsearch/Clusters/ClusterBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void Start()
4545
if (!this.SkipValidation)
4646
this.TaskRunner.ValidateAfterStart(this.Node.Client);
4747
if (this.NodeConfiguration.RunIntegrationTests && this.Node.Port != this.DesiredPort)
48-
throw new Exception($"The cluster that was started runs on {this.Node.Port} but this cluster wants {this.DesiredPort}");
48+
throw new Exception($"The cluster that was started of type {this.GetType().Name} runs on {this.Node.Port} but this cluster wants {this.DesiredPort}");
4949
this.SeedNode();
5050
}
5151

src/Tests/Indices/AliasManagement/GetAlias/GetAliasApiTests.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
using Tests.Framework;
88
using Tests.Framework.Integration;
99
using Tests.Framework.ManagedElasticsearch.Clusters;
10+
using Tests.Framework.ManagedElasticsearch.NodeSeeders;
1011
using Xunit;
1112

1213
namespace Tests.Indices.AliasManagement.GetAlias
1314
{
1415
public class GetAliasApiTests : ApiIntegrationTestBase<ReadOnlyCluster, IGetAliasResponse, IGetAliasRequest, GetAliasDescriptor, GetAliasRequest>
1516
{
16-
private static readonly Names Names = Infer.Names("alias, x", "y");
17+
private static readonly Names Names = Infer.Names(DefaultSeeder.ProjectsAliasName);
1718

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

@@ -27,10 +28,42 @@ protected override LazyResponses ClientUsage() => Calls(
2728
protected override bool ExpectIsValid => true;
2829
protected override int ExpectStatusCode => 200;
2930
protected override HttpMethod HttpMethod => HttpMethod.GET;
30-
protected override string UrlPath => $"_all/_alias/alias%2Cx%2Cy";
31+
protected override string UrlPath => $"_all/_alias/{DefaultSeeder.ProjectsAliasName}";
3132
protected override void ExpectResponse(IGetAliasResponse response)
3233
{
3334
response.Indices.Should().NotBeNull();
35+
response.Indices.Count.Should().BeGreaterThan(0);
36+
}
37+
protected override bool SupportsDeserialization => false;
38+
39+
protected override Func<GetAliasDescriptor, IGetAliasRequest> Fluent => d=>d
40+
.AllIndices()
41+
.Name(Names)
42+
;
43+
protected override GetAliasRequest Initializer => new GetAliasRequest(Nest.Indices.All, Names);
44+
}
45+
46+
public class GetAliasPartialMatchApiTests : ApiIntegrationTestBase<ReadOnlyCluster, IGetAliasResponse, IGetAliasRequest, GetAliasDescriptor, GetAliasRequest>
47+
{
48+
private static readonly Names Names = Infer.Names(DefaultSeeder.ProjectsAliasName,"x", "y");
49+
50+
public GetAliasPartialMatchApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
51+
52+
protected override LazyResponses ClientUsage() => Calls(
53+
fluent: (client, f) => client.GetAlias(f),
54+
fluentAsync: (client, f) => client.GetAliasAsync(f),
55+
request: (client, r) => client.GetAlias(r),
56+
requestAsync: (client, r) => client.GetAliasAsync(r)
57+
);
58+
59+
protected override bool ExpectIsValid => true;
60+
protected override int ExpectStatusCode => TestClient.VersionUnderTestSatisfiedBy("<5.5.0") ? 200 : 404;
61+
protected override HttpMethod HttpMethod => HttpMethod.GET;
62+
protected override string UrlPath => $"_all/_alias/{DefaultSeeder.ProjectsAliasName}%2Cx%2Cy";
63+
protected override void ExpectResponse(IGetAliasResponse response)
64+
{
65+
response.Indices.Should().NotBeNull();
66+
response.Indices.Count.Should().BeGreaterThan(0);
3467
}
3568
protected override bool SupportsDeserialization => false;
3669

src/Tests/tests.default.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
mode: i
99
# the elasticsearch version that should be started
1010
# Can be a snapshot version of sonatype or "latest" to get the latest snapshot of sonatype
11-
elasticsearch_version: 5.5.0
11+
elasticsearch_version: 5.4.1
1212
# cluster filter allows you to only run the integration tests of a particular cluster (cluster suffix not needed)
1313
# cluster_filter:
1414
# whether we want to forcefully reseed on the node, if you are starting the tests with a node already running

0 commit comments

Comments
 (0)