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

Commit 2f0a63a

Browse files
f-alizadaFarhad Alizada
andauthored
Determine the sku for groups extraction (#870)
Co-authored-by: Farhad Alizada <falizada@microsoft.com>
1 parent 98a373b commit 2f0a63a

File tree

7 files changed

+150
-3
lines changed

7 files changed

+150
-3
lines changed

src/ArmTemplates/Commands/Executors/ExtractorExecutor.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,13 @@ public async Task<Template<ApiManagementServiceResources>> GenerateApiManagement
878878

879879
if (apiManagementServiceTemplate?.HasResources() == true)
880880
{
881+
882+
var typedService = apiManagementServiceTemplate.TypedResources.ApiManagementServices.FirstOrDefault();
883+
if (typedService.Sku is not null)
884+
{
885+
this.extractorParameters.SetSkuType(typedService.Sku.Name);
886+
}
887+
881888
await FileWriter.SaveAsJsonAsync(
882889
apiManagementServiceTemplate,
883890
directory: baseFilesGenerationDirectory,
@@ -1156,6 +1163,9 @@ async Task GenerateTemplates(
11561163
{
11571164
throw new SingleAndMultipleApisCanNotExistTogetherException("Can't specify single API and multiple APIs to extract at the same time");
11581165
}
1166+
1167+
// Fetch ApiManagement Service instance template
1168+
await this.GenerateApiManagementServiceTemplate(baseFilesGenerationDirectory);
11591169

11601170
var apisToExtract = await this.GetApiNamesToExtract(singleApiName, multipleApiNames);
11611171
// generate different templates using extractors and write to output
@@ -1179,7 +1189,6 @@ async Task GenerateTemplates(
11791189
var apiReleasesTemplate = await this.GenerateApiReleasesTemplateAsync(baseFilesGenerationDirectory);
11801190
await this.GenerateGatewayTemplateAsync(singleApiName, baseFilesGenerationDirectory);
11811191
await this.GenerateGatewayApiTemplateAsync(singleApiName, multipleApiNames, baseFilesGenerationDirectory);
1182-
await this.GenerateApiManagementServiceTemplate(baseFilesGenerationDirectory);
11831192

11841193
var parametersTemplate = await this.GenerateParametersTemplateAsync(apisToExtract, loggerTemplate.TypedResources, backendTemplate.TypedResources, namedValueTemplate.TypedResources, identityProviderTemplate.TypedResources, openIdConnectProviderTemplate.TypedResources, baseFilesGenerationDirectory);
11851194

src/ArmTemplates/Extractor/EntityExtractors/GroupExtractor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public async Task<Template<GroupTemplateResources>> GenerateGroupsTemplateAsync(
4040
.GenerateTemplateWithApimServiceNameProperty()
4141
.Build<GroupTemplateResources>();
4242

43+
if (SKUTypes.IsConsumption(extractorParameters.CurrentSKU))
44+
{
45+
this.logger.LogInformation("Skipping generation of groups template for consumption sku...");
46+
return groupsTemplate;
47+
}
48+
4349
var allGroups = await this.groupsClient.GetAllAsync(extractorParameters);
4450
if (allGroups.IsNullOrEmpty())
4551
{

src/ArmTemplates/Extractor/EntityExtractors/ProductExtractor.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ async Task AddGroupsLinkedToProductResources(
159159
{
160160
var productResourceId = new string[] { $"[resourceId('Microsoft.ApiManagement/service/products', parameters('{ParameterNames.ApimServiceName}'), '{productTemplateResource.NewName}')]" };
161161

162+
if (SKUTypes.IsConsumption(extractorParameters.CurrentSKU))
163+
{
164+
this.logger.LogInformation("Skipping generation of groups resources attached to groups for consumption sku...");
165+
return ;
166+
}
167+
162168
try
163169
{
164170
var groupsLinkedToProduct = await this.groupsClient.GetAllLinkedToProductAsync(productTemplateResource.OriginalName, extractorParameters);
@@ -178,6 +184,5 @@ async Task AddGroupsLinkedToProductResources(
178184
throw;
179185
}
180186
}
181-
182187
}
183188
}

src/ArmTemplates/Extractor/Models/ExtractorParameters.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ public record ExtractorParameters
9696

9797
public Dictionary<string, ApiParameterProperty> ApiParameters { get; private set; }
9898

99+
public string CurrentSKU { get; private set; }
100+
101+
public void SetSkuType(string sku)
102+
{
103+
this.CurrentSKU = sku;
104+
}
105+
99106
public ExtractorParameters(ExtractorConsoleAppConfiguration extractorConfig)
100107
{
101108
this.SourceApimName = extractorConfig.SourceApimName;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// --------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License.
4+
// --------------------------------------------------------------------------
5+
6+
7+
using System;
8+
9+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models
10+
{
11+
public class SKUTypes
12+
{
13+
public const string Basic = "Basic";
14+
public const string Consumption = "Consumption";
15+
public const string Developer = "Developer";
16+
public const string Isolated = "Isolated";
17+
public const string Premium = "Premium";
18+
public const string Standard = "Standard";
19+
20+
public static bool IsConsumption(string skuValue)
21+
{
22+
if (string.IsNullOrEmpty(skuValue))
23+
{
24+
return false;
25+
}
26+
27+
return skuValue.Equals(Consumption, StringComparison.CurrentCultureIgnoreCase);
28+
}
29+
}
30+
}

tests/ArmTemplates.Tests/Extractor/Scenarios/GroupExtractorTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,33 @@ public async Task GenerateGroupsTemplates_ProperlyParsesResponse()
9494
groupTemplate.TypedResources.Groups.Count().Should().Be(4);
9595
groupTemplate.TypedResources.Groups.First(x => x.Properties.DisplayName.Equals("AwesomeGroup for test")).Should().NotBeNull();
9696
}
97+
98+
[Fact]
99+
public async Task GenerateGroupsTemplates_ForConsumptionSku_ShouldNotReturnAnyGroup()
100+
{
101+
// arrange
102+
var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GenerateGroupsTemplates_ForConsumptionSku_ShouldNotReturnAnyGroup));
103+
104+
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration();
105+
var extractorParameters = new ExtractorParameters(extractorConfig);
106+
extractorParameters.SetSkuType(SKUTypes.Consumption);
107+
var fileLocation = Path.Combine(MockClientUtils.ApiClientJsonResponsesPath, "ApiManagementListGroups_success_response.json");
108+
var mockedGroupsClient = await MockGroupsClient.GetMockedHttpGroupClient(new MockClientConfiguration(responseFileLocation: fileLocation));
109+
var groupExtractor = new GroupExtractor(this.GetTestLogger<GroupExtractor>(), new TemplateBuilder(), mockedGroupsClient);
110+
111+
var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
112+
this.GetTestLogger<ExtractorExecutor>(),
113+
groupExtractor: groupExtractor);
114+
extractorExecutor.SetExtractorParameters(extractorParameters);
115+
116+
// act
117+
var groupTemplate = await extractorExecutor.GenerateGroupsTemplateAsync(currentTestDirectory);
118+
119+
// assert
120+
File.Exists(Path.Combine(currentTestDirectory, extractorParameters.FileNames.Groups)).Should().BeFalse();
121+
122+
groupTemplate.Parameters.Should().ContainKey(ParameterNames.ApimServiceName);
123+
groupTemplate.TypedResources.Groups.Count().Should().Be(0);
124+
}
97125
}
98126
}

tests/ArmTemplates.Tests/Extractor/Scenarios/ProductsExtractorTests.cs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public async Task GenerateProductsTemplates_GeneratesTemplatesCorrectly_GivenApi
175175
);
176176

177177
//default values
178-
var mockedGroupsClient = MockGroupsClient.GetMockedApiClientWithEmptyValues();
178+
var mockedGroupsClient = MockGroupsClient.GetMockedApiClientWithDefaultValues();
179179
var mockedTagClient = MockTagClient.GetMockedApiClientWithEmptytValues();
180180

181181
var mockedPolicyClient = MockPolicyClient.GetMockedApiClientWithEmptyValues();
@@ -212,6 +212,68 @@ public async Task GenerateProductsTemplates_GeneratesTemplatesCorrectly_GivenApi
212212
productResources.Count.Should().Be(2);
213213
productResources.Any(x => x.OriginalName == "unlimited").Should().BeTrue();
214214
productResources.Any(x => x.OriginalName == "starter").Should().BeTrue();
215+
216+
var attachedGroups = templateResources.Where(x => x.Type == ResourceTypeConstants.ProductGroup).ToList();
217+
attachedGroups.Count.Should().Be(4);
218+
}
219+
220+
[Fact]
221+
public async Task GenerateProductsTemplates_GeneratesTemplatesCorrectly_GivenConsumptionSku()
222+
{
223+
// arrange
224+
var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GenerateProductsTemplates_GeneratesTemplatesCorrectly_GivenConsumptionSku));
225+
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration(
226+
apiName: null
227+
);
228+
var extractorParameters = new ExtractorParameters(extractorConfig);
229+
extractorParameters.SetSkuType(SKUTypes.Consumption);
230+
231+
var getAlldProductsResponseFileLocation = Path.Combine(MockClientUtils.ApiClientJsonResponsesPath, "ApiManagementListProducts_success_response.json");
232+
var mockedProductsClient = await MockProductsClient.GetMockedHttpProductClient(
233+
new MockClientConfiguration(responseFileLocation: getAlldProductsResponseFileLocation, urlPath: $"{MockSourceApimName}/products?api-version={GlobalConstants.ApiVersion}")
234+
);
235+
236+
//default values
237+
var mockedGroupsClient = MockGroupsClient.GetMockedApiClientWithDefaultValues();
238+
var mockedTagClient = MockTagClient.GetMockedApiClientWithEmptytValues();
239+
240+
var mockedPolicyClient = MockPolicyClient.GetMockedApiClientWithEmptyValues();
241+
var mockedPolicyExtractor = new PolicyExtractor(this.GetTestLogger<PolicyExtractor>(), mockedPolicyClient, new TemplateBuilder());
242+
243+
var productExtractor = new ProductExtractor(
244+
this.GetTestLogger<ProductExtractor>(),
245+
mockedPolicyExtractor,
246+
mockedProductsClient,
247+
mockedGroupsClient,
248+
mockedTagClient,
249+
new TemplateBuilder());
250+
251+
var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
252+
this.GetTestLogger<ExtractorExecutor>(),
253+
productExtractor: productExtractor);
254+
extractorExecutor.SetExtractorParameters(extractorParameters);
255+
256+
// act
257+
var productTemplate = await extractorExecutor.GenerateProductsTemplateAsync(
258+
singleApiName: null,
259+
currentTestDirectory);
260+
261+
// assert
262+
// generated product template files
263+
File.Exists(Path.Combine(currentTestDirectory, extractorParameters.FileNames.Products)).Should().BeTrue();
264+
265+
var templateParameters = productTemplate.Parameters;
266+
templateParameters.Should().ContainKey(ParameterNames.ApimServiceName);
267+
268+
var templateResources = productTemplate.Resources;
269+
// product resource
270+
var productResources = templateResources.Where(x => x.Type == ResourceTypeConstants.Product).ToList();
271+
productResources.Count.Should().Be(2);
272+
productResources.Any(x => x.OriginalName == "unlimited").Should().BeTrue();
273+
productResources.Any(x => x.OriginalName == "starter").Should().BeTrue();
274+
275+
var attachedGroups = templateResources.Where(x => x.Type == ResourceTypeConstants.ProductGroup).ToList();
276+
attachedGroups.Count.Should().Be(0);
215277
}
216278
}
217279
}

0 commit comments

Comments
 (0)