using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Api.Service;
using InfluxDB.Client.Core;
namespace InfluxDB.Client
{
public interface ISourcesApi
{
///
/// Creates a new Source and sets with the new identifier.
///
/// source to create
/// Cancellation token
/// created Source
Task CreateSourceAsync(Source source, CancellationToken cancellationToken = default);
///
/// Update a Source.
///
/// source update to apply
/// Cancellation token
/// updated source
Task UpdateSourceAsync(Source source, CancellationToken cancellationToken = default);
///
/// Delete a source.
///
/// ID of source to delete
/// Cancellation token
/// delete has been accepted
Task DeleteSourceAsync(string sourceId, CancellationToken cancellationToken = default);
///
/// Delete a source.
///
/// source to delete
/// Cancellation token
/// delete has been accepted
Task DeleteSourceAsync(Source source, CancellationToken cancellationToken = default);
///
/// Clone a source.
///
/// name of cloned source
/// ID of source to clone
/// Cancellation token
/// cloned source
Task CloneSourceAsync(string clonedName, string sourceId,
CancellationToken cancellationToken = default);
///
/// Clone a source.
///
/// name of cloned source
/// source to clone
/// Cancellation token
/// cloned source
Task CloneSourceAsync(string clonedName, Source source,
CancellationToken cancellationToken = default);
///
/// Retrieve a source.
///
/// ID of source to get
/// Cancellation token
/// source details
Task FindSourceByIdAsync(string sourceId, CancellationToken cancellationToken = default);
///
/// Get all sources.
///
/// Cancellation token
/// A list of sources
Task> FindSourcesAsync(CancellationToken cancellationToken = default);
///
/// Get a sources buckets (will return dbrps in the form of buckets if it is a v1 source).
///
/// filter buckets to a specific source
/// Cancellation token
/// The buckets for source. If source does not exist than return null.
Task> FindBucketsBySourceAsync(Source source, CancellationToken cancellationToken = default);
///
/// Get a sources buckets (will return dbrps in the form of buckets if it is a v1 source).
///
/// filter buckets to a specific source ID
/// Cancellation token
/// The buckets for source. If source does not exist than return null.
Task> FindBucketsBySourceIdAsync(string sourceId,
CancellationToken cancellationToken = default);
///
/// Get a sources health.
///
/// source to check health
/// Cancellation token
/// health of source
Task HealthAsync(Source source, CancellationToken cancellationToken = default);
///
/// Get a sources health.
///
/// source to check health
/// Cancellation token
/// health of source
Task HealthAsync(string sourceId, CancellationToken cancellationToken = default);
}
public class SourcesApi : ISourcesApi
{
private readonly SourcesService _service;
protected internal SourcesApi(SourcesService service)
{
Arguments.CheckNotNull(service, nameof(service));
_service = service;
}
///
/// Creates a new Source and sets with the new identifier.
///
/// source to create
/// Cancellation token
/// created Source
public Task CreateSourceAsync(Source source, CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(source, nameof(source));
return _service.PostSourcesAsync(source, cancellationToken: cancellationToken);
}
///
/// Update a Source.
///
/// source update to apply
/// Cancellation token
/// updated source
public Task UpdateSourceAsync(Source source, CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(source, nameof(source));
return _service.PatchSourcesIDAsync(source.Id, source, cancellationToken: cancellationToken);
}
///
/// Delete a source.
///
/// ID of source to delete
/// Cancellation token
/// delete has been accepted
public Task DeleteSourceAsync(string sourceId, CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(sourceId, nameof(sourceId));
return _service.DeleteSourcesIDAsync(sourceId, cancellationToken: cancellationToken);
}
///
/// Delete a source.
///
/// source to delete
/// Cancellation token
/// delete has been accepted
public Task DeleteSourceAsync(Source source, CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(source, nameof(source));
return DeleteSourceAsync(source.Id, cancellationToken);
}
///
/// Clone a source.
///
/// name of cloned source
/// ID of source to clone
/// Cancellation token
/// cloned source
public async Task CloneSourceAsync(string clonedName, string sourceId,
CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(clonedName, nameof(clonedName));
Arguments.CheckNonEmptyString(sourceId, nameof(sourceId));
var source = await FindSourceByIdAsync(sourceId, cancellationToken).ConfigureAwait(false);
return await CloneSourceAsync(clonedName, source, cancellationToken).ConfigureAwait(false);
}
///
/// Clone a source.
///
/// name of cloned source
/// source to clone
/// Cancellation token
/// cloned source
public Task CloneSourceAsync(string clonedName, Source source,
CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(clonedName, nameof(clonedName));
Arguments.CheckNotNull(source, nameof(source));
var cloned = new Source
{
Name = clonedName,
OrgID = source.OrgID,
Default = source.Default,
Type = source.Type,
Url = source.Url,
InsecureSkipVerify = source.InsecureSkipVerify,
Telegraf = source.Telegraf,
Token = source.Token,
Username = source.Username,
Password = source.Password,
SharedSecret = source.SharedSecret,
MetaUrl = source.MetaUrl,
DefaultRP = source.DefaultRP
};
return CreateSourceAsync(cloned, cancellationToken);
}
///
/// Retrieve a source.
///
/// ID of source to get
/// Cancellation token
/// source details
public Task FindSourceByIdAsync(string sourceId, CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(sourceId, nameof(sourceId));
return _service.GetSourcesIDAsync(sourceId, cancellationToken: cancellationToken);
}
///
/// Get all sources.
///
/// Cancellation token
/// A list of sources
public async Task> FindSourcesAsync(CancellationToken cancellationToken = default)
{
var response = await _service.GetSourcesAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
return response._Sources;
}
///
/// Get a sources buckets (will return dbrps in the form of buckets if it is a v1 source).
///
/// filter buckets to a specific source
/// Cancellation token
/// The buckets for source. If source does not exist than return null.
public Task> FindBucketsBySourceAsync(Source source, CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(source, nameof(source));
return FindBucketsBySourceIdAsync(source.Id, cancellationToken);
}
///
/// Get a sources buckets (will return dbrps in the form of buckets if it is a v1 source).
///
/// filter buckets to a specific source ID
/// Cancellation token
/// The buckets for source. If source does not exist than return null.
public async Task> FindBucketsBySourceIdAsync(string sourceId,
CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(sourceId, nameof(sourceId));
var response = await _service.GetSourcesIDBucketsAsync(sourceId, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return response._Buckets;
}
///
/// Get a sources health.
///
/// source to check health
/// Cancellation token
/// health of source
public Task HealthAsync(Source source, CancellationToken cancellationToken = default)
{
Arguments.CheckNotNull(source, nameof(source));
return HealthAsync(source.Id, cancellationToken);
}
///
/// Get a sources health.
///
/// source to check health
/// Cancellation token
/// health of source
public Task HealthAsync(string sourceId, CancellationToken cancellationToken = default)
{
Arguments.CheckNonEmptyString(sourceId, nameof(sourceId));
return InfluxDBClient.GetHealthAsync(
_service.GetSourcesIDHealthAsync(sourceId, cancellationToken: cancellationToken));
}
}
}