-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(backend): Add Health endpoint to the PeopleSoft API (#601)
* fix spelling of useAnonymous * add health checks * change startup path for VS
- Loading branch information
Showing
11 changed files
with
367 additions
and
3 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...ook.Source.PeopleSoft/src/Phonebook.Source.PeopleSoft/HealthChecks/HealthCheckBuilding.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Phonebook.Source.PeopleSoft.Models.Context; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Phonebook.Source.PeopleSoft.HealthChecks | ||
{ | ||
public class HealthCheckBuilding : IHealthCheck | ||
{ | ||
public HealthCheckBuilding(IServiceScopeFactory scopeFactory) | ||
{ | ||
ScopeFactory = scopeFactory; | ||
} | ||
public IServiceScopeFactory ScopeFactory { get; } | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using (var scope = ScopeFactory.CreateScope()) | ||
{ | ||
var result = await scope.ServiceProvider.GetService<ModelContext>().Buildings.OrderBy(d => d.Id).FirstAsync(); | ||
if (string.IsNullOrWhiteSpace(result.ShortName)) | ||
{ | ||
return HealthCheckResult.Unhealthy("Building data is not accessible! 💣"); | ||
} | ||
else | ||
{ | ||
return HealthCheckResult.Healthy("Building data is accessible."); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
return HealthCheckResult.Unhealthy($"Building data is not accessible! 💣 {ex.Message}"); | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...Source.PeopleSoft/src/Phonebook.Source.PeopleSoft/HealthChecks/HealthCheckBuildingPart.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Phonebook.Source.PeopleSoft.Models.Context; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Phonebook.Source.PeopleSoft.HealthChecks | ||
{ | ||
public class HealthCheckBuildingPart: IHealthCheck | ||
{ | ||
public HealthCheckBuildingPart(IServiceScopeFactory scopeFactory) | ||
{ | ||
ScopeFactory = scopeFactory; | ||
} | ||
public IServiceScopeFactory ScopeFactory { get; } | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using (var scope = ScopeFactory.CreateScope()) | ||
{ | ||
var result = await scope.ServiceProvider.GetService<ModelContext>().BuildingParts.OrderBy(d => d.Id).FirstAsync(); | ||
if (string.IsNullOrWhiteSpace(result.BuildingId?.ToString())) | ||
{ | ||
return HealthCheckResult.Unhealthy("BuildingPart data is not accessible! 💣"); | ||
} | ||
else | ||
{ | ||
return HealthCheckResult.Healthy("BuildingPart data is accessible."); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
return HealthCheckResult.Unhealthy($"BuildingPart data is not accessible! 💣 {ex.Message}"); | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
Phonebook.Source.PeopleSoft/src/Phonebook.Source.PeopleSoft/HealthChecks/HealthCheckFloor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Phonebook.Source.PeopleSoft.Models.Context; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Phonebook.Source.PeopleSoft.HealthChecks | ||
{ | ||
public class HealthCheckFloor: IHealthCheck | ||
{ | ||
public HealthCheckFloor(IServiceScopeFactory scopeFactory) | ||
{ | ||
ScopeFactory = scopeFactory; | ||
} | ||
public IServiceScopeFactory ScopeFactory { get; } | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using (var scope = ScopeFactory.CreateScope()) | ||
{ | ||
var result = await scope.ServiceProvider.GetService<ModelContext>().Floors.OrderBy(d => d.Id).FirstAsync(); | ||
if (string.IsNullOrWhiteSpace(result.Number.ToString())) | ||
{ | ||
return HealthCheckResult.Unhealthy("Floor data is not accessible! 💣"); | ||
} | ||
else | ||
{ | ||
return HealthCheckResult.Healthy("Floor data is accessible."); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
return HealthCheckResult.Unhealthy($"Floor data is not accessible! 💣 {ex.Message}"); | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ce.PeopleSoft/src/Phonebook.Source.PeopleSoft/HealthChecks/HealthCheckIdentityMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Phonebook.Source.PeopleSoft.HealthChecks | ||
{ | ||
public class HealthCheckIdentityMetadata : IHealthCheck | ||
{ | ||
public HealthCheckIdentityMetadata(IHttpClientFactory clientFactory, IConfiguration configuration) | ||
{ | ||
ClientFactory = clientFactory; | ||
Configuration = configuration; | ||
} | ||
|
||
public IHttpClientFactory ClientFactory { get; } | ||
public IConfiguration Configuration { get; } | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
if (Configuration.GetValue<bool>("useAnonymous")) | ||
{ | ||
return HealthCheckResult.Healthy("We don't use identity for anonymous."); | ||
} | ||
var metadataAddress = Configuration.GetValue<string>("WsFederationConfig:MetadataAddress"); | ||
if (string.IsNullOrWhiteSpace(metadataAddress)) | ||
{ | ||
return HealthCheckResult.Unhealthy("The metadata address for ws-federation is empty. Can't respond to authentification requests. 💣"); | ||
} | ||
var httpClient = ClientFactory.CreateClient(); | ||
var result = await httpClient.GetAsync(metadataAddress); | ||
if (result.IsSuccessStatusCode) | ||
{ | ||
return HealthCheckResult.Healthy("The identity provider is accessible."); | ||
} | ||
return HealthCheckResult.Unhealthy($"The identity provider return a bad status code. (Status code: {result.StatusCode} 💣"); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ook.Source.PeopleSoft/src/Phonebook.Source.PeopleSoft/HealthChecks/HealthCheckLocation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Phonebook.Source.PeopleSoft.Models.Context; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Phonebook.Source.PeopleSoft.HealthChecks | ||
{ | ||
public class HealthCheckLocation: IHealthCheck | ||
{ | ||
public HealthCheckLocation(IServiceScopeFactory scopeFactory) | ||
{ | ||
ScopeFactory = scopeFactory; | ||
} | ||
|
||
public IServiceScopeFactory ScopeFactory { get; } | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using (var scope = ScopeFactory.CreateScope()) | ||
{ | ||
var result = await scope.ServiceProvider.GetService<ModelContext>().Locations.OrderBy(d => d.Id).FirstAsync(); | ||
if (string.IsNullOrWhiteSpace(result.ShortName?.ToString())) | ||
{ | ||
return HealthCheckResult.Unhealthy("Location data is not accessible! 💣"); | ||
} | ||
else | ||
{ | ||
return HealthCheckResult.Healthy("Location data is accessible."); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
return HealthCheckResult.Unhealthy($"Location data is not accessible! 💣 {ex.Message}"); | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...book.Source.PeopleSoft/src/Phonebook.Source.PeopleSoft/HealthChecks/HealthCheckOrgUnit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Phonebook.Source.PeopleSoft.Models.Context; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Phonebook.Source.PeopleSoft.HealthChecks | ||
{ | ||
public class HealthCheckOrgUnit: IHealthCheck | ||
{ | ||
public HealthCheckOrgUnit(IServiceScopeFactory scopeFactory) | ||
{ | ||
ScopeFactory = scopeFactory; | ||
} | ||
public IServiceScopeFactory ScopeFactory { get; } | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using (var scope = ScopeFactory.CreateScope()) | ||
{ | ||
var result = await scope.ServiceProvider.GetService<ModelContext>().OrgUnits.OrderBy(d => d.Id).FirstAsync(); | ||
if (string.IsNullOrWhiteSpace(result.ShortName?.ToString())) | ||
{ | ||
return HealthCheckResult.Unhealthy("OrgUnit data is not accessible! 💣"); | ||
} | ||
else | ||
{ | ||
return HealthCheckResult.Healthy("OrgUnit data is accessible."); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
return HealthCheckResult.Unhealthy($"OrgUnit data is not accessible! 💣 {ex.Message}"); | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...k.Source.PeopleSoft/src/Phonebook.Source.PeopleSoft/HealthChecks/HealthCheckPersonData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Phonebook.Source.PeopleSoft.Models.Context; | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Phonebook.Source.PeopleSoft.HealthChecks | ||
{ | ||
public class HealthCheckPersonData : IHealthCheck | ||
{ | ||
public HealthCheckPersonData(IServiceScopeFactory scopeFactory) | ||
{ | ||
ScopeFactory = scopeFactory; | ||
} | ||
public IServiceScopeFactory ScopeFactory { get; } | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using (var scope = ScopeFactory.CreateScope()) | ||
{ | ||
var result = await scope.ServiceProvider.GetService<ModelContext>().Peoples.OrderBy(d => d.Id).FirstAsync(); | ||
if (string.IsNullOrWhiteSpace(result.ShortName)) | ||
{ | ||
return HealthCheckResult.Unhealthy("Person data is not accessible! 💣"); | ||
} | ||
else | ||
{ | ||
return HealthCheckResult.Healthy("Person data is accessible."); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
return HealthCheckResult.Unhealthy($"Person data is not accessible! 💣 {ex.Message}"); | ||
} | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
Phonebook.Source.PeopleSoft/src/Phonebook.Source.PeopleSoft/HealthChecks/HealthCheckRoom.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Phonebook.Source.PeopleSoft.Models.Context; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Phonebook.Source.PeopleSoft.HealthChecks | ||
{ | ||
public class HealthCheckRoom: IHealthCheck | ||
{ | ||
public HealthCheckRoom(IServiceScopeFactory scopeFactory) | ||
{ | ||
ScopeFactory = scopeFactory; | ||
} | ||
|
||
public IServiceScopeFactory ScopeFactory { get; } | ||
|
||
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using (var scope = ScopeFactory.CreateScope()) | ||
{ | ||
var result = await scope.ServiceProvider.GetService<ModelContext>().Rooms.OrderBy(d => d.Id).FirstAsync(); | ||
if (string.IsNullOrWhiteSpace(result.Number?.ToString())) | ||
{ | ||
return HealthCheckResult.Unhealthy("Room data is not accessible! 💣"); | ||
} | ||
else | ||
{ | ||
return HealthCheckResult.Healthy("Room data is accessible."); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
return HealthCheckResult.Unhealthy($"Room data is not accessible! 💣 {ex.Message}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.