From 341023decd35de76b482d4c2f66f23b463406e9f Mon Sep 17 00:00:00 2001 From: espent1004 Date: Sun, 2 Feb 2020 19:59:00 +0100 Subject: [PATCH] Adding the customisationUrl to the tenantManager rather than hardcoding into the eventBus implementation. --- .../EventBusRabbitMQ/EventBusRabbitMQ.cs | 25 ++++++++----------- .../Controllers/CustomisationsController.cs | 6 ++--- .../TenantManager/Database/DbInitializer.cs | 4 +-- .../TenantManager/Models/Customisation.cs | 1 + 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs index d44abb9bd..b60a353c6 100644 --- a/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs +++ b/src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs @@ -274,7 +274,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ return channel; } - private async void SendEventToTenant(String content, String id, String eventName) + private async void SendEventToTenant(String content, String id, String eventName, String url) { var temp = new SavedEvent(); temp.Content = content; @@ -285,9 +285,8 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ { try { - //TODO replace URL with response from tenantmanager var response = await client.PostAsync( - tenantACustomisationUrl + "api/SavedEvents", + url, new StringContent(myJson, Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode(); _logger.LogInformation("----- Event sent to tenant{@id} -----", id); @@ -300,12 +299,12 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ } } - private async Task IsEventCustomised(String eventName, int tenantId) + private async Task GetCustomisation(String eventName, int tenantId) { CustomisationInfo customisationInfo = new CustomisationInfo(); customisationInfo.EventName = eventName; customisationInfo.TenantId = tenantId; - Boolean isCustomised = false; + String customisationUrl = null; var builder = new UriBuilder(tenantManagerUrl + "api/Customisations/IsCustomised"); builder.Port = -1; @@ -322,8 +321,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ var response = await client.GetAsync( url); response.EnsureSuccessStatusCode(); - isCustomised = - JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result); + customisationUrl = response.Content.ReadAsStringAsync().Result; } catch (Exception e) { @@ -331,7 +329,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ } } - return isCustomised; + return customisationUrl; } private async Task ProcessEvent(string eventName, string message) @@ -364,14 +362,13 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ var eventType = _subsManager.GetEventTypeByName(eventName); var integrationEvent = JsonConvert.DeserializeObject(message, eventType); IntegrationEvent evt = (IntegrationEvent) integrationEvent; - if (IsEventCustomised(eventName, evt.TenantId).Result) //TODO fix tenantId part of request + var customisationUrl = GetCustomisation(eventName, evt.TenantId).Result; + if (evt.CheckForCustomisation && !String.IsNullOrEmpty(customisationUrl)) { //Checking if event should be sent to tenant, or handled normally - if (evt.CheckForCustomisation && evt.TenantId == 1)//TODO use tenantId to choose the correct endpoint to send the event to - { - SendEventToTenant(message, evt.Id.ToString(), eventName); - break; - } + SendEventToTenant(message, evt.Id.ToString(), eventName, + customisationUrl); + break; } var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType); diff --git a/src/Services/TenantManager/TenantManager/Controllers/CustomisationsController.cs b/src/Services/TenantManager/TenantManager/Controllers/CustomisationsController.cs index 23e230b70..ce3f617ff 100644 --- a/src/Services/TenantManager/TenantManager/Controllers/CustomisationsController.cs +++ b/src/Services/TenantManager/TenantManager/Controllers/CustomisationsController.cs @@ -44,16 +44,16 @@ namespace TenantManager.Controllers // GET: api/Customisations/5 [HttpGet("isCustomised")] - public async Task> IsCustomised(String eventName, int tenantId) + public async Task> IsCustomised(String eventName, int tenantId) { var customisation = await _context.Customisation.Include(c => c.Method).Include(c => c.Tenant).Where(c => c.Method.MethodName.Equals(eventName) && c.TenantId == tenantId).FirstOrDefaultAsync(); if (customisation == null) { - return false; + return NotFound(); } - return true; + return customisation.CustomisationUrl; } // PUT: api/Customisations/5 diff --git a/src/Services/TenantManager/TenantManager/Database/DbInitializer.cs b/src/Services/TenantManager/TenantManager/Database/DbInitializer.cs index f91994866..1345882b5 100644 --- a/src/Services/TenantManager/TenantManager/Database/DbInitializer.cs +++ b/src/Services/TenantManager/TenantManager/Database/DbInitializer.cs @@ -33,8 +33,8 @@ namespace TenantManager.Database var customisations = new[] { - new Customisation {Tenant=tenant1, Method=method1 }, - new Customisation {Tenant=tenant1, Method=method2} + new Customisation {Tenant=tenant1, Method=method1, CustomisationUrl = @"http://tenantacustomisation/api/savedevents"}, + new Customisation {Tenant=tenant1, Method=method2, CustomisationUrl = @"http://tenantacustomisation/api/savedevents"} }; foreach(Customisation c in customisations) diff --git a/src/Services/TenantManager/TenantManager/Models/Customisation.cs b/src/Services/TenantManager/TenantManager/Models/Customisation.cs index 6f0583f77..ac03942c9 100644 --- a/src/Services/TenantManager/TenantManager/Models/Customisation.cs +++ b/src/Services/TenantManager/TenantManager/Models/Customisation.cs @@ -9,6 +9,7 @@ namespace TenantManager.Models public class Customisation { public int CustomisationId { get; set; } + public String CustomisationUrl { get; set; } //Foreign keys public int TenantId { get; set; }