Skip to content

Support basic Admin API #370

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 7 commits into from
May 16, 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
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
- run:
name: Test
command:
dotnet test -c Release --filter "Feature!=StreamTransaction&RunningMode!=Cluster&Feature!=Analyzer"
dotnet test -c Release --filter "Feature!=StreamTransaction&RunningMode!=Cluster&Feature!=Analyzer&ServerVersion!=3_8_PLUS"

"test-arangodb-3_5":
working_directory: ~/arangodb-net-standard
Expand All @@ -80,7 +80,7 @@ jobs:
- run:
name: Test
command:
dotnet test -c Release --filter "RunningMode!=Cluster"
dotnet test -c Release --filter "RunningMode!=Cluster&ServerVersion!=3_8_PLUS"

"test-arangodb-3_6":
working_directory: ~/arangodb-net-standard
Expand All @@ -99,7 +99,7 @@ jobs:
- run:
name: Test
command:
dotnet test -c Release --filter "RunningMode!=Cluster"
dotnet test -c Release --filter "RunningMode!=Cluster&ServerVersion!=3_8_PLUS"

"test-arangodb-3_7":
working_directory: ~/arangodb-net-standard
Expand All @@ -118,7 +118,7 @@ jobs:
- run:
name: Test
command:
dotnet test -c Release --filter RunningMode!=Cluster
dotnet test -c Release --filter "RunningMode!=Cluster&ServerVersion!=3_8_PLUS"

"test-arangodb-3_8":
working_directory: ~/arangodb-net-standard
Expand Down
95 changes: 95 additions & 0 deletions arangodb-net-standard.Test/AdminApi/AdminApiClientTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using ArangoDBNetStandard;
using ArangoDBNetStandard.AdminApi;
using ArangoDBNetStandard.AdminApi.Models;
using ArangoDBNetStandard.Transport;
using Moq;
using Xunit;

namespace ArangoDBNetStandardTest.AdminApi
{
public class AdminApiClientTest : IClassFixture<AdminApiClientTestFixture>, IAsyncLifetime
{
private AdminApiClient _adminApi;
private ArangoDBClient _adb;

public AdminApiClientTest(AdminApiClientTestFixture fixture)
{
_adb = fixture.ArangoDBClient;
_adminApi = _adb.Admin;
}

public Task InitializeAsync()
{
return Task.CompletedTask;
}

public Task DisposeAsync()
{
return Task.CompletedTask;
}

/// <summary>
/// Works only on version 3.8 onwards.
/// </summary>
/// <returns></returns>
[Fact]
[Trait("ServerVersion", "3_8_PLUS")]
public async Task GetLogsAsync_ShouldSucceed()
{
var getResponse = await _adminApi.GetLogsAsync();
Assert.NotNull(getResponse);
}

[Fact]
public async Task PostReloadRoutingInfoAsync_ShouldSucceed()
{
var postResponse = await _adminApi.PostReloadRoutingInfoAsync();
Assert.True(postResponse);
}

/// <summary>
/// This test will run only in a cluster
/// </summary>
/// <returns></returns>
[Fact]
[Trait("RunningMode", "Cluster")]
public async Task GetServerIdAsync_ShouldSucceed()
{
var getResponse = await _adminApi.GetServerIdAsync();
Assert.NotNull(getResponse);
Assert.False(getResponse.Error);
Assert.NotNull(getResponse.Id);
}

[Fact]
public async Task GetServerRoleAsync_ShouldSucceed()
{
var getResponse = await _adminApi.GetServerRoleAsync();
Assert.NotNull(getResponse);
Assert.False(getResponse.Error);
Assert.NotNull(getResponse.Role);
}

[Fact]
public async Task GetServerEngineTypeAsync_ShouldSucceed()
{
var getResponse = await _adminApi.GetServerEngineTypeAsync();
Assert.NotNull(getResponse);
Assert.NotNull(getResponse.Name);
}

[Fact]
public async Task GetServerVersionAsync_ShouldSucceed()
{
var getResponse = await _adminApi.GetServerVersionAsync();
Assert.NotNull(getResponse);
Assert.NotNull(getResponse.License);
Assert.NotNull(getResponse.Version);
Assert.NotNull(getResponse.Server);
}
}
}
41 changes: 41 additions & 0 deletions arangodb-net-standard.Test/AdminApi/AdminApiClientTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using ArangoDBNetStandard;
using ArangoDBNetStandard.CollectionApi.Models;
using ArangoDBNetStandard.IndexApi.Models;
using System;
using System.Threading.Tasks;

namespace ArangoDBNetStandardTest.AdminApi
{
public class AdminApiClientTestFixture : ApiClientTestFixtureBase
{
public ArangoDBClient ArangoDBClient { get; internal set; }

public AdminApiClientTestFixture()
{
}

public override async Task InitializeAsync()
{
await base.InitializeAsync();
string dbName = nameof(AdminApiClientTestFixture);
await CreateDatabase(dbName);
Console.WriteLine("Database " + dbName + " created successfully");
ArangoDBClient = GetArangoDBClient(dbName);
try
{
var dbRes = await ArangoDBClient.Database.GetCurrentDatabaseInfoAsync();
if (dbRes.Error)
throw new Exception("GetCurrentDatabaseInfoAsync failed: " + dbRes.Code.ToString());
else
{
Console.WriteLine("Running tests in database " + dbRes.Result.Name);
}
}
catch (ApiErrorException ex)
{
Console.WriteLine(ex.Message);
throw ex;
}
}
}
}
198 changes: 198 additions & 0 deletions arangodb-net-standard/AdminApi/AdminApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
using System.Net;
using System.Threading.Tasks;
using ArangoDBNetStandard.Serialization;
using ArangoDBNetStandard.Transport;
using ArangoDBNetStandard.AdminApi.Models;

