Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 2583985

Browse files
authored
Fixes after all-apis-extractor run (#653)
* Fixes after all-apis-extraction * fix logger-extractor in right way * use Any for more convenient Linq usage Co-authored-by: Dmitrii Korolev <dmkorolev@microsoft.com>
1 parent aa944d4 commit 2583985

File tree

10 files changed

+116
-24
lines changed

10 files changed

+116
-24
lines changed

src/ArmTemplates/Commands/Executors/ExtractorExecutor.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class ExtractorExecutor
4646
readonly IProductApisExtractor productApisExtractor;
4747
readonly IProductExtractor productExtractor;
4848
readonly IPropertyExtractor propertyExtractor;
49-
readonly ITagApiExtractor apiTagExtractor;
49+
readonly ITagApiExtractor tagApiExtractor;
5050
readonly ITagExtractor tagExtractor;
5151
readonly IGroupExtractor groupExtractor;
5252
readonly IApiRevisionExtractor apiRevisionExtractor;
@@ -66,7 +66,7 @@ public ExtractorExecutor(
6666
IProductApisExtractor productApisExtractor,
6767
IProductExtractor productExtractor,
6868
IPropertyExtractor propertyExtractor,
69-
ITagApiExtractor apiTagExtractor,
69+
ITagApiExtractor tagApiExtractor,
7070
ITagExtractor tagExtractor,
7171
IGroupExtractor groupExtractor,
7272
IApiRevisionExtractor apiRevisionExtractor,
@@ -85,7 +85,7 @@ public ExtractorExecutor(
8585
this.productApisExtractor = productApisExtractor;
8686
this.propertyExtractor = propertyExtractor;
8787
this.productExtractor = productExtractor;
88-
this.apiTagExtractor = apiTagExtractor;
88+
this.tagApiExtractor = tagApiExtractor;
8989
this.tagExtractor = tagExtractor;
9090
this.groupExtractor = groupExtractor;
9191
this.apiRevisionExtractor = apiRevisionExtractor;
@@ -110,7 +110,7 @@ public static ExtractorExecutor BuildExtractorExecutor(
110110
IProductApisExtractor productApisExtractor = null,
111111
IProductExtractor productExtractor = null,
112112
IPropertyExtractor propertyExtractor = null,
113-
ITagApiExtractor apiTagExtractor = null,
113+
ITagApiExtractor tagApiExtractor = null,
114114
ITagExtractor tagExtractor = null,
115115
IGroupExtractor groupExtractor = null,
116116
IApiRevisionExtractor apiRevisionExtractor = null,
@@ -129,7 +129,7 @@ public static ExtractorExecutor BuildExtractorExecutor(
129129
productApisExtractor,
130130
productExtractor,
131131
propertyExtractor,
132-
apiTagExtractor,
132+
tagApiExtractor,
133133
tagExtractor,
134134
groupExtractor,
135135
apiRevisionExtractor,
@@ -413,30 +413,30 @@ await FileWriter.SaveAsJsonAsync(
413413
}
414414

415415
/// <summary>
416-
/// Generates api-tag templates in the desired folder
416+
/// Generates tag-api templates in the desired folder
417417
/// </summary>
418-
/// <param name="singleApiName">name of API to load api-tag from</param>
419-
/// <param name="multipleApiNames">multiple API names to load api-tag from</param>
418+
/// <param name="singleApiName">name of API to load tag-api from</param>
419+
/// <param name="multipleApiNames">multiple API names to load tag-api from</param>
420420
/// <param name="baseFilesGenerationDirectory">name of base folder where to save output files</param>
421-
/// <returns>generated api-tag template</returns>
422-
public async Task<Template<TagApiTemplateResources>> GenerateApiTagTemplateAsync(
421+
/// <returns>generated tag-api template</returns>
422+
public async Task<Template<TagApiTemplateResources>> GenerateTagApiTemplateAsync(
423423
string singleApiName,
424424
List<string> multipleApiNames,
425425
string baseFilesGenerationDirectory)
426426
{
427-
this.logger.LogInformation("Started generation of api-tag template...");
427+
this.logger.LogInformation("Started generation of tag-api template...");
428428

429-
var apiTagTemplate = await this.apiTagExtractor.GenerateApiTagsTemplateAsync(singleApiName, multipleApiNames, this.extractorParameters);
429+
var apiTagTemplate = await this.tagApiExtractor.GenerateApiTagsTemplateAsync(singleApiName, multipleApiNames, this.extractorParameters);
430430

431431
if (apiTagTemplate?.HasResources() == true)
432432
{
433433
await FileWriter.SaveAsJsonAsync(
434434
apiTagTemplate,
435435
directory: baseFilesGenerationDirectory,
436-
fileName: this.extractorParameters.FileNames.ApiTags);
436+
fileName: this.extractorParameters.FileNames.TagApi);
437437
}
438438

439-
this.logger.LogInformation("Finished generation of api-tag template...");
439+
this.logger.LogInformation("Finished generation of tag-api template...");
440440
return apiTagTemplate;
441441
}
442442

@@ -703,7 +703,7 @@ async Task GenerateTemplates(
703703
var apiVersionSetTemplate = await this.GenerateApiVersionSetTemplateAsync(singleApiName, baseFilesGenerationDirectory, apiTemplate.TypedResources.Apis);
704704
var authorizationServerTemplate = await this.GenerateAuthorizationServerTemplateAsync(singleApiName, baseFilesGenerationDirectory, apiTemplate.TypedResources.Apis);
705705
var tagTemplate = await this.GenerateTagTemplateAsync(singleApiName, apiTemplate.TypedResources, productTemplate.TypedResources, baseFilesGenerationDirectory);
706-
var apiTagTemplate = await this.GenerateApiTagTemplateAsync(singleApiName, multipleApiNames, baseFilesGenerationDirectory);
706+
var apiTagTemplate = await this.GenerateTagApiTemplateAsync(singleApiName, multipleApiNames, baseFilesGenerationDirectory);
707707
await this.GenerateGroupsTemplateAsync(baseFilesGenerationDirectory);
708708
await this.GenerateGatewayTemplateAsync(singleApiName, baseFilesGenerationDirectory);
709709

src/ArmTemplates/Common/Extensions/ParameterNamingHelper.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ static class ParameterNamingHelper
99

1010
public static string GenerateValidParameterName(string apiName, string prefix)
1111
{
12+
if (string.IsNullOrEmpty(apiName))
13+
{
14+
return string.Empty;
15+
}
16+
1217
var validApiName = ExcludeOtherFromLettersAndDigitsRegex.Replace(apiName, string.Empty);
1318

19+
if (string.IsNullOrEmpty(validApiName))
20+
{
21+
return string.Empty;
22+
}
23+
1424
if (char.IsDigit(validApiName.First()))
1525
{
1626
return prefix + validApiName;

src/ArmTemplates/Common/FileHandlers/FileNameGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static FileNames GenerateFileNames(string baseFileName)
2929
ProductAPIs = $@"{baseFileName}productAPIs.template.json",
3030
Gateway = $@"{baseFileName}gateways.template.json",
3131
GatewayApi = $@"{baseFileName}gateways-apis.template.json",
32-
ApiTags = $@"{baseFileName}apiTags.template.json",
32+
TagApi = $@"{baseFileName}apiTags.template.json",
3333
Parameters = $@"\{baseFileName}parameters.json",
3434
LinkedMaster = $@"\{baseFileName}master.template.json",
3535
Apis = "/Apis",

src/ArmTemplates/Common/FileHandlers/FileNames.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class FileNames
2222

2323
public string Groups { get; set; }
2424

25-
public string ApiTags { get; set; }
25+
public string TagApi { get; set; }
2626

2727
public string Gateway { get; set; }
2828

src/ArmTemplates/Extractor/EntityExtractors/APIVersionSetExtractor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public async Task<Template<ApiVersionSetTemplateResources>> GenerateApiVersionSe
4444
foreach (var apiVersionSet in apiVersionSets)
4545
{
4646
// only extract the product if this is a full extraction, or in the case of a single api, if it is found in products associated with the api
47-
if (string.IsNullOrEmpty(singleApiName) || apiTemplateResources.SingleOrDefault(api =>
47+
if (string.IsNullOrEmpty(singleApiName) || apiTemplateResources.Any(api =>
4848
api.Properties.ApiVersionSetId != null &&
49-
api.Properties.ApiVersionSetId.Contains(apiVersionSet.Name)) != null)
49+
api.Properties.ApiVersionSetId.Contains(apiVersionSet.Name)))
5050
{
5151
this.logger.LogDebug("Found '{0}' api-version-set", apiVersionSet.Name);
5252

src/ArmTemplates/Extractor/EntityExtractors/LoggerExtractor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Threading.Tasks;
33
using System.Collections.Generic;
44
using Newtonsoft.Json.Linq;
5-
using Newtonsoft.Json;
65
using System.Linq;
76
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Extensions;
87
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Logger;
@@ -11,8 +10,8 @@
1110
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Abstractions;
1211
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
1312
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Policy;
14-
using Microsoft.Azure.Management.ApiManagement.Models;
1513
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders.Abstractions;
14+
using Microsoft.Extensions.Logging;
1615

1716
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.EntityExtractors
1817
{
@@ -80,7 +79,7 @@ public async Task<Template> GenerateLoggerTemplateAsync(ExtractorParameters extr
8079
loggerResource.ApiVersion = GlobalConstants.ApiVersion;
8180
loggerResource.Scale = null;
8281

83-
if (singleApiName == null)
82+
if (string.IsNullOrEmpty(singleApiName))
8483
{
8584
// if the user is extracting all apis, extract all the loggers
8685
Console.WriteLine("'{0}' Logger found", loggerName);
@@ -98,6 +97,7 @@ public async Task<Template> GenerateLoggerTemplateAsync(ExtractorParameters extr
9897
isReferencedInPolicy = true;
9998
}
10099
}
100+
101101
string validApiName = ParameterNamingHelper.GenerateValidParameterName(singleApiName, ParameterPrefix.Api);
102102
if (extractorParameters.ParameterizeApiLoggerId && apiLoggerId.ContainsKey(validApiName))
103103
{

src/ArmTemplates/Extractor/EntityExtractors/MasterTemplateExtractor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public Template GenerateLinkedMasterTemplate(
181181
// apiTags
182182
if (apiTagsTemplate != null && apiTagsTemplate.Resources.Count() != 0)
183183
{
184-
string apiTagsUri = this.GenerateLinkedTemplateUri(extractorParameters.LinkedTemplatesUrlQueryString, extractorParameters.LinkedTemplatesSasToken, fileNames.ApiTags);
184+
string apiTagsUri = this.GenerateLinkedTemplateUri(extractorParameters.LinkedTemplatesUrlQueryString, extractorParameters.LinkedTemplatesSasToken, fileNames.TagApi);
185185
resources.Add(this.CreateLinkedMasterTemplateResource("apiTagsTemplate", apiTagsUri, apiTagDependsOn.ToArray()));
186186
}
187187
Console.WriteLine("Master template generated");

src/ArmTemplates/Extractor/EntityExtractors/ProductExtractor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public async Task<Template<ProductTemplateResources>> GenerateProductsTemplateAs
6565
productTemplateResource.ApiVersion = GlobalConstants.ApiVersion;
6666

6767
// only extract the product if this is a full extraction, or in the case of a single api, if it is found in products associated with the api
68-
if (singleApiName == null || productApiTemplateResources.SingleOrDefault(p => p.Name.Contains($"/{productOriginalName}/")) != null)
68+
if (singleApiName == null || productApiTemplateResources.Any(p => p.Name.Contains($"/{productOriginalName}/")))
6969
{
7070
this.logger.LogDebug("'{0}' product found", productOriginalName);
7171
productsTemplate.TypedResources.Products.Add(productTemplateResource);

src/ArmTemplates/Extractor/EntityExtractors/TagAPIExtractor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public TagApiExtractor(
3131
this.templateBuilder = templateBuilder;
3232

3333
this.tagClient = tagClient;
34+
this.apisClient = apisClient;
3435
}
3536

3637
public async Task<Template<TagApiTemplateResources>> GenerateApiTagsTemplateAsync(
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// --------------------------------------------------------------------------
2+
// <copyright file="TagApiExtractorTests.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// </copyright>
5+
// --------------------------------------------------------------------------
6+
7+
using System;
8+
using System.Collections.Generic;
9+
using System.IO;
10+
using System.Linq;
11+
using System.Text;
12+
using System.Threading.Tasks;
13+
using Castle.Core.Internal;
14+
using FluentAssertions;
15+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Commands.Executors;
16+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
17+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders;
18+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.EntityExtractors;
19+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
20+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Tests.Extractor.Abstractions;
21+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Tests.Moqs.ApiClients;
22+
using Moq;
23+
using Xunit;
24+
25+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Tests.Extractor.Scenarios
26+
{
27+
[Trait("Category", "Tag Api Extraction")]
28+
public class TagApiExtractorTests : ExtractorMockerWithOutputTestsBase
29+
{
30+
public TagApiExtractorTests() : base("tag-api-tests")
31+
{
32+
}
33+
34+
[Fact]
35+
public async Task GenerateTagApiTemplates_ProperlyLaysTheInformation()
36+
{
37+
// arrange
38+
var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GenerateTagApiTemplates_ProperlyLaysTheInformation));
39+
40+
var extractorConfig = this.GetMockedExtractorConsoleAppConfiguration(
41+
splitApis: false,
42+
apiVersionSetName: string.Empty,
43+
multipleApiNames: string.Empty,
44+
includeAllRevisions: false);
45+
var extractorParameters = new ExtractorParameters(extractorConfig);
46+
47+
var mockedTagClient = MockTagClient.GetMockedApiClientWithDefaultValues();
48+
var mockedApiClient = MockApisClient.GetMockedApiClientWithDefaultValues();
49+
var tagApiExtractor = new TagApiExtractor(
50+
this.GetTestLogger<TagApiExtractor>(),
51+
new TemplateBuilder(),
52+
mockedApiClient,
53+
mockedTagClient);
54+
55+
var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
56+
this.GetTestLogger<ExtractorExecutor>(),
57+
tagApiExtractor: tagApiExtractor);
58+
extractorExecutor.SetExtractorParameters(extractorParameters);
59+
60+
// act
61+
var tagApiTemplate = await extractorExecutor.GenerateTagApiTemplateAsync(
62+
It.IsAny<string>(),
63+
It.IsAny<List<string>>(),
64+
currentTestDirectory);
65+
66+
// assert
67+
File.Exists(Path.Combine(currentTestDirectory, extractorParameters.FileNames.TagApi)).Should().BeTrue();
68+
69+
tagApiTemplate.Parameters.Should().ContainKey(ParameterNames.ApimServiceName);
70+
tagApiTemplate.TypedResources.Tags.Count().Should().Be(4);
71+
tagApiTemplate.Resources.Count().Should().Be(4);
72+
73+
var resources = tagApiTemplate.TypedResources;
74+
75+
resources.Tags.Any(x => x.Name.Contains(MockTagClient.TagName1)).Should().BeTrue();
76+
resources.Tags.Any(x => x.Name.Contains(MockTagClient.TagName2)).Should().BeTrue();
77+
resources.Tags.All(x => x.ApiVersion == GlobalConstants.ApiVersion).Should().BeTrue();
78+
resources.Tags.All(x => !x.DependsOn.IsNullOrEmpty() && !string.IsNullOrEmpty(x.DependsOn.First())).Should().BeTrue();
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)