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)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
return await this.client.GetItemById<ServerInfo>(ipAddress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task UpsertServerInfo(ServerInfo serverInfo)
|
|
|
|
|
{
|
2020-08-09 02:26:41 +02:00
|
|
|
|
await this.client.UpsertItem(
|
|
|
|
|
item: serverInfo,
|
|
|
|
|
partitionKey: new PartitionKey(serverInfo.DocumentType));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task DeleteServers(IEnumerable<string> ids, string partitionKey = null)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|