Skip to content

Commit

Permalink
Fetching confgiuration, statistics, info from remote service
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Dębowski committed Dec 6, 2024
1 parent 8b55280 commit 1c068fa
Show file tree
Hide file tree
Showing 35 changed files with 479 additions and 70 deletions.
9 changes: 8 additions & 1 deletion hiPower.Manager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hiPower.Test.Integration",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hiPower.Repository", "src\hiPower.Repository\hiPower.Repository.csproj", "{90CDBFED-0814-4F69-9039-CC087D0B3C55}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hiPower.Repository.Azure", "src\hiPower.Repository.Azure\hiPower.Repository.Azure.csproj", "{0C64BF69-BBE7-4AAA-A9C0-4A48CF4521BC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hiPower.Repository.Azure", "src\hiPower.Repository.Azure\hiPower.Repository.Azure.csproj", "{0C64BF69-BBE7-4AAA-A9C0-4A48CF4521BC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hiPower.Server.Communication", "src\hiPower.Server.Communication\hiPower.Server.Communication.csproj", "{09F62807-E6CE-4C3B-904C-7CE16852D579}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -89,6 +91,10 @@ Global
{0C64BF69-BBE7-4AAA-A9C0-4A48CF4521BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C64BF69-BBE7-4AAA-A9C0-4A48CF4521BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C64BF69-BBE7-4AAA-A9C0-4A48CF4521BC}.Release|Any CPU.Build.0 = Release|Any CPU
{09F62807-E6CE-4C3B-904C-7CE16852D579}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{09F62807-E6CE-4C3B-904C-7CE16852D579}.Debug|Any CPU.Build.0 = Debug|Any CPU
{09F62807-E6CE-4C3B-904C-7CE16852D579}.Release|Any CPU.ActiveCfg = Release|Any CPU
{09F62807-E6CE-4C3B-904C-7CE16852D579}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -106,6 +112,7 @@ Global
{3C659985-42FD-4891-A56B-6D3D36BE939F} = {9AAC9818-174C-4CA7-A6A7-3A3CEE2A195C}
{90CDBFED-0814-4F69-9039-CC087D0B3C55} = {82B938FA-29E8-41B4-B4FA-AF77C343884E}
{0C64BF69-BBE7-4AAA-A9C0-4A48CF4521BC} = {82B938FA-29E8-41B4-B4FA-AF77C343884E}
{09F62807-E6CE-4C3B-904C-7CE16852D579} = {82B938FA-29E8-41B4-B4FA-AF77C343884E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F5E8CDB0-BE42-425F-B06D-EEF8F312C879}
Expand Down
8 changes: 8 additions & 0 deletions src/hiPower.Abstracts/IRemoteService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace hiPower.Abstracts;

public interface IRemoteService
{
Task<ErrorOr<IEnumerable<ConfigSetting>>> GetConfigurationAsync (RemoteServiceOptions options);
Task<ErrorOr<IEnumerable<StatisticsItem>>> GetStatisticsAsync (RemoteServiceOptions options);
Task<ErrorOr<ServerInfo>> GetInfoAsync (RemoteServiceOptions options);
}
10 changes: 5 additions & 5 deletions src/hiPower.Abstracts/IServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
public interface IServerService
{
Task<ErrorOr<IEnumerable<Server>>> GetAllAsync ();
Task<ErrorOr<IEnumerable<Server>>> GetAllAsync (string dataCenterId);
Task<ErrorOr<IEnumerable<Server>>> GetAllAsync (string dataCenterId);
Task<ErrorOr<Server>> GetAsync (string id);

Task<ErrorOr<Server>> CreateAsync(Server server);

Task<ErrorOr<Server>> UpdateAsync(string id, Server server);

Task<ErrorOr<bool>> DeleteAsync (string id);

Task<ErrorOr<IEnumerable<HintItem>>> GetAvailableDataCentersAsync ();

Task<ErrorOr<IEnumerable<ConfigSetting>>> GetRemoteConfigurationAsync(string id);
Task<ErrorOr<IEnumerable<StatisticsItem>>> GetRemoteStatisticsAsync (string id);
Task<ErrorOr<ServerInfo>> GetRemoteServerInfoAsync(string id);
}
2 changes: 1 addition & 1 deletion src/hiPower.Abstracts/IUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace hiPower.Abstracts;
public interface IUnitOfWork : IDisposable
{
IGenericRepository<ServerLocation> DataCenterRepository { get; }
IGenericRepository<Entity.Server> ServerRepository { get; }
IGenericRepository<Entity.ServerDetails> ServerRepository { get; }
Task SaveAsync ();
}
8 changes: 5 additions & 3 deletions src/hiPower.Abstracts/hiPower.Abstracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
</PropertyGroup>

