Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,16 @@ public async Task SetDependencyMappingsAsync(string serviceId, IEnumerable<Depen
if (string.IsNullOrWhiteSpace(mapping.DependencyName)) throw new ArgumentException("A dependency name is required.", nameof(mappings));
if (string.IsNullOrWhiteSpace(mapping.TargetServiceId)) throw new ArgumentException("A target service ID is required.", nameof(mappings));

var target = await GetServiceAsync(mapping.TargetServiceId, cancellationToken);
ServiceIdentityPayload target;
try
{
target = await GetServiceAsync(mapping.TargetServiceId, cancellationToken);
}
catch (HttpRequestException exception) when (exception.StatusCode == System.Net.HttpStatusCode.NotFound && !cancellationToken.IsCancellationRequested)
{
logger?.LogWarning(exception, "Skipping dependency mapping because target service '{TargetServiceId}' is not registered.", mapping.TargetServiceId);
continue;
}
var payload = new DependencyMappingPayload(mapping.DependencyName, target.Id);
var current = existing.FirstOrDefault(link => string.Equals(link.DependencyName.Trim(), mapping.DependencyName.Trim(), StringComparison.OrdinalIgnoreCase));
using var response = current is null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ await client.SetDependencyMappingsAsync("payments-api", new[]
Assert.EndsWith($"/api/services/{PaymentsApiServiceId}/dependency-links/{linkId}", handler.Requests[2].RequestUri!.ToString());
}

[Fact]
public async Task SetDependencyMappingsAsync_skips_dependencies_that_are_not_registered()
{
var targetId = Guid.Parse("55555555-5555-5555-5555-555555555555");
var handler = new QueueHandler(
JsonResponse(HttpStatusCode.OK, Array.Empty<object>()),
JsonResponse(HttpStatusCode.NotFound, new { message = "service not found" }),
JsonResponse(HttpStatusCode.OK, new { id = targetId, serviceId = SecurityServiceId, name = SecurityServiceName }),
JsonResponse(HttpStatusCode.Created, new { }));
var logger = new RecordingLogger<HealthMonitoringRegistrationClient>();
var client = new HealthMonitoringRegistrationClient(
new HttpClient(handler) { BaseAddress = new Uri(MonitoringBaseUrl) },
logger);

await client.SetDependencyMappingsAsync(PaymentsApiServiceId, new[]
{
new DependencyMappingOptions("Missing Service", "missing-service"),
new DependencyMappingOptions(SecurityServiceName, SecurityServiceId)
});

Assert.Equal(4, handler.Requests.Count);
Assert.Equal($"{MonitoringBaseUrl}/api/services/{SecurityServiceId}", handler.Requests[2].RequestUri!.ToString());
Assert.Equal(HttpMethod.Post, handler.Requests[3].Method);
Assert.Contains(logger.Messages, message => message.Contains("missing-service", StringComparison.OrdinalIgnoreCase));
}

private static HealthMonitoringRegistrationClient CreateClient(HttpMessageHandler handler) =>
new(new HttpClient(handler) { BaseAddress = new Uri(MonitoringBaseUrl) });

Expand Down
Loading