public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "paths/{path}")] HttpRequest req, string path) { _loggerHelper.LogMethodEnter(_logger); var correlationId = _httpRequestHelper.GetOrCreateDssCorrelationId(req); if (string.IsNullOrWhiteSpace(path)) { _loggerHelper.LogInformationMessage(_logger, correlationId, Message.UnableToLocatePathInQueryString); return(new BadRequestResult()); } _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to get path {path}"); var pathModel = await _pathService.Get(path); if (pathModel == null) { _loggerHelper.LogInformationMessage(_logger, correlationId, Message.PathDoesNotExist); return(new NoContentResult()); } _loggerHelper.LogMethodExit(_logger); return(new OkObjectResult(pathModel)); }
public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "paths")] HttpRequest req) { _loggerHelper.LogMethodEnter(_logger); var correlationId = _httpRequestHelper.GetOrCreateDssCorrelationId(req); var body = await req.GetBodyAsync <PathModel>(); if (body == null || body.Value == null) { _loggerHelper.LogInformationMessage(_logger, correlationId, Message.PayloadMalformed); return(new BadRequestResult()); } if (!body.IsValid) { _loggerHelper.LogInformationMessage(_logger, correlationId, string.Concat(Message.ValidationFailed, " ", body.ValidationResults)); return(new BadRequestObjectResult(body.ValidationResults)); } try { _loggerHelper.LogInformationMessage(_logger, correlationId, "Attempting to register path"); var registeredPath = await _pathService.Register(body.Value); _loggerHelper.LogMethodExit(_logger); return(new CreatedResult(registeredPath.Path, registeredPath)); } catch (Exception ex) { _loggerHelper.LogException(_logger, correlationId, ex); return(new UnprocessableEntityObjectResult(ex)); } }
private async Task SendToStoredProc(ChangeFeedMessageModel documentModel, ILogger log) { _loggerHelper.LogMethodEnter(log); if (documentModel == null) { _loggerHelper.LogInformationMessage(log, CorrelationId, "document model is null"); return; } var resourceName = GetResourceName(documentModel); var commandText = "Change_Feed_Insert_Update_" + resourceName; const string parameterName = "@Json"; if (string.IsNullOrWhiteSpace(resourceName)) { _loggerHelper.LogInformationMessage(log, CorrelationId, "resource Name is null"); return; } try { _loggerHelper.LogInformationMessage(log, CorrelationId, "attempting to insert document into SQL"); await _sqlServerProvider.UpsertResource(documentModel.Document, log, commandText, parameterName); } catch (Exception ex) { _loggerHelper.LogException(log, Guid.NewGuid(), "Error when trying to insert & update change feed request into SQL", ex); throw; } _loggerHelper.LogMethodExit(log); }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "paths/{path}/regions/{pageRegion}")] HttpRequest req, ILogger log, string path, int pageRegion, [Inject] ILoggerHelper loggerHelper, [Inject] IHttpRequestHelper httpRequestHelper, [Inject] IHttpResponseMessageHelper httpResponseMessageHelper, [Inject] IJsonHelper jsonHelper, [Inject] IRegionService regionService ) { loggerHelper.LogMethodEnter(log); // validate the parameters are present var dssCorrelationId = httpRequestHelper.GetDssCorrelationId(req); if (string.IsNullOrEmpty(dssCorrelationId)) { log.LogInformation($"Unable to locate '{nameof(dssCorrelationId)}' in request header"); } if (!Guid.TryParse(dssCorrelationId, out var correlationGuid)) { log.LogInformation($"Unable to parse '{nameof(dssCorrelationId)}' to a Guid"); correlationGuid = Guid.NewGuid(); } if (string.IsNullOrEmpty(path)) { loggerHelper.LogInformationMessage(log, correlationGuid, $"Missing value in request for '{nameof(path)}'"); return(httpResponseMessageHelper.BadRequest()); } if (pageRegion == 0 || !Enum.IsDefined(typeof(PageRegions), pageRegion)) { loggerHelper.LogInformationMessage(log, correlationGuid, $"Missing/invalid value in request for '{nameof(pageRegion)}'"); return(httpResponseMessageHelper.BadRequest()); } PageRegions pageRegionValue = (PageRegions)pageRegion; loggerHelper.LogInformationMessage(log, correlationGuid, $"Attempting to get Region {pageRegionValue} for Path {path}"); var regionModel = await regionService.GetAsync(path, pageRegionValue); if (regionModel == null) { loggerHelper.LogInformationMessage(log, correlationGuid, $"Region does not exist for {pageRegionValue} for Path {path}"); return(httpResponseMessageHelper.NoContent()); } loggerHelper.LogMethodExit(log); return(regionModel != null ? httpResponseMessageHelper.Ok(jsonHelper.SerializeObjectAndRenameIdProperty(regionModel, "id", nameof(Models.Region.DocumentId))) : httpResponseMessageHelper.NoContent()); }
public async Task Run([CosmosDBTrigger( DatabaseName, CollectionName, ConnectionStringSetting = ConnectionString, LeaseCollectionName = LeaseCollectionName, LeaseCollectionPrefix = LeaseCollectionPrefix, CreateLeaseCollectionIfNotExists = true )] IReadOnlyList <Document> documents, ILogger log) { try { foreach (var document in documents) { var changeFeedMessageModel = new ChangeFeedMessageModel() { Document = document, IsCustomer = true }; _loggerHelper.LogInformationMessage(log, Guid.NewGuid(), string.Format("Attempting to send document id: {0} to service bus queue", document.Id)); await _serviceBusClient.SendChangeFeedMessageAsync(document, changeFeedMessageModel); } } catch (Exception ex) { _loggerHelper.LogException(log, Guid.NewGuid(), "Error when trying to send message to service bus queue", ex); } }
public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "paths")] HttpRequest req) { _loggerHelper.LogMethodEnter(_logger); var correlationId = _httpRequestHelper.GetOrCreateDssCorrelationId(req); _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to get all paths"); var result = await _pathService.GetAll(); _loggerHelper.LogMethodExit(_logger); return(new OkObjectResult(result)); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "paths/{path}")] HttpRequest req, string path) { _loggerHelper.LogMethodEnter(_logger); var correlationId = _httpRequestHelper.GetOrCreateDssCorrelationId(req); if (string.IsNullOrEmpty(path)) { _loggerHelper.LogInformationMessage(_logger, correlationId, Message.UnableToLocatePathInQueryString); return(new BadRequestResult()); } var body = await req.GetBodyAsync <PathModel>(); if (body == null || body.Value == null) { _loggerHelper.LogInformationMessage(_logger, correlationId, Message.PayloadMalformed); return(new BadRequestResult()); } if (!body.IsValid) { _loggerHelper.LogInformationMessage(_logger, correlationId, Message.ValidationFailed); return(new BadRequestObjectResult(body.ValidationResults)); } _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to get path for {path}"); var currentPath = await _pathService.Get(path); if (currentPath == null) { _loggerHelper.LogInformationMessage(_logger, correlationId, Message.PathDoesNotExist); return(new NoContentResult()); } try { _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to get update path for {path}"); await _pathService.Update(body.Value); _loggerHelper.LogMethodExit(_logger); return(new OkResult()); } catch (Exception ex) { _loggerHelper.LogException(_logger, correlationId, ex); return(new UnprocessableEntityObjectResult(ex)); } }
public async System.Threading.Tasks.Task RunAsync( [ServiceBusTrigger("%QueueName%", Connection = "ServiceBusConnectionString")] Message queueItem, ILogger log) { var correlationId = Guid.NewGuid(); if (queueItem == null) { loggerHelper.LogInformationMessage(log, correlationId, "Brokered Message cannot be null"); return; } try { _changeFeedQueueProcessorService.CorrelationId = correlationId; await _changeFeedQueueProcessorService.SendToAzureSql(queueItem, log); } catch (Exception ex) { loggerHelper.LogException(log, correlationId, "Unable to send document to sql", ex); throw; } }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "paths/{path}/regions/{pageRegion}")] HttpRequest req, ILogger log, string path, int pageRegion, [Inject] ILoggerHelper loggerHelper, [Inject] IHttpRequestHelper httpRequestHelper, [Inject] IHttpResponseMessageHelper httpResponseMessageHelper, [Inject] IJsonHelper jsonHelper, [Inject] IRegionService regionService ) { loggerHelper.LogMethodEnter(log); // validate the parameters are present var correlationId = httpRequestHelper.GetOrCreateDssCorrelationId(req); if (string.IsNullOrEmpty(path)) { loggerHelper.LogInformationMessage(log, correlationId, $"Missing value in request for '{nameof(path)}'"); return(httpResponseMessageHelper.BadRequest()); } if (pageRegion == 0 || !Enum.IsDefined(typeof(PageRegions), pageRegion)) { loggerHelper.LogInformationMessage(log, correlationId, $"Missing/invalid value in request for '{nameof(pageRegion)}'"); return(httpResponseMessageHelper.BadRequest()); } PageRegions pageRegionValue = (PageRegions)pageRegion; JsonPatchDocument <Region> regionPatch; try { regionPatch = await httpRequestHelper.GetResourceFromRequest <JsonPatchDocument <Region> >(req); if (regionPatch == null) { loggerHelper.LogException(log, correlationId, "Request body is empty", null); return(httpResponseMessageHelper.BadRequest()); } } catch (Exception ex) { loggerHelper.LogException(log, correlationId, ex); return(httpResponseMessageHelper.BadRequest()); } loggerHelper.LogInformationMessage(log, correlationId, $"Attempting to get Region {pageRegionValue} for Path {path}"); var currentRegion = await regionService.GetAsync(path, pageRegionValue); if (currentRegion == null) { loggerHelper.LogInformationMessage(log, correlationId, $"Region does not exist for {pageRegionValue} for Path {path}"); return(httpResponseMessageHelper.NoContent()); } try { loggerHelper.LogInformationMessage(log, correlationId, $"Attempting to apply patch to {path} region {pageRegionValue}"); regionPatch?.ApplyTo(currentRegion); var validationResults = currentRegion.Validate(new ValidationContext(currentRegion)); if (validationResults.Any()) { loggerHelper.LogInformationMessage(log, correlationId, "Validation Failed"); return(httpResponseMessageHelper.UnprocessableEntity(validationResults.ToList())); } } catch (Exception ex) { loggerHelper.LogException(log, correlationId, ex); return(httpResponseMessageHelper.BadRequest()); } try { loggerHelper.LogInformationMessage(log, correlationId, $"Attempting to update path {path}"); var patchedRegion = await regionService.ReplaceAsync(currentRegion); loggerHelper.LogMethodExit(log); return(httpResponseMessageHelper.Ok(jsonHelper.SerializeObjectAndRenameIdProperty(patchedRegion, "id", nameof(Models.Region.DocumentId)))); } catch (Exception ex) { loggerHelper.LogException(log, correlationId, ex); return(httpResponseMessageHelper.UnprocessableEntity(new JsonException(ex.Message, ex))); } }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "paths/{path}/regions")] HttpRequest req, ILogger log, string path, [Inject] ILoggerHelper loggerHelper, [Inject] IHttpRequestHelper httpRequestHelper, [Inject] IHttpResponseMessageHelper httpResponseMessageHelper, [Inject] IJsonHelper jsonHelper, [Inject] IRegionService regionService ) { loggerHelper.LogMethodEnter(log); // validate the parameters are present var dssCorrelationId = httpRequestHelper.GetDssCorrelationId(req); if (string.IsNullOrEmpty(dssCorrelationId)) { log.LogInformation($"Unable to locate '{nameof(dssCorrelationId)}' in request header"); } if (!Guid.TryParse(dssCorrelationId, out var correlationGuid)) { log.LogInformation($"Unable to parse '{nameof(dssCorrelationId)}' to a Guid"); correlationGuid = Guid.NewGuid(); } if (string.IsNullOrEmpty(path)) { loggerHelper.LogInformationMessage(log, correlationGuid, $"Missing value in request for '{nameof(path)}'"); return(httpResponseMessageHelper.BadRequest()); } var pathRegex = new Regex(@"^[A-Za-z0-9.,-_]*$"); if (path.Length > 100 || !pathRegex.IsMatch(path)) { loggerHelper.LogInformationMessage(log, correlationGuid, $"Invalid value in request for '{nameof(path)}'"); return(httpResponseMessageHelper.BadRequest()); } Models.Region regionRequest; try { loggerHelper.LogInformationMessage(log, correlationGuid, "Attempt to get resource from body of the request"); regionRequest = await httpRequestHelper.GetResourceFromRequest <Models.Region>(req); if (regionRequest == null) { loggerHelper.LogInformationMessage(log, correlationGuid, "Missing body in req"); return(httpResponseMessageHelper.UnprocessableEntity()); } } catch (JsonException ex) { loggerHelper.LogError(log, correlationGuid, "Unable to retrieve body from req", ex); return(httpResponseMessageHelper.UnprocessableEntity(ex)); } if (path != regionRequest.Path) { loggerHelper.LogInformationMessage(log, correlationGuid, $"Request value for '{nameof(regionRequest.Path)}' does not match resource path value"); return(httpResponseMessageHelper.BadRequest()); } if (string.IsNullOrEmpty(regionRequest.RegionEndpoint)) { loggerHelper.LogInformationMessage(log, correlationGuid, $"Missing value in request for '{nameof(regionRequest.RegionEndpoint)}'"); return(httpResponseMessageHelper.BadRequest()); } const string PlaceMarkerStub = "{0}"; string regionEndpoint = regionRequest.RegionEndpoint; if (regionEndpoint.Contains(PlaceMarkerStub)) { // this is allowable, so replace with a valid string to permit the Uri.IsWellFormedUriString to check the resulting string regionEndpoint = regionEndpoint.Replace(PlaceMarkerStub, "valid"); } if (!Uri.IsWellFormedUriString(regionEndpoint, UriKind.Absolute)) { loggerHelper.LogInformationMessage(log, correlationGuid, $"Request value for '{nameof(regionRequest.RegionEndpoint)}' is not a valid absolute Uri"); return(httpResponseMessageHelper.BadRequest()); } if (regionRequest.PageRegion == PageRegions.None || !Enum.IsDefined(typeof(PageRegions), regionRequest.PageRegion)) { loggerHelper.LogInformationMessage(log, correlationGuid, $"Missing/invalid value in request for '{nameof(regionRequest.PageRegion)}'"); return(httpResponseMessageHelper.BadRequest()); } if (!string.IsNullOrEmpty(regionRequest.OfflineHtml)) { var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(regionRequest.OfflineHtml); if (htmlDoc.ParseErrors.Any()) { loggerHelper.LogInformationMessage(log, correlationGuid, $"Request value for '{nameof(regionRequest.OfflineHtml)}' contains malformed HTML"); return(httpResponseMessageHelper.BadRequest()); } } loggerHelper.LogInformationMessage(log, correlationGuid, string.Format("Attempting to create region {0}", regionRequest.DocumentId)); var createdRegion = await regionService.CreateAsync(regionRequest); loggerHelper.LogMethodExit(log); return(createdRegion != null ? httpResponseMessageHelper.Ok(jsonHelper.SerializeObjectAndRenameIdProperty(createdRegion, "id", nameof(Models.Region.DocumentId))) : httpResponseMessageHelper.NoContent()); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "paths/{path}")] HttpRequest req, string path) { _loggerHelper.LogMethodEnter(_logger); var correlationId = _httpRequestHelper.GetOrCreateDssCorrelationId(req); if (string.IsNullOrWhiteSpace(path)) { _loggerHelper.LogInformationMessage(_logger, correlationId, Message.UnableToLocatePathInQueryString); return(new BadRequestResult()); } JsonPatchDocument <PathModel> pathPatch = null; try { var requestBody = await req.ReadAsStringAsync(); pathPatch = JsonConvert.DeserializeObject <JsonPatchDocument <PathModel> >(requestBody); } catch (Exception ex) { _loggerHelper.LogException(_logger, correlationId, ex); return(new BadRequestResult()); } _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to get path {path}'"); var currentPath = await _pathService.Get(path); if (currentPath == null) { _loggerHelper.LogInformationMessage(_logger, correlationId, Message.PathDoesNotExist); return(new NoContentResult()); } try { _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to apply patch to {path}"); pathPatch.ApplyTo(currentPath); var validationResults = currentPath.Validate(new ValidationContext(currentPath)); if (validationResults.Any()) { _loggerHelper.LogInformationMessage(_logger, correlationId, Message.ValidationFailed); return(new BadRequestObjectResult(validationResults)); } } catch (Exception ex) { _loggerHelper.LogException(_logger, correlationId, ex); return(new BadRequestResult()); } try { _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to update path {path}"); await _pathService.Update(currentPath); _loggerHelper.LogMethodExit(_logger); return(new OkObjectResult(currentPath)); } catch (Exception ex) { _loggerHelper.LogException(_logger, correlationId, ex); return(new UnprocessableEntityObjectResult(ex)); } }