<ItemGroup>
<Using Include="ErrorOr"/>
<Using Include="hiPower.Dto.Manager"/>
<Using Include="hiPower.Dto.Remote"/>
<Using Include="ErrorOr" />
<Using Include="hiPower.Dto.Manager" />
<Using Include="hiPower.Dto.Remote" />
<Using Include="hiPower.Common.Type.Options"/>
</ItemGroup>

<ItemGroup>
Expand All @@ -18,6 +19,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\hiPower.Common.Type\hiPower.Common.Type.csproj" />
<ProjectReference Include="..\hiPower.Dto\hiPower.Dto.csproj" />
<ProjectReference Include="..\hiPower.Entity\hiPower.Entity.csproj" />
</ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions src/hiPower.Common.Type/Extensions/ProtocolEnumExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace hiPower.Common.Type.Extensions;

public static class ProtocolEnumExtension
{
public static Protocol ToProtocol(this string value)
{
var isDone = Enum.TryParse<Protocol>(value, out var protocol);
if (isDone)
{
return protocol;
}
return Protocol.HTTP;
}
}
4 changes: 4 additions & 0 deletions src/hiPower.Common.Type/Options/RemoteServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace hiPower.Common.Type.Options;

public record struct RemoteServiceOptions(Protocol Proto, string HostAddress, uint Port, string LocalId, string ApiKey, string AuthToken);

7 changes: 7 additions & 0 deletions src/hiPower.Common.Type/Protocol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace hiPower.Common.Type;

public enum Protocol
{
HTTP,
HTTPS
}
20 changes: 11 additions & 9 deletions src/hiPower.Core/Mappings/ServerMappring.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
namespace hiPower.Core.Mappings;
using hiPower.Common.Type.Extensions;
using hiPower.Common.Type.Options;

namespace hiPower.Core.Mappings;

internal class ServerMappring : IRegister
{
public void Register (TypeAdapterConfig config)
{
config.NewConfig<Dto.Manager.Server, Entity.Server> ()
config.NewConfig<Server, ServerDetails> ()
.Map (dest => dest.Id, src => src.Id)
.Map (dest => dest.LocalId, src => src.LocalId)
.Map (dest => dest.LocationId, src => src.LocationId)
Expand All @@ -17,11 +20,9 @@ public void Register (TypeAdapterConfig config)
.Map (dest => dest.Auth, src => src.Auth)
.Map (dest => dest.Version, src => src.Version)
.Map (dest => dest.OS, src => src.OS)
.Map (dest => dest.Configuration, src => src.Configuration)
.Map (dest => dest.Timeout, src => src.Timeout)
.Map (dest => dest.Retries, src => src.Retries);
.Map (dest => dest.Configuration, src => src.Configuration);

config.NewConfig<Entity.Server, Dto.Manager.Server> ()
config.NewConfig<ServerDetails, Server> ()
.Map (dest => dest.Id, src => src.Id)
.Map (dest => dest.LocalId, src => src.LocalId)
.Map (dest => dest.LocationId, src => src.LocationId)
Expand All @@ -34,8 +35,9 @@ public void Register (TypeAdapterConfig config)
.Map (dest => dest.Auth, src => src.Auth)
.Map (dest => dest.Version, src => src.Version)
.Map (dest => dest.OS, src => src.OS)
.Map (dest => dest.Configuration, src => src.Configuration)
.Map (dest => dest.Timeout, src => src.Timeout)
.Map (dest => dest.Retries, src => src.Retries);
.Map (dest => dest.Configuration, src => src.Configuration);

config.NewConfig<ServerDetails, RemoteServiceOptions> ()
.MapWith (src => new RemoteServiceOptions(src.Proto.ToProtocol(), src.HostAddress, Convert.ToUInt16(src.Port), src.LocalId, src.ApiKey, src.Auth));
}
}
88 changes: 75 additions & 13 deletions src/hiPower.Core/ServerService.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using hiPower.Abstracts;
using hiPower.Common.Type.Errors;
using hiPower.Common.Type.Options;
using MapsterMapper;

