|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | +using System.Linq; |
| 4 | +using System.Text.Json; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | +using Microsoft.AspNetCore.Mvc; |
| 9 | +using Microsoft.Azure.Functions.Worker; |
| 10 | +using SqlBulkSyncFunction.Models.Schema.Export; |
| 11 | + |
| 12 | +namespace SqlBulkSyncFunction.Functions; |
| 13 | + |
| 14 | +#nullable enable |
| 15 | + |
| 16 | +public partial class GetSyncJobConfig |
| 17 | +{ |
| 18 | + private static readonly JsonSerializerOptions ExportJsonOptions = new() |
| 19 | + { |
| 20 | + PropertyNameCaseInsensitive = true, |
| 21 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase |
| 22 | + }; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Accepts a schema tracking data export request, persists metadata to blob and table storage, and enqueues processing. |
| 26 | + /// </summary> |
| 27 | + [Function(nameof(GetSyncJobConfig) + nameof(PostSchemaTrackingExport))] |
| 28 | + public async Task<IActionResult> PostSchemaTrackingExport( |
| 29 | + [HttpTrigger( |
| 30 | + AuthorizationLevel.Function, |
| 31 | + "post", |
| 32 | + Route = "config/{area}/{id}/schema/tracking/{tableId}/export" |
| 33 | + )] |
| 34 | + HttpRequest req, |
| 35 | + string area, |
| 36 | + string id, |
| 37 | + string tableId, |
| 38 | + CancellationToken cancellationToken |
| 39 | + ) |
| 40 | + { |
| 41 | + ArgumentNullException.ThrowIfNull(req); |
| 42 | + |
| 43 | + SchemaTrackingExportRequestBody? body; |
| 44 | + try |
| 45 | + { |
| 46 | + body = await JsonSerializer |
| 47 | + .DeserializeAsync<SchemaTrackingExportRequestBody>(req.Body, ExportJsonOptions, cancellationToken) |
| 48 | + .ConfigureAwait(false); |
| 49 | + } |
| 50 | + catch (JsonException) |
| 51 | + { |
| 52 | + return new BadRequestObjectResult("Invalid JSON body."); |
| 53 | + } |
| 54 | + |
| 55 | + if (body == null) |
| 56 | + { |
| 57 | + return new BadRequestObjectResult("Body is required."); |
| 58 | + } |
| 59 | + |
| 60 | + var result = await schemaTrackingExportService |
| 61 | + .TryCreateExportJobAsync(area, id, tableId, body, cancellationToken) |
| 62 | + .ConfigureAwait(false); |
| 63 | + |
| 64 | + if (result.Code == ExportJobCreateResultCode.ValidationFailed) |
| 65 | + { |
| 66 | + return new BadRequestObjectResult("author, referenceId, and purpose must be non-empty strings."); |
| 67 | + } |
| 68 | + |
| 69 | + if (result.Code == ExportJobCreateResultCode.NotFound || result.Job == null) |
| 70 | + { |
| 71 | + return new NotFoundResult(); |
| 72 | + } |
| 73 | + |
| 74 | + var location = BuildExportStatusLocation(req, area, id, tableId, result.Job.CorrelationId); |
| 75 | + return new AcceptedResult(location: location, value: result.Job); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Returns detailed status for a single export job when <paramref name="correlationId"/> is present in the path, |
| 80 | + /// or lists export jobs for the table when the URL ends at <c>.../export/status</c> (catch-all binds empty). |
| 81 | + /// </summary> |
| 82 | + /// <remarks> |
| 83 | + /// A separate route without <c>{*correlationId}</c> would never win in the host: the catch-all matches the same URL with an empty remainder, |
| 84 | + /// and <see cref="SchemaTrackingExportService.TryGetExportStatusAsync"/> returns null for an empty id, producing 404. List behavior is therefore handled here. |
| 85 | + /// </remarks> |
| 86 | + [Function(nameof(GetSyncJobConfig) + nameof(GetSchemaTrackingExportStatus))] |
| 87 | + public async Task<IActionResult> GetSchemaTrackingExportStatus( |
| 88 | + [HttpTrigger( |
| 89 | + AuthorizationLevel.Function, |
| 90 | + "get", |
| 91 | + Route = "config/{area}/{id}/schema/tracking/{tableId}/export/status/{*correlationId}" |
| 92 | + )] |
| 93 | + HttpRequest req, |
| 94 | + string area, |
| 95 | + string id, |
| 96 | + string tableId, |
| 97 | + string correlationId, |
| 98 | + CancellationToken cancellationToken |
| 99 | + ) |
| 100 | + { |
| 101 | + ArgumentNullException.ThrowIfNull(req); |
| 102 | + |
| 103 | + if (string.IsNullOrWhiteSpace(correlationId)) |
| 104 | + { |
| 105 | + if (string.IsNullOrWhiteSpace(area) || |
| 106 | + string.IsNullOrWhiteSpace(id) || |
| 107 | + string.IsNullOrWhiteSpace(tableId) || |
| 108 | + syncJobsConfig?.Value?.Jobs?.TryGetValue(id, out var jobConfig) != true || |
| 109 | + jobConfig == null || |
| 110 | + !StringComparer.OrdinalIgnoreCase.Equals(area, jobConfig.Area) || |
| 111 | + jobConfig.Tables == null || |
| 112 | + !jobConfig.Tables.TryGetValue(tableId, out _)) |
| 113 | + { |
| 114 | + return new NotFoundResult(); |
| 115 | + } |
| 116 | + |
| 117 | + var items = await schemaTrackingExportService |
| 118 | + .ListExportJobsAsync(area, id, tableId, cancellationToken) |
| 119 | + .ConfigureAwait(false); |
| 120 | + |
| 121 | + return new OkObjectResult(items); |
| 122 | + } |
| 123 | + |
| 124 | + var status = await schemaTrackingExportService |
| 125 | + .TryGetExportStatusAsync(area, id, tableId, correlationId, cancellationToken) |
| 126 | + .ConfigureAwait(false); |
| 127 | + |
| 128 | + if (status == null) |
| 129 | + { |
| 130 | + return new NotFoundResult(); |
| 131 | + } |
| 132 | + |
| 133 | + return new OkObjectResult(status); |
| 134 | + } |
| 135 | + |
| 136 | + private static string BuildExportStatusLocation( |
| 137 | + HttpRequest req, |
| 138 | + string area, |
| 139 | + string jobId, |
| 140 | + string tableId, |
| 141 | + string correlationId |
| 142 | + ) |
| 143 | + { |
| 144 | + var encodedCorrelation = string.Join( |
| 145 | + "/", |
| 146 | + correlationId.Split('/', StringSplitOptions.RemoveEmptyEntries).Select(Uri.EscapeDataString) |
| 147 | + ); |
| 148 | + var pathBase = req.PathBase.Value?.TrimEnd('/') ?? string.Empty; |
| 149 | + var apiRoot = string.IsNullOrEmpty(pathBase) ? "/api" : pathBase; |
| 150 | + var path = string.Format( |
| 151 | + CultureInfo.InvariantCulture, |
| 152 | + "{0}/config/{1}/{2}/schema/tracking/{3}/export/status/{4}", |
| 153 | + apiRoot, |
| 154 | + Uri.EscapeDataString(area), |
| 155 | + Uri.EscapeDataString(jobId), |
| 156 | + Uri.EscapeDataString(tableId), |
| 157 | + encodedCorrelation |
| 158 | + ); |
| 159 | + return string.Format(CultureInfo.InvariantCulture, "{0}://{1}{2}", req.Scheme, req.Host.Value, path); |
| 160 | + } |
| 161 | +} |
0 commit comments