Skip to content

Commit

Permalink
feature(backend): Add Health endpoint to the PeopleSoft API (#601)
Browse files Browse the repository at this point in the history
* fix spelling of useAnonymous

* add health checks

* change startup path for VS
  • Loading branch information
paule96 authored Apr 29, 2020
1 parent 97866c2 commit f32792e
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 3 deletions.
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}");
}
}
}
}
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}");
}
}
}
}
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}");
}
}
}
}
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} 💣");
}
}
}
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}");
}
}
}
}
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}");
}
}
}
}
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}");
}
}
}
}
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}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"Phonebook.Source.PeopleSoft": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"launchUrl": "health",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
Expand Down
Loading

0 comments on commit f32792e

Please sign in to comment.