namespace ArangoDBNetStandard.AdminApi
{
/// <summary>
/// A client for interacting with ArangoDB Admin API,
/// implementing <see cref="IAdminApiClient"/>.
/// </summary>
public class AdminApiClient : ApiClientBase, IAdminApiClient
{
/// <summary>
/// The transport client used to communicate with the ArangoDB host.
/// </summary>
protected IApiClientTransport _client;

/// <summary>
/// The root path of the API.
/// </summary>
protected readonly string _adminApiPath = "_admin";

/// <summary>
/// Creates an instance of <see cref="AdminApiClient"/>
/// using the provided transport layer and the default JSON serialization.
/// </summary>
/// <param name="client">Transport client that the API client will use to communicate with ArangoDB</param>
public AdminApiClient(IApiClientTransport client)
: base(new JsonNetApiClientSerialization())
{
_client = client;
}

/// <summary>
/// Creates an instance of <see cref="AdminApiClient"/>
/// using the provided transport and serialization layers.
/// </summary>
/// <param name="client">Transport client that the API client will use to communicate with ArangoDB.</param>
/// <param name="serializer">Serializer to be used.</param>
public AdminApiClient(IApiClientTransport client, IApiClientSerialization serializer)
: base(serializer)
{
_client = client;
}

/// <summary>
/// Retrieves log messages from the server.
/// GET /_admin/log/entries
/// Works on ArangoDB 3.8 or later.
/// </summary>
/// <param name="query">Query string parameters</param>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#read-global-logs-from-the-server
/// </remarks>
public virtual async Task<GetLogsResponse> GetLogsAsync(GetLogsQuery query = null)
{
string uri = $"{_adminApiPath}/log/entries";
if (query != null)
{
uri += '?' + query.ToQueryString();
}
using (var response = await _client.GetAsync(uri).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
return DeserializeJsonFromStream<GetLogsResponse>(stream);
}
throw await GetApiErrorException(response).ConfigureAwait(false);
}
}

/// <summary>
/// Reloads the routing table.
/// POST /_admin/routing/reload
/// </summary>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#reloads-the-routing-information
/// </remarks>
public virtual async Task<bool> PostReloadRoutingInfoAsync()
{
string uri = $"{_adminApiPath}/routing/reload";
var body = new byte[] { };
using (var response = await _client.PostAsync(uri, body).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
return true;
}
throw await GetApiErrorException(response).ConfigureAwait(false);
}
}

/// <summary>
/// Retrieves the internal id of the server.
/// The method will fail if the server is not running in cluster mode.
/// GET /_admin/server/id
/// </summary>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#return-id-of-a-server-in-a-cluster
/// </remarks>
public virtual async Task<GetServerIdResponse> GetServerIdAsync()
{
string uri = $"{_adminApiPath}/server/id";
using (var response = await _client.GetAsync(uri).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
return DeserializeJsonFromStream<GetServerIdResponse>(stream);
}
throw await GetApiErrorException(response).ConfigureAwait(false);
}
}

/// <summary>
/// Retrieves the role of the server in a cluster.
/// GET /_admin/server/role
/// </summary>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/administration-and-monitoring.html#return-the-role-of-a-server-in-a-cluster
/// </remarks>
public virtual async Task<GetServerRoleResponse> GetServerRoleAsync()
{
string uri = $"{_adminApiPath}/server/role";
using (var response = await _client.GetAsync(uri).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
return DeserializeJsonFromStream<GetServerRoleResponse>(stream);
}
throw await GetApiErrorException(response).ConfigureAwait(false);
}
}

/// <summary>
/// Retrieves the server database engine type.
/// GET /_api/engine
/// </summary>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/miscellaneous-functions.html#return-server-database-engine-type
/// </remarks>
public virtual async Task<GetServerEngineTypeResponse> GetServerEngineTypeAsync()
{
string uri = "_api/engine";
using (var response = await _client.GetAsync(uri).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
return DeserializeJsonFromStream<GetServerEngineTypeResponse>(stream);
}
throw await GetApiErrorException(response).ConfigureAwait(false);
}
}

/// <summary>
/// Retrieves the server version.
/// GET /_api/version
/// </summary>
/// <param name="query">Query string parameters</param>
/// <returns></returns>
/// <remarks>
/// For further information see
/// https://www.arangodb.com/docs/stable/http/miscellaneous-functions.html#return-server-version
/// </remarks>
public virtual async Task<GetServerVersionResponse> GetServerVersionAsync(GetServerVersionQuery query = null)
{
string uri = "_api/version";
if (query != null)
{
uri += '?' + query.ToQueryString();
}
using (var response = await _client.GetAsync(uri).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
return DeserializeJsonFromStream<GetServerVersionResponse>(stream);
}
throw await GetApiErrorException(response).ConfigureAwait(false);
}
}
}
}
Loading