1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-05 06:45:37 +01:00
GiantsTools/Giants.Services/Store/CosmosDbServerRegistryStore.cs

89 lines
3.1 KiB
C#
Raw Normal View History

2020-08-09 01:31:16 +02:00
namespace Giants.Services
{
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Configuration;
2020-08-09 02:26:41 +02:00
using Microsoft.Extensions.Logging;
2020-08-09 01:31:16 +02:00
public class CosmosDbServerRegistryStore : IServerRegistryStore
{
2020-08-09 02:26:41 +02:00
private readonly ILogger<CosmosDbServerRegistryStore> logger;
2020-08-09 01:31:16 +02:00
private readonly IConfiguration configuration;
private CosmosDbClient client;
2020-08-09 02:26:41 +02:00
public CosmosDbServerRegistryStore(
ILogger<CosmosDbServerRegistryStore> logger,
IConfiguration configuration)
2020-08-09 01:31:16 +02:00
{
2020-08-09 02:26:41 +02:00
this.logger = logger;
2020-08-09 01:31:16 +02:00
this.configuration = configuration;
}
public async Task<IEnumerable<ServerInfo>> GetServerInfos(
2020-08-09 02:26:41 +02:00
Expression<Func<ServerInfo, bool>> whereExpression = null,
string partitionKey = null)
2020-08-09 01:31:16 +02:00
{
return await this.client.GetItems<ServerInfo>(whereExpression);
}
2020-08-09 02:26:41 +02:00
public async Task<IEnumerable<TSelect>> GetServerInfos<TSelect>(
Expression<Func<ServerInfo, TSelect>> selectExpression,
Expression<Func<ServerInfo, bool>> whereExpression = null,
string partitionKey = null)
{
2020-08-09 02:52:26 +02:00
ArgumentUtility.CheckForNull(selectExpression, nameof(selectExpression));
2020-08-09 02:26:41 +02:00
return await this.client.GetItems<ServerInfo, TSelect>(
selectExpression: selectExpression,
whereExpression: whereExpression,
partitionKey: partitionKey);
}
2020-08-09 01:31:16 +02:00
public async Task<ServerInfo> GetServerInfo(string ipAddress)
{
2020-08-09 02:52:26 +02:00
ArgumentUtility.CheckStringForNullOrEmpty(ipAddress, nameof(ipAddress));
2020-08-09 01:31:16 +02:00
return await this.client.GetItemById<ServerInfo>(ipAddress);
}
public async Task UpsertServerInfo(ServerInfo serverInfo)
{
2020-08-09 02:52:26 +02:00
ArgumentUtility.CheckForNull(serverInfo, nameof(serverInfo));
2020-08-09 02:26:41 +02:00
await this.client.UpsertItem(
item: serverInfo,
partitionKey: new PartitionKey(serverInfo.DocumentType));
}
2020-08-10 07:11:10 +02:00
public async Task DeleteServer(string id, string partitionKey = null)
{
await this.client.DeleteItem<ServerInfo>(id, partitionKey);
}
2020-08-09 02:26:41 +02:00
public async Task DeleteServers(IEnumerable<string> ids, string partitionKey = null)
{
2020-08-09 02:52:26 +02:00
ArgumentUtility.CheckEnumerableForNullOrEmpty(ids, nameof(ids));
2020-08-09 02:26:41 +02:00
foreach (string id in ids)
{
this.logger.LogInformation("Deleting server for host IP {IPAddress}", id);
await this.client.DeleteItem<ServerInfo>(id, partitionKey);
}
2020-08-09 01:31:16 +02:00
}
public async Task Initialize()
{
this.client = new CosmosDbClient(
connectionString: this.configuration["CosmosDbEndpoint"],
authKeyOrResourceToken: this.configuration["CosmosDbKey"],
databaseId: this.configuration["DatabaseId"],
containerId: this.configuration["ContainerId"]);
await this.client.Initialize();
}
}
2020-08-09 02:26:41 +02:00
}