public IEnumerable <GatewayAutomationResult> ValidateAsync(PublishFileModel publishFileModel) { if (publishFileModel != null && publishFileModel.Products.Any()) { var duplicateProducts = publishFileModel.Products.GroupBy(x => x.Name).Any(x => x.Count() > 1); if (duplicateProducts) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"Duplicate product names exist" }); } foreach (var product in publishFileModel.Products) { var productPrefix = $"Product: {product.Name}"; if (string.IsNullOrEmpty(product.Name)) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"[{productPrefix}] {nameof(product.Name)} does not exist" }); } this.ValidateFile(FileType.Logo, product.Logo, product.Name, nameof(product.Logo)); this.ValidateFile(FileType.Document, product.Documentation, product.Name, nameof(product.Documentation)); this.ValidatePolicies(product, productPrefix); } } return(this.ValidationResults); }
public IEnumerable <GatewayAutomationResult> ValidateAsync(PublishFileModel publishFileModel) { if (publishFileModel != null && publishFileModel.Apis.Any()) { var duplicateApis = publishFileModel.Apis.GroupBy(x => x.Name).Any(x => x.Count() > 1); if (duplicateApis) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"Duplicate api names exist" }); } using var client = this.gatewayClientFactory.CreateClient(); var products = client.GetAllProducts(this.options.SubscriptionId, this.options.ProviderId); var nameofproducts = products.Select(x => x.Name).ToList(); var consolidatedProductNames = nameofproducts.Union(publishFileModel.Products.Select(y => y.Name).ToList()); foreach (var api in publishFileModel.Apis) { var apiPrefix = $"API: {api.Name}, {api.Path}"; if (api.ApiVersions == null) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"Version shouldn't be null" }); continue; } if (api.ApiVersions .Where(v => v.IsCurrent.HasValue) .Count(v => v.IsCurrent == true) != 1) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"[{apiPrefix}] Only one Version must be set as current" }); } foreach (var version in api.ApiVersions) { foreach (var product in version.ProductNames) { if (!consolidatedProductNames.Contains(product)) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"Product {product} doesn't exist in DB or YAML" }); } } } var duplicateVersions = api.ApiVersions.GroupBy(x => x.VersionName).Any(x => x.Count() > 1); if (duplicateVersions) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"[{apiPrefix}] Duplicate version names exist" }); } if (string.IsNullOrEmpty(api.Name)) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"[{apiPrefix}] {nameof(api.Name)} does not exist" }); } if (string.IsNullOrEmpty(api.Path)) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"[{apiPrefix}] {nameof(api.Path)} does not exist" }); } foreach (var version in api.ApiVersions) { var apiVersionPrefix = $"API: {api.Name}, {api.Path}/{version.VersionName}"; if (string.IsNullOrEmpty(version.VersionName)) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"[{apiPrefix}] {nameof(version.VersionName)} does not exist" }); } if (string.IsNullOrEmpty(version.BackendLocation) || !Uri.IsWellFormedUriString(version.BackendLocation, UriKind.Absolute)) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"[{apiVersionPrefix}] {nameof(version.BackendLocation)} does not exist or not valid uri format" }); } if (!version.IsCurrent.HasValue) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"[{apiVersionPrefix}] {nameof(version.IsCurrent)} is not specified" }); } this.ValidateFile(FileType.Logo, version.ApiLogoFile, apiVersionPrefix, nameof(version.ApiLogoFile)); this.ValidateFile(FileType.Document, version.ApiDocumentation, apiVersionPrefix, nameof(version.ApiDocumentation)); this.ValidateFile(FileType.OpenApiSpec, version.OpenApiSpecFile, apiVersionPrefix, nameof(version.OpenApiSpecFile)); if (version.Revisions != null) { foreach (var revision in version.Revisions) { var apiRevisionPrefix = $"API Revision: {api.Name}, {api.Path}/{version.VersionName}"; if (string.IsNullOrEmpty(revision.RevisionDescription)) { this.ValidationResults.Add(new GatewayAutomationResult { IsError = true, ResultCode = ResultCode.ValidationFailed, Message = $"[{apiRevisionPrefix}] {nameof(revision.RevisionDescription)} not exist" }); } this.ValidateFile(FileType.OpenApiSpec, revision.OpenApiSpecFile, $"{api.Name} - {version.VersionName} - {revision.RevisionDescription}", nameof(revision.OpenApiSpecFile)); } } this.ValidatePolicies(version, apiVersionPrefix); } } } return(this.ValidationResults); }
private static async Task <ValidatePublishingRequest> GetValidatePublishingRequest(string folderPath, Guid providerId, PublishFileModel input) { var apis = new List <ApiValidationModel>(); foreach (var api in input.Apis) { foreach (var apiVersion in api.ApiVersions) { var fs = new FileStream(Path.Combine(folderPath, apiVersion.OpenApiSpecFile), FileMode.Open, FileAccess.Read); var revisions = apiVersion.Revisions?.Select(r => { var fsRev = new FileStream(Path.Combine(folderPath, r.OpenApiSpecFile), FileMode.Open, FileAccess.Read); return(new ApiRevisionValidationModel(fsRev, r.RevisionDescription)); }); apis.Add(new ApiValidationModel( api.Name, api.Path, apiVersion.VersionName, fs, apiVersion.ProductNames, revisions, await GetPoliciesValidationModel(folderPath, apiVersion.CustomPolicies, apiVersion.RateLimitPolicy).ConfigureAwait(false))); } } var products = await Task.WhenAll(input.Products.Select(async p => new ProductValidationModel( p.Name, await GetPoliciesValidationModel(folderPath, p.CustomPolicies, p.RateLimitPolicy).ConfigureAwait(false)))).ConfigureAwait(false); return(new ValidatePublishingRequest(providerId, apis, products)); }
private static IEnumerable <GatewayAutomationResult> PreValidateEntities(string folderPath, PublishFileModel publishFileModel, HttpClient httpClient, GatewayOptions options, LogicTokenProviderFactory logicTokenProvider) { var preValidations = new List <IPreValidation> { new ProductsPreValidation(folderPath), new ApisPreValidation(folderPath, httpClient, logicTokenProvider, options), }; var publishResults = new List <GatewayAutomationResult>(); foreach (var validation in preValidations) { var results = validation.ValidateAsync(publishFileModel); publishResults.AddRange(results); } return(publishResults); }