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

Commit

Permalink
Determine the sku for groups extraction (#870)
Browse files Browse the repository at this point in the history
Co-authored-by: Farhad Alizada <[email protected]>
  • Loading branch information
f-alizada and Farhad Alizada authored Feb 28, 2023
1 parent 98a373b commit 2f0a63a
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/ArmTemplates/Commands/Executors/ExtractorExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,13 @@ public async Task<Template<ApiManagementServiceResources>> GenerateApiManagement

if (apiManagementServiceTemplate?.HasResources() == true)
{

var typedService = apiManagementServiceTemplate.TypedResources.ApiManagementServices.FirstOrDefault();
if (typedService.Sku is not null)
{
this.extractorParameters.SetSkuType(typedService.Sku.Name);
}

await FileWriter.SaveAsJsonAsync(
apiManagementServiceTemplate,
directory: baseFilesGenerationDirectory,
Expand Down Expand Up @@ -1156,6 +1163,9 @@ async Task GenerateTemplates(
{
throw new SingleAndMultipleApisCanNotExistTogetherException("Can't specify single API and multiple APIs to extract at the same time");
}

// Fetch ApiManagement Service instance template
await this.GenerateApiManagementServiceTemplate(baseFilesGenerationDirectory);

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

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

Expand Down
6 changes: 6 additions & 0 deletions src/ArmTemplates/Extractor/EntityExtractors/GroupExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public async Task<Template<GroupTemplateResources>> GenerateGroupsTemplateAsync(
.GenerateTemplateWithApimServiceNameProperty()
.Build<GroupTemplateResources>();

if (SKUTypes.IsConsumption(extractorParameters.CurrentSKU))
{
this.logger.LogInformation("Skipping generation of groups template for consumption sku...");
return groupsTemplate;
}

var allGroups = await this.groupsClient.GetAllAsync(extractorParameters);
if (allGroups.IsNullOrEmpty())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ async Task AddGroupsLinkedToProductResources(
{
var productResourceId = new string[] { $"[resourceId('Microsoft.ApiManagement/service/products', parameters('{ParameterNames.ApimServiceName}'), '{productTemplateResource.NewName}')]" };

if (SKUTypes.IsConsumption(extractorParameters.CurrentSKU))
{
this.logger.LogInformation("Skipping generation of groups resources attached to groups for consumption sku...");
return ;
}

try
{
var groupsLinkedToProduct = await this.groupsClient.GetAllLinkedToProductAsync(productTemplateResource.OriginalName, extractorParameters);
Expand All @@ -178,6 +184,5 @@ async Task AddGroupsLinkedToProductResources(
throw;
}
}

}
}
7 changes: 7 additions & 0 deletions src/ArmTemplates/Extractor/Models/ExtractorParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public record ExtractorParameters

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

public string CurrentSKU { get; private set; }

public void SetSkuType(string sku)
{
this.CurrentSKU = sku;
}

public ExtractorParameters(ExtractorConsoleAppConfiguration extractorConfig)
{
this.SourceApimName = extractorConfig.SourceApimName;
Expand Down
30 changes: 30 additions & 0 deletions src/ArmTemplates/Extractor/Models/SKUValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// --------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// --------------------------------------------------------------------------


using System;

namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models
{
public class SKUTypes
{
public const string Basic = "Basic";
public const string Consumption = "Consumption";
public const string Developer = "Developer";
public const string Isolated = "Isolated";
public const string Premium = "Premium";
public const string Standard = "Standard";

public static bool IsConsumption(string skuValue)
{
if (string.IsNullOrEmpty(skuValue))
{
return false;
}

return skuValue.Equals(Consumption, StringComparison.CurrentCultureIgnoreCase);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,33 @@ public async Task GenerateGroupsTemplates_ProperlyParsesResponse()
groupTemplate.TypedResources.Groups.Count().Should().Be(4);
groupTemplate.TypedResources.Groups.First(x => x.Properties.DisplayName.Equals("AwesomeGroup for test")).Should().NotBeNull();
}

[Fact]
public async Task GenerateGroupsTemplates_ForConsumptionSku_ShouldNotReturnAnyGroup()
{
// arrange
var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GenerateGroupsTemplates_ForConsumptionSku_ShouldNotReturnAnyGroup));

var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration();
var extractorParameters = new ExtractorParameters(extractorConfig);
extractorParameters.SetSkuType(SKUTypes.Consumption);
var fileLocation = Path.Combine(MockClientUtils.ApiClientJsonResponsesPath, "ApiManagementListGroups_success_response.json");
var mockedGroupsClient = await MockGroupsClient.GetMockedHttpGroupClient(new MockClientConfiguration(responseFileLocation: fileLocation));
var groupExtractor = new GroupExtractor(this.GetTestLogger<GroupExtractor>(), new TemplateBuilder(), mockedGroupsClient);

var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
this.GetTestLogger<ExtractorExecutor>(),
groupExtractor: groupExtractor);
extractorExecutor.SetExtractorParameters(extractorParameters);

// act
var groupTemplate = await extractorExecutor.GenerateGroupsTemplateAsync(currentTestDirectory);

// assert
File.Exists(Path.Combine(currentTestDirectory, extractorParameters.FileNames.Groups)).Should().BeFalse();

groupTemplate.Parameters.Should().ContainKey(ParameterNames.ApimServiceName);
groupTemplate.TypedResources.Groups.Count().Should().Be(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public async Task GenerateProductsTemplates_GeneratesTemplatesCorrectly_GivenApi
);

//default values
var mockedGroupsClient = MockGroupsClient.GetMockedApiClientWithEmptyValues();
var mockedGroupsClient = MockGroupsClient.GetMockedApiClientWithDefaultValues();
var mockedTagClient = MockTagClient.GetMockedApiClientWithEmptytValues();

var mockedPolicyClient = MockPolicyClient.GetMockedApiClientWithEmptyValues();
Expand Down Expand Up @@ -212,6 +212,68 @@ public async Task GenerateProductsTemplates_GeneratesTemplatesCorrectly_GivenApi
productResources.Count.Should().Be(2);
productResources.Any(x => x.OriginalName == "unlimited").Should().BeTrue();
productResources.Any(x => x.OriginalName == "starter").Should().BeTrue();

var attachedGroups = templateResources.Where(x => x.Type == ResourceTypeConstants.ProductGroup).ToList();
attachedGroups.Count.Should().Be(4);
}

[Fact]
public async Task GenerateProductsTemplates_GeneratesTemplatesCorrectly_GivenConsumptionSku()
{
// arrange
var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GenerateProductsTemplates_GeneratesTemplatesCorrectly_GivenConsumptionSku));
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration(
apiName: null
);
var extractorParameters = new ExtractorParameters(extractorConfig);
extractorParameters.SetSkuType(SKUTypes.Consumption);

var getAlldProductsResponseFileLocation = Path.Combine(MockClientUtils.ApiClientJsonResponsesPath, "ApiManagementListProducts_success_response.json");
var mockedProductsClient = await MockProductsClient.GetMockedHttpProductClient(
new MockClientConfiguration(responseFileLocation: getAlldProductsResponseFileLocation, urlPath: $"{MockSourceApimName}/products?api-version={GlobalConstants.ApiVersion}")
);

//default values
var mockedGroupsClient = MockGroupsClient.GetMockedApiClientWithDefaultValues();
var mockedTagClient = MockTagClient.GetMockedApiClientWithEmptytValues();

var mockedPolicyClient = MockPolicyClient.GetMockedApiClientWithEmptyValues();
var mockedPolicyExtractor = new PolicyExtractor(this.GetTestLogger<PolicyExtractor>(), mockedPolicyClient, new TemplateBuilder());

var productExtractor = new ProductExtractor(
this.GetTestLogger<ProductExtractor>(),
mockedPolicyExtractor,
mockedProductsClient,
mockedGroupsClient,
mockedTagClient,
new TemplateBuilder());

var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
this.GetTestLogger<ExtractorExecutor>(),
productExtractor: productExtractor);
extractorExecutor.SetExtractorParameters(extractorParameters);

// act
var productTemplate = await extractorExecutor.GenerateProductsTemplateAsync(
singleApiName: null,
currentTestDirectory);

// assert
// generated product template files
File.Exists(Path.Combine(currentTestDirectory, extractorParameters.FileNames.Products)).Should().BeTrue();

var templateParameters = productTemplate.Parameters;
templateParameters.Should().ContainKey(ParameterNames.ApimServiceName);

var templateResources = productTemplate.Resources;
// product resource
var productResources = templateResources.Where(x => x.Type == ResourceTypeConstants.Product).ToList();
productResources.Count.Should().Be(2);
productResources.Any(x => x.OriginalName == "unlimited").Should().BeTrue();
productResources.Any(x => x.OriginalName == "starter").Should().BeTrue();

var attachedGroups = templateResources.Where(x => x.Type == ResourceTypeConstants.ProductGroup).ToList();
attachedGroups.Count.Should().Be(0);
}
}
}

0 comments on commit 2f0a63a

Please sign in to comment.