namespace hiPower.Core;

public class ServerService(IUnitOfWork unit,
IMapper mapper) : IServerService
IMapper mapper,
IRemoteService remoteService) : IServerService
{
public async Task<ErrorOr<Dto.Manager.Server>> CreateAsync (Dto.Manager.Server server)
public async Task<ErrorOr<Server>> CreateAsync (Server server)
{
var result = await unit.ServerRepository.CreateAsync(server.Adapt<Entity.Server>());
var result = await unit.ServerRepository.CreateAsync(server.Adapt<ServerDetails>());
await unit.SaveAsync ();
return mapper.Map<Dto.Manager.Server> (result);
return mapper.Map<Server> (result);
}

public async Task<ErrorOr<bool>> DeleteAsync (string id)
Expand All @@ -20,31 +22,31 @@ public async Task<ErrorOr<bool>> DeleteAsync (string id)
return true;
}

public async Task<ErrorOr<IEnumerable<Dto.Manager.Server>>> GetAllAsync (string dataCenterId)
public async Task<ErrorOr<IEnumerable<Server>>> GetAllAsync (string dataCenterId)
{
var result = await unit.ServerRepository.GetAll(x => x.LocationId.Equals(dataCenterId.ToUpper()), null, null);
if (result.Any ())
{
return result.Adapt<IEnumerable<Dto.Manager.Server>> ()
return result.Adapt<IEnumerable<Server>> ()
.ToErrorOr ();
}
return Error.NotFound();
}

public async Task<ErrorOr<IEnumerable<Dto.Manager.Server>>> GetAllAsync ()
public async Task<ErrorOr<IEnumerable<Server>>> GetAllAsync ()
{
var result = await unit.ServerRepository.GetAll(null, null, null);
return result.Adapt<IEnumerable<Dto.Manager.Server>>()
return result.Adapt<IEnumerable<Server>>()
.ToErrorOr();
}

public async Task<ErrorOr<Dto.Manager.Server>> GetAsync (string id)
public async Task<ErrorOr<Server>> GetAsync (string id)
{
var result = await unit.ServerRepository
.GetAsync(x => x.Id.Equals(id.ToUpper()), null);
if (result is not null)
{
return mapper.Map<Dto.Manager.Server> (result);
return mapper.Map<Server> (result);
}
return Error.NotFound();
}
Expand All @@ -60,15 +62,75 @@ public async Task<ErrorOr<IEnumerable<HintItem>>> GetAvailableDataCentersAsync (
return Error.NotFound ();
}

public async Task<ErrorOr<Dto.Manager.Server>> UpdateAsync (string id, Dto.Manager.Server server)
public async Task<ErrorOr<Server>> UpdateAsync (string id, Server server)
{
if (id.ToUpper ().Equals (server.Id.ToUpper ()))
{
var updateServer = server.Adapt<Entity.Server> ();
var updateServer = server.Adapt<Entity.ServerDetails> ();
var result = unit.ServerRepository.Update(updateServer);
await unit.SaveAsync ();
return await Task.FromResult(result.Adapt<Dto.Manager.Server>());
return await Task.FromResult(result.Adapt<Server>());
}
return ServerServiceErrors.UpdateError;
}

