/** * Generate email payload for given alarm and email action. * Creates subject, recipients, and body based on action and alarm */ private string GeneratePayload(EmailAction emailAction, AsaAlarmApiModel alarm) { string emailTemplate = File.ReadAllText(this.servicesConfig.TemplateFolder + EMAIL_TEMPLATE_FILE_NAME); string alarmDate = DateTimeOffset.FromUnixTimeMilliseconds(alarm.DateCreated).ToString(DATE_FORMAT_STRING); emailTemplate = emailTemplate.Replace("${subject}", emailAction.GetSubject()); emailTemplate = emailTemplate.Replace( "${alarmDate}", DateTimeOffset.FromUnixTimeMilliseconds(alarm.DateCreated).ToString(DATE_FORMAT_STRING)); emailTemplate = emailTemplate.Replace("${ruleId}", alarm.RuleId); emailTemplate = emailTemplate.Replace("${ruleDescription}", alarm.RuleDescription); emailTemplate = emailTemplate.Replace("${ruleSeverity}", alarm.RuleSeverity); emailTemplate = emailTemplate.Replace("${deviceId}", alarm.DeviceId); emailTemplate = emailTemplate.Replace("${notes}", emailAction.GetNotes()); emailTemplate = emailTemplate.Replace("${alarmUrl}", this.GenerateRuleDetailUrl(alarm.RuleId)); EmailActionPayload payload = new EmailActionPayload { Recipients = emailAction.GetRecipients(), Subject = emailAction.GetSubject(), Body = emailTemplate }; return(JsonConvert.SerializeObject(payload)); }
/// <summary> /// Execute the given email action for the given alarm. /// Sends a post request to Logic App with alarm information /// </summary> public async Task Execute(IAction action, object metadata) { if (metadata.GetType() != typeof(AsaAlarmApiModel) || action.GetType() != typeof(EmailAction)) { string errorMessage = "Email action expects metadata to be alarm and action" + " to be EmailAction, will not send email"; this.logger.Error(errorMessage, () => { }); return; } try { AsaAlarmApiModel alarm = (AsaAlarmApiModel)metadata; EmailAction emailAction = (EmailAction)action; string payload = this.GeneratePayload(emailAction, alarm); HttpRequest httpRequest = new HttpRequest(this.servicesConfig.LogicAppEndpointUrl); httpRequest.SetContent(payload); IHttpResponse response = await this.httpClient.PostAsync(httpRequest); if (!response.IsSuccess) { this.logger.Error("Could not execute email action against logic app", () => { }); } } catch (JsonException e) { this.logger.Error("Could not create email payload to send to logic app,", () => new { e }); } catch (Exception e) { this.logger.Error("Could not execute email action against logic app", () => new { e }); } }
public async Task EmailAction_CausesPostToLogicApp() { // Arrange JArray emailArray = new JArray(new object[] { "*****@*****.**" }); Dictionary <string, object> actionParameters = new Dictionary <string, object> { { "Recipients", emailArray }, { "Notes", "Test Note" }, { "Subject", "Test Subject" } }; EmailAction testAction = new EmailAction(actionParameters); AsaAlarmApiModel alarm = new AsaAlarmApiModel { DateCreated = 1539035437937, DateModified = 1539035437937, DeviceId = "Test Device Id", MessageReceived = 1539035437937, RuleDescription = "Test Rule description", RuleId = "TestRuleId", RuleSeverity = "Warning", Actions = new List <IAction> { testAction } }; var response = new HttpResponse(HttpStatusCode.OK, "", null); this.httpClientMock.Setup(x => x.PostAsync(It.IsAny <IHttpRequest>())).ReturnsAsync(response); List <AsaAlarmApiModel> alarmList = new List <AsaAlarmApiModel> { alarm }; // Act await this.actionManager.ExecuteAlarmActions(alarmList); // Assert this.httpClientMock.Verify(x => x.PostAsync(It.IsAny <IHttpRequest>())); }