diff --git a/Giants.Services/Services/IServerRegistryService.cs b/Giants.Services/Services/IServerRegistryService.cs index 991dea7..1db18e0 100644 --- a/Giants.Services/Services/IServerRegistryService.cs +++ b/Giants.Services/Services/IServerRegistryService.cs @@ -1,13 +1,12 @@ namespace Giants.Services { - using System; using System.Collections.Generic; - using System.Net; - using System.Text; using System.Threading.Tasks; public interface IServerRegistryService { + Task DeleteServer(string ipAddress); + Task> GetAllServers(); Task AddServer(ServerInfo server); diff --git a/Giants.Services/Services/ServerRegistryService.cs b/Giants.Services/Services/ServerRegistryService.cs index 56173cc..5fa99bb 100644 --- a/Giants.Services/Services/ServerRegistryService.cs +++ b/Giants.Services/Services/ServerRegistryService.cs @@ -85,6 +85,18 @@ return serverInfo.Values; } + public async Task DeleteServer(string ipAddress) + { + ArgumentUtility.CheckStringForNullOrEmpty(ipAddress, nameof(ipAddress)); + + ServerInfo serverInfo = await this.registryStore.GetServerInfo(ipAddress); + + if (serverInfo != null) + { + await this.registryStore.DeleteServer(serverInfo.id); + } + } + private async Task> PopulateCache(ICacheEntry entry) { entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1); diff --git a/Giants.Services/Store/CosmosDbServerRegistryStore.cs b/Giants.Services/Store/CosmosDbServerRegistryStore.cs index f771e76..fac462f 100644 --- a/Giants.Services/Store/CosmosDbServerRegistryStore.cs +++ b/Giants.Services/Store/CosmosDbServerRegistryStore.cs @@ -58,6 +58,11 @@ partitionKey: new PartitionKey(serverInfo.DocumentType)); } + public async Task DeleteServer(string id, string partitionKey = null) + { + await this.client.DeleteItem(id, partitionKey); + } + public async Task DeleteServers(IEnumerable ids, string partitionKey = null) { ArgumentUtility.CheckEnumerableForNullOrEmpty(ids, nameof(ids)); diff --git a/Giants.Services/Store/IServerRegistryStore.cs b/Giants.Services/Store/IServerRegistryStore.cs index d0f5016..b33c3d3 100644 --- a/Giants.Services/Store/IServerRegistryStore.cs +++ b/Giants.Services/Store/IServerRegistryStore.cs @@ -9,6 +9,12 @@ { Task Initialize(); + Task DeleteServer(string id, string partitionKey = null); + + Task DeleteServers(IEnumerable ids, string partitionKey = null); + + Task GetServerInfo(string ipAddress); + Task> GetServerInfos(Expression> whereExpression = null, string partitionKey = null); Task> GetServerInfos( @@ -16,10 +22,6 @@ Expression> whereExpression = null, string partitionKey = null); - Task GetServerInfo(string ipAddress); - Task UpsertServerInfo(ServerInfo serverInfo); - - Task DeleteServers(IEnumerable ids, string partitionKey = null); } } diff --git a/Giants.Services/Store/InMemoryServerRegistryStore.cs b/Giants.Services/Store/InMemoryServerRegistryStore.cs index c1ba7df..b1f079b 100644 --- a/Giants.Services/Store/InMemoryServerRegistryStore.cs +++ b/Giants.Services/Store/InMemoryServerRegistryStore.cs @@ -38,12 +38,26 @@ public Task> GetServerInfos(Expression> whereExpression = null, string partitionKey = null) { - throw new NotImplementedException(); + IQueryable serverInfoQuery = this.servers.Values.AsQueryable(); + + if (whereExpression != null) + { + serverInfoQuery = serverInfoQuery.Where(whereExpression); + } + + return Task.FromResult(serverInfoQuery.AsEnumerable()); } public Task> GetServerInfos(Expression> selectExpression, Expression> whereExpression = null, string partitionKey = null) { - throw new NotImplementedException(); + IQueryable serverInfoQuery = this.servers.Values.AsQueryable(); + + if (whereExpression != null) + { + serverInfoQuery = serverInfoQuery.Where(whereExpression); + } + + return Task.FromResult(serverInfoQuery.Select(selectExpression).AsEnumerable()); } public Task DeleteServers(IEnumerable ids, string partitionKey = null) @@ -55,5 +69,12 @@ return Task.CompletedTask; } + + public Task DeleteServer(string id, string partitionKey = null) + { + this.servers.TryRemove(id, out ServerInfo _); + + return Task.CompletedTask; + } } } diff --git a/Giants.WebApi/Controllers/ServersController.cs b/Giants.WebApi/Controllers/ServersController.cs index 9fd3323..403600b 100644 --- a/Giants.WebApi/Controllers/ServersController.cs +++ b/Giants.WebApi/Controllers/ServersController.cs @@ -33,6 +33,16 @@ namespace Giants.Web.Controllers this.serverRegistryService = serverRegistryService; } + [HttpDelete] + public async Task DeleteServer() + { + string requestIpAddress = this.GetRequestIpAddress(); + + this.logger.LogInformation("Deleting server from {IPAddress}", requestIpAddress); + + await this.serverRegistryService.DeleteServer(requestIpAddress); + } + [HttpGet] public async Task> GetServers() {