public async Task<ErrorOr<IEnumerable<ConfigSetting>>> GetRemoteConfigurationAsync(string id)
{
var service = await GetAsync(id);

if (service.IsError && service.FirstError.Type == ErrorType.NotFound)
{
return Error.NotFound();
}

var options = service.Value.Adapt<RemoteServiceOptions> ();
var response = await remoteService.GetConfigurationAsync (options);

if (response.IsError)
{
return response.FirstError;
}

return response;
}

public async Task<ErrorOr<IEnumerable<StatisticsItem>>> GetRemoteStatisticsAsync (string id)
{
var service = await GetAsync(id);

if (service.IsError && service.FirstError.Type == ErrorType.NotFound)
{
return Error.NotFound ();
}

var options = service.Value.Adapt<RemoteServiceOptions> ();
var response = await remoteService.GetStatisticsAsync (options);

if (response.IsError)
{
return response.FirstError;
}

return response;
}

public async Task<ErrorOr<ServerInfo>> GetRemoteServerInfoAsync (string id)
{
var service = await GetAsync(id);

if (service.IsError && service.FirstError.Type == ErrorType.NotFound)
{
return Error.NotFound ();
}

var options = service.Value.Adapt<RemoteServiceOptions> ();
var response = await remoteService.GetInfoAsync (options);

if (response.IsError)
{
return response.FirstError;
}

return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace hiPower.Database.Configuration;

internal class ServerEntityConfiguration : IEntityTypeConfiguration<Server>
internal class ServerEntityConfiguration : IEntityTypeConfiguration<ServerDetails>
{
public void Configure (EntityTypeBuilder<Server> builder)
public void Configure (EntityTypeBuilder<ServerDetails> builder)
{
builder.ToTable ($"{Prefix.Table}{nameof(Server)}");
builder.ToTable ($"{Prefix.Table}Server");

builder.HasKey ( t => t.Id );

Expand Down
2 changes: 1 addition & 1 deletion src/hiPower.Database/ManagerDbContext.DbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace hiPower.Database;

public partial class ManagerDbContext
{
public DbSet<Server> Servers { get; set; }
public DbSet<ServerDetails> Servers { get; set; }
public DbSet<ServerLocation> Locations { get; set; }
}
8 changes: 8 additions & 0 deletions src/hiPower.Dto/Manager/SettingsItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace hiPower.Dto.Manager;

public class SettingsItem
{
public string Name { get; set; }
public string Type { get; set; }
public string Value { get; set; }
}
4 changes: 4 additions & 0 deletions src/hiPower.Dto/Remote/ConfigSetting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace hiPower.Dto.Remote;

public record class ConfigSetting(string Name, string Type, string Value);

5 changes: 5 additions & 0 deletions src/hiPower.Dto/Remote/MasterZone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace hiPower.Dto.Remote;

public class MasterZone
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace hiPower.Dto.Remote;

public sealed class ServerItem
public sealed class ServerInfo
{

[JsonPropertyName ("id")]
Expand Down
10 changes: 10 additions & 0 deletions src/hiPower.Dto/Remote/SlaveZone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using hiPower.Common.Type;

namespace hiPower.Dto.Remote;

public class SlaveZone
{
public string Name { get; set; }
public ZoneKind Kind { get; set; }
public IEnumerable<string> Nameservers { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace hiPower.Dto.Remote;

public class StatisticsItemResult
public class StatisticsItem
{
public string Name { get; set; }
public string Type { get; set; }
Expand Down
Loading

0 comments on commit 1c068fa

Please sign in to comment.