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; using Microsoft.Extensions.Logging; public class CosmosDbServerRegistryStore : IServerRegistryStore { private readonly ILogger logger; private readonly IConfiguration configuration; private CosmosDbClient client; public CosmosDbServerRegistryStore( ILogger logger, IConfiguration configuration) { this.logger = logger; this.configuration = configuration; } public async Task> GetServerInfos( Expression> whereExpression = null, string partitionKey = null) { return await this.client.GetItems(whereExpression); } public async Task> GetServerInfos( Expression> selectExpression, Expression> whereExpression = null, string partitionKey = null) { return await this.client.GetItems( selectExpression: selectExpression, whereExpression: whereExpression, partitionKey: partitionKey); } public async Task GetServerInfo(string ipAddress) { return await this.client.GetItemById(ipAddress); } public async Task UpsertServerInfo(ServerInfo serverInfo) { await this.client.UpsertItem( item: serverInfo, partitionKey: new PartitionKey(serverInfo.DocumentType)); } public async Task DeleteServers(IEnumerable ids, string partitionKey = null) { foreach (string id in ids) { this.logger.LogInformation("Deleting server for host IP {IPAddress}", id); await this.client.DeleteItem(id, partitionKey); } } 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(); } } }