Skip to content

Commit

Permalink
Address PR comments, changing response type for Has Privileges API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
codebrain committed Mar 8, 2019
1 parent 7e5bfce commit 768e1c6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Nest
{
[JsonConverter(typeof(HasPrivilegesResponseJsonConverter))]
public interface IHasPrivilegesResponse : IResponse
{
[JsonProperty("username")]
Expand All @@ -12,25 +16,79 @@ public interface IHasPrivilegesResponse : IResponse
bool HasAllRequested { get; }

[JsonProperty("cluster")]
IReadOnlyDictionary<string, IReadOnlyDictionary<string, bool>> Cluster { get; }
IReadOnlyDictionary<string, bool> Clusters { get; }

[JsonProperty("index")]
IReadOnlyDictionary<string, IReadOnlyDictionary<string, IReadOnlyDictionary<string, bool>>> Index { get; }
IReadOnlyCollection<ResourcePrivileges> Indices { get; }

[JsonProperty("application")]
IReadOnlyDictionary<string, IReadOnlyDictionary<string, IReadOnlyDictionary<string, bool>>> Application { get; }
IReadOnlyDictionary<string, IReadOnlyCollection<ResourcePrivileges>> Applications { get; }
}

public class HasPrivilegesResponse : ResponseBase, IHasPrivilegesResponse
{
public string Username { get; internal set; }
public bool HasAllRequested { get; internal set; }
public IReadOnlyDictionary<string, IReadOnlyDictionary<string, bool>> Cluster { get;internal set; }
public IReadOnlyDictionary<string, IReadOnlyDictionary<string, IReadOnlyDictionary<string, bool>>> Index { get;internal set; }
public IReadOnlyDictionary<string, IReadOnlyDictionary<string, IReadOnlyDictionary<string, bool>>> Application

public IReadOnlyDictionary<string, bool> Clusters { get; internal set; } = EmptyReadOnly<string, bool>.Dictionary;

public IReadOnlyCollection<ResourcePrivileges> Indices { get; internal set; } = EmptyReadOnly<ResourcePrivileges>.Collection;

public IReadOnlyDictionary<string, IReadOnlyCollection<ResourcePrivileges>> Applications { get; internal set; } =
EmptyReadOnly<string, IReadOnlyCollection<ResourcePrivileges>>.Dictionary;
}

public class ResourcePrivileges
{
public string Resource { get; internal set; }

public IReadOnlyDictionary<string, bool> Privileges { get; internal set; } = EmptyReadOnly<string, bool>.Dictionary;
}

internal class HasPrivilegesResponseJsonConverter : JsonConverter

This comment has been minimized.

Copy link
@russcam

russcam Mar 8, 2019

Contributor

I don't think a JsonConverter is needed for the complete response; it looks like just one JsonConverter is needed to convert a JSON object to a readonly collection of ResourcePrivileges, and to attribute just that property with a JsonConverterAttribute to use that JsonConverter. JSON.Net's default deserialization can take care of the rest.

This comment has been minimized.

Copy link
@codebrain

codebrain Mar 18, 2019

Author Contributor

Addressed in 0fbbb00

{
public override bool CanConvert(Type objectType) => true;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotSupportedException();
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
get;
internal set;
var response = new HasPrivilegesResponse();
var jsonObject = JObject.Load(reader);

response.Username = jsonObject.Property("username").Value.Value<string>();
response.HasAllRequested = jsonObject.Property("has_all_requested").Value.Value<bool>();

response.Clusters = jsonObject.Property("cluster")
.Value.Value<JObject>()
.Properties()
.ToDictionary(c => c.Name, c => c.Value.Value<bool>());

response.Indices = jsonObject.Property("index")
.Value.Value<JObject>()
.Properties()
.Select(o => new ResourcePrivileges
{
Resource = o.Name,
Privileges = o.Value.Value<JObject>()
.Properties()
.ToDictionary(p => p.Name, p => p.Value.Value<bool>())
}).ToList().AsReadOnly();

var apps = new Dictionary<string, IReadOnlyCollection<ResourcePrivileges>>();
foreach (var applicationProp in jsonObject.Property("application").Value.Value<JObject>())
{
apps.Add(applicationProp.Key, applicationProp.Value.Value<JObject>()
.Properties()
.Select(o => new ResourcePrivileges
{
Resource = o.Name,
Privileges = o.Value.Value<JObject>()
.Properties()
.ToDictionary(p => p.Name, p => p.Value.Value<bool>())
}).ToList().AsReadOnly());
}
response.Applications = apps;

return response;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,11 @@ [I] public async Task HasPrivilegesResponse() => await Assert<HasPrivilegesRespo
{
r.IsValid.Should().BeTrue();
r.ApiCall.HttpStatusCode.Should().Be(200);

r.Username.Should().Be($"user-{v}");

r.HasAllRequested.Should().Be(true);

r.Application.Should().NotBeEmpty();
r.Applications.Should().NotBeEmpty();
var app = $"app-{v}";
var hasApp = r.Application.TryGetValue(app, out var privilegesDict);
var hasApp = r.Applications.TryGetValue(app, out var privilegesDict);
hasApp.Should().BeTrue($"expect `{app}` to be returned");
privilegesDict.Should().NotBeNull($"expect `{app}`'s value not to be null");
});
Expand Down

0 comments on commit 768e1c6

Please sign in to comment.